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.client.MetacatException;
|
7
|
import edu.ucsb.nceas.metacat.service.XMLSchemaParser;
|
8
|
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
|
9
|
|
10
|
import java.io.File;
|
11
|
import java.io.StringReader;
|
12
|
|
13
|
import junit.framework.Test;
|
14
|
import junit.framework.TestCase;
|
15
|
import junit.framework.TestResult;
|
16
|
import junit.framework.TestSuite;
|
17
|
|
18
|
/**
|
19
|
* Junit test for XMLSchemaService
|
20
|
* @author tao
|
21
|
*
|
22
|
*/
|
23
|
public class XMLSchemaServiceTest extends MCTestCase
|
24
|
{
|
25
|
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>";
|
26
|
public static String withCommentAndPrefix="<!-- comment --> <!-- comment -->"+withPrefix;
|
27
|
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>";
|
28
|
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>";
|
29
|
|
30
|
/**
|
31
|
* Constructor to build the test
|
32
|
*
|
33
|
* @param name the name of the test method
|
34
|
*/
|
35
|
public XMLSchemaServiceTest(String name)
|
36
|
{
|
37
|
super(name);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Establish a testing framework by initializing appropriate objects
|
42
|
*/
|
43
|
public void setUp()
|
44
|
{
|
45
|
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Release any objects after tests are complete
|
50
|
*/
|
51
|
public void tearDown()
|
52
|
{
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Create a suite of tests to be run together
|
57
|
*/
|
58
|
public static Test suite()
|
59
|
{
|
60
|
TestSuite suite = new TestSuite();
|
61
|
suite.addTest(new XMLSchemaServiceTest("initialize"));
|
62
|
suite.addTest(new XMLSchemaServiceTest("testGetBaseUrlFromSchemaURL"));
|
63
|
suite.addTest(new XMLSchemaServiceTest("testFindNamespaceInDocuments"));
|
64
|
suite.addTest(new XMLSchemaServiceTest("testFindNamespaceAndSchemaLocalLocation"));
|
65
|
suite.addTest(new XMLSchemaServiceTest("testFindNoNamespaceSchemaLocalLocation"));
|
66
|
suite.addTest(new XMLSchemaServiceTest("testIsNamespaceRegistered"));
|
67
|
suite.addTest(new XMLSchemaServiceTest("testFindNoNamespaceSchemaLocationAttr"));
|
68
|
return suite;
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Run an initial test that always passes to check that the test
|
73
|
* harness is working.
|
74
|
*/
|
75
|
public void initialize()
|
76
|
{
|
77
|
assertTrue(1 == 1);
|
78
|
}
|
79
|
|
80
|
public void testGetBaseUrlFromSchemaURL()
|
81
|
{
|
82
|
String url="http://www.example.com/metacat/example.xsd";
|
83
|
String base = "http://www.example.com/metacat/";
|
84
|
String baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
|
85
|
assertTrue("The base url should be "+base, baseURL.equals(base));
|
86
|
url = "www.example.com/example.xsd";
|
87
|
baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
|
88
|
assertTrue("The base url should be "+null, baseURL==null);
|
89
|
url="http://www.example.com/metacat/";
|
90
|
baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
|
91
|
assertTrue("The base url should be "+url, baseURL.equals(url));
|
92
|
}
|
93
|
|
94
|
public void testFindNamespaceInDocuments() throws Exception{
|
95
|
StringReader reader = new StringReader(withPrefix);
|
96
|
String namespace = XMLSchemaService.findDocumentNamespace(reader);
|
97
|
assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.0"));
|
98
|
|
99
|
reader = new StringReader(withCommentAndPrefix);
|
100
|
namespace = XMLSchemaService.findDocumentNamespace(reader);
|
101
|
assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.0"));
|
102
|
|
103
|
File file = new File("./test/eml-sample.xml");
|
104
|
reader = new StringReader(FileUtils.readFileToString(file));
|
105
|
namespace = XMLSchemaService.findDocumentNamespace(reader);
|
106
|
assertTrue(namespace.equals("eml://ecoinformatics.org/eml-2.0.1"));
|
107
|
|
108
|
reader = new StringReader(defaultNamespace);
|
109
|
String namespace2 = XMLSchemaService.findDocumentNamespace(reader);
|
110
|
assertTrue(namespace2.equals("eml://ecoinformatics.org/eml-2.1.1"));
|
111
|
|
112
|
reader = new StringReader(noNamespace);
|
113
|
namespace = XMLSchemaService.findDocumentNamespace(reader);
|
114
|
assertTrue(namespace == null);
|
115
|
}
|
116
|
|
117
|
public void testFindNamespaceAndSchemaLocalLocation() throws Exception {
|
118
|
String formatId = null;
|
119
|
String namespace = null;
|
120
|
String location = null;
|
121
|
try {
|
122
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
123
|
fail("we can't get here. The above line should throw an exception.");
|
124
|
} catch (MetacatException e) {
|
125
|
assertTrue(e.getMessage().contains("not registered in the Metacat"));
|
126
|
}
|
127
|
|
128
|
//registered formatid
|
129
|
formatId = "http://www.isotc211.org/2005/gmd-noaa";
|
130
|
namespace = null;
|
131
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
132
|
assertTrue(location.contains("/schema/isotc211-noaa/gmd/gmd.xsd"));
|
133
|
|
134
|
//registered formatid
|
135
|
formatId = "http://www.isotc211.org/2005/gmd-noaa";
|
136
|
namespace = "http://www.isotc211.org/2005/gmd";
|
137
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
138
|
assertTrue(location.contains("/schema/isotc211-noaa/gmd/gmd.xsd"));
|
139
|
|
140
|
//unregistered formatid
|
141
|
formatId = "eml://ecoinformatics.org/eml-2.1.0";
|
142
|
namespace = "eml://ecoinformatics.org/eml-2.1.0";
|
143
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
144
|
assertTrue(location.contains("/schema/eml-2.1.0/eml.xsd "));
|
145
|
|
146
|
//unregistered formatid
|
147
|
formatId = "http://www.isotc211.org/2005/gco";
|
148
|
namespace = "http://www.isotc211.org/2005/gco";
|
149
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
150
|
assertTrue(location.contains("/schema/isotc211/gco/gco.xsd"));
|
151
|
|
152
|
formatId = "http://foo.com";
|
153
|
namespace = "http://foo.com";
|
154
|
try {
|
155
|
location = XMLSchemaService.getInstance().findNamespaceAndSchemaLocalLocation(formatId, namespace);
|
156
|
fail("we can't get here. The above line should throw an exception.");
|
157
|
} catch (MetacatException e) {
|
158
|
assertTrue(e.getMessage().contains("not registered in the Metacat"));
|
159
|
}
|
160
|
}
|
161
|
|
162
|
public void testFindNoNamespaceSchemaLocalLocation() throws Exception {
|
163
|
String formatId = null;
|
164
|
String nonamespaceURI = null;
|
165
|
String location = null;
|
166
|
try {
|
167
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
168
|
fail("we can't get here. The above line should throw an exception.");
|
169
|
} catch (MetacatException e) {
|
170
|
assertTrue(e.getMessage().contains("not registered in the Metacat"));
|
171
|
}
|
172
|
|
173
|
assertTrue(location == null);
|
174
|
|
175
|
formatId = "FGDC-STD-001-1998";
|
176
|
nonamespaceURI = null;
|
177
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
178
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
179
|
|
180
|
formatId = "FGDC-STD-001-1998";
|
181
|
nonamespaceURI = "unkown";
|
182
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
183
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
184
|
|
185
|
formatId = "FGDC-STD-001-1998";
|
186
|
nonamespaceURI = "https://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
|
187
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
188
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
189
|
|
190
|
formatId = "FGDC-STD-001-1998";
|
191
|
nonamespaceURI = "http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
|
192
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
193
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
194
|
|
195
|
|
196
|
formatId = "FGDC-STD-001";
|
197
|
nonamespaceURI = "http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
|
198
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
199
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
200
|
|
201
|
formatId = "FGDC-STD-001";
|
202
|
nonamespaceURI = "https://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd";
|
203
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
204
|
assertTrue(location.contains("/schema/fgdc-std-001-1998/fgdc-std-001-1998.xsd"));
|
205
|
|
206
|
formatId = "foo-001";
|
207
|
nonamespaceURI = "https://www.fgdc.gov/foo";
|
208
|
try {
|
209
|
location = XMLSchemaService.getInstance().findNoNamespaceSchemaLocalLocation(formatId, nonamespaceURI);
|
210
|
fail("we can't get here. The above line should throw an exception.");
|
211
|
} catch (MetacatException e) {
|
212
|
assertTrue(e.getMessage().contains("not registered in the Metacat"));
|
213
|
}
|
214
|
}
|
215
|
|
216
|
public void testIsNamespaceRegistered() throws Exception {
|
217
|
String namespace = null;
|
218
|
boolean registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
|
219
|
assertTrue(registered == false);
|
220
|
|
221
|
namespace = "http://foo";
|
222
|
registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
|
223
|
assertTrue(!registered);
|
224
|
|
225
|
namespace = "eml://ecoinformatics.org/eml-2.0.1";
|
226
|
registered = XMLSchemaService.getInstance().isNamespaceRegistered(namespace);
|
227
|
assertTrue(registered);
|
228
|
}
|
229
|
|
230
|
public void testFindNoNamespaceSchemaLocationAttr() throws Exception {
|
231
|
File file = new File("./test/eml-sample.xml");
|
232
|
StringReader reader = new StringReader(FileUtils.readFileToString(file));
|
233
|
String noNamespaceLocation = XMLSchemaService.findNoNamespaceSchemaLocationAttr(reader);
|
234
|
assertTrue(noNamespaceLocation == null);
|
235
|
|
236
|
file = new File("./test/fgdc.xml");
|
237
|
reader = new StringReader(FileUtils.readFileToString(file));
|
238
|
noNamespaceLocation = XMLSchemaService.findNoNamespaceSchemaLocationAttr(reader);
|
239
|
assertTrue(noNamespaceLocation.equals("http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd"));
|
240
|
}
|
241
|
}
|