1 /* |
|
2 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 * contributor license agreements. See the NOTICE file distributed with |
|
4 * this work for additional information regarding copyright ownership. |
|
5 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 * (the "License"); you may not use this file except in compliance with |
|
7 * the License. You may obtain a copy of the License at |
|
8 * |
|
9 * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 * |
|
11 * Unless required by applicable law or agreed to in writing, software |
|
12 * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 * See the License for the specific language governing permissions and |
|
15 * limitations under the License. |
|
16 */ |
|
17 |
|
18 import java.io.File; |
|
19 import java.net.URL; |
|
20 |
|
21 import javax.xml.XMLConstants; |
|
22 import javax.xml.parsers.DocumentBuilder; |
|
23 import javax.xml.parsers.DocumentBuilderFactory; |
|
24 import javax.xml.transform.Result; |
|
25 import javax.xml.transform.Source; |
|
26 import javax.xml.transform.dom.DOMResult; |
|
27 import javax.xml.transform.dom.DOMSource; |
|
28 import javax.xml.validation.Schema; |
|
29 import javax.xml.validation.SchemaFactory; |
|
30 import javax.xml.validation.Validator; |
|
31 |
|
32 |
|
33 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; |
|
34 import com.sun.org.apache.xerces.internal.impl.Constants; |
|
35 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar; |
|
36 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; |
|
37 import com.sun.org.apache.xerces.internal.xs.ItemPSVI; |
|
38 import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration; |
|
39 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; |
|
40 import javax.xml.transform.stream.StreamSource; |
|
41 import org.w3c.dom.Document; |
|
42 import org.w3c.dom.Node; |
|
43 |
|
44 public abstract class BaseTest { |
|
45 protected final static String ROOT_TYPE = Constants.XERCES_PROPERTY_PREFIX |
|
46 + Constants.ROOT_TYPE_DEFINITION_PROPERTY; |
|
47 |
|
48 protected final static String IGNORE_XSI_TYPE = Constants.XERCES_FEATURE_PREFIX |
|
49 + Constants.IGNORE_XSI_TYPE_FEATURE; |
|
50 |
|
51 protected final static String ID_IDREF_CHECKING = Constants.XERCES_FEATURE_PREFIX |
|
52 + Constants.ID_IDREF_CHECKING_FEATURE; |
|
53 |
|
54 protected final static String IDC_CHECKING = Constants.XERCES_FEATURE_PREFIX |
|
55 + Constants.IDC_CHECKING_FEATURE; |
|
56 |
|
57 protected final static String UNPARSED_ENTITY_CHECKING = Constants.XERCES_FEATURE_PREFIX |
|
58 + Constants.UNPARSED_ENTITY_CHECKING_FEATURE; |
|
59 |
|
60 protected final static String USE_GRAMMAR_POOL_ONLY = Constants.XERCES_FEATURE_PREFIX |
|
61 + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE; |
|
62 |
|
63 protected final static String DYNAMIC_VALIDATION = Constants.XERCES_FEATURE_PREFIX |
|
64 + Constants.DYNAMIC_VALIDATION_FEATURE; |
|
65 |
|
66 protected final static String DOCUMENT_CLASS_NAME = Constants.XERCES_PROPERTY_PREFIX |
|
67 + Constants.DOCUMENT_CLASS_NAME_PROPERTY; |
|
68 |
|
69 protected Schema schema; |
|
70 protected Validator fValidator; |
|
71 |
|
72 protected SpecialCaseErrorHandler fErrorHandler; |
|
73 |
|
74 DocumentBuilder builder; |
|
75 protected Document fDocument; |
|
76 |
|
77 protected ElementPSVI fRootNode; |
|
78 |
|
79 protected URL fDocumentURL; |
|
80 protected String documentPath; |
|
81 protected String fDocumentId; |
|
82 |
|
83 static String errMessage; |
|
84 |
|
85 int passed = 0, failed = 0; |
|
86 |
|
87 public static boolean isWindows = false; |
|
88 static { |
|
89 if (System.getProperty("os.name").indexOf("Windows")>-1) { |
|
90 isWindows = true; |
|
91 } |
|
92 }; |
|
93 public static final String USER_DIR = System.getProperty("user.dir", "."); |
|
94 public static final String BASE_DIR = System.getProperty("test.src", USER_DIR) |
|
95 .replaceAll("\\" + System.getProperty("file.separator"), "/"); |
|
96 |
|
97 protected abstract String getSchemaFile(); |
|
98 |
|
99 protected abstract String getXMLDocument(); |
|
100 |
|
101 public BaseTest(String name) { |
|
102 fErrorHandler = new SpecialCaseErrorHandler(getRelevantErrorIDs()); |
|
103 } |
|
104 |
|
105 protected void setUp() throws Exception { |
|
106 |
|
107 DocumentBuilderFactory docFactory = DocumentBuilderFactory |
|
108 .newInstance(); |
|
109 docFactory.setAttribute(DOCUMENT_CLASS_NAME, |
|
110 "com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl"); |
|
111 docFactory.setNamespaceAware(true); |
|
112 builder = docFactory.newDocumentBuilder(); |
|
113 |
|
114 documentPath = BASE_DIR + "/" + getXMLDocument(); |
|
115 System.out.println("documentPath:"+documentPath); |
|
116 if (isWindows) { |
|
117 fDocumentId = "file:/" + documentPath; |
|
118 } else { |
|
119 fDocumentId = "file:" + documentPath; |
|
120 } |
|
121 //fDocumentURL = ClassLoader.getSystemResource(documentPath); |
|
122 //fDocumentURL = getClass().getResource(documentPath); |
|
123 System.out.println("fDocumentId:"+fDocumentId); |
|
124 //System.out.println("fDocumentURL.toExternalForm:"+fDocumentURL.toExternalForm()); |
|
125 /** |
|
126 if (fDocumentURL == null) { |
|
127 throw new FileNotFoundException("Couldn't find xml file for test: " + documentPath); |
|
128 } |
|
129 fDocument = builder.parse(fDocumentURL.toExternalForm()); |
|
130 fRootNode = (ElementPSVI) fDocument.getDocumentElement(); |
|
131 */ |
|
132 SchemaFactory sf = SchemaFactory |
|
133 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
|
134 sf.setFeature(USE_GRAMMAR_POOL_ONLY, getUseGrammarPoolOnly()); |
|
135 String schemaPath = BASE_DIR + "/" + getSchemaFile(); |
|
136 /** |
|
137 URL schemaURL = ClassLoader.getSystemResource(schemaPath); |
|
138 if (schemaURL == null) { |
|
139 throw new FileNotFoundException("Couldn't find schema file for test: " + schemaPath); |
|
140 } |
|
141 */ |
|
142 schema = sf.newSchema(new StreamSource(new File(schemaPath))); |
|
143 } |
|
144 |
|
145 protected void tearDown() throws Exception { |
|
146 fValidator = null; |
|
147 fDocument = null; |
|
148 fRootNode = null; |
|
149 fErrorHandler.reset(); |
|
150 System.out.println("\nNumber of tests passed: " + passed); |
|
151 System.out.println("Number of tests failed: " + failed + "\n"); |
|
152 |
|
153 if (errMessage != null) { |
|
154 throw new RuntimeException(errMessage); |
|
155 } |
|
156 } |
|
157 |
|
158 protected void validateDocument() throws Exception { |
|
159 Source source = new DOMSource(fDocument); |
|
160 source.setSystemId(fDocumentId); |
|
161 Result result = new DOMResult(fDocument); |
|
162 fValidator.validate(source, result); |
|
163 } |
|
164 |
|
165 protected void validateFragment() throws Exception { |
|
166 Source source = new DOMSource((Node) fRootNode); |
|
167 source.setSystemId(fDocumentId); |
|
168 Result result = new DOMResult((Node) fRootNode); |
|
169 fValidator.validate(source, result); |
|
170 } |
|
171 |
|
172 protected void reset() throws Exception { |
|
173 try { |
|
174 System.out.println("new File(documentPath)" + new File(documentPath)); |
|
175 |
|
176 fDocument = builder.parse(new File(documentPath)); |
|
177 fRootNode = (ElementPSVI) fDocument.getDocumentElement(); |
|
178 System.out.println("fDocument" + fDocument); |
|
179 System.out.println("fRootNode" + fRootNode); |
|
180 fValidator = schema.newValidator(); |
|
181 fErrorHandler.reset(); |
|
182 fValidator.setErrorHandler(fErrorHandler); |
|
183 fValidator.setFeature(DYNAMIC_VALIDATION, false); |
|
184 } catch (Exception e) { |
|
185 e.printStackTrace(); |
|
186 } |
|
187 } |
|
188 |
|
189 protected PSVIElementNSImpl getChild(int n) { |
|
190 int numFound = 0; |
|
191 Node child = ((Node) fRootNode).getFirstChild(); |
|
192 while (child != null) { |
|
193 if (child.getNodeType() == Node.ELEMENT_NODE) { |
|
194 numFound++; |
|
195 if (numFound == n) { |
|
196 return (PSVIElementNSImpl) child; |
|
197 } |
|
198 } |
|
199 child = child.getNextSibling(); |
|
200 } |
|
201 return null; |
|
202 } |
|
203 |
|
204 protected String[] getRelevantErrorIDs() { |
|
205 return new String[] {}; |
|
206 } |
|
207 |
|
208 protected boolean getUseGrammarPoolOnly() { |
|
209 return false; |
|
210 } |
|
211 |
|
212 // specialized asserts |
|
213 |
|
214 protected void assertValidity(short expectedValidity, short actualValidity) { |
|
215 String expectedString = expectedValidity == ItemPSVI.VALIDITY_VALID ? "valid" |
|
216 : (expectedValidity == ItemPSVI.VALIDITY_INVALID ? "invalid" |
|
217 : "notKnown"); |
|
218 String actualString = actualValidity == ItemPSVI.VALIDITY_VALID ? "valid" |
|
219 : (actualValidity == ItemPSVI.VALIDITY_INVALID ? "invalid" |
|
220 : "notKnown"); |
|
221 String message = "{validity} was <" + actualString |
|
222 + "> but it should have been <" + expectedString + ">"; |
|
223 assertEquals(message, expectedValidity, actualValidity); |
|
224 } |
|
225 |
|
226 protected void assertValidationAttempted(short expectedAttempted, |
|
227 short actualAttempted) { |
|
228 String expectedString = expectedAttempted == ItemPSVI.VALIDATION_FULL ? "full" |
|
229 : (expectedAttempted == ItemPSVI.VALIDATION_PARTIAL ? "partial" |
|
230 : "none"); |
|
231 String actualString = actualAttempted == ItemPSVI.VALIDATION_FULL ? "full" |
|
232 : (actualAttempted == ItemPSVI.VALIDATION_PARTIAL ? "partial" |
|
233 : "none"); |
|
234 String message = "{validity} was <" + actualString |
|
235 + "> but it should have been <" + expectedString + ">"; |
|
236 assertEquals(message, expectedAttempted, actualAttempted); |
|
237 } |
|
238 |
|
239 protected void assertElementName(String expectedName, String actualName) { |
|
240 assertEquals("Local name of element declaration is wrong.", |
|
241 expectedName, actualName); |
|
242 } |
|
243 |
|
244 protected void assertElementNull(XSElementDeclaration elem) { |
|
245 assertNull("Element declaration should be null.", elem); |
|
246 } |
|
247 |
|
248 protected void assertElementNamespace(String expectedName, String actualName) { |
|
249 assertEquals("Namespace of element declaration is wrong.", |
|
250 expectedName, actualName); |
|
251 } |
|
252 |
|
253 protected void assertElementNamespaceNull(String actualName) { |
|
254 assertNull("Local name of element declaration should be null.", |
|
255 actualName); |
|
256 } |
|
257 |
|
258 protected void assertTypeName(String expectedName, String actualName) { |
|
259 assertEquals("Local name of type definition is wrong.", expectedName, |
|
260 actualName); |
|
261 } |
|
262 |
|
263 protected void assertTypeNull(XSTypeDefinition type) { |
|
264 assertNull("Type definition should be null.", type); |
|
265 } |
|
266 |
|
267 protected void assertTypeNamespace(String expectedName, String actualName) { |
|
268 assertEquals("Namespace of type definition is wrong.", expectedName, |
|
269 actualName); |
|
270 } |
|
271 |
|
272 protected void assertTypeNamespaceNull(String actualName) { |
|
273 assertNull("Namespace of type definition should be null.", actualName); |
|
274 } |
|
275 |
|
276 protected void assertError(String error) { |
|
277 assertTrue("Error <" + error + "> should have occured, but did not.", |
|
278 fErrorHandler.specialCaseFound(error)); |
|
279 } |
|
280 |
|
281 protected void assertNoError(String error) { |
|
282 assertFalse("Error <" + error |
|
283 + "> should not have occured (but it did)", fErrorHandler |
|
284 .specialCaseFound(error)); |
|
285 } |
|
286 |
|
287 protected void assertAnyType(XSTypeDefinition type) { |
|
288 assertEquals("Type is supposed to be anyType", SchemaGrammar.fAnyType, |
|
289 type); |
|
290 } |
|
291 |
|
292 void assertEquals(String msg, Object expected, Object actual) { |
|
293 if (!expected.equals(actual)) { |
|
294 fail(msg + " Expected: " + expected + " Actual: " + actual); |
|
295 } else { |
|
296 success(null); |
|
297 } |
|
298 } |
|
299 void assertNull(String msg, Object value) { |
|
300 if (value != null) { |
|
301 fail(msg); |
|
302 } else { |
|
303 success(null); |
|
304 } |
|
305 } |
|
306 void assertTrue(String msg, boolean value) { |
|
307 if (!value) { |
|
308 fail(msg); |
|
309 } else { |
|
310 success(null); |
|
311 } |
|
312 } |
|
313 void assertFalse(String msg, boolean value) { |
|
314 if (value) { |
|
315 fail(msg); |
|
316 } else { |
|
317 success(null); |
|
318 } |
|
319 } |
|
320 void fail(String errMsg) { |
|
321 if (errMessage == null) { |
|
322 errMessage = errMsg; |
|
323 } else { |
|
324 errMessage = errMessage + "\n" + errMsg; |
|
325 } |
|
326 failed++; |
|
327 } |
|
328 |
|
329 void success(String msg) { |
|
330 passed++; |
|
331 System.out.println(msg); |
|
332 if (msg != null) { |
|
333 if (msg.length() != 0) { |
|
334 System.out.println(msg); |
|
335 } |
|
336 } |
|
337 } |
|
338 } |
|