Project

General

Profile

« Previous | Next » 

Revision 9934

Added by Jing Tao over 8 years ago

Add a junit test class.

View differences:

test/edu/ucsb/nceas/metacattest/service/XMLNamespaceParserTest.java
1
package edu.ucsb.nceas.metacattest.service;
2

  
3
import org.apache.commons.io.FileUtils;
4

  
5
import edu.ucsb.nceas.MCTestCase;
6
import edu.ucsb.nceas.metacat.service.XMLNamespaceParser;
7

  
8

  
9
import java.io.File;
10
import java.io.StringReader;
11

  
12
import junit.framework.Test;
13
import junit.framework.TestCase;
14
import junit.framework.TestResult;
15
import junit.framework.TestSuite;
16

  
17
/**
18
 * Junit test for XMLSchemaService
19
 * @author tao
20
 *
21
 */
22
public class XMLNamespaceParserTest extends MCTestCase {
23
    public static String withPrefix = "<eml:eml packageId=\"eml.2.1\" system=\"knb2\" xmlns:eml=\"http://eml-example\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:stmml=\"http://www.xml-cml.org/schema/stmml\" xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"><tile>title</title></eml>";
24
    public static String withCommentAndPrefix="<!-- comment -->  <!-- comment -->"+withPrefix;              
25
    public static String defaultNamespace = "<root xmlns=\"eml://ecoinformatics.org/eml-2.1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:stmml=\"http://www.xml-cml.org/schema/stmml\" xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"><title>title1</title></root>";
26
    public static String noNamespace = "<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:stmml=\"http://www.xml-cml.org/schema/stmml\" xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"><title>title1</title></root>";
27
    
28
  /**
29
   * Constructor to build the test
30
   *
31
   * @param name the name of the test method
32
   */
33
  public XMLNamespaceParserTest(String name) {
34
    super(name);
35
  }
36

  
37
  /**
38
   * Establish a testing framework by initializing appropriate objects
39
   */
40
  public void setUp() {
41
    
42
  }
43

  
44
  /**
45
   * Release any objects after tests are complete
46
   */
47
  public void tearDown() {
48
  }
49

  
50
  /**
51
   * Create a suite of tests to be run together
52
   */
53
  public static Test suite() {
54
    TestSuite suite = new TestSuite();
55
    suite.addTest(new XMLSchemaServiceTest("initialize"));
56
    suite.addTest(new XMLNamespaceParserTest("testGetNamespace"));
57
    suite.addTest(new XMLNamespaceParserTest("testGetNoNamespaceSchemaLocation"));
58
    return suite;
59
  }
60

  
61
  /**
62
   * Run an initial test that always passes to check that the test
63
   * harness is working.
64
   */
65
  public void initialize() {
66
    assertTrue(1 == 1);
67
  }
68
  
69
 
70
  
71
  public void testGetNamespace() throws Exception{
72
      StringReader reader = new StringReader(withPrefix);
73
      XMLNamespaceParser parser = new XMLNamespaceParser(reader);
74
      parser.parse();
75
      String namespace = parser.getNamespace();
76
      assertTrue(namespace.equals("http://eml-example"));
77
      
78
      reader = new StringReader(withCommentAndPrefix);
79
      parser = new XMLNamespaceParser(reader);
80
      parser.parse();
81
      namespace = parser.getNamespace();
82
      assertTrue(namespace.equals("http://eml-example"));
83
      
84
      File file = new File("./test/eml-sample.xml");
85
      reader = new StringReader(FileUtils.readFileToString(file));
86
      parser = new XMLNamespaceParser(reader);
87
      parser.parse();
88
      namespace = parser.getNamespace();
89
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.1"));
90
      
91
      reader = new StringReader(defaultNamespace);
92
      parser = new XMLNamespaceParser(reader);
93
      parser.parse();
94
      namespace = parser.getNamespace();
95
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.1.1"));
96
      
97
      reader = new StringReader(noNamespace);
98
      parser = new XMLNamespaceParser(reader);
99
      parser.parse();
100
      namespace = parser.getNamespace();
101
      assertTrue(namespace == null);
102
  }
103
  
104
  
105
  public void testGetNoNamespaceSchemaLocation() throws Exception{
106
      StringReader reader = new StringReader(withCommentAndPrefix);
107
      XMLNamespaceParser parser = new XMLNamespaceParser(reader);
108
      parser.parse();
109
      String namespace = parser.getNamespace();
110
      String noNamespaceSchemaLocation = parser.getNoNamespaceSchemaLocation();
111
      assertTrue(namespace.equals("http://eml-example"));
112
      assertTrue(noNamespaceSchemaLocation == null);
113
      
114
      File file = new File("./test/eml-sample.xml");
115
      reader = new StringReader(FileUtils.readFileToString(file));
116
      parser = new XMLNamespaceParser(reader);
117
      parser.parse();
118
      namespace = parser.getNamespace();
119
      noNamespaceSchemaLocation = parser.getNoNamespaceSchemaLocation();
120
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.1"));
121
      assertTrue(noNamespaceSchemaLocation == null);
122
      
123
      file = new File("./test/fgdc.xml");
124
      reader = new StringReader(FileUtils.readFileToString(file));
125
      parser = new XMLNamespaceParser(reader);
126
      parser.parse();
127
      namespace = parser.getNamespace();
128
      noNamespaceSchemaLocation = parser.getNoNamespaceSchemaLocation();
129
      assertTrue(namespace == null);
130
      assertTrue(noNamespaceSchemaLocation.equals("http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd"));
131
      
132
      reader = new StringReader(defaultNamespace);
133
      parser = new XMLNamespaceParser(reader);
134
      parser.parse();
135
      namespace = parser.getNamespace();
136
      noNamespaceSchemaLocation = parser.getNoNamespaceSchemaLocation();
137
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.1.1"));
138
      assertTrue(noNamespaceSchemaLocation == null);
139
      
140
      reader = new StringReader(noNamespace);
141
      parser = new XMLNamespaceParser(reader);
142
      parser.parse();
143
      namespace = parser.getNamespace();
144
      noNamespaceSchemaLocation = parser.getNoNamespaceSchemaLocation();
145
      assertTrue(namespace == null);
146
      assertTrue(noNamespaceSchemaLocation == null);
147
  }
148
}

Also available in: Unified diff