Project

General

Profile

1 5942 tao
package edu.ucsb.nceas.metacattest.service;
2
3 9950 tao
import org.apache.commons.io.FileUtils;
4
import edu.ucsb.nceas.MCTestCase;
5 5942 tao
import edu.ucsb.nceas.metacat.service.XMLSchemaParser;
6
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
7
8 9950 tao
import java.io.File;
9
import java.io.StringReader;
10
11 5942 tao
import junit.framework.Test;
12
import junit.framework.TestCase;
13
import junit.framework.TestResult;
14
import junit.framework.TestSuite;
15
16
/**
17
 * Junit test for XMLSchemaService
18
 * @author tao
19
 *
20
 */
21 9950 tao
public class XMLSchemaServiceTest extends MCTestCase
22 5942 tao
{
23 9950 tao
    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
28 5942 tao
  /**
29
   * Constructor to build the test
30
   *
31
   * @param name the name of the test method
32
   */
33
  public XMLSchemaServiceTest(String name)
34
  {
35
    super(name);
36
  }
37
38
  /**
39
   * Establish a testing framework by initializing appropriate objects
40
   */
41
  public void setUp()
42
  {
43
44
  }
45
46
  /**
47
   * Release any objects after tests are complete
48
   */
49
  public void tearDown()
50
  {
51
  }
52
53
  /**
54
   * Create a suite of tests to be run together
55
   */
56
  public static Test suite()
57
  {
58
    TestSuite suite = new TestSuite();
59
    suite.addTest(new XMLSchemaServiceTest("initialize"));
60
    suite.addTest(new XMLSchemaServiceTest("testGetBaseUrlFromSchemaURL"));
61 9950 tao
    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"));
66 5942 tao
    return suite;
67
  }
68
69
  /**
70
   * Run an initial test that always passes to check that the test
71
   * harness is working.
72
   */
73
  public void initialize()
74
  {
75
    assertTrue(1 == 1);
76
  }
77
78
  public void testGetBaseUrlFromSchemaURL()
79
  {
80 8265 leinfelder
      String url="http://www.example.com/metacat/example.xsd";
81
      String base = "http://www.example.com/metacat/";
82 5942 tao
      String baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
83
      assertTrue("The base url should be "+base, baseURL.equals(base));
84
      url = "www.example.com/example.xsd";
85
      baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
86
      assertTrue("The base url should be "+null, baseURL==null);
87 8265 leinfelder
      url="http://www.example.com/metacat/";
88 5942 tao
      baseURL = XMLSchemaService.getBaseUrlFromSchemaURL(url);
89
      assertTrue("The base url should be "+url, baseURL.equals(url));
90
  }
91 9950 tao
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
  }
215 5942 tao
}