Path: blob/master/test/jdk/javax/xml/jaxp/testng/validation/BaseTest.java
41153 views
package validation;12import java.io.File;3import java.io.FileNotFoundException;4import java.net.URL;56import javax.xml.XMLConstants;7import javax.xml.parsers.DocumentBuilder;8import javax.xml.parsers.DocumentBuilderFactory;9import javax.xml.transform.Result;10import javax.xml.transform.Source;11import javax.xml.transform.dom.DOMResult;12import javax.xml.transform.dom.DOMSource;13import javax.xml.validation.Schema;14import javax.xml.validation.SchemaFactory;15import javax.xml.validation.Validator;161718import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl;19import com.sun.org.apache.xerces.internal.impl.Constants;20import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;21import com.sun.org.apache.xerces.internal.xs.ElementPSVI;22import com.sun.org.apache.xerces.internal.xs.ItemPSVI;23import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;24import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;25import java.security.Policy;26import javax.xml.transform.stream.StreamSource;27import org.w3c.dom.Document;28import org.w3c.dom.Node;2930public abstract class BaseTest {31protected final static String ROOT_TYPE = Constants.XERCES_PROPERTY_PREFIX32+ Constants.ROOT_TYPE_DEFINITION_PROPERTY;3334protected final static String IGNORE_XSI_TYPE = Constants.XERCES_FEATURE_PREFIX35+ Constants.IGNORE_XSI_TYPE_FEATURE;3637protected final static String ID_IDREF_CHECKING = Constants.XERCES_FEATURE_PREFIX38+ Constants.ID_IDREF_CHECKING_FEATURE;3940protected final static String IDC_CHECKING = Constants.XERCES_FEATURE_PREFIX41+ Constants.IDC_CHECKING_FEATURE;4243protected final static String UNPARSED_ENTITY_CHECKING = Constants.XERCES_FEATURE_PREFIX44+ Constants.UNPARSED_ENTITY_CHECKING_FEATURE;4546protected final static String USE_GRAMMAR_POOL_ONLY = Constants.XERCES_FEATURE_PREFIX47+ Constants.USE_GRAMMAR_POOL_ONLY_FEATURE;4849protected final static String DYNAMIC_VALIDATION = Constants.XERCES_FEATURE_PREFIX50+ Constants.DYNAMIC_VALIDATION_FEATURE;5152protected final static String DOCUMENT_CLASS_NAME = Constants.XERCES_PROPERTY_PREFIX53+ Constants.DOCUMENT_CLASS_NAME_PROPERTY;5455public static boolean isWindows = false;56static {57if (System.getProperty("os.name").indexOf("Windows")>-1) {58isWindows = true;59}60};6162protected Schema schema;63protected Validator fValidator;6465protected SpecialCaseErrorHandler fErrorHandler;6667protected DocumentBuilder builder;68protected Document fDocument;6970protected ElementPSVI fRootNode;71protected URL fDocumentURL;72protected URL fSchemaURL;7374static String errMessage;7576int passed = 0, failed = 0;77private boolean hasSM;78private Policy orig;7980protected abstract String getSchemaFile();8182protected abstract String getXMLDocument();8384public BaseTest(String name) {85fErrorHandler = new SpecialCaseErrorHandler(getRelevantErrorIDs());86}8788protected void setUp() throws Exception {89if (System.getSecurityManager() != null) {90hasSM = true;91System.setSecurityManager(null);92}9394orig = Policy.getPolicy();9596DocumentBuilderFactory docFactory = DocumentBuilderFactory97.newInstance();98docFactory.setAttribute(DOCUMENT_CLASS_NAME,99"com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl");100docFactory.setNamespaceAware(true);101builder = docFactory.newDocumentBuilder();102// build the location URL of the document103String filepath = System.getProperty("test.src", ".");104String packageDir = this.getClass().getPackage().getName().replace('.',105'/');106String documentPath = filepath + "/" + packageDir + "/" + getXMLDocument();107String schemaPath = filepath + "/" + packageDir + "/" + getSchemaFile();108109if (isWindows) {110fDocumentURL = new URL("file:/" + documentPath);111fSchemaURL = new URL("file:/" + schemaPath);112} else {113fDocumentURL = new URL("file:" + documentPath);114fSchemaURL = new URL("file:" + schemaPath);115}116if (fDocumentURL == null) {117throw new FileNotFoundException("Couldn't find xml file for test: " + documentPath);118}119120SchemaFactory sf = SchemaFactory121.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);122sf.setFeature(USE_GRAMMAR_POOL_ONLY, getUseGrammarPoolOnly());123124if (fSchemaURL == null) {125throw new FileNotFoundException("Couldn't find schema file for test: " + schemaPath);126}127schema = sf.newSchema(fSchemaURL);128129// String schemaPath = "./jaxp-ri/src/unit-test/apache/xerces/jdk8037819/" + getSchemaFile();130// Schema schema = sf.newSchema(new StreamSource(new File(schemaPath)));131}132133protected void tearDown() throws Exception {134System.setSecurityManager(null);135Policy.setPolicy(orig);136if (hasSM) {137System.setSecurityManager(new SecurityManager());138}139140builder = null;141schema = null;142fRootNode = null;143fErrorHandler.reset();144System.out.println("\nNumber of tests passed: " + passed);145System.out.println("Number of tests failed: " + failed + "\n");146147if (errMessage != null) {148throw new RuntimeException(errMessage);149}150}151152protected void validateDocument() throws Exception {153Source source = new DOMSource(fDocument);154source.setSystemId(fDocumentURL.toExternalForm());155Result result = new DOMResult(fDocument);156157fValidator.validate(source, result);158}159160protected void validateFragment() throws Exception {161Source source = new DOMSource((Node) fRootNode);162source.setSystemId(fDocumentURL.toExternalForm());163Result result = new DOMResult((Node) fRootNode);164fValidator.validate(source, result);165}166167protected void reset() throws Exception {168// fDocument = builder.parse(new File("./jaxp-ri/src/unit-test/apache/xerces/jdk8037819/" + getXMLDocument()));169fDocument = builder.parse(fDocumentURL.toExternalForm());170fRootNode = (ElementPSVI) fDocument.getDocumentElement();171fValidator = schema.newValidator();172fErrorHandler.reset();173fValidator.setErrorHandler(fErrorHandler);174fValidator.setFeature(DYNAMIC_VALIDATION, false);175}176177protected PSVIElementNSImpl getChild(int n) {178int numFound = 0;179Node child = ((Node) fRootNode).getFirstChild();180while (child != null) {181if (child.getNodeType() == Node.ELEMENT_NODE) {182numFound++;183if (numFound == n) {184return (PSVIElementNSImpl) child;185}186}187child = child.getNextSibling();188}189return null;190}191192protected String[] getRelevantErrorIDs() {193return new String[] {};194}195196protected boolean getUseGrammarPoolOnly() {197return false;198}199200// specialized asserts201202protected void assertValidity(short expectedValidity, short actualValidity) {203String expectedString = expectedValidity == ItemPSVI.VALIDITY_VALID ? "valid"204: (expectedValidity == ItemPSVI.VALIDITY_INVALID ? "invalid"205: "notKnown");206String actualString = actualValidity == ItemPSVI.VALIDITY_VALID ? "valid"207: (actualValidity == ItemPSVI.VALIDITY_INVALID ? "invalid"208: "notKnown");209String message = "{validity} was <" + actualString210+ "> but it should have been <" + expectedString + ">";211assertEquals(message, expectedValidity, actualValidity);212}213214protected void assertValidationAttempted(short expectedAttempted,215short actualAttempted) {216String expectedString = expectedAttempted == ItemPSVI.VALIDATION_FULL ? "full"217: (expectedAttempted == ItemPSVI.VALIDATION_PARTIAL ? "partial"218: "none");219String actualString = actualAttempted == ItemPSVI.VALIDATION_FULL ? "full"220: (actualAttempted == ItemPSVI.VALIDATION_PARTIAL ? "partial"221: "none");222String message = "{validity} was <" + actualString223+ "> but it should have been <" + expectedString + ">";224assertEquals(message, expectedAttempted, actualAttempted);225}226227protected void assertElementName(String expectedName, String actualName) {228assertEquals("Local name of element declaration is wrong.",229expectedName, actualName);230}231232protected void assertElementNull(XSElementDeclaration elem) {233assertNull("Element declaration should be null.", elem);234}235236protected void assertElementNamespace(String expectedName, String actualName) {237assertEquals("Namespace of element declaration is wrong.",238expectedName, actualName);239}240241protected void assertElementNamespaceNull(String actualName) {242assertNull("Local name of element declaration should be null.",243actualName);244}245246protected void assertTypeName(String expectedName, String actualName) {247assertEquals("Local name of type definition is wrong.", expectedName,248actualName);249}250251protected void assertTypeNull(XSTypeDefinition type) {252assertNull("Type definition should be null.", type);253}254255protected void assertTypeNamespace(String expectedName, String actualName) {256assertEquals("Namespace of type definition is wrong.", expectedName,257actualName);258}259260protected void assertTypeNamespaceNull(String actualName) {261assertNull("Namespace of type definition should be null.", actualName);262}263264protected void assertError(String error) {265assertTrue("Error <" + error + "> should have occured, but did not.",266fErrorHandler.specialCaseFound(error));267}268269protected void assertNoError(String error) {270assertFalse("Error <" + error271+ "> should not have occured (but it did)", fErrorHandler272.specialCaseFound(error));273}274275protected void assertAnyType(XSTypeDefinition type) {276assertEquals("Type is supposed to be anyType", SchemaGrammar.fAnyType,277type);278}279280void assertEquals(String msg, Object expected, Object actual) {281if (!expected.equals(actual)) {282fail(msg + " Expected: " + expected + " Actual: " + actual);283} else {284success(null);285}286}287void assertNull(String msg, Object value) {288if (value != null) {289fail(msg);290} else {291success(null);292}293}294public void assertTrue(String msg, boolean value) {295if (!value) {296fail(msg);297} else {298success(null);299}300}301public void assertFalse(String msg, boolean value) {302if (value) {303fail(msg);304} else {305success(null);306}307}308public void fail(String errMsg) {309if (errMessage == null) {310errMessage = errMsg;311} else {312errMessage = errMessage + "\n" + errMsg;313}314failed++;315}316317public void success(String msg) {318passed++;319if (msg != null) {320if (msg.length() != 0) {321System.out.println(msg);322}323}324}325}326327328