Project

General

Profile

« Previous | Next » 

Revision 9950

Added by Jing Tao almost 8 years ago

Add the test methods for methods which can figure out the schema location.

View differences:

XMLSchemaServiceTest.java
1 1
package edu.ucsb.nceas.metacattest.service;
2 2

  
3
import org.apache.commons.io.FileUtils;
4
import edu.ucsb.nceas.MCTestCase;
3 5
import edu.ucsb.nceas.metacat.service.XMLSchemaParser;
4 6
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
5 7

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

  
6 11
import junit.framework.Test;
7 12
import junit.framework.TestCase;
8 13
import junit.framework.TestResult;
......
13 18
 * @author tao
14 19
 *
15 20
 */
16
public class XMLSchemaServiceTest extends TestCase
21
public class XMLSchemaServiceTest extends MCTestCase
17 22
{
18
  
23
    public static String withPrefix = "<eml:eml packageId=\"eml.2.1\" system=\"knb2\" xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\" 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
    
19 28
  /**
20 29
   * Constructor to build the test
21 30
   *
......
49 58
    TestSuite suite = new TestSuite();
50 59
    suite.addTest(new XMLSchemaServiceTest("initialize"));
51 60
    suite.addTest(new XMLSchemaServiceTest("testGetBaseUrlFromSchemaURL"));
61
    suite.addTest(new XMLSchemaServiceTest("testFindNamespaceInDocuments"));
62
    suite.addTest(new XMLSchemaServiceTest("testFindNamespaceAndSchemaLocalLocation"));
63
    suite.addTest(new XMLSchemaServiceTest("testFindNoNamespaceSchemaLocalLocation"));
64
    suite.addTest(new XMLSchemaServiceTest("testIsNamespaceRegistered"));
65
    suite.addTest(new XMLSchemaServiceTest("testFindNoNamespaceSchemaLocationAttr"));
52 66
    return suite;
53 67
  }
54 68

  
......
74 88
      baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
75 89
      assertTrue("The base url should be "+url, baseURL.equals(url));
76 90
  }
91
  
92
  public void testFindNamespaceInDocuments() throws Exception{
93
      StringReader reader = new StringReader(withPrefix);
94
      String namespace = XMLSchemaService.findDocumentNamespace(reader);
95
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.0"));
96
      
97
      reader = new StringReader(withCommentAndPrefix);
98
      namespace = XMLSchemaService.findDocumentNamespace(reader);
99
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.0"));
100
      
101
      File file = new File("./test/eml-sample.xml");
102
      reader = new StringReader(FileUtils.readFileToString(file));
103
      namespace = XMLSchemaService.findDocumentNamespace(reader);
104
      assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.1"));
105
      
106
      reader = new StringReader(defaultNamespace);
107
      String namespace2 = XMLSchemaService.findDocumentNamespace(reader);
108
      assertTrue(namespace2.equals("eml://ecoinformatics.org/eml-2.1.1"));
109
      
110
      reader = new StringReader(noNamespace);
111
      namespace = XMLSchemaService.findDocumentNamespace(reader);
112
      assertTrue(namespace == null);
113
  }
114
  
115
  public void testFindNamespaceAndSchemaLocalLocation() throws Exception {
116
      String formatId = null;
117
      String namespace = null;
118
      String location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
119
      assertTrue(location == null);
120
      
121
      //registered formatid
122
      formatId = "http://www.isotc211.org/2005/gmd-noaa";
123
      namespace = null;
124
      location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
125
      assertTrue(location.contains("/schema/isotc211-noaa/gmd/gmd.xsd"));
126
      
127
    //registered formatid
128
      formatId = "http://www.isotc211.org/2005/gmd-noaa";
129
      namespace = "http://www.isotc211.org/2005/gmd";
130
      location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
131
      assertTrue(location.contains("/schema/isotc211-noaa/gmd/gmd.xsd"));
132
      
133
      //unregistered formatid
134
      formatId = "eml://ecoinformatics.org/eml-2.1.0";
135
      namespace = "eml://ecoinformatics.org/eml-2.1.0";
136
      location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
137
      assertTrue(location.contains("/schema/eml-2.1.0/eml.xsd "));
138
      
139
      //unregistered formatid
140
      formatId = "http://www.isotc211.org/2005/gco";
141
      namespace = "http://www.isotc211.org/2005/gco";
142
      location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
143
      assertTrue(location.contains("/schema/isotc211/gco/gco.xsd"));
144
      
145
  }
146
  
147
  public void testFindNoNamespaceSchemaLocalLocation() throws Exception {
148
      String formatId = null;
149
      String nonamespaceURI = null;
150
      String location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
151
      assertTrue(location == null);
152
      
153
      formatId = "FGDC-STD-001-1998";
154
      nonamespaceURI = null;
155
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
156
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
157
      
158
      formatId = "FGDC-STD-001-1998";
159
      nonamespaceURI = "unkown";
160
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
161
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
162
      
163
      formatId = "FGDC-STD-001-1998";
164
      nonamespaceURI = "https://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
165
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
166
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
167
      
168
      formatId = "FGDC-STD-001-1998";
169
      nonamespaceURI = "http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
170
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
171
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
172
      
173
      
174
      formatId = "FGDC-STD-001";
175
      nonamespaceURI = "http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
176
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
177
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
178
      
179
      formatId = "FGDC-STD-001";
180
      nonamespaceURI = "https://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
181
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
182
      assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
183
      
184
      formatId = "FGDC-STD-001";
185
      nonamespaceURI = "https://www.fgdc.gov/metadata-std-001-1998.xsd";
186
      location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
187
      assertTrue(location == null);
188
  }
189
  
190
  public void testIsNamespaceRegistered() throws Exception {
191
      String namespace = null;
192
      boolean registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
193
      assertTrue(registered == false);
194
      
195
      namespace = "http://foo";
196
      registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
197
      assertTrue(!registered);
198
      
199
      namespace = "eml://ecoinformatics.org/eml-2.0.1";
200
      registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
201
      assertTrue(registered);
202
  }
203
  
204
  public void testFindNoNamespaceSchemaLocationAttr() throws Exception {
205
      File file = new File("./test/eml-sample.xml");
206
      StringReader reader = new StringReader(FileUtils.readFileToString(file));
207
      String noNamespaceLocation = XMLSchemaService.findNoNamespaceSchemaLocationAttr(reader);
208
      assertTrue(noNamespaceLocation == null);
209
      
210
      file = new File("./test/fgdc.xml");
211
      reader = new StringReader(FileUtils.readFileToString(file));
212
      noNamespaceLocation = XMLSchemaService.findNoNamespaceSchemaLocationAttr(reader);
213
      assertTrue(noNamespaceLocation.equals("http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd"));
214
  }
77 215
}

Also available in: Unified diff