8264454: Jaxp unit test from open jdk needs to be improved

Reviewed-by: joehw
This commit is contained in:
Mahendra Chhipa 2021-04-08 21:21:11 +00:00 committed by Joe Wang
parent 5bd6c74547
commit 308f6796da
8 changed files with 151 additions and 686 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,12 +23,10 @@
package common;
import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -46,32 +44,22 @@ public class Bug6350682 {
@Test
public void testSAXParserFactory() {
try {
runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
if (Bug6350682.class.getClassLoader() == null)
System.out.println("this class loader is NULL");
else
System.out.println("this class loader is NOT NULL");
SAXParserFactory factory = SAXParserFactory.newInstance();
Assert.assertTrue(factory != null, "Failed to get an instance of a SAXParserFactory");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
// This test run in othervm so change in environment need not to be recovered at the end of test.
runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
if (Bug6350682.class.getClassLoader() == null) {
System.out.println("this class loader is NULL");
} else {
System.out.println("this class loader is NOT NULL");
}
SAXParserFactory factory = SAXParserFactory.newInstance();
Assert.assertNotNull(factory, "Failed to get an instance of a SAXParserFactory");
}
@Test
public void testTransformerFactory() {
try {
runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
TransformerFactory factory = TransformerFactory.newInstance();
Assert.assertTrue(factory != null, "Failed to get an instance of a TransformerFactory");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
} catch (TransformerFactoryConfigurationError error) {
error.printStackTrace();
Assert.fail(error.toString());
}
// This test run in othervm so change in environment need not to be recovered at the end of test.
runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
TransformerFactory factory = TransformerFactory.newInstance();
Assert.assertNotNull(factory, "Failed to get an instance of a TransformerFactory");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,14 +23,15 @@
package common;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.Assert;
import java.net.URL;
import java.net.URLClassLoader;
import javax.xml.parsers.SAXParserFactory;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
/*
* @test
* @bug 6723276
@ -41,27 +42,28 @@ import javax.xml.parsers.SAXParserFactory;
*/
@Listeners({jaxp.library.BasePolicy.class})
public class Bug6723276Test {
private static final String ERR_MSG = "org.apache.xerces.jaxp.SAXParserFactoryImpl not found";
@Test
public void test1() {
public void testWithDefaultContextClassLoader() {
try {
SAXParserFactory.newInstance();
} catch (Exception e) {
if (e.getMessage().indexOf("org.apache.xerces.jaxp.SAXParserFactoryImpl not found") > 0) {
if (e.getMessage().contains(ERR_MSG)) {
Assert.fail(e.getMessage());
}
}
}
@Test
public void test2() {
public void testWithGivenURLContextClassLoader() {
try {
System.out.println(Thread.currentThread().getContextClassLoader());
System.out.println(ClassLoader.getSystemClassLoader().getParent());
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader().getParent()));
SAXParserFactory.newInstance();
} catch (Exception e) {
if (e.getMessage().indexOf("org.apache.xerces.jaxp.SAXParserFactoryImpl not found") > 0) {
if (e.getMessage().contains(ERR_MSG)) {
Assert.fail(e.getMessage());
}
}

View File

@ -1,10 +0,0 @@
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Bug6941169.xsd">
<fooTest>
test
information
</fooTest>
</root>

View File

@ -1,11 +0,0 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="fooTest" type="xs:anySimpleType" fixed="test information"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -1,496 +0,0 @@
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package common;
import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
import static jaxp.library.JAXPTestUtilities.getSystemProperty;
import static jaxp.library.JAXPTestUtilities.setSystemProperty;
import java.io.FilePermission;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import jaxp.library.JAXPTestUtilities;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/*
* @test
* @bug 6941169
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true common.Bug6941169Test
* @run testng/othervm common.Bug6941169Test
* @summary Test use-service-mechanism feature.
*/
@Test(singleThreaded = true)
@Listeners({ jaxp.library.FilePolicy.class })
public class Bug6941169Test {
static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
// impl specific feature
final String ORACLE_FEATURE_SERVICE_MECHANISM = "http://www.oracle.com/feature/use-service-mechanism";
static String _xml = Bug6941169Test.class.getResource("Bug6941169.xml").getPath();
static String _xsd = Bug6941169Test.class.getResource("Bug6941169.xsd").getPath();
@Test
public void testValidation_SAX_withoutServiceMech() {
System.out.println("Validation using SAX Source; Service mechnism is turned off; SAX Impl should be the default:");
InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
SAXSource ss = new SAXSource(is);
setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
long start = System.currentTimeMillis();
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
Schema schema = factory.newSchema(new StreamSource(_xsd));
Validator validator = schema.newValidator();
validator.validate(ss, null);
} catch (Exception e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(SAX_FACTORY_ID);
}
@Test
public void testValidation_SAX_withServiceMech() {
System.out.println("Validation using SAX Source. Using service mechnism (by default) to find SAX Impl:");
InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
SAXSource ss = new SAXSource(is);
setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
long start = System.currentTimeMillis();
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(_xsd));
Validator validator = schema.newValidator();
validator.validate(ss, null);
Assert.fail("User impl MySAXFactoryImpl should be used.");
} catch (Exception e) {
String error = e.getMessage();
if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
// expected
}
// System.out.println(e.getMessage());
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(SAX_FACTORY_ID);
}
@Test
public void testValidation_SAX_withSM() throws Exception {
if(System.getSecurityManager() == null)
return;
System.out.println("Validation using SAX Source with security manager:");
InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
SAXSource ss = new SAXSource(is);
setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
long start = System.currentTimeMillis();
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
Schema schema = factory.newSchema(new StreamSource(_xsd));
Validator validator = schema.newValidator();
validator.validate(ss, null);
} catch (Exception e) {
String error = e.getMessage();
if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
} finally {
clearSystemProperty(SAX_FACTORY_ID);
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
}
@Test
public void testTransform_DOM_withoutServiceMech() {
System.out.println("Transform using DOM Source; Service mechnism is turned off; Default DOM Impl should be the default:");
DOMSource domSource = new DOMSource();
domSource.setSystemId(_xml);
// DOMSource domSource = new
// DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
Transformer t = factory.newTransformer();
StringWriter result = new StringWriter();
StreamResult streamResult = new StreamResult(result);
t.transform(domSource, streamResult);
System.out.println("Writing to " + result.toString());
} catch (Exception e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
} catch (Error e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(DOM_FACTORY_ID);
}
/** this is by default */
@Test
public void testTransform_DOM_withServiceMech() {
System.out.println("Transform using DOM Source; By default, the factory uses services mechanism to look up impl:");
DOMSource domSource = new DOMSource();
domSource.setSystemId(_xml);
// DOMSource domSource = new
// DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer t = factory.newTransformer();
StringWriter result = new StringWriter();
StreamResult streamResult = new StreamResult(result);
t.transform(domSource, streamResult);
System.out.println("Writing to " + result.toString());
Assert.fail("User impl MyDOMFactoryImpl should be used.");
} catch (Exception e) {
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
// expected
}
System.out.println(error);
} catch (Error e) {
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
// expected
}
System.out.println(error);
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(DOM_FACTORY_ID);
}
@Test
public void testTransform_DOM_withSM() throws Exception {
if(System.getSecurityManager() == null)
return;
System.out.println("Transform using DOM Source; Security Manager is set:");
DOMSource domSource = new DOMSource();
domSource.setSystemId(_xml);
// DOMSource domSource = new
// DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
TransformerFactory factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
TransformerFactory.class.getClassLoader());
Transformer t = factory.newTransformer();
StringWriter result = new StringWriter();
StreamResult streamResult = new StreamResult(result);
t.transform(domSource, streamResult);
System.out.println("Writing to " + result.toString());
} catch (Exception e) {
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
} catch (Error e) {
String error = e.getMessage();
if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
} finally {
clearSystemProperty(DOM_FACTORY_ID);
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
}
@Test
public void testXPath_DOM_withoutServiceMech() {
final String XPATH_EXPRESSION = "/fooTest";
System.out.println("Evaluate DOM Source; Service mechnism is turned off; Default DOM Impl should be used:");
Document doc = getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
xPathFactory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
XPath xPath = xPathFactory.newXPath();
String xPathResult = xPath.evaluate(XPATH_EXPRESSION, doc);
} catch (Exception e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
} catch (Error e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl is used");
}
// System.out.println(e.getMessage());
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(DOM_FACTORY_ID);
}
@Test
public void testXPath_DOM_withServiceMech() {
/**
* This is in conflict with the test testXPath_DOM_withSM where the system
* default parser is used when the security manager is present. The test
* is therefore skipped when the security manager is present.
*/
if (System.getSecurityManager() != null) {
return;
}
final String XPATH_EXPRESSION = "/fooTest";
System.out.println("Evaluate DOM Source; Service mechnism is on by default; It would try to use MyDOMFactoryImpl:");
InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
InputSource source = new InputSource(input);
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
Assert.fail("User impl MyDOMFactoryImpl should be used.");
} catch (Exception e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
System.out.println("Tried to locate MyDOMFactoryImpl");
} else {
Assert.fail(e.getMessage());
}
// System.out.println(e.getMessage());
} catch (Error e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
System.out.println("Tried to locate MyDOMFactoryImpl");
} else {
Assert.fail(e.getMessage());
}
// System.out.println(e.getMessage());
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
clearSystemProperty(DOM_FACTORY_ID);
}
@Test
public void testXPath_DOM_withSM() throws Exception {
if(System.getSecurityManager() == null)
return;
final String XPATH_EXPRESSION = "/fooTest";
System.out.println("Evaluate DOM Source; Security Manager is set:");
InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
InputSource source = new InputSource(input);
setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
long start = System.currentTimeMillis();
try {
XPathFactory xPathFactory = XPathFactory.newInstance("http://java.sun.com/jaxp/xpath/dom",
"com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null);
XPath xPath = xPathFactory.newXPath();
String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
System.out.println("Use default impl");
} catch (Exception e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl should be used");
}
// System.out.println(e.getMessage());
} catch (Error e) {
// e.printStackTrace();
String error = e.getMessage();
if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
Assert.fail(e.getMessage());
} else {
System.out.println("Default impl should be used");
}
// System.out.println(e.getMessage());
} finally {
clearSystemProperty(DOM_FACTORY_ID);
}
long end = System.currentTimeMillis();
double elapsedTime = ((end - start));
System.out.println("Time elapsed: " + elapsedTime);
}
@Test
public void testSM() {
SecurityManager sm = System.getSecurityManager();
if (System.getSecurityManager() != null) {
System.out.println("Security manager not cleared: " + sm.toString());
} else {
System.out.println("Security manager cleared: ");
}
}
private static Document getDocument(InputStream in) {
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(in);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
return document;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,14 +23,13 @@
package common;
import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
import static jaxp.library.JAXPTestUtilities.setSystemProperty;
import javax.xml.XMLConstants;
import javax.xml.transform.TransformerFactory;
import javax.xml.validation.SchemaFactory;
import javax.xml.xpath.XPathFactory;
import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
import static jaxp.library.JAXPTestUtilities.setSystemProperty;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -45,14 +44,14 @@ import org.testng.annotations.Test;
@Listeners({ jaxp.library.BasePolicy.class })
@Test(singleThreaded = true)
public class Bug7143711Test {
static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
private static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
private static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
// impl specific feature
final String ORACLE_FEATURE_SERVICE_MECHANISM = "http://www.oracle.com/feature/use-service-mechanism";
private static final String ORACLE_FEATURE_SERVICE_MECHANISM = "http://www.oracle.com/feature/use-service-mechanism";
@Test
public void testValidation_SAX_withSM() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@ import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -44,67 +45,38 @@ public class Bug6320118 {
DatatypeFactory df;
@BeforeClass
public void createDataTypeFactory() throws DatatypeConfigurationException {
df = DatatypeFactory.newInstance();
}
@Test
public void test1() {
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
Assert.fail(e.getMessage());
}
try {
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar(1970, 1, 1, 24, 0, 0, 0, 0);
} catch (IllegalArgumentException e) {
Assert.fail(e.getMessage());
}
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar(1970, 1, 1, 24, 0, 0, 0, 0);
Assert.assertEquals(calendar.getYear(), 1970);
Assert.assertEquals(calendar.getMonth(), 1);
Assert.assertEquals(calendar.getDay(), 2);
Assert.assertEquals(calendar.getHour(), 0, "hour 24 needs to be treated as hour 0 of next day");
}
@Test
public void test2() {
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
Assert.fail(e.getMessage());
}
try {
XMLGregorianCalendar calendar = df.newXMLGregorianCalendarTime(24, 0, 0, 0);
} catch (IllegalArgumentException e) {
Assert.fail(e.getMessage());
}
XMLGregorianCalendar calendar = df.newXMLGregorianCalendarTime(24, 0, 0, 0);
Assert.assertEquals(calendar.getHour(), 0, "hour 24 needs to be treated as hour 0 of next day");
}
@Test
@Test(expectedExceptions = IllegalArgumentException.class)
public void test3() {
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
Assert.fail(e.getMessage());
}
try {
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar();
// Must fail as other params are not 0 but undefined
calendar.setHour(24);
Assert.fail("test3() - Expected IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
// falls through
}
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar();
// Must fail as other params are not 0 but undefined
calendar.setHour(24);
}
@Test
public void test4() {
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
Assert.fail(e.getMessage());
}
try {
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar();
calendar.setTime(24, 0, 0, 0);
} catch (IllegalArgumentException e) {
Assert.fail(e.getMessage());
}
XMLGregorianCalendar calendar = df.newXMLGregorianCalendar();
calendar.setTime(24, 0, 0, 0);
Assert.assertEquals(calendar.getHour(), 0, "hour 24 needs to be treated as hour 0 of next day");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,6 +33,7 @@ import javax.xml.datatype.Duration;
import javax.xml.namespace.QName;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -55,15 +56,10 @@ public class Bug6937964Test {
*/
private static final String TEST_VALUE_FAIL = "*FAIL*";
private static final String FIELD_UNDEFINED = "FIELD_UNDEFINED";
static final DatatypeConstants.Field[] fields = { DatatypeConstants.YEARS, DatatypeConstants.MONTHS, DatatypeConstants.DAYS, DatatypeConstants.HOURS,
DatatypeConstants.MINUTES, DatatypeConstants.SECONDS };
@Test
public void test() throws DatatypeConfigurationException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationYearMonth("P20Y15M");
int years = d.getYears();
System.out.println(d.getYears() == 21 ? "pass" : "fail");
}
@ -72,7 +68,7 @@ public class Bug6937964Test {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationYearMonth("P20Y15M");
int years = d.getYears();
Assert.assertTrue(years == 21, "Return value should be normalized");
Assert.assertEquals(years, 21, "Return value should be normalized");
}
@Test
@ -81,7 +77,7 @@ public class Bug6937964Test {
Duration d = dtf.newDurationYearMonth(671976000000L);
int years = d.getYears();
System.out.println("Years: " + years);
Assert.assertTrue(years == 21, "Return value should be normalized");
Assert.assertEquals(years, 21, "Return value should be normalized");
}
@Test
@ -91,7 +87,7 @@ public class Bug6937964Test {
BigInteger mon = new BigInteger("15");
Duration d = dtf.newDurationYearMonth(true, year, mon);
int years = d.getYears();
Assert.assertTrue(years == 21, "Return value should be normalized");
Assert.assertEquals(years, 21, "Return value should be normalized");
}
@Test
@ -99,7 +95,7 @@ public class Bug6937964Test {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationYearMonth(true, 20, 15);
int years = d.getYears();
Assert.assertTrue(years == 21, "Return value should be normalized");
Assert.assertEquals(years, 21, "Return value should be normalized");
}
@Test
@ -107,7 +103,7 @@ public class Bug6937964Test {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationDayTime("P1DT23H59M65S");
int days = d.getDays();
Assert.assertTrue(days == 2, "Return value should be normalized");
Assert.assertEquals(days, 2, "Return value should be normalized");
}
@Test
@ -115,7 +111,7 @@ public class Bug6937964Test {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationDayTime(172805000L);
int days = d.getDays();
Assert.assertTrue(days == 2, "Return value should be normalized");
Assert.assertEquals(days, 2, "Return value should be normalized");
}
@Test
@ -128,7 +124,7 @@ public class Bug6937964Test {
Duration d = dtf.newDurationDayTime(true, day, hour, min, sec);
int days = d.getDays();
System.out.println("Days: " + days);
Assert.assertTrue(days == 2, "Return value should be normalized");
Assert.assertEquals(days, 2, "Return value should be normalized");
}
@Test
@ -137,16 +133,27 @@ public class Bug6937964Test {
Duration d = dtf.newDurationDayTime(true, 1, 23, 59, 65);
int days = d.getDays();
System.out.println("Days: " + days);
Assert.assertTrue(days == 2, "Return value should be normalized");
Assert.assertEquals(days, 2, "Return value should be normalized");
}
@Test
public final void testNewDurationYearMonthLexicalRepresentation1() {
@DataProvider(name = "lexicalvalues")
public Object[][] actualAndExpectedLexicals() {
Object actualAndExpected [][] = {
{"P13M", "P1Y1M"},
{"-P13M", "-P1Y1M"},
{"P1Y", "P1Y"},
{"-P1Y", "-P1Y"},
{"P1Y25M", "P3Y1M"},
{"-P1Y25M", "-P3Y1M"}
};
return actualAndExpected;
}
@Test(dataProvider = "lexicalvalues")
public void testNewDurationYearMonthLexicalRepresentation1(String actualLex, String expectedLex) {
/**
* Lexical test values to test.
*/
final String[] TEST_VALUES_LEXICAL = { "P13M", "P1Y1M", "-P13M", "-P1Y1M", "P1Y", "P1Y", "-P1Y", "-P1Y", "P1Y25M", "P3Y1M", "-P1Y25M", "-P3Y1M" };
DatatypeFactory datatypeFactory = null;
try {
@ -158,78 +165,70 @@ public class Bug6937964Test {
if (DEBUG) {
System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
}
if (DEBUG) {
System.err.println("testing value: \"" + actualLex + "\", expecting: \"" + expectedLex + "\"");
}
// test each value
for (int onTestValue = 0; onTestValue < TEST_VALUES_LEXICAL.length; onTestValue = onTestValue + 2) {
try {
Duration duration = datatypeFactory.newDurationYearMonth(actualLex);
if (DEBUG) {
System.err.println("testing value: \"" + TEST_VALUES_LEXICAL[onTestValue] + "\", expecting: \"" + TEST_VALUES_LEXICAL[onTestValue + 1] + "\"");
System.err.println("Duration created: \"" + duration.toString() + "\"");
}
// was this expected to fail?
Assert.assertNotEquals(expectedLex, TEST_VALUE_FAIL, "the value \"" + actualLex + "\" is invalid " +
"yet it created the Duration \"" + duration.toString() + "\"" );
// right XMLSchemaType?
// TODO: enable test, it should pass, it fails with Exception(s)
// for now due to a bug
try {
Duration duration = datatypeFactory.newDurationYearMonth(TEST_VALUES_LEXICAL[onTestValue]);
if (DEBUG) {
System.err.println("Duration created: \"" + duration.toString() + "\"");
}
// was this expected to fail?
if (TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is invalid yet it created the Duration \"" + duration.toString() + "\"");
}
// right XMLSchemaType?
// TODO: enable test, it should pass, it fails with Exception(s)
// for now due to a bug
try {
QName xmlSchemaType = duration.getXMLSchemaType();
if (!xmlSchemaType.equals(DatatypeConstants.DURATION_YEARMONTH)) {
Assert.fail("Duration created with XMLSchemaType of\"" + xmlSchemaType + "\" was expected to be \""
+ DatatypeConstants.DURATION_YEARMONTH + "\" and has the value \"" + duration.toString() + "\"");
}
QName xmlSchemaType = duration.getXMLSchemaType();
Assert.assertEquals(xmlSchemaType, DatatypeConstants.DURATION_YEARMONTH, "Duration created with " +
"XMLSchemaType of\"" + xmlSchemaType + "\" was expected to be \""
+ DatatypeConstants.DURATION_YEARMONTH + "\" and has the value \"" + duration.toString() + "\"");
} catch (IllegalStateException illegalStateException) {
// TODO; this test really should pass
System.err.println("Please fix this bug that is being ignored, for now: " + illegalStateException.getMessage());
}
// does it have the right value?
if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(duration.toString())) {
Assert.fail("Duration created with \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" was expected to be \""
+ TEST_VALUES_LEXICAL[onTestValue + 1] + "\" and has the value \"" + duration.toString() + "\"");
}
Assert.assertEquals(duration.toString(), expectedLex, "Duration created with \"" + actualLex +
"\" was expected to be \"" + expectedLex + "\" and has the value \"" + duration.toString() + "\"");
// Duration created with correct value
} catch (Exception exception) {
if (DEBUG) {
System.err.println("Exception in creating duration: \"" + exception.toString() + "\"");
}
// was this expected to succed?
if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is valid yet it failed with \"" + exception.toString() + "\"");
}
Assert.assertEquals(TEST_VALUE_FAIL, expectedLex, "the value \"" + actualLex + "\" is valid yet " +
"it failed with \"" + exception.toString() + "\"");
// expected failure
}
}
}
/**
* TCK test failure
*/
@Test
public void testNewDurationDayTime005() {
@DataProvider(name = "lexDayTime")
public Object[][] lexDayTimeData() {
BigInteger one = new BigInteger("1");
BigInteger zero = new BigInteger("0");
BigDecimal bdZero = new BigDecimal("0");
BigDecimal bdOne = new BigDecimal("1");
Object[][] values = {
// lex, isPositive, years, month, days, hours, minutes, seconds
{ "P1D", Boolean.TRUE, null, null, one, zero, zero, bdZero }, { "PT1H", Boolean.TRUE, null, null, zero, one, zero, bdZero },
{ "PT1M", Boolean.TRUE, null, null, zero, zero, one, bdZero }, { "PT1.1S", Boolean.TRUE, null, null, zero, zero, zero, bdOne },
{ "-PT1H1.1S", Boolean.FALSE, null, null, zero, one, zero, bdOne }, };
return values;
}
/**
* TCK test failure
*/
@Test(dataProvider = "lexDayTime")
public void testNewDurationDayTime005(String lex, boolean isPositive, BigInteger year, BigInteger month, BigInteger days,
BigInteger hour, BigInteger minutes, BigDecimal seconds) {
StringBuffer result = new StringBuffer();
DatatypeFactory df = null;
@ -239,34 +238,56 @@ public class Bug6937964Test {
Assert.fail(e.toString());
}
for (int valueIndex = 0; valueIndex < values.length; ++valueIndex) {
Duration duration = null;
try {
duration = df.newDurationDayTime(values[valueIndex][1].equals(Boolean.TRUE), ((BigInteger) values[valueIndex][4]).intValue(),
((BigInteger) values[valueIndex][5]).intValue(), ((BigInteger) values[valueIndex][6]).intValue(),
((BigDecimal) values[valueIndex][7]).intValue());
} catch (IllegalArgumentException e) {
result.append("; unexpected " + e + " trying to create duration \'" + values[valueIndex][0] + "\'");
Duration duration = null;
try {
duration = df.newDurationDayTime(isPositive, days, hour, minutes, seconds.toBigInteger());
} catch (IllegalArgumentException e) {
result.append("; unexpected " + e + " trying to create duration \'" + lex + "\'");
}
if (duration != null) {
if ((duration.getSign() == 1) != isPositive) {
result.append("; " + lex + ": wrong sign " + duration.getSign() + ", expected " + isPositive);
}
if (duration != null) {
if ((duration.getSign() == 1) != values[valueIndex][1].equals(Boolean.TRUE)) {
result.append("; " + values[valueIndex][0] + ": wrong sign " + duration.getSign() + ", expected " + values[valueIndex][1]);
}
for (int i = 0; i < fields.length; ++i) {
Number value = duration.getField(fields[i]);
if ((value != null && values[valueIndex][2 + i] == null) || (value == null && values[valueIndex][2 + i] != null)
|| (value != null && !value.equals(values[valueIndex][2 + i]))) {
result.append("; " + values[valueIndex][0] + ": wrong value of the field " + fields[i] + ": \'" + value + "\'" + ", expected \'"
+ values[valueIndex][2 + i] + "\'");
}
}
Number value = duration.getField(DatatypeConstants.YEARS);
if ((value != null && year == null) || (value == null && year != null) || (value != null && !value.equals(year))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.YEARS + ": \'" + value + "\'" + ", expected \'"
+ year + "\'");
}
value = duration.getField(DatatypeConstants.MONTHS);
if ((value != null && month == null) || (value == null && month != null) || (value != null && !value.equals(month))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.MONTHS + ": \'" + value + "\'" + ", expected \'"
+ month + "\'");
}
value = duration.getField(DatatypeConstants.DAYS);
if ((value != null && days == null) || (value == null && days != null) || (value != null && !value.equals(days))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.DAYS + ": \'" + value + "\'" + ", expected \'"
+ days + "\'");
}
value = duration.getField(DatatypeConstants.HOURS);
if ((value != null && hour == null) || (value == null && hour != null) || (value != null && !value.equals(hour))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.HOURS + ": \'" + value + "\'" + ", expected \'"
+ hour + "\'");
}
value = duration.getField(DatatypeConstants.MINUTES);
if ((value != null && minutes == null) || (value == null && minutes != null) || (value != null && !value.equals(minutes))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.MINUTES + ": \'" + value + "\'" + ", expected \'"
+ minutes + "\'");
}
value = duration.getField(DatatypeConstants.SECONDS);
if ((value != null && seconds == null) || (value == null && seconds != null) || (value != null && !value.equals(seconds))) {
result.append("; " + lex + ": wrong value of the field " + DatatypeConstants.SECONDS + ": \'" + value + "\'" + ", expected \'"
+ seconds + "\'");
}
}
if (result.length() > 0) {
if(result.length() > 0) {
Assert.fail(result.substring(2));
}
System.out.println("OK");
}
}