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