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