8051563: Update JAXP functional tests

Reviewed-by: lancea, joehw
This commit is contained in:
Tristan Yan 2015-01-15 19:10:56 -08:00 committed by Joe Wang
parent 7d89dd0039
commit 120cde915b
79 changed files with 4393 additions and 5334 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,64 +23,59 @@
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
import javax.xml.transform.Transformer; import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXResult;
import jaxp.library.JAXPFileBaseTest;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/** /**
* This tests DocumentBuilderFactory for namespace processing and no-namespace * This tests DocumentBuilderFactory for namespace processing and no-namespace
* processing. * processing.
*/ */
public class DBFNamespaceTest { public class DBFNamespaceTest extends JAXPFileBaseTest {
/** /**
* Provide input for the cases that supporting namespace or not. * Provide input for the cases that supporting namespace or not.
* @return a two-dimensional array contains factory, output file name and
* golden validate file name.
*/ */
@DataProvider(name = "input-provider") @DataProvider(name = "input-provider")
public Object[][] getInput() { public Object[][] getInput() {
DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance();
String outputfile1 = USER_DIR + FILE_SEP + "dbfnstest01.out"; String outputfile1 = USER_DIR + "dbfnstest01.out";
String goldfile1 = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest01GF.out"; String goldfile1 = GOLDEN_DIR + "dbfnstest01GF.out";
DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance();
dbf2.setNamespaceAware(true); dbf2.setNamespaceAware(true);
String outputfile2 = USER_DIR + FILE_SEP + "dbfnstest02.out"; String outputfile2 = USER_DIR + "dbfnstest02.out";
String goldfile2 = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest02GF.out"; String goldfile2 = GOLDEN_DIR + "dbfnstest02GF.out";
return new Object[][] { { dbf1, outputfile1, goldfile1 }, { dbf2, outputfile2, goldfile2 } }; return new Object[][] { { dbf1, outputfile1, goldfile1 }, { dbf2, outputfile2, goldfile2 } };
} }
/** /**
* Test to parse and transform a document without supporting namespace and * Test to parse and transform a document without supporting namespace and
* with supporting namespace. * with supporting namespace.
* @param dbf a Document Builder factory for creating document object.
* @param outputfile output file name.
* @param goldfile golden validate file name.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "input-provider") @Test(dataProvider = "input-provider")
public void testNamespaceTest(DocumentBuilderFactory dbf, String outputfile, String goldfile) { public void testNamespaceTest(DocumentBuilderFactory dbf, String outputfile,
try { String goldfile) throws Exception {
Document doc = dbf.newDocumentBuilder().parse(new File(TestUtils.XML_DIR, "namespace1.xml")); Document doc = dbf.newDocumentBuilder().parse(new File(XML_DIR, "namespace1.xml"));
dummyTransform(doc, outputfile); dummyTransform(doc, outputfile);
assertTrue(compareWithGold(goldfile, outputfile)); assertTrue(compareWithGold(goldfile, outputfile));
} catch (SAXException | IOException | ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
failUnexpected(e);
}
} }
/** /**
@ -89,16 +84,14 @@ public class DBFNamespaceTest {
* not chosen, namespaceURI in callbacks should be an empty string otherwise * not chosen, namespaceURI in callbacks should be an empty string otherwise
* it should be namespaceURI. * it should be namespaceURI.
* *
* @throws TransformerFactoryConfigurationError * @throws Exception If any errors occur.
* @throws TransformerException
* @throws IOException
*/ */
private void dummyTransform(Document document, String fileName) throws TransformerFactoryConfigurationError, TransformerException, IOException { private void dummyTransform(Document document, String fileName)
throws Exception {
DOMSource domSource = new DOMSource(document); DOMSource domSource = new DOMSource(document);
Transformer transformer = TransformerFactory.newInstance().newTransformer(); try(MyCHandler chandler = MyCHandler.newInstance(new File(fileName))) {
File file = new File(fileName); TransformerFactory.newInstance().newTransformer().
System.out.println("The fileName is " + file.getAbsolutePath()); transform(domSource, new SAXResult(chandler));
transformer.transform(domSource, new SAXResult(MyCHandler.newInstance(file))); }
} }
} }

View file

@ -1,451 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* This checks the methods of DocumentBuilderFactoryImpl
*/
public class DocumentBuilderFactory01 {
/**
* Testcase to test the default functionality of schema support method.
*/
@Test
public void testCheckSchemaSupport1() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "test.xml"));
assertFalse(eh.errorOccured);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the default functionality of schema support method. In
* this case the schema source property is set.
*/
@Test
public void testCheckSchemaSupport2() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", new InputSource(new FileInputStream(
new File(TestUtils.XML_DIR, "test.xsd"))));
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "test1.xml"));
assertFalse(eh.errorOccured);
} catch (IllegalArgumentException | ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the default functionality of schema support method. In
* this case the schema source property is set.
*/
@Test
public void testCheckSchemaSupport3() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
spf.setNamespaceAware(true);
SAXParser sp = spf.newSAXParser();
sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
new InputSource(new FileInputStream(new File(TestUtils.XML_DIR, "test.xsd"))));
DefaultHandler dh = new DefaultHandler();
sp.parse(new File(TestUtils.XML_DIR, "test1.xml"), dh);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the default functionality of newInstance method. To test
* the isCoalescing method and setCoalescing This checks to see if the CDATA
* and text nodes got combined In that case it will print "<xml>This
* is not parsed</xml> yet".
*/
@Test
public void testCheckDocumentBuilderFactory02() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory01.xml"));
Element e = (Element) doc.getElementsByTagName("html").item(0);
NodeList nl = e.getChildNodes();
assertEquals(nl.item(0).getNodeValue().trim(), "<xml>This is not parsed</xml> yet");
} catch (IOException | SAXException | ParserConfigurationException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the isIgnoringComments. By default it is false.
*/
@Test
public void testCheckDocumentBuilderFactory03() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
assertFalse(dbf.isIgnoringComments());
}
/**
* Testcase to test the isValidating. By default it is false, set it to true
* and then use a document which is not valid. It should throw a warning or
* an error at least. The test passes in case retval 0 is set in the error
* method .
*/
@Test
public void testCheckDocumentBuilderFactory04() {
try {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml"));
assertTrue(eh.errorOccured);
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setValidating. By default it is false, use a
* document which is not valid. It should not throw a warning or an error.
* The test passes in case the retval equals 1 .
*/
@Test
public void testCheckDocumentBuilderFactory16() {
try {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml"));
assertFalse(eh.errorOccured);
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setValidating. By default it is false, use a
* document which is valid. It should not throw a warning or an error. The
* test passes in case the retval equals 1.
*/
@Test
public void testCheckDocumentBuilderFactory17() {
try {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml"));
assertFalse(eh.errorOccured);
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* To test the isExpandEntityReferences. By default it is true.
*/
@Test
public void testCheckDocumentBuilderFactory05() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertTrue(dbf.isExpandEntityReferences());
assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the default functionality of setValidating method. The
* xml file has a DTD which has namespaces defined. The parser takes care to
* check if the namespaces using elements and defined attributes are there
* or not.
*/
@Test
public void testCheckDocumentBuilderFactory06() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
MyErrorHandler eh = MyErrorHandler.newInstance();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml"));
assertTrue(doc instanceof Document);
assertFalse(eh.errorOccured);
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setExpandEntityReferences.
*/
@Test
public void testCheckDocumentBuilderFactory07() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertTrue(dbf.isExpandEntityReferences());
assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setExpandEntityReferences.
*/
@Test
public void testCheckDocumentBuilderFactory08() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertNull(nl.item(0).getNodeValue());
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setIgnoringComments. By default it is set to false.
* explicitly setting it to false, it recognizes the comment which is in
* Element Node Hence the Element's child node is not null.
*/
@Test
public void testCheckDocumentBuilderFactory09() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(false);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")));
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertNotNull(nl.item(0).getNodeValue());
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse(InputSource).
*/
@Test
public void testCheckDocumentBuilderFactory10() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new BufferedReader(new FileReader(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")))));
assertTrue(doc instanceof Document);
} catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse InputStream with SystemID as a second parameter.
*/
@Test
public void testCheckDocumentBuilderFactory11() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), new File(TestUtils.XML_DIR).toURI()
.toASCIIString());
assertTrue(doc instanceof Document);
} catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse InputStream with empty SystemID as a second
* parameter.
*/
@Test
public void testCheckDocumentBuilderFactory12() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), " ");
assertTrue(doc instanceof Document);
} catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse(uri).
*/
@Test
public void testCheckDocumentBuilderFactory13() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File(TestUtils.XML_DIR + FILE_SEP + "dbf10import.xsl").toURI().toASCIIString());
assertTrue(doc instanceof Document);
} catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse (uri) with empty string as parameter should
* throw Sax Exception.
*
* @throws SAXException
* If any parse errors occur.
*/
@Test(expectedExceptions = SAXException.class)
public void testCheckDocumentBuilderFactory14() throws SAXException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.parse("");
} catch (ParserConfigurationException | IOException e) {
failUnexpected(e);
}
}
/**
* This tests for the parse (uri) with null uri as parameter should throw
* IllegalArgumentException.
*
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testCheckDocumentBuilderFactory15() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
String uri = null;
docBuilder.parse(uri);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setIgnoringComments. By default it is set to false,
* setting this to true, It does not recognize the comment, Here the
* nodelist has a length 0 because the ignoring comments is true.
*/
@Test
public void testCheckIgnoringComments() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory08.xml")));
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertEquals(nl.getLength(), 0);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the default behaviour of setIgnoringComments. By default
* it is set to false, this is similar to case 9 but not setIgnoringComments
* explicitly, it does not recognize the comment.
*/
@Test
public void testCheckIgnoringComments1() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")));
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertFalse(dbf.isIgnoringComments());
assertNotNull(nl.item(0).getNodeValue());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
}
}

View file

@ -1,87 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* This tests the setIgnoringElementWhitespace and setIgnoringComments of
* DocumentBuilderFactory
*/
public class DocumentBuilderFactory02 {
/**
* This testcase tests for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
*/
@Test
public void testCheckElementContentWhitespace() {
try {
String goldFile = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfactory02GF.out";
String outputFile = USER_DIR + FILE_SEP + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.errorOccured);
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
saxResult.setHandler(MyCHandler.newInstance(new File(outputFile)));
transformer.transform(domSource, saxResult);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
failUnexpected(e);
}
}
}

View file

@ -0,0 +1,462 @@
/*
* Copyright (c) 1999, 2015, 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 javax.xml.parsers.ptests;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.FileReader;
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* This checks the methods of DocumentBuilderFactoryImpl.
*/
public class DocumentBuilderFactoryTest extends JAXPFileBaseTest {
/**
* Test the default functionality of schema support method.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckSchemaSupport1() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "test.xml"));
assertFalse(eh.isErrorOccured());
}
/**
* Test the default functionality of schema support method. In
* this case the schema source property is set.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckSchemaSupport2() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "test.xsd"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
new InputSource(fis));
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "test1.xml"));
assertFalse(eh.isErrorOccured());
}
}
/**
* Test the default functionality of schema support method. In
* this case the schema source property is set.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckSchemaSupport3() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "test.xsd"))) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
spf.setNamespaceAware(true);
SAXParser sp = spf.newSAXParser();
sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
new InputSource(fis));
DefaultHandler dh = new DefaultHandler();
// Not expect any unrecoverable error here.
sp.parse(new File(XML_DIR, "test1.xml"), dh);
}
}
/**
* Test the default functionality of newInstance method. To test
* the isCoalescing method and setCoalescing This checks to see if the CDATA
* and text nodes got combined In that case it will print "&lt;xml&gt;This
* is not parsed&lt;/xml&gt; yet".
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory02() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml"));
Element e = (Element) doc.getElementsByTagName("html").item(0);
NodeList nl = e.getChildNodes();
assertEquals(nl.getLength(), 1);
}
/**
* Test the isIgnoringComments. By default it is false.
*/
@Test
public void testCheckDocumentBuilderFactory03() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
assertFalse(dbf.isIgnoringComments());
}
/**
* Test the isValidating. By default it is false, set it to true and then
* use a document which is not valid. It should throw a warning or
* an error at least. The test passes in case retval 0 is set in the error
* method .
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory04() throws Exception {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
assertTrue(eh.isErrorOccured());
}
/**
* Test the setValidating. By default it is false, use a
* document which is not valid. It should not throw a warning or an error.
* The test passes in case the return value equals 1.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory16() throws Exception {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
assertFalse(eh.isErrorOccured());
}
/**
* Test the setValidating. By default it is false, use a
* document which is valid. It should not throw a warning or an error. The
* test passes in case the return value equals 1.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory17() throws Exception {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
assertFalse(eh.isErrorOccured());
}
/**
* Test the isExpandEntityReferences. By default it is true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory05() throws Exception {
try(FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory02.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertTrue(dbf.isExpandEntityReferences());
assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
}
}
/**
* Test the default functionality of setValidating method. The
* XML file has a DTD which has namespaces defined. The parser takes care to
* check if the namespaces using elements and defined attributes are there
* or not.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory06() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
MyErrorHandler eh = MyErrorHandler.newInstance();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
assertTrue(doc instanceof Document);
assertFalse(eh.isErrorOccured());
}
/**
* Test the setExpandEntityReferences.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory07() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory02.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertTrue(dbf.isExpandEntityReferences());
assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
}
}
/**
* Test the setExpandEntityReferences.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory08() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory02.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertNull(nl.item(0).getNodeValue());
}
}
/**
* Test the setIgnoringComments. By default it is set to false.
* explicitly setting it to false, it recognizes the comment which is in
* Element Node Hence the Element's child node is not null.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory09() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory07.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(false);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertNotNull(nl.item(0).getNodeValue());
}
}
/**
* This tests for the parse(InputSource).
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory10() throws Exception {
try (BufferedReader br = new BufferedReader(new FileReader(new File(
XML_DIR, "DocumentBuilderFactory07.xml")))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(br));
assertNotNull(doc);
}
}
/**
* This tests for the parse InputStream with SystemID as a second parameter.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory11() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "dbf10import.xsl"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI()
.toASCIIString());
assertNotNull(doc);
}
}
/**
* This tests for the parse InputStream with empty SystemID as a second
* parameter.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory12() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "dbf10import.xsl"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis, " ");
assertNotNull(doc);
}
}
/**
* This tests for the parse(uri).
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory13() throws Exception {
// Accesing default working directory.
String workingDir = getSystemProperty("user.dir");
setPermissions(new FilePermission(workingDir + "/*", "read"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
.toURI().toASCIIString());
assertNotNull(doc);
}
/**
* This tests for the parse(uri) with empty string as parameter should
* throw Sax Exception.
* @throws Exception If any errors occur.
*/
@Test(expectedExceptions = SAXException.class)
public void testCheckDocumentBuilderFactory14() throws Exception {
// Accesing default working directory.
String workingDir = getSystemProperty("user.dir");
setPermissions(new FilePermission(workingDir, "read"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.parse("");
}
/**
* This tests for the parse (uri) with null uri as parameter should throw
* IllegalArgumentException.
* @throws Exception If any errors occur.
*
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testCheckDocumentBuilderFactory15() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
String uri = null;
docBuilder.parse(uri);
}
/**
* Test the setIgnoringComments. By default it is set to false,
* setting this to true, It does not recognize the comment, Here the
* nodelist has a length 0 because the ignoring comments is true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckIgnoringComments() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory08.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertEquals(nl.getLength(), 0);
}
}
/**
* Test the default behaviour of setIgnoringComments. By default
* it is set to false, this is similar to case 9 but not setIgnoringComments
* explicitly, it does not recognize the comment.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckIgnoringComments1() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory07.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("body").item(0);
NodeList nl = e.getChildNodes();
assertFalse(dbf.isIgnoringComments());
assertNotNull(nl.item(0).getNodeValue());
}
}
/**
* Test for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckElementContentWhitespace() throws Exception {
String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
String outputFile = USER_DIR + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.isErrorOccured());
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
saxResult.setHandler(handler);
transformer.transform(domSource, saxResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,33 +24,32 @@
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.FilePermission;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver; import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* This checks for the methods of DocumentBuilder * This checks for the methods of DocumentBuilder
*/ */
public class DocumentBuilderImpl01 implements EntityResolver { public class DocumentBuilderImpl01 extends JAXPFileReadOnlyBaseTest
implements EntityResolver {
/** /**
* Provide DocumentBuilder. * Provide DocumentBuilder.
* *
* @throws ParserConfigurationException * @return data provider has single DocumentBuilder.
* @throws ParserConfigurationException if a DocumentBuilder cannot be
* created which satisfies the configuration requested.
*/ */
@DataProvider(name = "builder-provider") @DataProvider(name = "builder-provider")
public Object[][] getBuilder() throws ParserConfigurationException { public Object[][] getBuilder() throws ParserConfigurationException {
@ -60,17 +59,18 @@ public class DocumentBuilderImpl01 implements EntityResolver {
} }
/** /**
* Testcase to test the default functionality of isValidation method. Expect * Test the default functionality of isValidation method. Expect
* to return false because not setting the validation. * to return false because not setting the validation.
* @param docBuilder document builder instance.
*/ */
@Test(dataProvider = "builder-provider") @Test(dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) { public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
assertFalse(docBuilder.isValidating()); assertFalse(docBuilder.isValidating());
} }
/** /**
* Testcase to test the default functionality of isNamespaceAware method. * Test the default functionality of isNamespaceAware method.
* @param docBuilder document builder instance.
*/ */
@Test(dataProvider = "builder-provider") @Test(dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) { public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
@ -78,51 +78,71 @@ public class DocumentBuilderImpl01 implements EntityResolver {
} }
/** /**
* Testcase to test the parse(InputStream). * Test the parse(InputStream).
* @param docBuilder document builder instance.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "builder-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) { public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder)
try { throws Exception {
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml"))); try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
} catch (SAXException | IOException e) { "DocumentBuilderImpl01.xml"))) {
failUnexpected(e); assertNotNull(docBuilder.parse(fis));
} }
} }
/** /**
* Testcase to test the parse(File). * Test the parse(File).
*
* @param docBuilder document builder instance.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "builder-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) { public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder)
try { throws Exception {
Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml")); assertNotNull(docBuilder.parse(new File(XML_DIR,
} catch (SAXException | IOException e) { "DocumentBuilderImpl01.xml")));
failUnexpected(e); }
/**
* Test the parse(InputStream,systemId).
* @param docBuilder document builder instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
throws Exception {
setPermissions(new FilePermission(XML_DIR + "../-",
"read"));
try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
"DocumentBuilderImpl02.xml"))) {
assertNotNull(docBuilder.parse(fis, new File(XML_DIR).toURI()
.toASCIIString() + FILE_SEP));
} }
} }
/** /**
* Testcase to test the parse(InputStream,systemId). * Test the setEntityResolver.
*/ * @param docBuilder document builder instance.
@Test(dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder) {
try {
Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl02.xml")), new File(TestUtils.XML_DIR).toURI()
.toASCIIString() + FILE_SEP);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase to test the setEntityResolver.
*/ */
@Test(dataProvider = "builder-provider") @Test(dataProvider = "builder-provider")
public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) { public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
docBuilder.setEntityResolver(this); docBuilder.setEntityResolver(this);
resolveEntity("publicId", "http://www.myhost.com/today"); assertNotNull(resolveEntity("publicId", "http://www.myhost.com/today"));
} }
/**
* Allow the application to resolve external entities.
*
* @param publicId The public identifier of the external entity
* being referenced, or null if none was supplied.
* @param systemId The system identifier of the external entity
* being referenced.
* @return An InputSource object describing the new input source,
* or null to request that the parser open a regular
* URI connection to the system identifier.
*/
@Override
public InputSource resolveEntity(String publicId, String systemId) { public InputSource resolveEntity(String publicId, String systemId) {
if (systemId.equals("http://www.myhost.com/today")) if (systemId.equals("http://www.myhost.com/today"))
return new InputSource(systemId); return new InputSource(systemId);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,7 @@ package javax.xml.parsers.ptests;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPBaseTest;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
@ -35,7 +36,7 @@ import org.testng.annotations.Test;
* Class containing the test cases for SAXParserFactory/DocumentBuilderFactory * Class containing the test cases for SAXParserFactory/DocumentBuilderFactory
* newInstance methods. * newInstance methods.
*/ */
public class FactoryConfErrorTest { public class FactoryConfErrorTest extends JAXPBaseTest {
/** /**
* Set properties DocumentBuilderFactory and SAXParserFactory to invalid * Set properties DocumentBuilderFactory and SAXParserFactory to invalid
@ -43,8 +44,8 @@ public class FactoryConfErrorTest {
*/ */
@BeforeTest @BeforeTest
public void setup() { public void setup() {
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "xx"); setSystemProperty("javax.xml.parsers.DocumentBuilderFactory", "xx");
System.setProperty("javax.xml.parsers.SAXParserFactory", "xx"); setSystemProperty("javax.xml.parsers.SAXParserFactory", "xx");
} }
/** /**
@ -53,8 +54,8 @@ public class FactoryConfErrorTest {
*/ */
@AfterTest @AfterTest
public void cleanup() { public void cleanup() {
System.clearProperty("javax.xml.parsers.DocumentBuilderFactory"); setSystemProperty("javax.xml.parsers.DocumentBuilderFactory", null);
System.clearProperty("javax.xml.parsers.SAXParserFactory"); setSystemProperty("javax.xml.parsers.SAXParserFactory", null);
} }
/** /**
@ -67,7 +68,7 @@ public class FactoryConfErrorTest {
} }
/** /**
* To test exeception thrown if javax.xml.parsers.DocumentBuilderFactory is * To test exception thrown if javax.xml.parsers.DocumentBuilderFactory is
* invalid. * invalid.
*/ */
@Test(expectedExceptions = FactoryConfigurationError.class) @Test(expectedExceptions = FactoryConfigurationError.class)

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,24 +22,16 @@
*/ */
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPBaseTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
/** /**
* Class containing the test cases for SAXParserFactory API * Class containing the test cases for SAXParserFactory API.
*/ */
public class SAXParserFactTest { public class SAXParserFactTest extends JAXPBaseTest {
private static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
@ -49,20 +41,17 @@ public class SAXParserFactTest {
private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities"; private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
/** /**
* Testcase to test if newSAXParser() method returns SAXParser. * Test if newSAXParser() method returns SAXParser.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testParser01() { public void testParser01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxparser = spf.newSAXParser(); spf.newSAXParser();
} catch (ParserConfigurationException | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the default functionality (No validation) of the parser. * Test the default functionality (No validation) of the parser.
*/ */
@Test @Test
public void testValidate01() { public void testValidate01() {
@ -71,7 +60,7 @@ public class SAXParserFactTest {
} }
/** /**
* Testcase to test the functionality of setValidating and isvalidating * Test the functionality of setValidating and isvalidating
* methods. * methods.
*/ */
@Test @Test
@ -82,7 +71,7 @@ public class SAXParserFactTest {
} }
/** /**
* Parser should not be namespaceaware by default. * Parser should not be namespace-aware by default.
*/ */
@Test @Test
public void testNamespace01() { public void testNamespace01() {
@ -91,7 +80,7 @@ public class SAXParserFactTest {
} }
/** /**
* Testcase to test the functionality of setNamespaceAware and * Test the functionality of setNamespaceAware and
* isNamespaceAware methods. * isNamespaceAware methods.
*/ */
@Test @Test
@ -102,29 +91,26 @@ public class SAXParserFactTest {
} }
/** /**
* Testcase to test the functionality of setNamespaceAware and getFeature() * Test the functionality of setNamespaceAware and getFeature()
* methods for namespaces property. * methods for namespaces property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature01() { public void testFeature01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertFalse(spf.getFeature(NAMESPACES)); assertFalse(spf.getFeature(NAMESPACES));
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
assertTrue(spf.getFeature(NAMESPACES)); assertTrue(spf.getFeature(NAMESPACES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setFeature and getFeature methods * Test the functionality of setFeature and getFeature methods
* for namespaces property. * for namespaces property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature02() { public void testFeature02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(NAMESPACES, true); spf.setFeature(NAMESPACES, true);
@ -132,18 +118,15 @@ public class SAXParserFactTest {
spf.setFeature(NAMESPACES, false); spf.setFeature(NAMESPACES, false);
assertFalse(spf.getFeature(NAMESPACES)); assertFalse(spf.getFeature(NAMESPACES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setFeature and getFeature methods * Test the functionality of setFeature and getFeature methods
* for namespace-prefixes property. * for namespace-prefixes property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature03() { public void testFeature03() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(NAMESPACE_PREFIXES, true); spf.setFeature(NAMESPACE_PREFIXES, true);
@ -151,118 +134,89 @@ public class SAXParserFactTest {
spf.setFeature(NAMESPACE_PREFIXES, false); spf.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(spf.getFeature(NAMESPACE_PREFIXES)); assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of getFeature method for * Test the functionality of getFeature method for
* string-interning property. * string-interning property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature04() { public void testFeature04() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertTrue(spf.getFeature(STRING_INTERNING)); assertTrue(spf.getFeature(STRING_INTERNING));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of getFeature and setValidating * Test the functionality of getFeature and setValidating
* methods for validation property. * methods for validation property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature05() { public void testFeature05() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertFalse(spf.getFeature(VALIDATION)); assertFalse(spf.getFeature(VALIDATION));
spf.setValidating(true); spf.setValidating(true);
assertTrue(spf.getFeature(VALIDATION)); assertTrue(spf.getFeature(VALIDATION));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setFeature and getFeature methods * Test the functionality of setFeature and getFeature methods
* for validation property. * for validation property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature06() { public void testFeature06() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(VALIDATION, true); spf.setFeature(VALIDATION, true);
assertTrue(spf.getFeature(VALIDATION)); assertTrue(spf.getFeature(VALIDATION));
spf.setFeature(VALIDATION, false); spf.setFeature(VALIDATION, false);
assertFalse(spf.getFeature(VALIDATION)); assertFalse(spf.getFeature(VALIDATION));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of getFeature method for * Test the functionality of getFeature method for
* external-general-entities property. * external-general-entities property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature07() { public void testFeature07() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES)); assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setFeature and getFeature methods * Test the functionality of setFeature and getFeature methods
* for external-general-entities property. * for external-general-entities property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature08() { public void testFeature08() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(EXTERNAL_G_ENTITIES, false); spf.setFeature(EXTERNAL_G_ENTITIES, false);
assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES)); assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of getFeature method for * Test the functionality of getFeature method for
* external-parameter-entities property. * external-parameter-entities property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature09() { public void testFeature09() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES)); assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES));
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setFeature method for * Test the functionality of setFeature method for
* external-parameter-entitie property. * external-parameter-entitie property.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testFeature10() { public void testFeature10() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(EXTERNAL_P_ENTITIES, false); spf.setFeature(EXTERNAL_P_ENTITIES, false);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { assertFalse(spf.getFeature(EXTERNAL_P_ENTITIES));
failUnexpected(e);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,16 +23,14 @@
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.IOException; import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import jaxp.library.JAXPFileReadOnlyBaseTest;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.HandlerBase; import org.xml.sax.HandlerBase;
@ -43,16 +41,15 @@ import org.xml.sax.helpers.DefaultHandler;
/** /**
* Class contains the test cases for SAXParser API * Class contains the test cases for SAXParser API
*/ */
public class SAXParserTest { public class SAXParserTest extends JAXPFileReadOnlyBaseTest {
/** /**
* Provide SAXParser. * Provide SAXParser.
* *
* @throws SAXException * @return a data provider contains a SAXParser instance.
* @throws ParserConfigurationException * @throws Exception If any errors occur.
*/ */
@DataProvider(name = "parser-provider") @DataProvider(name = "parser-provider")
public Object[][] getParser() throws ParserConfigurationException, SAXException { public Object[][] getParser() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxparser = spf.newSAXParser(); SAXParser saxparser = spf.newSAXParser();
return new Object[][] { { saxparser } }; return new Object[][] { { saxparser } };
@ -62,498 +59,454 @@ public class SAXParserTest {
* Test case with FileInputStream null, parsing should fail and throw * Test case with FileInputStream null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse01(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse01(SAXParser saxparser) throws Exception {
FileInputStream instream = null; FileInputStream instream = null;
HandlerBase handler = new HandlerBase(); saxparser.parse(instream, new HandlerBase());
saxparser.parse(instream, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with an error in xml file, parsing should fail and throw * Test with by setting URI as null, parsing should fail and throw
* SAXException.
*
* @throws SAXException
*/
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse02(SAXParser saxparser) throws SAXException {
try {
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
saxparser.parse(instream, handler);
} catch (IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with a valid in xml file, parser should parse the xml document.
*/
@Test(dataProvider = "parser-provider")
public void testParse03(SAXParser saxparser) {
try {
HandlerBase handler = new HandlerBase();
saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler);
} catch (IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase with valid input stream, parser should parse the xml document
* successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse04(SAXParser saxparser) {
try {
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
saxparser.parse(instream, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with valid input source, parser should parse the xml document
* successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse05(SAXParser saxparser) {
try {
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
saxparser.parse(instream, handler, new File(TestUtils.XML_DIR).toURI().toASCIIString());
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with uri null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse07(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse02(SAXParser saxparser) throws Exception {
String uri = null; String uri = null;
HandlerBase handler = new HandlerBase(); saxparser.parse(uri, new HandlerBase());
saxparser.parse(uri, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with non-existant uri, parsing should fail and throw * Test with non-existence URI, parsing should fail and throw IOException.
* IOException.
* *
* @throws SAXException * @param saxparser a SAXParser instance.
* @throws IOException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = { SAXException.class, IOException.class }, dataProvider = "parser-provider") @Test(expectedExceptions = { SAXException.class },
public void testParse08(SAXParser saxparser) throws SAXException, IOException { dataProvider = "parser-provider")
String uri = " "; public void testParse03(SAXParser saxparser) throws Exception {
String workingDir = getSystemProperty("user.dir");
HandlerBase handler = new HandlerBase(); setPermissions(new FilePermission(workingDir, "read"));
saxparser.parse(uri, handler);
}
/**
* Testcase with proper uri, parser should parse successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse09(SAXParser saxparser) {
try { try {
File file = new File(TestUtils.XML_DIR, "correct.xml"); saxparser.parse("", new HandlerBase());
HandlerBase handler = new HandlerBase(); } finally {
saxparser.parse(file.toURI().toASCIIString(), handler); setPermissions();
} catch (SAXException | IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Testcase with File null, parsing should fail and throw * Test with File null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse10(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse04(SAXParser saxparser) throws Exception {
File file = null; File file = null;
HandlerBase handler = new HandlerBase(); saxparser.parse(file, new HandlerBase());
saxparser.parse(file, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with empty string as File, parsing should fail and throw * Test with empty string as File, parsing should fail and throw
* SAXException. * SAXException.
* *
* @throws SAXException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse11(SAXParser saxparser) throws SAXException { public void testParse05(SAXParser saxparser) throws Exception {
String workingDir = getSystemProperty("user.dir");
setPermissions(new FilePermission(workingDir, "read"));
try { try {
HandlerBase handler = new HandlerBase(); saxparser.parse(new File(""), new HandlerBase());
File file = new File(""); } finally {
saxparser.parse(file, handler); setPermissions();
} catch (IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Testcase with xml file that has errors parsing should fail and throw * Test with input source null, parsing should fail and throw
* SAXException.
*
* @throws SAXException
*/
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse12(SAXParser saxparser) throws SAXException {
try {
HandlerBase handler = new HandlerBase();
File file = new File(TestUtils.XML_DIR, "valid.xml");
saxparser.parse(file, handler);
} catch (IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with xml file that has no errors Parser should successfully
* parse the xml document.
*/
@Test(dataProvider = "parser-provider")
public void testParse13(SAXParser saxparser) {
try {
HandlerBase handler = new HandlerBase();
File file = new File(TestUtils.XML_DIR, "correct.xml");
saxparser.parse(file, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with input source null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse14(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse06(SAXParser saxparser) throws Exception {
InputSource is = null; InputSource is = null;
HandlerBase handler = new HandlerBase(); saxparser.parse(is, new HandlerBase());
saxparser.parse(is, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with input source attached an invaild xml, parsing should fail * Test with FileInputStream null, parsing should fail and throw
* and throw SAXException.
*
* @throws SAXException
*/
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse15(SAXParser saxparser) throws SAXException {
try {
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
InputSource is = new InputSource(instream);
saxparser.parse(is, handler);
} catch (IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with input source attached an vaild xml, parser should
* successfully parse the xml document.
*/
@Test(dataProvider = "parser-provider")
public void testParse16(SAXParser saxparser) {
try {
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
InputSource is = new InputSource(instream);
saxparser.parse(is, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with FileInputStream null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse17(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse07(SAXParser saxparser) throws Exception {
FileInputStream instream = null; FileInputStream instream = null;
DefaultHandler handler = new DefaultHandler(); saxparser.parse(instream, new DefaultHandler());
saxparser.parse(instream, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with an error in xml file, parsing should fail and throw * Test with URI null, parsing should fail and throw
* SAXException.
*
* @throws SAXException
*/
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse18(SAXParser saxparser) throws SAXException {
try {
DefaultHandler handler = new DefaultHandler();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
saxparser.parse(instream, handler);
} catch (IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with valid input stream, parser should parse the xml document
* successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse19(SAXParser saxparser) {
try {
DefaultHandler handler = new DefaultHandler();
saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler);
} catch (IOException | SAXException e) {
failUnexpected(e);
}
}
/**
* Testcase with valid input stream, parser should parse the xml document
* successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse20(SAXParser saxparser) {
try {
DefaultHandler handler = new DefaultHandler();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
saxparser.parse(instream, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with valid input source, parser should parse the xml document
* successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse21(SAXParser saxparser) {
try {
DefaultHandler handler = new DefaultHandler();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
saxparser.parse(instream, handler, new File(TestUtils.XML_DIR).toURI().toASCIIString());
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with uri null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse23(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse08(SAXParser saxparser) throws Exception {
String uri = null; String uri = null;
DefaultHandler handler = new DefaultHandler(); saxparser.parse(uri, new DefaultHandler());
saxparser.parse(uri, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with non-existant uri, parsing should fail and throw * Test with non-existence URI, parsing should fail and throw
* SAXException or IOException. * SAXException or IOException.
* *
* @throws SAXException * @param saxparser a SAXParser instance.
* @throws IOException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = { SAXException.class, IOException.class }, dataProvider = "parser-provider") @Test(expectedExceptions = { SAXException.class, IOException.class },
public void testParse24(SAXParser saxparser) throws SAXException, IOException { dataProvider = "parser-provider")
public void testParse09(SAXParser saxparser) throws Exception {
String workingDir = getSystemProperty("user.dir");
setPermissions(new FilePermission(workingDir + "/../-", "read"));
String uri = " "; String uri = " ";
DefaultHandler handler = new DefaultHandler();
saxparser.parse(uri, handler);
}
/**
* Testcase with proper uri, parser should parse successfully.
*/
@Test(dataProvider = "parser-provider")
public void testParse25(SAXParser saxparser) {
try { try {
File file = new File(TestUtils.XML_DIR, "correct.xml"); saxparser.parse(uri, new DefaultHandler());
} finally {
DefaultHandler handler = new DefaultHandler(); setPermissions();
saxparser.parse(file.toURI().toASCIIString(), handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Testcase with File null, parsing should fail and throw * Test with empty string as File, parsing should fail and throw
* IllegalArgumentException.
*
* @throws IllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider")
public void testParse26(SAXParser saxparser) throws IllegalArgumentException {
try {
DefaultHandler handler = new DefaultHandler();
saxparser.parse((File) null, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with empty string as File, parsing should fail and throw
* SAXException. * SAXException.
* *
* @throws SAXException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse27(SAXParser saxparser) throws SAXException { public void testParse10(SAXParser saxparser) throws Exception {
try { String workingDir = getSystemProperty("user.dir");
DefaultHandler handler = new DefaultHandler(); setPermissions(new FilePermission(workingDir, "read"));
File file = new File(""); File file = new File("");
saxparser.parse(file, handler);
} catch (IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with xml file that has errors, parsing should fail and throw
* SAXException.
*
* @throws SAXException
*/
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider")
public void testParse28(SAXParser saxparser) throws SAXException {
try { try {
DefaultHandler handler = new DefaultHandler(); saxparser.parse(file, new DefaultHandler());
File file = new File(TestUtils.XML_DIR, "valid.xml"); } finally {
saxparser.parse(file, handler); setPermissions();
} catch (IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Testcase with xml file that has no errors, parser should successfully * Test with File null, parsing should fail and throw
* parse the xml document.
*/
@Test(dataProvider = "parser-provider")
public void testParse29(SAXParser saxparser) {
try {
DefaultHandler handler = new DefaultHandler();
File file = new File(TestUtils.XML_DIR, "correct.xml");
saxparser.parse(file, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
}
/**
* Testcase with input source null, parsing should fail and throw
* IllegalArgumentException. * IllegalArgumentException.
* *
* @throws IllegalArgumentException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse30(SAXParser saxparser) throws IllegalArgumentException { dataProvider = "parser-provider")
try { public void testParse11(SAXParser saxparser) throws Exception {
InputSource is = null; saxparser.parse((File) null, new DefaultHandler());
DefaultHandler handler = new DefaultHandler();
saxparser.parse(is, handler);
} catch (SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testcase with an invalid xml file, parser should throw SAXException. * Test with input source null, parsing should fail and throw
* IllegalArgumentException.
* *
* @throws SAXException * @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") @Test(expectedExceptions = IllegalArgumentException.class,
public void testParse31(SAXParser saxparser) throws SAXException { dataProvider = "parser-provider")
try { public void testParse12(SAXParser saxparser) throws Exception {
DefaultHandler handler = new DefaultHandler(); InputSource is = null;
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml")); saxparser.parse(is, new DefaultHandler());
InputSource is = new InputSource(instream); }
saxparser.parse(is, handler);
} catch (IOException e) { /**
failUnexpected(e); * Test with an error in XML file, parsing should fail and throw
* SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse13(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(new File(
XML_DIR, "invalid.xml"))) {
saxparser.parse(instream, new HandlerBase());
} }
} }
/** /**
* Test case to parse an xml file that not use namespaces. * Test with a valid in XML file, parser should parse the XML document.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse32(SAXParser saxparser) { public void testParse14(SAXParser saxparser) throws Exception {
try { saxparser.parse(new File(XML_DIR, "parsertest.xml"),
DefaultHandler handler = new DefaultHandler(); new HandlerBase());
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml")); }
InputSource is = new InputSource(instream);
saxparser.parse(is, handler); /**
} catch (SAXException | IOException e) { * Test with valid input stream, parser should parse the XML document
failUnexpected(e); * successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse15(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(new File(XML_DIR,
"correct.xml"))) {
saxparser.parse(instream, new HandlerBase());
} }
} }
/** /**
* Test case to parse an xml file that uses namespaces. * Test with valid input source, parser should parse the XML document
* successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse33() { public void testParse16(SAXParser saxparser) throws Exception {
try { try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "parsertest.xml"))) {
saxparser.parse(instream, new HandlerBase(),
new File(XML_DIR).toURI().toASCIIString());
}
}
/**
* Test with proper URI, parser should parse successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse17(SAXParser saxparser) throws Exception {
File file = new File(XML_DIR, "correct.xml");
saxparser.parse(file.toURI().toASCIIString(), new HandlerBase());
}
/**
* Test with XML file that has errors parsing should fail and throw
* SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse18(SAXParser saxparser) throws Exception {
saxparser.parse(new File(XML_DIR, "valid.xml"), new HandlerBase());
}
/**
* Test with XML file that has no errors Parser should successfully
* parse the XML document.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse19(SAXParser saxparser) throws Exception {
saxparser.parse(new File(XML_DIR, "correct.xml"), new HandlerBase());
}
/**
* Test with input source attached an invalid XML, parsing should fail
* and throw SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse20(SAXParser saxparser) throws Exception {
try(FileInputStream instream = new FileInputStream(new File(XML_DIR,
"invalid.xml"))) {
saxparser.parse(new InputSource(instream), new HandlerBase());
}
}
/**
* Test with input source attached an valid XML, parser should
* successfully parse the XML document.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse21(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(new File(XML_DIR,
"correct.xml"))) {
saxparser.parse(new InputSource(instream), new HandlerBase());
}
}
/**
* Test with an error in xml file, parsing should fail and throw
* SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse22(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "invalid.xml"))) {
saxparser.parse(instream, new DefaultHandler());
}
}
/**
* Test with valid input stream, parser should parse the XML document
* successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse23(SAXParser saxparser) throws Exception {
DefaultHandler handler = new DefaultHandler();
saxparser.parse(new File(XML_DIR, "parsertest.xml"), handler);
}
/**
* Test with valid input stream, parser should parse the XML document
* successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse24(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(new File(XML_DIR,
"correct.xml"))) {
DefaultHandler handler = new DefaultHandler();
saxparser.parse(instream, handler);
}
}
/**
* Test with valid input source, parser should parse the XML document
* successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse25(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "parsertest.xml"))) {
saxparser.parse(instream, new DefaultHandler(),
new File(XML_DIR).toURI().toASCIIString());
}
}
/**
* Test with proper URI, parser should parse successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse26(SAXParser saxparser) throws Exception {
File file = new File(XML_DIR, "correct.xml");
saxparser.parse(file.toURI().toASCIIString(), new DefaultHandler());
}
/**
* Test with XML file that has errors, parsing should fail and throw
* SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse27(SAXParser saxparser) throws Exception {
saxparser.parse(new File(XML_DIR, "valid.xml"), new DefaultHandler());
}
/**
* Test with XML file that has no errors, parser should successfully
* parse the XML document.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse28(SAXParser saxparser) throws Exception {
saxparser.parse(new File(XML_DIR, "correct.xml"), new DefaultHandler());
}
/**
* Test with an invalid XML file, parser should throw SAXException.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class,
dataProvider = "parser-provider")
public void testParse29(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "invalid.xml"))) {
saxparser.parse(new InputSource(instream), new DefaultHandler());
}
}
/**
* Test case to parse an XML file that not use namespaces.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider")
public void testParse30(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "correct.xml"))) {
saxparser.parse(new InputSource(instream), new DefaultHandler());
}
}
/**
* Test case to parse an XML file that uses namespaces.
*
* @throws Exception If any errors occur.
*/
@Test(groups = {"readLocalFiles"})
public void testParse31() throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "ns4.xml"))) {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser(); spf.newSAXParser().parse(instream, new HandlerBase());
HandlerBase handler = new HandlerBase();
FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "ns4.xml"));
saxparser.parse(instream, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,260 +23,239 @@
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.Parser;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DeclHandler; import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler; import org.xml.sax.ext.LexicalHandler;
/** /**
* Class contains the test cases for SAXParser API * Class contains the test cases for SAXParser API
*/ */
public class SAXParserTest02 { public class SAXParserTest02 extends JAXPBaseTest {
final String DOM_NODE = "http://xml.org/sax/properties/dom-node"; private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node";
final String XML_STRING = "http://xml.org/sax/properties/xml-string"; private static final String XML_STRING = "http://xml.org/sax/properties/xml-string";
final String DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler"; private static final String DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
final String LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler"; private static final String LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
/** /**
* Provide SAXParser. * Provide SAXParser.
* *
* @throws SAXException * @return a data provider contains a SAXParser instance.
* @throws ParserConfigurationException * @throws Exception If any errors occur.
*/ */
@DataProvider(name = "parser-provider") @DataProvider(name = "parser-provider")
public Object[][] getParser() throws ParserConfigurationException, SAXException { public Object[][] getParser() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxparser = spf.newSAXParser(); SAXParser saxparser = spf.newSAXParser();
return new Object[][] { { saxparser } }; return new Object[][] { { saxparser } };
} }
/** /**
* Testcase to test the default functionality (No validation) of the parser. * Test to test the default functionality (No validation) of the parser.
*
* @param saxparser a SAXParser instance.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testValidate01(SAXParser saxparser) { public void testValidate01(SAXParser saxparser) {
try {
assertFalse(saxparser.isValidating()); assertFalse(saxparser.isValidating());
} catch (FactoryConfigurationError e) {
failUnexpected(e);
}
} }
/** /**
* Testcase to test the functionality of setValidating and isvalidating * Test to test the functionality of setValidating and isValidating
* methods. * methods.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testValidate02() { public void testValidate02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true); spf.setValidating(true);
spf.newSAXParser(); spf.newSAXParser();
assertTrue(spf.isValidating()); assertTrue(spf.isValidating());
} catch (FactoryConfigurationError | ParserConfigurationException | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test case to test isNamespaceAware() method. By default, namespaces are * Test isNamespaceAware() method. By default, namespaces are
* not supported. * not supported.
*
* @param saxparser a SAXParser instance.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testNamespace01(SAXParser saxparser) { public void testNamespace01(SAXParser saxparser) {
try {
assertFalse(saxparser.isNamespaceAware()); assertFalse(saxparser.isNamespaceAware());
} catch (FactoryConfigurationError e) {
failUnexpected(e);
}
} }
/** /**
* Test case to test setnamespaceAware() method. * Test case to test setnamespaceAware() method.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testNamespace02() { public void testNamespace02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser(); SAXParser saxparser = spf.newSAXParser();
assertTrue(saxparser.isNamespaceAware()); assertTrue(saxparser.isNamespaceAware());
} catch (FactoryConfigurationError | ParserConfigurationException | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test case to test if the getParser() method returns instance of Parser. * Test case to test if the getParser() method returns instance of Parser.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testParser01(SAXParser saxparser) { public void testParser01(SAXParser saxparser) throws SAXException {
try { assertNotNull(saxparser.getParser());
Parser parser = saxparser.getParser();
} catch (FactoryConfigurationError | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test case to test if the getXMLReader() method returns instance of * Test case to test if the getXMLReader() method returns instance of
* XMLReader. * XMLReader.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testXmlReader01(SAXParser saxparser) { public void testXmlReader01(SAXParser saxparser) throws SAXException {
try { assertNotNull(saxparser.getXMLReader());
XMLReader xmlReader = saxparser.getXMLReader();
} catch (FactoryConfigurationError | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test whether the xml-string property is not supported. * Test whether the xml-string property is not supported.
* *
* @throws SAXNotSupportedException * @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(expectedExceptions = SAXNotSupportedException.class, dataProvider = "parser-provider") @Test(expectedExceptions = SAXNotSupportedException.class,
public void testProperty01(SAXParser saxparser) throws SAXNotSupportedException { dataProvider = "parser-provider")
try { public void testProperty01(SAXParser saxparser) throws SAXException {
Object object = saxparser.getProperty(XML_STRING); saxparser.getProperty(XML_STRING);
} catch (SAXNotRecognizedException e) {
failUnexpected(e);
}
} }
/** /**
* Test whether the dom-node property is not supported. * Test whether the dom-node property is not supported.
* *
* @throws SAXNotSupportedException * @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(expectedExceptions = SAXNotSupportedException.class, dataProvider = "parser-provider") @Test(expectedExceptions = SAXNotSupportedException.class,
public void testProperty02(SAXParser saxparser) throws SAXNotSupportedException { dataProvider = "parser-provider")
try { public void testProperty02(SAXParser saxparser) throws SAXException {
Object object = saxparser.getProperty(DOM_NODE); saxparser.getProperty(DOM_NODE);
} catch (SAXNotRecognizedException e) {
failUnexpected(e);
}
} }
/** /**
* Test the default lexical-handler not exists. * Test the default lexical-handler not exists.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testProperty03(SAXParser saxparser) { public void testProperty03(SAXParser saxparser) throws SAXException {
try {
assertNull(saxparser.getProperty(LEXICAL_HANDLER)); assertNull(saxparser.getProperty(LEXICAL_HANDLER));
} catch (SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test the default declaration-handler not exists. * Test the default declaration-handler not exists.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testProperty04(SAXParser saxparser) { public void testProperty04(SAXParser saxparser) throws SAXException {
try {
assertNull(saxparser.getProperty(DECL_HANDLER)); assertNull(saxparser.getProperty(DECL_HANDLER));
} catch (SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test to set and get the lexical-handler. * Test to set and get the lexical-handler.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testProperty05(SAXParser saxparser) { public void testProperty05(SAXParser saxparser) throws SAXException {
try {
MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
saxparser.setProperty(LEXICAL_HANDLER, myLexicalHandler); saxparser.setProperty(LEXICAL_HANDLER, myLexicalHandler);
Object object = saxparser.getProperty(LEXICAL_HANDLER); assertTrue(saxparser.getProperty(LEXICAL_HANDLER) instanceof LexicalHandler);
assertTrue(object instanceof LexicalHandler);
} catch (SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test to set and get the declaration-handler. * Test to set and get the declaration-handler.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/ */
@Test(dataProvider = "parser-provider") @Test(dataProvider = "parser-provider")
public void testProperty06(SAXParser saxparser) { public void testProperty06(SAXParser saxparser) throws SAXException {
try {
MyDeclHandler myDeclHandler = new MyDeclHandler(); MyDeclHandler myDeclHandler = new MyDeclHandler();
saxparser.setProperty(DECL_HANDLER, myDeclHandler); saxparser.setProperty(DECL_HANDLER, myDeclHandler);
Object object = saxparser.getProperty(DECL_HANDLER); assertTrue(saxparser.getProperty(DECL_HANDLER) instanceof DeclHandler);
assertTrue(object instanceof DeclHandler);
} catch (SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Customized LexicalHandler used for test. * Customized LexicalHandler used for test. An empty implementation for
* LexicalHandler.
*/ */
private class MyLexicalHandler implements LexicalHandler { private class MyLexicalHandler implements LexicalHandler {
@Override
public void comment(char[] ch, int start, int length) { public void comment(char[] ch, int start, int length) {
} }
@Override
public void endCDATA() { public void endCDATA() {
} }
@Override
public void endDTD() { public void endDTD() {
} }
@Override
public void endEntity(String name) { public void endEntity(String name) {
} }
@Override
public void startCDATA() { public void startCDATA() {
} }
@Override
public void startDTD(String name, String publicId, String systemId) { public void startDTD(String name, String publicId, String systemId) {
} }
@Override
public void startEntity(String name) { public void startEntity(String name) {
} }
} }
/** /**
* Customized DeclHandler used for test. * Customized DeclHandler used for test. An empty implementation for
* DeclHandler.
*/ */
private class MyDeclHandler implements DeclHandler { private class MyDeclHandler implements DeclHandler {
@Override
public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) { public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) {
} }
@Override
public void elementDecl(String name, String model) { public void elementDecl(String name, String model) {
} }
@Override
public void externalEntityDecl(String name, String publicId, String systemId) { public void externalEntityDecl(String name, String publicId, String systemId) {
} }
@Override
public void internalEntityDecl(String name, String value) { public void internalEntityDecl(String name, String value) {
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,17 +23,17 @@
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.FilePermission;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.fail;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -41,68 +41,70 @@ import org.xml.sax.SAXException;
/** /**
* Class contains the test cases for SAXParser API * Class contains the test cases for SAXParser API
*/ */
public class SAXParserTest03 { public class SAXParserTest03 extends JAXPFileReadOnlyBaseTest {
/** /**
* Provide SAXParserFactory. * Provide SAXParserFactory.
* *
* @throws Exception * @return a dimensional contains.
*/ */
@DataProvider(name = "input-provider") @DataProvider(name = "input-provider")
public Object[][] getFactory() { public Object[][] getFactory() {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true); spf.setValidating(true);
MyErrorHandler handler = MyErrorHandler.newInstance(); return new Object[][] { { spf, MyErrorHandler.newInstance() } };
return new Object[][] { { spf, handler } };
} }
/** /**
* parsertest.xml holds a valid document. This method tests the validating * parsertest.xml holds a valid document. This method tests the validating
* parser. * parser.
*
* @param spf a Parser factory.
* @param handler an error handler for capturing events.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "input-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) { public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler)
try { throws Exception {
SAXParser saxparser = spf.newSAXParser(); spf.newSAXParser().parse(new File(XML_DIR, "parsertest.xml"), handler);
saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler); assertFalse(handler.isErrorOccured());
assertFalse(handler.errorOccured);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* validns.xml holds a valid document with XML namespaces in it. This method * validns.xml holds a valid document with XML namespaces in it. This method
* tests the Validating parser with namespace processing on. * tests the Validating parser with namespace processing on.
*
* @param spf a Parser factory.
* @param handler an error handler for capturing events.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "input-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) { public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler)
try { throws Exception {
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser(); spf.newSAXParser().parse(new File(XML_DIR, "validns.xml"), handler);
saxparser.parse(new File(TestUtils.XML_DIR, "validns.xml"), handler); assertFalse(handler.isErrorOccured());
assertFalse(handler.errorOccured);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* invalidns.xml holds an invalid document with XML namespaces in it. This * invalidns.xml holds an invalid document with XML namespaces in it. This
* method tests the validating parser with namespace processing on. It * method tests the validating parser with namespace processing on. It
* should throw validation error. * should throw validation error.
*
* @param spf a Parser factory.
* @param handler an error handler for capturing events.
* @throws Exception If any errors occur.
*/ */
@Test(dataProvider = "input-provider") @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) { public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler)
throws Exception {
try { try {
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser(); SAXParser saxparser = spf.newSAXParser();
saxparser.parse(new File(TestUtils.XML_DIR, "invalidns.xml"), handler); saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler);
failUnexpected(new RuntimeException()); fail("Expecting SAXException here");
} catch (ParserConfigurationException | SAXException | IOException e) { } catch (SAXException e) {
if (e instanceof SAXException) { assertTrue(handler.isErrorOccured());
assertTrue(handler.errorOccured);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,21 +26,16 @@ package javax.xml.transform.ptests;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.sax.TransformerHandler;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
@ -48,7 +43,6 @@ import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.XMLReaderFactory;
@ -56,18 +50,18 @@ import org.xml.sax.helpers.XMLReaderFactory;
* DOM parse on test file to be compared with golden output file. No Exception * DOM parse on test file to be compared with golden output file. No Exception
* is expected. * is expected.
*/ */
public class DOMResultTest01 { public class DOMResultTest extends JAXPFileBaseTest {
/** /**
* Unit test for simple DOM parsing. * Unit test for simple DOM parsing.
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String resultFile = CLASS_DIR + "domresult01.out"; String resultFile = USER_DIR + "domresult01.out";
String goldFile = GOLDEN_DIR + "domresult01GF.out"; String goldFile = GOLDEN_DIR + "domresult01GF.out";
String xsltFile = XML_DIR + "cities.xsl"; String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml"; String xmlFile = XML_DIR + "cities.xml";
try {
XMLReader reader = XMLReaderFactory.createXMLReader(); XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance(); = (SAXTransformerFactory) TransformerFactory.newInstance();
@ -86,18 +80,6 @@ public class DOMResultTest01 {
writeNodes(node, writer); writeNodes(node, writer);
} }
assertTrue(compareWithGold(goldFile, resultFile)); assertTrue(compareWithGold(goldFile, resultFile));
} catch (SAXException | TransformerConfigurationException
| IllegalArgumentException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if(Files.exists(resultPath))
Files.delete(resultPath);
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,12 +24,14 @@
package javax.xml.transform.ptests; package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FilePermission;
import javax.xml.transform.ErrorListener; import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -37,7 +39,7 @@ import org.testng.annotations.Test;
/** /**
* Class containing the test cases for ErrorListener interface * Class containing the test cases for ErrorListener interface
*/ */
public class ErrorListenerTest implements ErrorListener { public class ErrorListenerTest extends JAXPBaseTest implements ErrorListener {
/** /**
* Define ErrorListener's status. * Define ErrorListener's status.
*/ */
@ -58,9 +60,10 @@ public class ErrorListenerTest implements ErrorListener {
try { try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setErrorListener (listener); tfactory.setErrorListener (listener);
setPermissions(new FilePermission(XML_DIR + "invalid.xsl", "read"));
tfactory.newTransformer(new StreamSource( tfactory.newTransformer(new StreamSource(
new File(XML_DIR + "invalid.xsl"))); new File(XML_DIR + "invalid.xsl")));
fail("We expect an Exception here"); fail("Expect TransformerConfigurationException here");
} catch (TransformerConfigurationException ex) { } catch (TransformerConfigurationException ex) {
assertEquals(listener.status, ListenerStatus.FATAL); assertEquals(listener.status, ListenerStatus.FATAL);
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,80 +25,73 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Unit test for SAXSource sourceToInputSource API. * Unit test for SAXSource sourceToInputSource API.
*/ */
public class SAXSourceTest01 { public class SAXSourceTest extends JAXPFileReadOnlyBaseTest {
/** /**
* Test file name * Test style-sheet file name
*/ */
private final String TEST_FILE = XML_DIR + "cities.xsl"; private final String TEST_FILE = XML_DIR + "cities.xsl";
/** /**
* Test obtaining a SAX InputSource object from a Source object. * Test obtaining a SAX InputSource object from a Source object.
*
* @throws IOException reading file error.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void source2inputsource01() { public void source2inputsource01() throws IOException {
try { try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
StreamSource streamSource = new StreamSource ( StreamSource streamSource = new StreamSource(fis);
new FileInputStream (TEST_FILE));
assertNotNull(SAXSource.sourceToInputSource(streamSource)); assertNotNull(SAXSource.sourceToInputSource(streamSource));
} catch (FileNotFoundException ex) {
failUnexpected(ex);
} }
} }
/** /**
* This test case tries to get InputSource from DOMSource using * This test case tries to get InputSource from DOMSource using
* sourceToInputSource method. It is not possible and hence null is * sourceToInputSource method. It is not possible and hence null is
* expected. This is a negative test case * expected. This is a negative test case,
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void source2inputsource02() { public void source2inputsource02() throws Exception {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.newDocumentBuilder().parse(new File(TEST_FILE)); dbf.newDocumentBuilder().parse(new File(TEST_FILE));
assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); assertNull(SAXSource.sourceToInputSource(new DOMSource(null)));
} catch (ParserConfigurationException | SAXException | IOException ex) {
failUnexpected(ex);
}
} }
/** /**
* This test case tries to get InputSource from SAXSource using * This test case tries to get InputSource from SAXSource using
* sourceToInputSource method. This will also check if the systemId * sourceToInputSource method. This will also check if the systemId
* remained the same. This is a positive test case. * remained the same. This is a positive test case.
*
* @throws IOException reading file error.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void source2inputsource03() { public void source2inputsource03() throws IOException {
String SYSTEM_ID = "file:///" + XML_DIR; String SYSTEM_ID = "file:///" + XML_DIR;
try { try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
SAXSource saxSource = SAXSource saxSource =
new SAXSource(new InputSource(new FileInputStream(TEST_FILE))); new SAXSource(new InputSource(fis));
saxSource.setSystemId(SYSTEM_ID); saxSource.setSystemId(SYSTEM_ID);
assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(),
SYSTEM_ID); SYSTEM_ID);
} catch (FileNotFoundException ex) {
failUnexpected(ex);
} }
} }
} }

View file

@ -0,0 +1,423 @@
/*
* Copyright (c) 2003, 2015, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerhandler() method which takes StreamSource as argument can
* be set to XMLReader.
*/
public class SAXTFactoryTest extends JAXPFileBaseTest {
/**
* Test style-sheet file.
*/
private static final String XSLT_FILE = XML_DIR + "cities.xsl";
/**
* Test style-sheet file.
*/
private static final String XSLT_INCL_FILE = XML_DIR + "citiesinclude.xsl";
/**
* Test XML file.
*/
private static final String XML_FILE = XML_DIR + "cities.xml";
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a transformer handler which write out the
* result to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase01() throws Exception {
String outputFile = USER_DIR + "saxtf001.out";
String goldFile = GOLDEN_DIR + "saxtf001GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE));
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a content handler which write out the result
* to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase02() throws Exception {
String outputFile = USER_DIR + "saxtf002.out";
String goldFile = GOLDEN_DIR + "saxtf002GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile);
FileInputStream fis = new FileInputStream(XSLT_FILE)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
SAXSource ss = new SAXSource();
ss.setInputSource(new InputSource(fis));
TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is
* namespace awareness, DocumentBuilder parse xslt file as DOMSource.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase03() throws Exception {
String outputFile = USER_DIR + "saxtf003.out";
String goldFile = GOLDEN_DIR + "saxtf003GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Negative test for newTransformerHandler when relative URI is in XML file.
*
* @throws Exception If any errors occur.
*/
@Test(expectedExceptions = TransformerConfigurationException.class)
public void transformerHandlerTest04() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_INCL_FILE));
DOMSource domSource= new DOMSource(document);
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
saxTFactory.newTransformerHandler(domSource);
}
/**
* Unit test for XMLReader parsing when relative URI is used in xsl file and
* SystemId was set.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase05() throws Exception {
String outputFile = USER_DIR + "saxtf005.out";
String goldFile = GOLDEN_DIR + "saxtf005GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_INCL_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
domSource.setSystemId("file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test newTransformerHandler with a DOMSource.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase06() throws Exception {
String outputFile = USER_DIR + "saxtf006.out";
String goldFile = GOLDEN_DIR + "saxtf006GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Node node = (Node)docBuilder.parse(new File(XSLT_INCL_FILE));
DOMSource domSource = new DOMSource(node, "file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Test newTransformerHandler with a Template Handler.
*
* @throws Exception If any errors occur.
*/
public void testcase08() throws Exception {
String outputFile = USER_DIR + "saxtf008.out";
String goldFile = GOLDEN_DIR + "saxtf008GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
TransformerHandler tfhandler
= saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Test newTransformerHandler with a Template Handler along with a relative
* URI in the style-sheet file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase09() throws Exception {
String outputFile = USER_DIR + "saxtf009.out";
String goldFile = GOLDEN_DIR + "saxtf009GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
thandler.setSystemId("file:///" + XML_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_INCL_FILE);
TransformerHandler tfhandler=
saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for contentHandler setter/getter along reader as handler's
* parent.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase10() throws Exception {
String outputFile = USER_DIR + "saxtf010.out";
String goldFile = GOLDEN_DIR + "saxtf010GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter =
saxTFactory.newXMLFilter(new StreamSource(XSLT_FILE));
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for contentHandler setter/getter with parent.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase11() throws Exception {
String outputFile = USER_DIR + "saxtf011.out";
String goldFile = GOLDEN_DIR + "saxtf011GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(domSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for contentHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase12() throws Exception {
String outputFile = USER_DIR + "saxtf012.out";
String goldFile = GOLDEN_DIR + "saxtf012GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
InputSource is = new InputSource(new FileInputStream(XSLT_FILE));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* Unit test for TemplatesHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase13() throws Exception {
String outputFile = USER_DIR + "saxtf013.out";
String goldFile = GOLDEN_DIR + "saxtf013GF.out";
try(FileInputStream fis = new FileInputStream(XML_FILE)) {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
// I have put this as it was complaining about systemid
thandler.setSystemId("file:///" + USER_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
XMLFilter filter
= saxTFactory.newXMLFilter(thandler.getTemplates());
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
filter.parse(new InputSource(fis));
}
assertTrue(compareWithGold(goldFile, outputFile));
}
}

View file

@ -1,91 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerhandler() method which takes StreamSource as argument can
* be set to XMLReader.
*/
public class SAXTFactoryTest001 {
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a transformer handler which write out the
* result to output file. Test verifies output file is same as golden file.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf001.out";
String goldFile = GOLDEN_DIR + "saxtf001GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = saxTFactory.newTransformerHandler(
new StreamSource(xsltFile));
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | TransformerConfigurationException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,95 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerhandler() method which takes SAXSource as argument can
* be set to XMLReader.
*/
public class SAXTFactoryTest002 {
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a content handler which write out the result
* to output file. Test verifies output file is same as golden file.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf002.out";
String goldFile = GOLDEN_DIR + "saxtf002GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile);
FileInputStream fis = new FileInputStream(xsltFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
SAXSource ss = new SAXSource();
ss.setInputSource(new InputSource(fis));
TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,102 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerhandler() method which takes DOMSource as argument can
* be set to XMLReader.
*/
public class SAXTFactoryTest003 {
/**
* Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is
* namespace awareness, DocumentBuilder parse xslt file as DOMSource.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf003.out";
String goldFile = GOLDEN_DIR + "saxtf003GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(xsltFile));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (TransformerConfigurationException | ParserConfigurationException
| SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,65 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/*
* TransformerConfigurationException expected when there is relative URI is used
* in citiesinclude.xsl file
*/
public class SAXTFactoryTest004 {
/**
* Negative test for newTransformerHandler when relative URI is in XML file.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/
@Test(expectedExceptions = TransformerConfigurationException.class)
public void transformerHandlerTest01() throws TransformerConfigurationException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XML_DIR + "citiesinclude.xsl"));
DOMSource domSource= new DOMSource(document);
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
saxTFactory.newTransformerHandler(domSource);
} catch (ParserConfigurationException | IOException | SAXException ex) {
failUnexpected(ex);
}
}
}

View file

@ -1,104 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test SAXSource API when relative URI is used in xsl file and SystemId was set
*/
public class SAXTFactoryTest005 {
/**
* Unit test for XMLReader parsing when relative URI is used in xsl file and
* SystemId was set.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf005.out";
String goldFile = GOLDEN_DIR + "saxtf005GF.out";
String xsltFile = XML_DIR + "citiesinclude.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(xsltFile));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
domSource.setSystemId("file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (TransformerConfigurationException | ParserConfigurationException
| SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,97 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerHandler with a DOMSource and StreamResult set.
*/
public class SAXTFactoryTest006 extends TransformerTestConst{
/**
* Unit test newTransformerHandler with a DOMSource.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf006.out";
String goldFile = GOLDEN_DIR + "saxtf006GF.out";
String xsltFile = XML_DIR + "citiesinclude.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Node node = (Node)docBuilder.parse(new File(xsltFile));
DOMSource domSource = new DOMSource(node, "file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (TransformerConfigurationException | ParserConfigurationException
| SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,91 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerHandler with a Template Handler.
*/
public class SAXTFactoryTest008 {
/**
* Test newTransformerHandler with a Template Handler.
*/
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf008.out";
String goldFile = GOLDEN_DIR + "saxtf008GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
reader.setContentHandler(thandler);
reader.parse(xsltFile);
TransformerHandler tfhandler
= saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,93 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test newTransformerHandler with a Template Handler along with a relative URI
* in the xslt file.
*/
public class SAXTFactoryTest009 {
/**
* Test newTransformerHandler with a Template Handler along with a relative
* URI in the xslt file.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf009.out";
String goldFile = GOLDEN_DIR + "saxtf009GF.out";
String xsltFile = XML_DIR + "citiesinclude.xsl";
String xmlFile = XML_DIR + "cities.xml";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
thandler.setSystemId("file:///" + XML_DIR);
reader.setContentHandler(thandler);
reader.parse(xsltFile);
TransformerHandler tfhandler=
saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(xmlFile);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,91 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test XMLFilter parse InputSource along with customized ContentHandler.
*/
public class SAXTFactoryTest010 {
/**
* Unit test for contentHandler setter/getter along reader as handler's
* parent.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf010.out";
String goldFile = GOLDEN_DIR + "saxtf010GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter =
saxTFactory.newXMLFilter(new StreamSource(xsltFile));
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(xmlFile));
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,103 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test XMLFilter parse InputSource along with customized ContentHandler by
* using SAX parser as it's reader.
*/
public class SAXTFactoryTest011 {
/**
* Unit test for contentHandler setter/getter with parent.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf011.out";
String goldFile = GOLDEN_DIR + "saxtf011GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(xsltFile));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(domSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(xmlFile));
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException
| ParserConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,93 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test XMLFilter parse InputSource along with customized ContentHandler by
* using SAX parser as it's reader.
*/
public class SAXTFactoryTest012 {
/**
* Unit test for contentHandler setter/getter.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf012.out";
String goldFile = GOLDEN_DIR + "saxtf012GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
InputSource is = new InputSource(new FileInputStream(xsltFile));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(xmlFile));
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,94 +0,0 @@
/*
* Copyright (c) 2014, 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 javax.xml.transform.ptests;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Test XMLFilter parse InputSource along with TemplatesHandler.
*/
public class SAXTFactoryTest013 {
/**
* Unit test for TemplatesHandler setter/getter.
*/
@Test
public void testcase01() {
String outputFile = CLASS_DIR + "saxtf013.out";
String goldFile = GOLDEN_DIR + "saxtf013GF.out";
String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml";
try {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
// I have put this as it was complaining about systemid
thandler.setSystemId("file:///" + CLASS_DIR);
reader.setContentHandler(thandler);
reader.parse(xsltFile);
XMLFilter filter
= saxTFactory.newXMLFilter(thandler.getTemplates());
filter.setParent(reader);
filter.setContentHandler(
new MyContentHandler(outputFile));
filter.parse(new InputSource(new FileInputStream(xmlFile)));
assertTrue(compareWithGold(goldFile, outputFile));
} catch (SAXException | IOException | TransformerConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -34,10 +34,11 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import static jaxp.library.JAXPTestUtilities.failUnexpected;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -47,7 +48,7 @@ import org.xml.sax.SAXException;
* Test a StreamResult using a file name that contains URL characters that need * Test a StreamResult using a file name that contains URL characters that need
* to be encoded. * to be encoded.
*/ */
public class StreamResultTest01 { public class StreamResultTest extends JAXPFileBaseTest {
/** /**
* Unit test for StreamResult. * Unit test for StreamResult.
*/ */
@ -82,7 +83,7 @@ public class StreamResultTest01 {
DOMSource domSource = new DOMSource(document); DOMSource domSource = new DOMSource(document);
StreamSource streamSource = new StreamSource(new FileInputStream(xmlFile)); StreamSource streamSource = new StreamSource(new FileInputStream(xmlFile));
File streamResultFile = new File(CLASS_DIR + file); File streamResultFile = new File(USER_DIR + file);
StreamResult streamResult = new StreamResult(streamResultFile); StreamResult streamResult = new StreamResult(streamResultFile);
Transformer transformer = TransformerFactory.newInstance().newTransformer(domSource); Transformer transformer = TransformerFactory.newInstance().newTransformer(domSource);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,11 +24,8 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
@ -36,21 +33,20 @@ import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Class containing the test cases for SAXParserFactory API * Class containing the test cases for SAXParserFactory API
*/ */
public class TfClearParamTest { public class TfClearParamTest extends JAXPFileReadOnlyBaseTest {
/** /**
* Test xslt file. * Test style-sheet file name.
*/ */
private final String XSL_FILE = XML_DIR + "cities.xsl"; private final String XSL_FILE = XML_DIR + "cities.xsl";
@ -72,193 +68,164 @@ public class TfClearParamTest {
/** /**
* Obtains transformer's parameter with the same name that set before. Value * Obtains transformer's parameter with the same name that set before. Value
* should be same as set one. * should be same as set one.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/ */
@Test @Test
public void clear01() { public void clear01() throws TransformerConfigurationException {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(); Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE); transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME).toString(), PARAM_VALUE); assertEquals(transformer.getParameter(LONG_PARAM_NAME).toString(), PARAM_VALUE);
} catch (TransformerConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtains transformer's parameter with the a name that wasn't set before. * Obtains transformer's parameter with the a name that wasn't set before.
* Null is expected. * Null is expected.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/ */
@Test @Test
public void clear02() { public void clear02() throws TransformerConfigurationException {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(); Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE); transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters(); transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME)); assertNull(transformer.getParameter(LONG_PARAM_NAME));
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a stream source with
* the a name that set before. Value should be same as set one.
*/
@Test
public void clear03() {
try {
Transformer transformer = TransformerFactory.newInstance().
newTransformer(new StreamSource(new File(XSL_FILE)));
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a stream source with
* the a name that wasn't set before. Null is expected.
*/
@Test
public void clear04() {
try {
Transformer transformer = TransformerFactory.newInstance().
newTransformer(new StreamSource(new File(XSL_FILE)));
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a sax source with
* the a name that set before. Value should be same as set one.
*/
@Test
public void clear05() {
try {
InputSource is = new InputSource(new FileInputStream(XSL_FILE));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a sax source with
* the a name that wasn't set before. Null is expected.
*/
@Test
public void clear06() {
try {
InputSource is = new InputSource(new FileInputStream(XSL_FILE));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a dom source with
* the a name that set before. Value should be same as set one.
*/
@Test
public void clear07() {
try {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(XSL_FILE));
DOMSource domSource = new DOMSource((Node)document);
Transformer transformer = tfactory.newTransformer(domSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
} catch (IOException | ParserConfigurationException
| TransformerConfigurationException | SAXException ex){
failUnexpected(ex);
}
}
/**
* Obtains transformer's parameter whose initiated with a dom source with
* the a name that wasn't set before. Null is expected.
*/
@Test
public void clear08() {
try {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(XSL_FILE));
DOMSource domSource = new DOMSource((Node)document);
Transformer transformer = tfactory.newTransformer(domSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
} catch (IOException | ParserConfigurationException
| TransformerConfigurationException | SAXException ex){
failUnexpected(ex);
}
} }
/** /**
* Obtains transformer's parameter with a short name that set before. Value * Obtains transformer's parameter with a short name that set before. Value
* should be same as set one. * should be same as set one.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/ */
@Test @Test
public void clear09() { public void clear03() throws TransformerConfigurationException {
try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer(); Transformer transformer = tfactory.newTransformer();
transformer.setParameter(SHORT_PARAM_NAME, PARAM_VALUE); transformer.setParameter(SHORT_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(SHORT_PARAM_NAME).toString(), PARAM_VALUE); assertEquals(transformer.getParameter(SHORT_PARAM_NAME).toString(), PARAM_VALUE);
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* Obtains transformer's parameter with a short name that set with an integer * Obtains transformer's parameter with a short name that set with an integer
* object before. Value should be same as the set integer object. * object before. Value should be same as the set integer object.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/ */
@Test @Test
public void clear10() { public void clear04() throws TransformerConfigurationException {
try { Transformer transformer = TransformerFactory.newInstance().newTransformer();
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
int intObject = 5; int intObject = 5;
transformer.setParameter(SHORT_PARAM_NAME, intObject); transformer.setParameter(SHORT_PARAM_NAME, intObject);
assertEquals(transformer.getParameter(SHORT_PARAM_NAME), intObject); assertEquals(transformer.getParameter(SHORT_PARAM_NAME), intObject);
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
} }
/**
* Obtains transformer's parameter whose initiated with a stream source with
* the a name that set before. Value should be same as set one.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/
@Test (groups = {"readLocalFiles"})
public void clear05() throws TransformerConfigurationException {
Transformer transformer = TransformerFactory.newInstance().
newTransformer(new StreamSource(new File(XSL_FILE)));
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
}
/**
* Obtains transformer's parameter whose initiated with a stream source with
* the a name that wasn't set before. Null is expected.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/
@Test (groups = {"readLocalFiles"})
public void clear06() throws TransformerConfigurationException {
Transformer transformer = TransformerFactory.newInstance().
newTransformer(new StreamSource(new File(XSL_FILE)));
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
}
/**
* Obtains transformer's parameter whose initiated with a sax source with
* the a name that set before. Value should be same as set one.
* @throws Exception If any errors occur.
*/
@Test (groups = {"readLocalFiles"})
public void clear07() throws Exception {
try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(new InputSource(fis));
Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
}
}
/**
* Obtains transformer's parameter whose initiated with a sax source with
* the a name that wasn't set before. Null is expected.
* @throws Exception If any errors occur.
*/
@Test (groups = {"readLocalFiles"})
public void clear08() throws Exception {
try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(new InputSource(fis));
Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
}
}
/**
* Obtains transformer's parameter whose initiated with a dom source with
* the a name that set before. Value should be same as set one.
* @throws Exception If any errors occur.
*/
@Test (groups = {"readLocalFiles"})
public void clear09() throws Exception {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(XSL_FILE));
DOMSource domSource = new DOMSource((Node)document);
Transformer transformer = tfactory.newTransformer(domSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
}
/**
* Obtains transformer's parameter whose initiated with a dom source with
* the a name that wasn't set before. Null is expected.
* @throws Exception If any errors occur.
*/
@Test (groups = {"readLocalFiles"})
public void clear10() throws Exception {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(XSL_FILE));
DOMSource domSource = new DOMSource((Node)document);
Transformer transformer = tfactory.newTransformer(domSource);
transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
transformer.clearParameters();
assertNull(transformer.getParameter(LONG_PARAM_NAME));
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,12 +23,14 @@
package javax.xml.transform.ptests; package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FilePermission;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
@ -38,13 +40,14 @@ import org.testng.annotations.Test;
/** /**
* Basic test for TransformerException specification. * Basic test for TransformerException specification.
*/ */
public class TransformerExcpTest { public class TransformerExcpTest extends JAXPBaseTest {
/** /**
* Transform an unformatted xslt file. TransformerException is thrown. * Transform an unformatted style-sheet file. TransformerException is thrown.
*/ */
@Test @Test
public void tfexception() { public void tfexception() {
try { try {
setPermissions(new FilePermission(XML_DIR + "-", "read"));
// invalid.xsl has well-formedness error. Therefore transform throws // invalid.xsl has well-formedness error. Therefore transform throws
// TransformerException // TransformerException
StreamSource streamSource StreamSource streamSource
@ -60,6 +63,8 @@ public class TransformerExcpTest {
assertNotNull(e.getException()); assertNotNull(e.getException());
assertNull(e.getLocationAsString()); assertNull(e.getLocationAsString());
assertEquals(e.getMessageAndLocation(),e.getMessage()); assertEquals(e.getMessageAndLocation(),e.getMessage());
} finally {
setPermissions();
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,39 +24,35 @@ package javax.xml.transform.ptests;
import java.io.*; import java.io.*;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.*; import javax.xml.parsers.*;
import javax.xml.transform.*; import javax.xml.transform.*;
import javax.xml.transform.dom.*; import javax.xml.transform.dom.*;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.stream.*; import javax.xml.transform.stream.*;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.*; import org.w3c.dom.*;
import org.xml.sax.SAXException;
/** /**
* Class containing the test cases for TransformerFactory API's * Class containing the test cases for TransformerFactory API's
* getAssociatedStyleSheet method. * getAssociatedStyleSheet method.
*/ */
public class TransformerFactoryTest { public class TransformerFactoryTest extends JAXPFileBaseTest {
/** /**
* This test case checks for the getAssociatedStylesheet method * This test case checks for the getAssociatedStylesheet method
* of TransformerFactory. * of TransformerFactory.
* The style sheet returned is then copied to an tfactory01.out * The style sheet returned is then copied to an tfactory01.out
* It will then be verified to see if it matches the golden files * It will then be verified to see if it matches the golden files.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void tfactory01() { public void tfactory01() throws Exception {
String outputFile = CLASS_DIR + "tfactory01.out"; String outputFile = USER_DIR + "tfactory01.out";
String goldFile = GOLDEN_DIR + "tfactory01GF.out"; String goldFile = GOLDEN_DIR + "tfactory01GF.out";
String xmlFile = XML_DIR + "TransformerFactoryTest.xml"; String xmlFile = XML_DIR + "TransformerFactoryTest.xml";
String xmlURI = "file:///" + XML_DIR; String xmlURI = "file:///" + XML_DIR;
@ -76,10 +72,7 @@ public class TransformerFactoryTest {
"Modern", null); "Modern", null);
Transformer t = tFactory.newTransformer(); Transformer t = tFactory.newTransformer();
t.transform(s, streamResult); t.transform(s, streamResult);
assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException | ParserConfigurationException
| TransformerException | SAXException ex) {
failUnexpected(ex);
} }
assertTrue(compareWithGold(goldFile, outputFile));
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,12 +24,9 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.ErrorListener; import javax.xml.transform.ErrorListener;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerConfigurationException;
@ -39,63 +36,58 @@ import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Basic test cases for Transformer API * Basic test cases for Transformer API
*/ */
public class TransformerTest { public class TransformerTest extends JAXPFileReadOnlyBaseTest {
/** /**
* XSLT file serves every test method. * XSLT file serves every test method.
*/ */
private final static String TEST_XSL = XML_DIR + "cities.xsl"; private final static String TEST_XSL = XML_DIR + "cities.xsl";
/** /**
* This tests if newTransformer(StreamSource) method returns Transformer * This tests if newTransformer(StreamSource) method returns Transformer.
* @throws TransformerConfigurationException If for some reason the
* TransformerHandler can not be created.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer01() { public void transformer01() throws TransformerConfigurationException {
try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
StreamSource streamSource = new StreamSource( StreamSource streamSource = new StreamSource(
new File(TEST_XSL)); new File(TEST_XSL));
Transformer transformer = tfactory.newTransformer(streamSource); Transformer transformer = tfactory.newTransformer(streamSource);
assertNotNull(transformer); assertNotNull(transformer);
} catch (TransformerConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* This tests if newTransformer(SAXSource) method returns Transformer * This tests if newTransformer(SAXSource) method returns Transformer.
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer02() { public void transformer02() throws Exception {
try { try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
InputSource is = new InputSource( SAXSource saxSource = new SAXSource(new InputSource(fis));
new FileInputStream(TEST_XSL));
SAXSource saxSource = new SAXSource(is);
Transformer transformer = tfactory.newTransformer(saxSource); Transformer transformer = tfactory.newTransformer(saxSource);
assertNotNull(transformer); assertNotNull(transformer);
} catch (TransformerConfigurationException | FileNotFoundException ex){
failUnexpected(ex);
} }
} }
/** /**
* This tests if newTransformer(DOMSource) method returns Transformer * This tests if newTransformer(DOMSource) method returns Transformer.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer03() { public void transformer03() throws Exception {
try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@ -106,18 +98,15 @@ public class TransformerTest {
Transformer transformer = tfactory.newTransformer(domSource); Transformer transformer = tfactory.newTransformer(domSource);
assertNotNull(transformer); assertNotNull(transformer);
} catch (TransformerConfigurationException | IOException
| ParserConfigurationException | SAXException ex){
failUnexpected(ex);
}
} }
/** /**
* This tests set/get ErrorListener methods of Transformer * This tests set/get ErrorListener methods of Transformer.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer04() { public void transformer04() throws Exception {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
@ -129,18 +118,15 @@ public class TransformerTest {
transformer.setErrorListener(new MyErrorListener()); transformer.setErrorListener(new MyErrorListener());
assertNotNull(transformer.getErrorListener()); assertNotNull(transformer.getErrorListener());
assertTrue(transformer.getErrorListener() instanceof MyErrorListener); assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
} catch (IOException | IllegalArgumentException | ParserConfigurationException
| TransformerConfigurationException | SAXException ex){
failUnexpected(ex);
}
} }
/** /**
* This tests getOutputProperties() method of Transformer * This tests getOutputProperties() method of Transformer.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer05() { public void transformer05() throws Exception {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
@ -157,18 +143,15 @@ public class TransformerTest {
assertEquals(prop.getProperty("standalone"), "no"); assertEquals(prop.getProperty("standalone"), "no");
assertEquals(prop.getProperty("version"), "1.0"); assertEquals(prop.getProperty("version"), "1.0");
assertEquals(prop.getProperty("omit-xml-declaration"), "no"); assertEquals(prop.getProperty("omit-xml-declaration"), "no");
} catch (ParserConfigurationException | SAXException | IOException
| TransformerConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* This tests getOutputProperty() method of Transformer * This tests getOutputProperty() method of Transformer.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void transformer06() { public void transformer06() throws Exception {
try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@ -179,10 +162,6 @@ public class TransformerTest {
Transformer transformer = tfactory.newTransformer(domSource); Transformer transformer = tfactory.newTransformer(domSource);
assertEquals(transformer.getOutputProperty("method"), "xml"); assertEquals(transformer.getOutputProperty("method"), "xml");
} catch (ParserConfigurationException | SAXException | IOException
| TransformerConfigurationException | IllegalArgumentException ex){
failUnexpected(ex);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,42 +25,34 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/** /**
* Here a transformer is created using DOMSource. Some specific output property * Here a transformer is created using DOMSource. Some specific output property
* is set on transformer. Then transform(StreamSource, StreamResult) is tested. * is set on transformer. Then transform(StreamSource, StreamResult) is tested.
*/ */
public class TransformerTest02 { public class TransformerTest02 extends JAXPFileBaseTest {
/** /**
* Unit test for transform(StreamSource, StreamResult). * Unit test for transform(StreamSource, StreamResult).
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String outputFile = CLASS_DIR + "transformer02.out"; String outputFile = USER_DIR + "transformer02.out";
String goldFile = GOLDEN_DIR + "transformer02GF.out"; String goldFile = GOLDEN_DIR + "transformer02GF.out";
String xsltFile = XML_DIR + "cities.xsl"; String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml"; String xmlFile = XML_DIR + "cities.xml";
@ -69,9 +61,8 @@ public class TransformerTest02 {
FileOutputStream fos = new FileOutputStream(outputFile)) { FileOutputStream fos = new FileOutputStream(outputFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
Document document = db.parse(new File(xsltFile)); parse(new File(xsltFile)));
DOMSource domSource = new DOMSource(document);
Transformer transformer = TransformerFactory.newInstance(). Transformer transformer = TransformerFactory.newInstance().
newTransformer(domSource); newTransformer(domSource);
@ -79,20 +70,8 @@ public class TransformerTest02 {
StreamResult streamResult = new StreamResult(fos); StreamResult streamResult = new StreamResult(fos);
transformer.setOutputProperty("indent", "no"); transformer.setOutputProperty("indent", "no");
transformer.transform( streamSource, streamResult); transformer.transform(streamSource, streamResult);
}
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException | IllegalArgumentException
| ParserConfigurationException | TransformerException
| SAXException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,30 +25,20 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties; import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/** /**
* Here Properties Object is populated with required properties.A transformer * Here Properties Object is populated with required properties.A transformer
@ -56,13 +46,15 @@ import org.xml.sax.SAXException;
* for transformer. Then transform(StreamSource, StreamResult) is used for * for transformer. Then transform(StreamSource, StreamResult) is used for
* transformation. This tests the setOutputProperties() method. * transformation. This tests the setOutputProperties() method.
*/ */
public class TransformerTest03 { public class TransformerTest03 extends JAXPFileBaseTest {
/** /**
* Test for Transformer.setOutputProperties method. * Test for Transformer.setOutputProperties method.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String outputFile = CLASS_DIR + "transformer03.out"; String outputFile = USER_DIR + "transformer03.out";
String goldFile = GOLDEN_DIR + "transformer03GF.out"; String goldFile = GOLDEN_DIR + "transformer03GF.out";
String xsltFile = XML_DIR + "cities.xsl"; String xsltFile = XML_DIR + "cities.xsl";
String xmlFile = XML_DIR + "cities.xml"; String xmlFile = XML_DIR + "cities.xml";
@ -81,29 +73,14 @@ public class TransformerTest03 {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
Document document = db.parse(new File(xsltFile)); parse(new File(xsltFile)));
DOMSource domSource = new DOMSource(document);
Transformer transformer = TransformerFactory.newInstance(). Transformer transformer = TransformerFactory.newInstance().
newTransformer(domSource); newTransformer(domSource);
StreamSource streamSource = new StreamSource(fis);
StreamResult streamResult = new StreamResult(fos);
transformer.setOutputProperties(properties); transformer.setOutputProperties(properties);
transformer.transform( streamSource, streamResult); transformer.transform(new StreamSource(fis), new StreamResult(fos));
}
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException
| IOException | TransformerException ex){
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,15 +24,10 @@ package javax.xml.transform.ptests;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver; import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
@ -40,18 +35,17 @@ import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* URIResolver should be invoked when transform happens. * URIResolver should be invoked when transform happens.
*/ */
public class URIResolverTest implements URIResolver { public class URIResolverTest extends JAXPFileBaseTest implements URIResolver {
/** /**
* System ID constant. * System ID constant.
*/ */
@ -72,9 +66,8 @@ public class URIResolverTest implements URIResolver {
*/ */
private final static String XSL_TEMP_FILE = "temp/cities.xsl"; private final static String XSL_TEMP_FILE = "temp/cities.xsl";
/** /**
* expected Href. * expected HREF.
*/ */
private final String validateHref; private final String validateHref;
@ -83,6 +76,14 @@ public class URIResolverTest implements URIResolver {
*/ */
private final String validateBase; private final String validateBase;
/**
* Default constructor for testng invocation.
*/
public URIResolverTest(){
validateHref = null;
validateBase = null;
}
/** /**
* Constructor for setting expected Href and expected Base URI. * Constructor for setting expected Href and expected Base URI.
* @param validateHref expected Href * @param validateHref expected Href
@ -110,31 +111,31 @@ public class URIResolverTest implements URIResolver {
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using StreamSource. xsl file has xsl:include in it * created using StreamSource. style-sheet file has xsl:include in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver01() { public static void resolver01() throws Exception {
try { try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)) {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
StreamSource streamSource = new StreamSource(new FileInputStream(XSL_INCLUDE_FILE)); StreamSource streamSource = new StreamSource(fis);
streamSource.setSystemId(SYSTEM_ID); streamSource.setSystemId(SYSTEM_ID);
assertNotNull(tfactory.newTransformer(streamSource));
Transformer transformer = tfactory.newTransformer(streamSource);
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
} }
} }
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using DOMSource. xsl file has xsl:include in it * created using DOMSource. style-sheet file has xsl:include in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver02() { public static void resolver02() throws Exception {
try {
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
@ -145,131 +146,109 @@ public class URIResolverTest implements URIResolver {
Document document = db.parse(XSL_INCLUDE_FILE); Document document = db.parse(XSL_INCLUDE_FILE);
DOMSource domSource = new DOMSource(document, SYSTEM_ID); DOMSource domSource = new DOMSource(document, SYSTEM_ID);
Transformer transformer = tfactory.newTransformer(domSource); assertNotNull(tfactory.newTransformer(domSource));
} catch (IOException | ParserConfigurationException
| TransformerConfigurationException | SAXException ex){
failUnexpected(ex);
}
} }
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using SAXSource. xsl file has xsl:include in it * created using SAXSource. style-sheet file has xsl:include in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver03() { public static void resolver03() throws Exception {
try { try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)){
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
InputSource is = new InputSource(new FileInputStream(XSL_INCLUDE_FILE)); InputSource is = new InputSource(fis);
is.setSystemId(SYSTEM_ID); is.setSystemId(SYSTEM_ID);
SAXSource saxSource = new SAXSource(is); SAXSource saxSource = new SAXSource(is);
assertNotNull(tfactory.newTransformer(saxSource));
Transformer transformer = tfactory.newTransformer(saxSource);
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
} }
} }
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using StreamSource. xsl file has xsl:import in it * created using StreamSource. style-sheet file has xsl:import in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver04() { public static void resolver04() throws Exception {
try { try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)) {
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
StreamSource streamSource = new StreamSource(fis);
StreamSource streamSource = new StreamSource(new FileInputStream(XSL_IMPORT_FILE));
streamSource.setSystemId(SYSTEM_ID); streamSource.setSystemId(SYSTEM_ID);
assertNotNull(tfactory.newTransformer(streamSource));
Transformer transformer = tfactory.newTransformer(streamSource);
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
} }
} }
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using DOMSource. xsl file has xsl:import in it * created using DOMSource. style-sheet file has xsl:import in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver05() { public static void resolver05() throws Exception {
try {
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(XSL_IMPORT_FILE)); Document document = db.parse(new File(XSL_IMPORT_FILE));
DOMSource domSource = new DOMSource(document, SYSTEM_ID); DOMSource domSource = new DOMSource(document, SYSTEM_ID);
assertNotNull(tfactory.newTransformer(domSource));
Transformer transformer = tfactory.newTransformer(domSource);
} catch (ParserConfigurationException | SAXException | IOException
| TransformerConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* This is to test the URIResolver.resolve() method when a transformer is * This is to test the URIResolver.resolve() method when a transformer is
* created using SAXSource. xsl file has xsl:import in it * created using SAXSource. style-sheet file has xsl:import in it.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void resolver06() { public static void resolver06() throws Exception {
try { try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)){
URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID); URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
TransformerFactory tfactory = TransformerFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setURIResolver(resolver); tfactory.setURIResolver(resolver);
InputSource is = new InputSource(fis);
InputSource is = new InputSource(new FileInputStream(XSL_IMPORT_FILE));
is.setSystemId(SYSTEM_ID); is.setSystemId(SYSTEM_ID);
SAXSource saxSource = new SAXSource(is); SAXSource saxSource = new SAXSource(is);
assertNotNull(tfactory.newTransformer(saxSource));
Transformer transformer = tfactory.newTransformer(saxSource);
} catch (FileNotFoundException | TransformerConfigurationException ex){
failUnexpected(ex);
} }
} }
/** /**
* This is to test the URIResolver.resolve() method when there is an error * This is to test the URIResolver.resolve() method when there is an error
* in the file. * in the file.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public static void docResolver01() { public static void docResolver01() throws Exception {
try { try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) {
URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID); URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID);
TransformerFactory tfactory = TransformerFactory.newInstance(); StreamSource streamSource = new StreamSource(fis);
StreamSource streamSource = new StreamSource(
new FileInputStream(XML_DIR + FILE_SEP + "doctest.xsl"));
streamSource.setSystemId(SYSTEM_ID); streamSource.setSystemId(SYSTEM_ID);
System.err.println(streamSource.getSystemId());
Transformer transformer = tfactory.newTransformer(streamSource); Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);
transformer.setURIResolver(resolver); transformer.setURIResolver(resolver);
File f = new File(XML_DIR + FILE_SEP + "myFake.xml"); File f = new File(XML_DIR + "myFake.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = DocumentBuilderFactory.newInstance().
DocumentBuilder builder = factory.newDocumentBuilder(); newDocumentBuilder().parse(f);
Document document = builder.parse(f);
// Use a Transformer for output // Use a Transformer for output
DOMSource source = new DOMSource(document); DOMSource source = new DOMSource(document);
System.err.println("Ignore the following output -- just dumping it here");
StreamResult result = new StreamResult(System.err); StreamResult result = new StreamResult(System.err);
// No exception is expected because resolver resolve wrong URI.
transformer.transform(source, result); transformer.transform(source, result);
} catch (IOException | ParserConfigurationException | SAXException
| TransformerException ex) {
failUnexpected(ex);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,19 +23,22 @@
package javax.xml.transform.ptests.othervm; package javax.xml.transform.ptests.othervm;
import javax.xml.transform.*; import javax.xml.transform.*;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.fail;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
* Negative test for set invalid TransformerFactory property. * Negative test for set invalid TransformerFactory property.
*/ */
public class TFCErrorTest{ public class TFCErrorTest extends JAXPBaseTest {
@Test(expectedExceptions = ClassNotFoundException.class) @Test(expectedExceptions = ClassNotFoundException.class)
public void tfce01() throws Exception { public void tfce01() throws Exception {
try{ try{
System.setProperty("javax.xml.transform.TransformerFactory","xx"); setSystemProperty("javax.xml.transform.TransformerFactory","xx");
TransformerFactory tFactory = TransformerFactory.newInstance(); TransformerFactory.newInstance();
} catch (TransformerFactoryConfigurationError error) { fail("Expect TransformerFactoryConfigurationError here");
throw error.getException(); } catch (TransformerFactoryConfigurationError expected) {
throw expected.getException();
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
package javax.xml.xpath.ptests; package javax.xml.xpath.ptests;
import java.io.IOException; import java.io.FilePermission;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -31,7 +31,6 @@ import java.nio.file.Paths;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import static javax.xml.xpath.XPathConstants.BOOLEAN; import static javax.xml.xpath.XPathConstants.BOOLEAN;
import static javax.xml.xpath.XPathConstants.NODE; import static javax.xml.xpath.XPathConstants.NODE;
@ -41,7 +40,7 @@ import static javax.xml.xpath.XPathConstants.STRING;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR; import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -49,12 +48,11 @@ import org.w3c.dom.Attr;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Class containing the test cases for XPathExpression API. * Class containing the test cases for XPathExpression API.
*/ */
public class XPathExpressionTest { public class XPathExpressionTest extends JAXPFileReadOnlyBaseTest {
/** /**
* Document object for testing XML file. * Document object for testing XML file.
*/ */
@ -87,13 +85,11 @@ public class XPathExpressionTest {
/** /**
* Create Document object and XPath object for every time * Create Document object and XPath object for every time
* @throws ParserConfigurationException If the factory class cannot be * @throws Exception If any errors occur.
* loaded, instantiated
* @throws SAXException If any parse errors occur.
* @throws IOException If operation on xml file failed.
*/ */
@BeforeTest @BeforeTest
public void setup() throws ParserConfigurationException, SAXException, IOException { public void setup() throws Exception {
setPermissions(new FilePermission(XML_PATH.toFile().toString(), "read"));
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile()); document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
xpath = XPathFactory.newInstance().newXPath(); xpath = XPathFactory.newInstance().newXPath();
} }
@ -101,230 +97,200 @@ public class XPathExpressionTest {
/** /**
* Test for evaluate(java.lang.Object item,QName returnType)throws * Test for evaluate(java.lang.Object item,QName returnType)throws
* XPathExpressionException. * XPathExpressionException.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression01() { public void testCheckXPathExpression01() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(document, STRING), "6"); evaluate(document, STRING), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) throws NPE if input * evaluate(java.lang.Object item,QName returnType) throws NPE if input
* source is null. * source is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression02() { public void testCheckXPathExpression02() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING); xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) throws NPE if returnType * evaluate(java.lang.Object item,QName returnType) throws NPE if returnType
* is null. * is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression03() { public void testCheckXPathExpression03() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(document, null); xpath.compile(EXPRESSION_NAME_A).evaluate(document, null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for method evaluate(java.lang.Object item,QName returnType).If a * Test for method evaluate(java.lang.Object item,QName returnType).If a
* request is made to evaluate the expression in the absence of a context * request is made to evaluate the expression in the absence of a context
* item, simple expressions, such as "1+1", can be evaluated. * item, simple expressions, such as "1+1", can be evaluated.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression04() { public void testCheckXPathExpression04() throws XPathExpressionException {
try {
assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2"); assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) throws IAE If returnType * evaluate(java.lang.Object item,QName returnType) throws IAE If returnType
* is not one of the types defined in XPathConstants. * is not one of the types defined in XPathConstants.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testCheckXPathExpression05() { public void testCheckXPathExpression05() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME); xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) return correct boolean * evaluate(java.lang.Object item,QName returnType) return correct boolean
* value if returnType is Boolean. * value if returnType is Boolean.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression06() { public void testCheckXPathExpression06() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(document, BOOLEAN), true); evaluate(document, BOOLEAN), true);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) return correct boolean * evaluate(java.lang.Object item,QName returnType) return correct boolean
* value if returnType is Boolean. * value if returnType is Boolean.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression07() { public void testCheckXPathExpression07() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_B). assertEquals(xpath.compile(EXPRESSION_NAME_B).
evaluate(document, BOOLEAN), false); evaluate(document, BOOLEAN), false);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) return correct number * evaluate(java.lang.Object item,QName returnType) return correct number
* value when return type is Double. * value when return type is Double.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression08() { public void testCheckXPathExpression08() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(document, NUMBER), 6d); evaluate(document, NUMBER), 6d);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) evaluate an attribute * evaluate(java.lang.Object item,QName returnType) evaluate an attribute
* value which returnType is Node. * value which returnType is Node.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression09() { public void testCheckXPathExpression09() throws XPathExpressionException {
try {
Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A). Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
evaluate(document, NODE); evaluate(document, NODE);
assertEquals(attr.getValue(), "6"); assertEquals(attr.getValue(), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item,QName returnType) evaluate an attribute * evaluate(java.lang.Object item,QName returnType) evaluate an attribute
* value which returnType is NodeList. * value which returnType is NodeList.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression10() { public void testCheckXPathExpression10() throws XPathExpressionException {
try {
NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A). NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
evaluate(document, NODESET); evaluate(document, NODESET);
Attr attr = (Attr) nodeList.item(0); Attr attr = (Attr) nodeList.item(0);
assertEquals(attr.getValue(), "6"); assertEquals(attr.getValue(), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for evaluate(java.lang.Object item) when returnType is left off of * Test for evaluate(java.lang.Object item) when returnType is left off of
* the XPath.evaluate method, all expressions are evaluated to a String * the XPath.evaluate method, all expressions are evaluated to a String
* value. * value.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression11() { public void testCheckXPathExpression11() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6"); assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item) throws NPE if expression is null. * evaluate(java.lang.Object item) throws NPE if expression is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression12() { public void testCheckXPathExpression12() throws XPathExpressionException {
try {
xpath.compile(null).evaluate(document); xpath.compile(null).evaluate(document);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item) when a request is made to evaluate the * evaluate(java.lang.Object item) when a request is made to evaluate the
* expression in the absence of a context item, simple expressions, such as * expression in the absence of a context item, simple expressions, such as
* "1+1", can be evaluated. * "1+1", can be evaluated.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathExpression13() { public void testCheckXPathExpression13() throws XPathExpressionException {
try {
assertEquals(xpath.compile("1+1").evaluate(document), "2"); assertEquals(xpath.compile("1+1").evaluate(document), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(java.lang.Object item) throws NPE if document is null. * evaluate(java.lang.Object item) throws NPE if document is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression14() { public void testCheckXPathExpression14() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(null); xpath.compile(EXPRESSION_NAME_A).evaluate(null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* valuate(InputSource source) return a string value if return type is * valuate(InputSource source) return a string value if return type is
* String. * String.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void testCheckXPathExpression15() { public void testCheckXPathExpression15() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is)), "6"); evaluate(new InputSource(is)), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source) throws NPE if input source is null. * evaluate(InputSource source) throws NPE if input source is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression16() { public void testCheckXPathExpression16() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(null); xpath.compile(EXPRESSION_NAME_A).evaluate(null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(InputSource source) throws NPE if expression is null * evaluate(InputSource source) throws NPE if expression is null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression17() { public void testCheckXPathExpression17() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(null).evaluate(new InputSource(is)); xpath.compile(null).evaluate(new InputSource(is));
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
@ -332,14 +298,12 @@ public class XPathExpressionTest {
* evaluate(InputSource source) throws XPathExpressionException if * evaluate(InputSource source) throws XPathExpressionException if
* returnType is String junk characters. * returnType is String junk characters.
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPathExpression18() throws XPathExpressionException { public void testCheckXPathExpression18() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile("-*&").evaluate(new InputSource(is)); xpath.compile("-*&").evaluate(new InputSource(is));
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -347,67 +311,63 @@ public class XPathExpressionTest {
* evaluate(InputSource source) throws XPathExpressionException if * evaluate(InputSource source) throws XPathExpressionException if
* expression is a blank string " ". * expression is a blank string " ".
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPathExpression19() throws XPathExpressionException { public void testCheckXPathExpression19() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(" ").evaluate(new InputSource(is)); xpath.compile(" ").evaluate(new InputSource(is));
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Test for evaluate(InputSource source,QName returnType) returns a string * Test for evaluate(InputSource source,QName returnType) returns a string
* value if returnType is String. * value if returnType is String.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression20() { public void testCheckXPathExpression20() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is), STRING), "6"); evaluate(new InputSource(is), STRING), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source,QName returnType) throws NPE if source is * evaluate(InputSource source,QName returnType) throws NPE if source is
* null. * null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression21() { public void testCheckXPathExpression21() throws XPathExpressionException {
try {
xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING); xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* evaluate(InputSource source,QName returnType) throws NPE if expression is * evaluate(InputSource source,QName returnType) throws NPE if expression is
* null. * null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression22() { public void testCheckXPathExpression22() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(null).evaluate(new InputSource(is), STRING); xpath.compile(null).evaluate(new InputSource(is), STRING);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source,QName returnType) throws NPE if returnType is * evaluate(InputSource source,QName returnType) throws NPE if returnType is
* null. * null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPathExpression23() { public void testCheckXPathExpression23() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null); xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
@ -415,14 +375,12 @@ public class XPathExpressionTest {
* evaluate(InputSource source,QName returnType) throws * evaluate(InputSource source,QName returnType) throws
* XPathExpressionException if expression is junk characters. * XPathExpressionException if expression is junk characters.
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPathExpression24() throws XPathExpressionException { public void testCheckXPathExpression24() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile("-*&").evaluate(new InputSource(is), STRING); xpath.compile("-*&").evaluate(new InputSource(is), STRING);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -430,14 +388,12 @@ public class XPathExpressionTest {
* evaluate(InputSource source,QName returnType) throws * evaluate(InputSource source,QName returnType) throws
* XPathExpressionException if expression is blank " ". * XPathExpressionException if expression is blank " ".
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPathExpression25() throws XPathExpressionException { public void testCheckXPathExpression25() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(" ").evaluate(new InputSource(is), STRING); xpath.compile(" ").evaluate(new InputSource(is), STRING);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -445,85 +401,85 @@ public class XPathExpressionTest {
* evaluate(InputSource source,QName returnType) throws * evaluate(InputSource source,QName returnType) throws
* IllegalArgumentException if returnType is not one of the types defined * IllegalArgumentException if returnType is not one of the types defined
* in XPathConstants. * in XPathConstants.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = IllegalArgumentException.class)
public void testCheckXPathExpression26() { public void testCheckXPathExpression26() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME); xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source,QName returnType) return a correct boolean * evaluate(InputSource source,QName returnType) return a correct boolean
* value if returnType is Boolean. * value if returnType is Boolean.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression27() { public void testCheckXPathExpression27() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is), BOOLEAN), true); evaluate(new InputSource(is), BOOLEAN), true);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source,QName returnType) return a correct boolean * evaluate(InputSource source,QName returnType) return a correct boolean
* value if returnType is Boolean. * value if returnType is Boolean.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression28() { public void testCheckXPathExpression28() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.compile(EXPRESSION_NAME_B). assertEquals(xpath.compile(EXPRESSION_NAME_B).
evaluate(new InputSource(is), BOOLEAN), false); evaluate(new InputSource(is), BOOLEAN), false);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* evaluate(InputSource source,QName returnType) return a correct number * evaluate(InputSource source,QName returnType) return a correct number
* value if returnType is Number. * value if returnType is Number.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression29() { public void testCheckXPathExpression29() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.compile(EXPRESSION_NAME_A). assertEquals(xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is), NUMBER), 6d); evaluate(new InputSource(is), NUMBER), 6d);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Test for evaluate(InputSource source,QName returnType) returns a node if * Test for evaluate(InputSource source,QName returnType) returns a node if
* returnType is Node. * returnType is Node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression30() { public void testCheckXPathExpression30() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A). Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is), NODE); evaluate(new InputSource(is), NODE);
assertEquals(attr.getValue(), "6"); assertEquals(attr.getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Test for evaluate(InputSource source,QName returnType) return a node list * Test for evaluate(InputSource source,QName returnType) return a node list
* if returnType is NodeList. * if returnType is NodeList.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPathExpression31() { public void testCheckXPathExpression31() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A). NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
evaluate(new InputSource(is), NODESET); evaluate(new InputSource(is), NODESET);
assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,14 +26,14 @@ package javax.xml.xpath.ptests;
import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL; import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException; import javax.xml.xpath.XPathFactoryConfigurationException;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPBaseTest;
import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNotNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
* Class containing the test cases for XPathFactory API. * Class containing the test cases for XPathFactory API.
*/ */
public class XPathFactoryTest { public class XPathFactoryTest extends JAXPBaseTest {
/** /**
* Valid URL for creating a XPath factory. * Valid URL for creating a XPath factory.
*/ */
@ -54,21 +54,21 @@ public class XPathFactoryTest {
/** /**
* XPathFactory.newInstance(String uri) throws NPE if uri is null. * XPathFactory.newInstance(String uri) throws NPE if uri is null.
*
* @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
private void testCheckXPathFactory02() { public void testCheckXPathFactory02() throws XPathFactoryConfigurationException {
try {
XPathFactory.newInstance(null); XPathFactory.newInstance(null);
} catch (XPathFactoryConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPathFactory.newInstance(String uri) throws XPFCE if uri is just a blank * XPathFactory.newInstance(String uri) throws XPFCE if uri is just a blank
* string. * string.
* *
* @throws XPathFactoryConfigurationException * @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test(expectedExceptions = XPathFactoryConfigurationException.class) @Test(expectedExceptions = XPathFactoryConfigurationException.class)
public void testCheckXPathFactory03() throws XPathFactoryConfigurationException { public void testCheckXPathFactory03() throws XPathFactoryConfigurationException {
@ -78,21 +78,21 @@ public class XPathFactoryTest {
/** /**
* Test for constructor - XPathFactory.newInstance(String uri) with valid * Test for constructor - XPathFactory.newInstance(String uri) with valid
* url - "http://java.sun.com/jaxp/xpath/dom". * url - "http://java.sun.com/jaxp/xpath/dom".
*
* @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test @Test
public void testCheckXPathFactory04() { public void testCheckXPathFactory04() throws XPathFactoryConfigurationException {
try {
assertNotNull(XPathFactory.newInstance(VALID_URL)); assertNotNull(XPathFactory.newInstance(VALID_URL));
} catch (XPathFactoryConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for constructor - XPathFactory.newInstance(String uri) with invalid * Test for constructor - XPathFactory.newInstance(String uri) with invalid
* url - "http://java.sun.com/jaxp/xpath/dom1". * url - "http://java.sun.com/jaxp/xpath/dom1".
* *
* @throws XPathFactoryConfigurationException * @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test(expectedExceptions = XPathFactoryConfigurationException.class) @Test(expectedExceptions = XPathFactoryConfigurationException.class)
public void testCheckXPathFactory05() throws XPathFactoryConfigurationException { public void testCheckXPathFactory05() throws XPathFactoryConfigurationException {
@ -112,26 +112,24 @@ public class XPathFactoryTest {
* Test for constructor - XPathFactory.newInstance(String uri) with valid * Test for constructor - XPathFactory.newInstance(String uri) with valid
* url - "http://java.sun.com/jaxp/xpath/dom" and creating XPath with * url - "http://java.sun.com/jaxp/xpath/dom" and creating XPath with
* newXPath(). * newXPath().
*
* @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test @Test
public void testCheckXPathFactory07() { public void testCheckXPathFactory07() throws XPathFactoryConfigurationException {
try {
assertNotNull(XPathFactory.newInstance(VALID_URL).newXPath()); assertNotNull(XPathFactory.newInstance(VALID_URL).newXPath());
} catch (XPathFactoryConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for constructor - XPathFactory.newInstance(String uri) with valid * Test for constructor - XPathFactory.newInstance(String uri) with valid
* uri - DOM_OBJECT_MODEL.toString(). * uri - DOM_OBJECT_MODEL.toString().
*
* @throws XPathFactoryConfigurationException If the specified object model
* is unavailable, or if there is a configuration error.
*/ */
@Test @Test
public void testCheckXPathFactory08() { public void testCheckXPathFactory08() throws XPathFactoryConfigurationException {
try {
assertNotNull(XPathFactory.newInstance(DOM_OBJECT_MODEL)); assertNotNull(XPathFactory.newInstance(DOM_OBJECT_MODEL));
} catch (XPathFactoryConfigurationException ex) {
failUnexpected(ex);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,7 @@ package javax.xml.xpath.ptests;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -34,7 +34,7 @@ import org.testng.annotations.Test;
/** /**
* Class containing the test cases for XPathFunctionResolver. * Class containing the test cases for XPathFunctionResolver.
*/ */
public class XPathFunctionResolverTest { public class XPathFunctionResolverTest extends JAXPBaseTest {
/** /**
* A XPath for evaluation environment and expressions. * A XPath for evaluation environment and expressions.
*/ */
@ -54,26 +54,22 @@ public class XPathFunctionResolverTest {
/** /**
* Test for resolveFunction(QName functionName,int arity). evaluate will * Test for resolveFunction(QName functionName,int arity). evaluate will
* continue as long as functionName is meaningful. * continue as long as functionName is meaningful.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPathFunctionResolver01() { public void testCheckXPathFunctionResolver01() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate("round(1.7)", (Object)null), "2"); assertEquals(xpath.evaluate("round(1.7)", (Object)null), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for resolveFunction(QName functionName,int arity); evaluate throws * Test for resolveFunction(QName functionName,int arity); evaluate throws
* NPE if functionName is null. * NPE if functionName is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPathFunctionResolver02() { public void testCheckXPathFunctionResolver02() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(null, "5"), "2"); assertEquals(xpath.evaluate(null, "5"), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
package javax.xml.xpath.ptests; package javax.xml.xpath.ptests;
import java.io.IOException; import java.io.FilePermission;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -33,7 +33,6 @@ import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import static javax.xml.xpath.XPathConstants.BOOLEAN; import static javax.xml.xpath.XPathConstants.BOOLEAN;
import static javax.xml.xpath.XPathConstants.NODE; import static javax.xml.xpath.XPathConstants.NODE;
@ -43,7 +42,7 @@ import static javax.xml.xpath.XPathConstants.STRING;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR; import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertNull;
@ -53,12 +52,11 @@ import org.w3c.dom.Attr;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Class containing the test cases for XPath API. * Class containing the test cases for XPath API.
*/ */
public class XPathTest { public class XPathTest extends JAXPFileReadOnlyBaseTest {
/** /**
* Document object for testing XML file. * Document object for testing XML file.
*/ */
@ -91,13 +89,11 @@ public class XPathTest {
/** /**
* Create Document object and XPath object for every time * Create Document object and XPath object for every time
* @throws ParserConfigurationException If the factory class cannot be * @throws Exception If any errors occur.
* loaded, instantiated
* @throws SAXException If any parse errors occur.
* @throws IOException If operation on xml file failed.
*/ */
@BeforeTest @BeforeTest
public void setup() throws ParserConfigurationException, SAXException, IOException { public void setup() throws Exception {
setPermissions(new FilePermission(XML_DIR + "-", "read"));
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile()); document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
xpath = XPathFactory.newInstance().newXPath(); xpath = XPathFactory.newInstance().newXPath();
} }
@ -105,62 +101,54 @@ public class XPathTest {
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item, QName returnType) which return type is String. * item, QName returnType) which return type is String.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath01() { public void testCheckXPath01() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6"); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.compile(java.lang.String expression) and then * Test for XPath.compile(java.lang.String expression) and then
* evaluate(java.lang.Object item, QName returnType). * evaluate(java.lang.Object item, QName returnType).
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath02() { public void testCheckXPath02() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6"); assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item) when the third argument is left off of the XPath.evaluate method, * item) when the third argument is left off of the XPath.evaluate method,
* all expressions are evaluated to a String value. * all expressions are evaluated to a String value.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath03() { public void testCheckXPath03() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6"); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.compile(java.lang.String expression). If expression is * Test for XPath.compile(java.lang.String expression). If expression is
* null, should throw NPE. * null, should throw NPE.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath04() { public void testCheckXPath04() throws XPathExpressionException {
try {
xpath.compile(null); xpath.compile(null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.compile(java.lang.String expression). If expression cannot * Test for XPath.compile(java.lang.String expression). If expression cannot
* be compiled junk characters, should throw XPathExpressionException. * be compiled junk characters, should throw XPathExpressionException.
* *
* @throws XPathExpressionException * @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(expectedExceptions = XPathExpressionException.class)
public void testCheckXPath05() throws XPathExpressionException { public void testCheckXPath05() throws XPathExpressionException {
@ -171,7 +159,7 @@ public class XPathTest {
* Test for XPath.compile(java.lang.String expression). If expression is * Test for XPath.compile(java.lang.String expression). If expression is
* blank, should throw XPathExpressionException * blank, should throw XPathExpressionException
* *
* @throws XPathExpressionException * @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(expectedExceptions = XPathExpressionException.class)
public void testCheckXPath06() throws XPathExpressionException { public void testCheckXPath06() throws XPathExpressionException {
@ -181,55 +169,46 @@ public class XPathTest {
/** /**
* Test for XPath.compile(java.lang.String expression). The expression * Test for XPath.compile(java.lang.String expression). The expression
* cannot be evaluated as this does not exist. * cannot be evaluated as this does not exist.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath07() { public void testCheckXPath07() throws XPathExpressionException {
try {
assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), ""); assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), "");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item, QName returnType). If String expression is null, should throw NPE * item, QName returnType). If String expression is null, should throw NPE.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath08() { public void testCheckXPath08() throws XPathExpressionException {
try {
xpath.evaluate(null, document, STRING); xpath.evaluate(null, document, STRING);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item, QName returnType). If item is null, should throw NPE. * item, QName returnType). If item is null, should throw NPE.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath09() { public void testCheckXPath09() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, null, STRING); xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item, QName returnType). If returnType is null, should throw NPE. * item, QName returnType). If returnType is null, should throw NPE.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath10() { public void testCheckXPath10() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, document, null); xpath.evaluate(EXPRESSION_NAME_A, document, null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
@ -237,23 +216,20 @@ public class XPathTest {
* item, QName returnType). If a request is made to evaluate the expression * item, QName returnType). If a request is made to evaluate the expression
* in the absence of a context item, simple expressions, such as "1+1", can * in the absence of a context item, simple expressions, such as "1+1", can
* be evaluated. * be evaluated.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath11() { public void testCheckXPath11() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate("1+1", document, STRING), "2"); assertEquals(xpath.evaluate("1+1", document, STRING), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) throws XPathExpressionException if expression is a empty * returnType) throws XPathExpressionException if expression is a empty
* string "". * string "".
* .
* *
* @throws XPathExpressionException * @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(expectedExceptions = XPathExpressionException.class)
public void testCheckXPath12() throws XPathExpressionException { public void testCheckXPath12() throws XPathExpressionException {
@ -264,161 +240,141 @@ public class XPathTest {
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) throws IllegalArgumentException if returnType is not one of * returnType) throws IllegalArgumentException if returnType is not one of
* the types defined in XPathConstants. * the types defined in XPathConstants.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testCheckXPath13() { public void testCheckXPath13() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME); xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) returns correct boolean value if returnType is Boolean. * returnType) returns correct boolean value if returnType is Boolean.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath14() { public void testCheckXPath14() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) returns false as expression is not successful in evaluating * returnType) returns false as expression is not successful in evaluating
* to any result if returnType is Boolean. * to any result if returnType is Boolean.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath15() { public void testCheckXPath15() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false); assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) returns correct number value if return type is Number. * returnType) returns correct number value if return type is Number.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath16() { public void testCheckXPath16() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
* returnType) returns correct string value if return type is Node. * returnType) returns correct string value if return type is Node.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath17() { public void testCheckXPath17() throws XPathExpressionException {
try {
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6"); assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item, QName returnType). If return type is NodeList,the evaluated value * item, QName returnType). If return type is NodeList,the evaluated value
* equals to "6" as expected. * equals to "6" as expected.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath18() { public void testCheckXPath18() throws XPathExpressionException {
try {
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET); NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET);
assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item). If expression is null, should throw NPE. * item). If expression is null, should throw NPE.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath19() { public void testCheckXPath19() throws XPathExpressionException {
try {
xpath.evaluate(null, document); xpath.evaluate(null, document);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
* item). If a request is made to evaluate the expression in the absence of * item). If a request is made to evaluate the expression in the absence of
* a context item, simple expressions, such as "1+1", can be evaluated. * a context item, simple expressions, such as "1+1", can be evaluated.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test @Test
public void testCheckXPath20() { public void testCheckXPath20() throws XPathExpressionException {
try {
assertEquals(xpath.evaluate("1+1", document), "2"); assertEquals(xpath.evaluate("1+1", document), "2");
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, java.lang.Object item) throws * XPath.evaluate(java.lang.String expression, java.lang.Object item) throws
* NPE if InputSource is null. * NPE if InputSource is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath21() { public void testCheckXPath21() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, null); xpath.evaluate(EXPRESSION_NAME_A, null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source) return * XPath.evaluate(java.lang.String expression, InputSource source) return
* correct value by looking for Node. * correct value by looking for Node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test (groups = {"readLocalFiles"})
public void testCheckXPath22() { public void testCheckXPath22() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6"); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source) throws * XPath.evaluate(java.lang.String expression, InputSource source) throws
* NPE if InputSource is null. * NPE if InputSource is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath23() { public void testCheckXPath23() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, null); xpath.evaluate(EXPRESSION_NAME_A, null);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source) throws * XPath.evaluate(java.lang.String expression, InputSource source) throws
* NPE if String expression is null. * NPE if String expression is null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPath24() { public void testCheckXPath24() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(null, new InputSource(is)); xpath.evaluate(null, new InputSource(is));
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
@ -427,14 +383,12 @@ public class XPathTest {
* If expression is junk characters, expression cannot be evaluated, should * If expression is junk characters, expression cannot be evaluated, should
* throw XPathExpressionException. * throw XPathExpressionException.
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPath25() throws XPathExpressionException { public void testCheckXPath25() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate("-*&", new InputSource(is)); xpath.evaluate("-*&", new InputSource(is));
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -442,66 +396,62 @@ public class XPathTest {
* XPath.evaluate(java.lang.String expression, InputSource source) throws * XPath.evaluate(java.lang.String expression, InputSource source) throws
* XPathExpressionException if expression is blank " ". * XPathExpressionException if expression is blank " ".
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPath26() throws XPathExpressionException { public void testCheckXPath26() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(" ", new InputSource(is)); xpath.evaluate(" ", new InputSource(is));
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, QName * XPath.evaluate(java.lang.String expression, InputSource source, QName
* returnType) returns correct string value which return type is String. * returnType) returns correct string value which return type is String.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath27() { public void testCheckXPath27() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6"); assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, QName * XPath.evaluate(java.lang.String expression, InputSource source, QName
* returnType) throws NPE if source is null. * returnType) throws NPE if source is null.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testCheckXPath28() { public void testCheckXPath28() throws XPathExpressionException {
try {
xpath.evaluate(EXPRESSION_NAME_A, null, STRING); xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
} catch (XPathExpressionException ex) {
failUnexpected(ex);
}
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, QName * XPath.evaluate(java.lang.String expression, InputSource source, QName
* returnType) throws NPE if expression is null. * returnType) throws NPE if expression is null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPath29() { public void testCheckXPath29() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(null, new InputSource(is), STRING); xpath.evaluate(null, new InputSource(is), STRING);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) throws NPE if returnType is null . * QName returnType) throws NPE if returnType is null.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void testCheckXPath30() { public void testCheckXPath30() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null); xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
@ -509,14 +459,12 @@ public class XPathTest {
* XPath.evaluate(java.lang.String expression, InputSource source, QName * XPath.evaluate(java.lang.String expression, InputSource source, QName
* returnType) throws XPathExpressionException if expression is junk characters. * returnType) throws XPathExpressionException if expression is junk characters.
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPath31() throws XPathExpressionException { public void testCheckXPath31() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate("-*&", new InputSource(is), STRING); xpath.evaluate("-*&", new InputSource(is), STRING);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -524,14 +472,12 @@ public class XPathTest {
* XPath.evaluate(java.lang.String expression, InputSource source, QName * XPath.evaluate(java.lang.String expression, InputSource source, QName
* returnType) throws XPathExpressionException if expression is blank " ". * returnType) throws XPathExpressionException if expression is blank " ".
* *
* @throws XPathExpressionException * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = XPathExpressionException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
public void testCheckXPath32() throws XPathExpressionException { public void testCheckXPath32() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(" ", new InputSource(is), STRING); xpath.evaluate(" ", new InputSource(is), STRING);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
@ -539,84 +485,84 @@ public class XPathTest {
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) throws IllegalArgumentException if returnType is not * QName returnType) throws IllegalArgumentException if returnType is not
* one of the types defined in XPathConstants. * one of the types defined in XPathConstants.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = IllegalArgumentException.class)
public void testCheckXPath33() { public void testCheckXPath33() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME); xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) return correct boolean value if return type is Boolean. * QName returnType) return correct boolean value if return type is Boolean.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath34() { public void testCheckXPath34() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
BOOLEAN), true); BOOLEAN), true);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) return correct boolean value if return type is Boolean. * QName returnType) return correct boolean value if return type is Boolean.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath35() { public void testCheckXPath35() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is), assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
BOOLEAN), false); BOOLEAN), false);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) return correct number value if return type is Number. * QName returnType) return correct number value if return type is Number.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath36() { public void testCheckXPath36() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
NUMBER), 6d); NUMBER), 6d);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource source, * XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) return correct string value if return type is Node. * QName returnType) return correct string value if return type is Node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath37() { public void testCheckXPath37() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
new InputSource(is), NODE)).getValue(), "6"); new InputSource(is), NODE)).getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Test for XPath.evaluate(java.lang.String expression, InputSource source, * Test for XPath.evaluate(java.lang.String expression, InputSource source,
* QName returnType) which return type is NodeList. * QName returnType) which return type is NodeList.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath38() { public void testCheckXPath38() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
new InputSource(is), NODESET); new InputSource(is), NODESET);
assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
@ -624,57 +570,57 @@ public class XPathTest {
* Test for XPath.evaluate(java.lang.String expression, InputSource iSource, * Test for XPath.evaluate(java.lang.String expression, InputSource iSource,
* QName returnType). If return type is Boolean, should return false as * QName returnType). If return type is Boolean, should return false as
* expression is not successful in evaluating to any result. * expression is not successful in evaluating to any result.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath52() { public void testCheckXPath52() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is), assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
BOOLEAN), false); BOOLEAN), false);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
* returnType) returns correct number value which return type is Number. * returnType) returns correct number value which return type is Number.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath53() { public void testCheckXPath53() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
NUMBER), 6d); NUMBER), 6d);
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
* returnType) returns a node value if returnType is Node. * returnType) returns a node value if returnType is Node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath54() { public void testCheckXPath54() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
new InputSource(is), NODE)).getValue(), "6"); new InputSource(is), NODE)).getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
* returnType) returns a node list if returnType is NodeList. * returnType) returns a node list if returnType is NodeList.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckXPath55() { public void testCheckXPath55() throws Exception {
try (InputStream is = Files.newInputStream(XML_PATH)) { try (InputStream is = Files.newInputStream(XML_PATH)) {
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
new InputSource(is), NODESET); new InputSource(is), NODESET);
assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
} catch (XPathExpressionException | IOException ex) {
failUnexpected(ex);
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
*/ */
package org.xml.sax.ptests; package org.xml.sax.ptests;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -30,7 +31,7 @@ import org.xml.sax.helpers.AttributesImpl;
/** /**
* Class containing the test cases for AttributesImpl API. * Class containing the test cases for AttributesImpl API.
*/ */
public class AttrImplTest { public class AttrImplTest extends JAXPBaseTest {
private static final String CAR_URI = "http://www.cars.com/xml"; private static final String CAR_URI = "http://www.cars.com/xml";
private static final String CAR_LOCALNAME = "part"; private static final String CAR_LOCALNAME = "part";

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,20 +23,13 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -45,18 +38,19 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* ContentHandler has Attributes as one of its arguments. Attributes * ContentHandler has Attributes as one of its arguments. Attributes
* pertaining to an element are taken into this argument and various methods * pertaining to an element are taken into this argument and various methods
* of Attributes interfaces are tested. This program uses Namespace processing * of Attributes interfaces are tested. This program uses Namespace processing
* with namespaces in xml file. This program does not use Validation * with namespaces in XML file. This program does not use Validation
*/ */
public class AttributesNSTest { public class AttributesNSTest extends JAXPFileBaseTest {
/** /**
* Test for Attribute Interface's setter/getter. * Test for Attribute Interface's setter/getter.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String outputFile = CLASS_DIR + "AttributesNS.out"; String outputFile = USER_DIR + "AttributesNS.out";
String goldFile = GOLDEN_DIR + "AttributesNSGF.out"; String goldFile = GOLDEN_DIR + "AttributesNSGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
// http://www.saxproject.com/?selected=namespaces namespace-prefixes // http://www.saxproject.com/?selected=namespaces namespace-prefixes
@ -68,16 +62,5 @@ public class AttributesNSTest {
saxParser.parse(new File(xmlFile), myAttrCHandler); saxParser.parse(new File(xmlFile), myAttrCHandler);
myAttrCHandler.flushAndClose(); myAttrCHandler.flushAndClose();
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException | ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,20 +23,13 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -48,17 +41,19 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* This program uses Namespace processing without any namepsaces in xml file. * This program uses Namespace processing without any namepsaces in xml file.
* This program uses Validation * This program uses Validation
*/ */
public class AttributesTest { public class AttributesTest extends JAXPFileBaseTest {
/** /**
* Unit test for Attributes interface. Prints all attributes into output * Unit test for Attributes interface. Prints all attributes into output
* file. Check it with golden file. * file. Check it with golden file.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String outputFile = CLASS_DIR + "Attributes.out"; String outputFile = USER_DIR + "Attributes.out";
String goldFile = GOLDEN_DIR + "AttributesGF.out"; String goldFile = GOLDEN_DIR + "AttributesGF.out";
String xmlFile = XML_DIR + "family.xml"; String xmlFile = XML_DIR + "family.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
spf.setFeature("http://xml.org/sax/features/namespace-prefixes", spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
@ -69,16 +64,5 @@ public class AttributesTest {
saxParser.parse(new File(xmlFile), myAttrCHandler); saxParser.parse(new File(xmlFile), myAttrCHandler);
myAttrCHandler.flushAndClose(); myAttrCHandler.flushAndClose();
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException | ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,24 +26,18 @@ import java.io.BufferedWriter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.Locator; import org.xml.sax.Locator;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -52,43 +46,34 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* transverses XML and print all visited node when XMLreader parses XML. Test * transverses XML and print all visited node when XMLreader parses XML. Test
* verifies output is same as the golden file. * verifies output is same as the golden file.
*/ */
public class ContentHandlerTest { public class ContentHandlerTest extends JAXPFileBaseTest {
/** /**
* Content event handler visit all nodes to print to output file. * Content event handler visit all nodes to print to output file.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testcase01() { public void testcase01() throws Exception {
String outputFile = CLASS_DIR + "Content.out"; String outputFile = USER_DIR + "Content.out";
String goldFile = GOLDEN_DIR + "ContentGF.out"; String goldFile = GOLDEN_DIR + "ContentGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try(FileInputStream instream = new FileInputStream(xmlFile)) { try(FileInputStream instream = new FileInputStream(xmlFile);
MyContentHandler cHandler = new MyContentHandler(outputFile)) {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
ContentHandler cHandler = new MyContentHandler(outputFile);
xmlReader.setContentHandler(cHandler); xmlReader.setContentHandler(cHandler);
InputSource is = new InputSource(instream); xmlReader.parse(new InputSource(instream));
xmlReader.parse(is); }
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch( IOException | SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }
/** /**
* A content write out handler. * A content write out handler.
*/ */
class MyContentHandler extends XMLFilterImpl { class MyContentHandler extends XMLFilterImpl implements AutoCloseable {
/** /**
* Prefix to every exception. * Prefix to every exception.
*/ */
@ -258,4 +243,14 @@ class MyContentHandler extends XMLFilterImpl {
throw new SAXException(WRITE_ERROR, ex); throw new SAXException(WRITE_ERROR, ex);
} }
} }
/**
* Close the writer if it's initiated.
* @throws IOException if any IO error when close buffered writer.
*/
@Override
public void close() throws IOException {
if (bWriter != null)
bWriter.close();
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,15 +26,11 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
@ -42,7 +38,6 @@ import org.xml.sax.Locator;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -50,17 +45,18 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* XMLReader parse XML with default handler that transverses XML and * XMLReader parse XML with default handler that transverses XML and
* print all visited node. Test verifies output is same as the golden file. * print all visited node. Test verifies output is same as the golden file.
*/ */
public class DefaultHandlerTest { public class DefaultHandlerTest extends JAXPFileBaseTest {
/** /**
* Test default handler that transverses XML and print all visited node. * Test default handler that transverses XML and print all visited node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testDefaultHandler() { public void testDefaultHandler() throws Exception {
String outputFile = CLASS_DIR + "DefaultHandler.out"; String outputFile = USER_DIR + "DefaultHandler.out";
String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser(); SAXParser saxparser = spf.newSAXParser();
@ -71,25 +67,10 @@ public class DefaultHandlerTest {
String newAbsolutePath = Absolutepath; String newAbsolutePath = Absolutepath;
if (File.separatorChar == '\\') if (File.separatorChar == '\\')
newAbsolutePath = Absolutepath.replace('\\', '/'); newAbsolutePath = Absolutepath.replace('\\', '/');
String uri = "file:///" + newAbsolutePath; saxparser.parse("file:///" + newAbsolutePath, handler);
saxparser.parse(uri, handler);
} catch (IOException | ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
// Need close the output file before we compare it with golden file.
try {
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,23 +26,19 @@ import java.io.BufferedWriter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -50,14 +46,16 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* ErrorHandler unit test. Set a ErrorHandle to XMLReader. Capture fatal error * ErrorHandler unit test. Set a ErrorHandle to XMLReader. Capture fatal error
* events in ErrorHandler. * events in ErrorHandler.
*/ */
public class EHFatalTest { public class EHFatalTest extends JAXPFileBaseTest {
/** /**
* Error Handler to capture all error events to output file. Verifies the * Error Handler to capture all error events to output file. Verifies the
* output file is same as golden file. * output file is same as golden file.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testEHFatal() { public void testEHFatal() throws Exception {
String outputFile = CLASS_DIR + "EHFatal.out"; String outputFile = USER_DIR + "EHFatal.out";
String goldFile = GOLDEN_DIR + "EHFatalGF.out"; String goldFile = GOLDEN_DIR + "EHFatalGF.out";
String xmlFile = XML_DIR + "invalid.xml"; String xmlFile = XML_DIR + "invalid.xml";
@ -68,25 +66,12 @@ public class EHFatalTest {
xmlReader.setErrorHandler(eHandler); xmlReader.setErrorHandler(eHandler);
InputSource is = new InputSource(instream); InputSource is = new InputSource(instream);
xmlReader.parse(is); xmlReader.parse(is);
} catch (IOException | ParserConfigurationException ex) { fail("Parse should throw SAXException");
failUnexpected(ex); } catch (SAXException expected) {
} catch (SAXException ex) { // This is expected.
System.out.println("This is expected:" + ex);
} }
// Need close the output file before we compare it with golden file. // Need close the output file before we compare it with golden file.
try {
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,6 +23,7 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.util.Enumeration; import java.util.Enumeration;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -31,7 +32,7 @@ import org.xml.sax.helpers.NamespaceSupport;
/** /**
* Unit test cases for NamespaceSupport API * Unit test cases for NamespaceSupport API
*/ */
public class NSSupportTest { public class NSSupportTest extends JAXPBaseTest {
/** /**
* Empty prefix name. * Empty prefix name.
*/ */

View file

@ -0,0 +1,161 @@
/*
* Copyright (c) 2003, 2015, 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 org.xml.sax.ptests;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.XMLReader;
/**
* Class containing the test cases for Namespace Table defined at
* http://www.megginson.com/SAX/Java/namespaces.html
*/
public class NSTableTest extends JAXPBaseTest {
private static final String NAMESPACES =
"http://xml.org/sax/features/namespaces";
private static final String NAMESPACE_PREFIXES =
"http://xml.org/sax/features/namespace-prefixes";
/**
* Here namespace processing and namespace-prefixes are enabled.
* The testcase tests XMLReader for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void xrNSTable01() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlReader.getFeature(NAMESPACES));
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is enabled. This will make namespace-prefixes
* disabled. The testcase tests XMLReader for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void xrNSTable02() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(NAMESPACES));
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is disabled. This will make namespace-prefixes
* enabled. The testcase tests XMLReader for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void xrNSTable03() throws Exception {
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
assertFalse(xmlReader.getFeature(NAMESPACES));
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is disabled, and namespace-prefixes is
* disabled. This will make namespace processing on.The testcase tests
* XMLReader for this. This behavior only apply to crimson, not
* XERCES.
*
* @throws Exception If any errors occur.
*/
@Test
public void xrNSTable04() throws Exception {
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing and namespace-prefixes are enabled.
* The testcase tests SAXParserFactory for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void spNSTable01() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setFeature(NAMESPACE_PREFIXES,true);
assertTrue(spf.getFeature(NAMESPACES));
assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is enabled. This will make namespace-prefixes
* disabled. The testcase tests SAXParserFactory for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void spNSTable02() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
assertTrue(spf.getFeature(NAMESPACES));
assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is disabled. This will make namespace-prefixes
* enabled. The testcase tests SAXParserFactory for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void spNSTable03() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
assertFalse(spf.getFeature(NAMESPACES));
assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
}
/**
* Here namespace processing is disabled, and namespace-prefixes is
* disabled. This will make namespace processing on.The testcase tests
* SAXParserFactory for this. This behavior only apply to crimson,
* not xerces.
*
* @throws Exception If any errors occur.
*/
@Test
public void spNSTable04() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
}
}

View file

@ -1,193 +0,0 @@
/*
* Copyright (c) 2014, 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 org.xml.sax.ptests;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
/**
* Class containing the test cases for Namespace Table defined at
* http://www.megginson.com/SAX/Java/namespaces.html
*/
public class NSTableTest01 {
private static final String NAMESPACES =
"http://xml.org/sax/features/namespaces";
private static final String NAMESPACE_PREFIXES =
"http://xml.org/sax/features/namespace-prefixes";
/**
* Here namespace processing and namespace-prefixes are enabled.
* The testcase tests XMLReader for this.
*/
@Test
public void xrNSTable01() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlReader.getFeature(NAMESPACES));
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is enabled. This will make namespace-prefixes
* disabled. The testcase tests XMLReader for this.
*/
@Test
public void xrNSTable02() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
assertTrue(xmlReader.getFeature(NAMESPACES));
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is disabled. This will make namespace-prefixes
* enabled. The testcase tests XMLReader for this.
*/
@Test
public void xrNSTable03() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
assertFalse(xmlReader.getFeature(NAMESPACES));
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is disabled, and namespace-prefixes is
* disabled. This will make namespace processing on.The testcase tests
* XMLReader for this. This behavior only apply to crimson, not
* xerces
*/
@Test
public void xrNSTable04() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing and namespace-prefixes are enabled.
* The testcase tests SAXParserFactory for this.
*/
@Test
public void spNSTable01() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setFeature(NAMESPACE_PREFIXES,true);
assertTrue(spf.getFeature(NAMESPACES));
assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXNotRecognizedException
| SAXNotSupportedException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is enabled. This will make namespace-prefixes
* disabled. The testcase tests SAXParserFactory for this.
*/
@Test
public void spNSTable02() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
assertTrue(spf.getFeature(NAMESPACES));
assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXNotRecognizedException
| SAXNotSupportedException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is disabled. This will make namespace-prefixes
* enabled. The testcase tests SAXParserFactory for this.
*/
@Test
public void spNSTable03() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
assertFalse(spf.getFeature(NAMESPACES));
assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXNotRecognizedException
| SAXNotSupportedException ex) {
failUnexpected(ex);
}
}
/**
* Here namespace processing is disabled, and namespace-prefixes is
* disabled. This will make namespace processing on.The testcase tests
* SAXParserFactory for this. This behavior only apply to crimson,
* not xerces.
*/
@Test
public void spNSTable04() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXNotRecognizedException
| SAXNotSupportedException ex) {
failUnexpected(ex);
}
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,10 +23,8 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
@ -35,7 +33,6 @@ import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.ParserAdapter;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
@ -47,7 +44,7 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* Unit test cases for ParserAdapter API. By default the only features recognized * Unit test cases for ParserAdapter API. By default the only features recognized
* are namespaces and namespace-prefixes. * are namespaces and namespace-prefixes.
*/ */
public class ParserAdapterTest { public class ParserAdapterTest extends JAXPFileReadOnlyBaseTest {
/** /**
* namespaces feature name. * namespaces feature name.
*/ */
@ -67,10 +64,9 @@ public class ParserAdapterTest {
/** /**
* Initiate ParserAdapter. * Initiate ParserAdapter.
* @throws ParserConfigurationException * @throws Exception If any errors occur.
* @throws SAXException
*/ */
ParserAdapterTest() throws ParserConfigurationException, SAXException { ParserAdapterTest() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader); XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader);
@ -151,129 +147,111 @@ public class ParserAdapterTest {
/** /**
* parserAdapter.getFeature(NAMESPACES) returns true be default. * parserAdapter.getFeature(NAMESPACES) returns true be default.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void getFeature01() { public void getFeature01() throws Exception {
try {
assertTrue(parserAdapter.getFeature(NAMESPACES)); assertTrue(parserAdapter.getFeature(NAMESPACES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default. * parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void getFeature02() { public void getFeature02() throws Exception {
try {
assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES)); assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* SAXNotRecognizedException thrown when feature name is not known one. * SAXNotRecognizedException thrown when feature name is not known one.
* @throws org.xml.sax.SAXNotRecognizedException expected Exception *
* @exception Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXNotRecognizedException.class) @Test(expectedExceptions = SAXNotRecognizedException.class)
public void getFeature03() throws SAXNotRecognizedException { public void getFeature03() throws Exception {
try {
parserAdapter.getFeature("no-meaning-feature"); parserAdapter.getFeature("no-meaning-feature");
} catch (SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain getFeature after it's set returns set value. * Obtain getFeature after it's set returns set value.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void setFeature01() { public void setFeature01() throws Exception {
try {
parserAdapter.setFeature(NAMESPACES, false); parserAdapter.setFeature(NAMESPACES, false);
assertFalse(parserAdapter.getFeature(NAMESPACES)); assertFalse(parserAdapter.getFeature(NAMESPACES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain getFeature after it's set returns set value. * Obtain getFeature after it's set returns set value.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void setFeature02() { public void setFeature02() throws Exception {
try {
parserAdapter.setFeature(NAMESPACE_PREFIXES, false); parserAdapter.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES)); assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain getFeature after it's set returns set value. * Obtain getFeature after it's set returns set value.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void setFeature03() { public void setFeature03() throws Exception {
try {
parserAdapter.setFeature(NAMESPACES, true); parserAdapter.setFeature(NAMESPACES, true);
assertTrue(parserAdapter.getFeature(NAMESPACES)); assertTrue(parserAdapter.getFeature(NAMESPACES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain getFeature after it's set returns set value. * Obtain getFeature after it's set returns set value.
*
* @exception Exception If any errors occur.
*/ */
@Test @Test
public void setFeature04() { public void setFeature04() throws Exception {
try {
parserAdapter.setFeature(NAMESPACE_PREFIXES, true); parserAdapter.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES)); assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES));
} catch (SAXNotRecognizedException | SAXNotSupportedException ex) {
failUnexpected(ex);
}
} }
/** /**
* NPE expected when parsing a null object by ParserAdapter. * NPE expected when parsing a null object by ParserAdapter.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void parse01() { public void parse01() throws Exception {
try {
parserAdapter.parse((InputSource)null); parserAdapter.parse((InputSource)null);
} catch (IOException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* SAXException expected when parsing a wrong-formatter XML with ParserAdapter. * SAXException expected when parsing a wrong-formatter XML with ParserAdapter.
* @throws org.xml.sax.SAXException *
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class)
public void parse02() throws SAXException { public void parse02() throws Exception {
try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
InputSource is = new InputSource(fis); InputSource is = new InputSource(fis);
parserAdapter.parse(is); parserAdapter.parse(is);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Parse a well-formatter XML with ParserAdapter. * Parse a well-formatter XML with ParserAdapter.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void parse03() { public void parse03() throws Exception {
try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) { try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) {
InputSource is = new InputSource(fis); InputSource is = new InputSource(fis);
parserAdapter.parse(is); parserAdapter.parse(is);
} catch (IOException | SAXException ex) {
failUnexpected(ex);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,21 +26,16 @@ import java.io.BufferedWriter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -48,12 +43,14 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* Entity resolver should be invoked in XML parse. This test verifies parsing * Entity resolver should be invoked in XML parse. This test verifies parsing
* process by checking the output with golden file. * process by checking the output with golden file.
*/ */
public class ResolverTest { public class ResolverTest extends JAXPFileBaseTest {
/** /**
* Unit test for entityResolver setter. * Unit test for entityResolver setter.
*
* @throws Exception If any errors occur.
*/ */
public void testResolver() { public void testResolver() throws Exception {
String outputFile = CLASS_DIR + "EntityResolver.out"; String outputFile = USER_DIR + "EntityResolver.out";
String goldFile = GOLDEN_DIR + "EntityResolverGF.out"; String goldFile = GOLDEN_DIR + "EntityResolverGF.out";
String xmlFile = XML_DIR + "publish.xml"; String xmlFile = XML_DIR + "publish.xml";
@ -64,23 +61,8 @@ public class ResolverTest {
xmlReader.setEntityResolver(eResolver); xmlReader.setEntityResolver(eResolver);
InputSource is = new InputSource(instream); InputSource is = new InputSource(instream);
xmlReader.parse(is); xmlReader.parse(is);
} catch(IOException | SAXException | ParserConfigurationException ex ) {
failUnexpected(ex);
} }
// Need close the output file before we compare it with golden file.
try {
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,20 +23,12 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -44,91 +36,64 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* This class contains the testcases to test SAXParser with regard to * This class contains the testcases to test SAXParser with regard to
* Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html * Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html
*/ */
public class SAXParserNSTableTest { public class SAXParserNSTableTest extends JAXPFileBaseTest {
/** /**
* namespace processing is enabled. namespace-prefix is also is enabled. * namespace processing is enabled. namespace-prefix is also is enabled.
* So it is a True-True combination. * So it is a True-True combination.
* The test is to test SAXParser with these conditions * The test is to test SAXParser with these conditions.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testWithTrueTrue() { public void testWithTrueTrue() throws Exception {
String outputFile = CLASS_DIR + "SPNSTableTT.out"; String outputFile = USER_DIR + "SPNSTableTT.out";
String goldFile = GOLDEN_DIR + "NSTableTTGF.out"; String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
spf.setFeature("http://xml.org/sax/features/namespace-prefixes", spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
true); true);
try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
SAXParser saxParser = spf.newSAXParser(); spf.newSAXParser().parse(new File(xmlFile), handler);
saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); }
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
/** /**
* namespace processing is enabled. Hence namespace-prefix is * namespace processing is enabled. Hence namespace-prefix is
* expected to be automaically off. So it is a True-False combination. * expected to be automatically off. So it is a True-False combination.
* The test is to test SAXParser with these conditions * The test is to test SAXParser with these conditions.
*
* @throws Exception If any errors occur.
*/ */
public void testWithTrueFalse() { public void testWithTrueFalse() throws Exception {
String outputFile = CLASS_DIR + "SPNSTableTF.out"; String outputFile = USER_DIR + "SPNSTableTF.out";
String goldFile = GOLDEN_DIR + "NSTableTFGF.out"; String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser(); try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); spf.newSAXParser().parse(new File(xmlFile), handler);
}
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
/** /**
* namespace processing is not enabled. Hence namespace-prefix is * namespace processing is not enabled. Hence namespace-prefix is
* expected to be automaically on. So it is a False-True combination. * expected to be automatically on. So it is a False-True combination.
* The test is to test SAXParser with these conditions * The test is to test SAXParser with these conditions.
*
* @throws Exception If any errors occur.
*/ */
public void testWithFalseTrue() { public void testWithFalseTrue() throws Exception {
String outputFile = CLASS_DIR + "SPNSTableFT.out"; String outputFile = USER_DIR + "SPNSTableFT.out";
String goldFile = GOLDEN_DIR + "NSTableFTGF.out"; String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser(); try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); spf.newSAXParser().parse(new File(xmlFile), handler);
}
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException | IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,14 +26,10 @@ import java.io.BufferedWriter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -42,7 +38,6 @@ import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -50,45 +45,34 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter
* to write to output file. Test verifies output is same as the golden file. * to write to output file. Test verifies output is same as the golden file.
*/ */
public class XMLFilterCBTest { public class XMLFilterCBTest extends JAXPFileBaseTest {
public void testXMLFilterCB() { /**
String outputFile = CLASS_DIR + "XMLFilter.out"; * Test XMLFilter working with XML reader.
*
* @throws Exception If any errors occur.
*/
public void testXMLFilterCB() throws Exception {
String outputFile = USER_DIR + "XMLFilter.out";
String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; String goldFile = GOLDEN_DIR + "XMLFilterGF.out";
String xmlFile = XML_DIR + "namespace1.xml"; String xmlFile = XML_DIR + "namespace1.xml";
try (FileInputStream fis = new FileInputStream(xmlFile)){ try (FileInputStream fis = new FileInputStream(xmlFile);
MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile);
myXmlFilter.setParent(xmlReader); myXmlFilter.setParent(xmlReader);
InputSource is = new InputSource(fis); myXmlFilter.parse(new InputSource(fis));
myXmlFilter.parse(is);
} catch( SAXException | IOException | ParserConfigurationException ex) {
failUnexpected(ex);
} }
// Need close the output file before we compare it with golden file. // Need close the output file before we compare it with golden file.
try {
assertTrue(compareWithGold(goldFile, outputFile)); assertTrue(compareWithGold(goldFile, outputFile));
} catch (IOException ex) {
failUnexpected(ex);
} finally {
try {
Path outputPath = Paths.get(outputFile);
if(Files.exists(outputPath))
Files.delete(outputPath);
} catch (IOException ex) {
failCleanup(ex, outputFile);
}
}
} }
} }
/** /**
* Writer XMLFiler which write all tags to output file when event happens. * Writer XMLFiler which write all tags to output file when event happens.
*/ */
class MyXMLFilter extends XMLFilterImpl{ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable {
/** /**
* FileWriter to write string to output file. * FileWriter to write string to output file.
*/ */
@ -278,4 +262,14 @@ class MyXMLFilter extends XMLFilterImpl{
throw new SAXException(ex); throw new SAXException(ex);
} }
} }
/**
* Close writer handler.
* @throws IOException if any I/O error when close writer handler.
*/
@Override
public void close() throws IOException {
if (bWriter != null)
bWriter.close();
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,18 +23,14 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -42,7 +38,7 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
/** /**
* Unit test for XMLFilter. * Unit test for XMLFilter.
*/ */
public class XMLFilterTest { public class XMLFilterTest extends JAXPFileReadOnlyBaseTest {
/** /**
* name spaces constant. * name spaces constant.
*/ */
@ -129,11 +125,11 @@ public class XMLFilterTest {
/** /**
* By default true is expected get namespaces feature. * By default true is expected get namespaces feature.
* @throws SAXException *
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void getFeature01() throws SAXException { public void getFeature01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
@ -141,127 +137,102 @@ public class XMLFilterTest {
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlFilter.setParent(xmlReader); xmlFilter.setParent(xmlReader);
assertTrue(xmlFilter.getFeature(NAMESPACES)); assertTrue(xmlFilter.getFeature(NAMESPACES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* By default false is expected get namespaces-prefix feature. * By default false is expected get namespaces-prefix feature.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void getFeature02() { public void getFeature02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlFilter.setParent(xmlReader); xmlFilter.setParent(spf.newSAXParser().getXMLReader());
assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* SAXNotRecognizedException is expected when get a feature by an invalid * SAXNotRecognizedException is expected when get a feature by an invalid
* feature name. * feature name.
* @throws org.xml.sax.SAXNotRecognizedException If the feature *
* value can't be assigned or retrieved from the parent. * @throws Exception If any errors occur.
* @throws org.xml.sax.SAXNotSupportedException When the
* parent recognizes the feature name but
* cannot determine its value at this time.
*/ */
@Test(expectedExceptions = SAXNotRecognizedException.class) @Test(expectedExceptions = SAXNotRecognizedException.class)
public void getFeature03() throws SAXNotRecognizedException, public void getFeature03() throws Exception {
SAXNotSupportedException {
new XMLFilterImpl().getFeature("no-meaning-feature"); new XMLFilterImpl().getFeature("no-meaning-feature");
} }
/** /**
* Set namespaces feature to a value to XMLFilter. it's expected same when * Set namespaces feature to a value to XMLFilter. it's expected same when
* obtain it again. * obtain it again.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void setFeature01() { public void setFeature01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlFilter.setParent(xmlReader); xmlFilter.setParent(spf.newSAXParser().getXMLReader());
xmlFilter.setFeature(NAMESPACES, false); xmlFilter.setFeature(NAMESPACES, false);
assertFalse(xmlFilter.getFeature(NAMESPACES)); assertFalse(xmlFilter.getFeature(NAMESPACES));
xmlFilter.setFeature(NAMESPACES, true); xmlFilter.setFeature(NAMESPACES, true);
assertTrue(xmlFilter.getFeature(NAMESPACES)); assertTrue(xmlFilter.getFeature(NAMESPACES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* Set namespaces-prefix feature to a value to XMLFilter. it's expected same * Set namespaces-prefix feature to a value to XMLFilter. it's expected same
* when obtain it again. * when obtain it again.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void setFeature02() { public void setFeature02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlFilter.setParent(xmlReader); xmlFilter.setParent(spf.newSAXParser().getXMLReader());
xmlFilter.setFeature(NAMESPACE_PREFIXES, false); xmlFilter.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
xmlFilter.setFeature(NAMESPACE_PREFIXES, true); xmlFilter.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES)); assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* NullPointerException is expected when parse a null InputSource. * NullPointerException is expected when parse a null InputSource.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void parse01() { public void parse01() throws Exception {
try {
new XMLFilterImpl().parse((InputSource)null); new XMLFilterImpl().parse((InputSource)null);
} catch (IOException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* SAXException is expected when parsing a invalid formatted XML file. * SAXException is expected when parsing a invalid formatted XML file.
* @throws org.xml.sax.SAXException when parse a incorrect formatted XML *
* file. * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void parse02() throws SAXException { public void parse02() throws Exception {
XMLFilterImpl xmlFilter = new XMLFilterImpl();
try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
InputSource is = new InputSource(fis); new XMLFilterImpl().parse(new InputSource(fis));
xmlFilter.parse(is);
} catch (IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* No exception when parse a normal XML file. * No exception when parse a normal XML file.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
public void parse03() { public void parse03() throws Exception {
XMLFilterImpl xmlFilter = new XMLFilterImpl();
try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) {
InputSource is = new InputSource(fis); new XMLFilterImpl().parse(new InputSource(fis));
xmlFilter.parse(is);
} catch (IOException | SAXException ex) {
failUnexpected(ex);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,10 +23,9 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.FilePermission;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -40,7 +39,7 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
/** /**
* Class containing the test cases for XMLReaderAdapter API * Class containing the test cases for XMLReaderAdapter API
*/ */
public class XMLReaderAdapterTest { public class XMLReaderAdapterTest extends JAXPBaseTest {
/** /**
* http://xml.org/sax/features/namespace-prefixes property name. * http://xml.org/sax/features/namespace-prefixes property name.
*/ */
@ -58,60 +57,51 @@ public class XMLReaderAdapterTest {
} }
/** /**
* To test the constructor that uses XMLReader * To test the constructor that uses XMLReader.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void constructor02() { public void constructor02() throws Exception {
try { XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertNotNull(new XMLReaderAdapter(xmlReader)); assertNotNull(new XMLReaderAdapter(xmlReader));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* To test the parse method. The specification says that this method * To test the parse method. The specification says that this method
* will throw an exception if the embedded XMLReader does not support * will throw an exception if the embedded XMLReader does not support
* the http://xml.org/sax/features/namespace-prefixes property. * the http://xml.org/sax/features/namespace-prefixes property.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void nsfeature01() { public void nsfeature01() throws Exception {
try { XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
} }
assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY)); assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* To test the parse method. The specification says that this method * To test the parse method. The specification says that this method
* will throw an exception if the embedded XMLReader does not support * will throw an exception if the embedded XMLReader does not support
* the http://xml.org/sax/features/namespace-prefixes property. * the http://xml.org/sax/features/namespace-prefixes property.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void parse01() { public void parse01() throws Exception {
setPermissions(new FilePermission(XML_DIR + "/-", "read"));
try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) { try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) {
SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
} }
XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader); XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader);
InputSource is = new InputSource(fis);
xmlRA.setDocumentHandler(new HandlerBase()); xmlRA.setDocumentHandler(new HandlerBase());
xmlRA.parse(is); xmlRA.parse(new InputSource(fis));
} catch (IOException | SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
} }
setPermissions();
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
*/ */
package org.xml.sax.ptests; package org.xml.sax.ptests;
import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -30,7 +31,7 @@ import org.xml.sax.helpers.XMLReaderFactory;
/** /**
* Unit test for XMLReaderFactory.createXMLReader API. * Unit test for XMLReaderFactory.createXMLReader API.
*/ */
public class XMLReaderFactoryTest { public class XMLReaderFactoryTest extends JAXPBaseTest {
/** /**
* No exception expected when create XMLReader by default. * No exception expected when create XMLReader by default.
* @throws org.xml.sax.SAXException when xml reader creation failed. * @throws org.xml.sax.SAXException when xml reader creation failed.
@ -48,12 +49,7 @@ public class XMLReaderFactoryTest {
*/ */
@Test @Test
public void createReader02() throws SAXException { public void createReader02() throws SAXException {
//Disable this test because this is only work for apache implementation. setSystemProperty("org.xml.sax.driver",
/*System.setProperty("org.xml.sax.driver",
"org.apache.xerces.parsers.SAXParser");
assertNotNull(XMLReaderFactory.
createXMLReader("org.apache.xerces.parsers.SAXParser"));*/
System.setProperty("org.xml.sax.driver",
"com.sun.org.apache.xerces.internal.parsers.SAXParser"); "com.sun.org.apache.xerces.internal.parsers.SAXParser");
assertNotNull(XMLReaderFactory. assertNotNull(XMLReaderFactory.
createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser")); createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser"));

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,17 +23,14 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
import static org.xml.sax.ptests.SAXTestConst.XML_DIR; import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
@ -41,7 +38,7 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
* Namespace Table defined at * Namespace Table defined at
* http://www.megginson.com/SAX/Java/namespaces.html * http://www.megginson.com/SAX/Java/namespaces.html
*/ */
public class XMLReaderNSTableTest { public class XMLReaderNSTableTest extends JAXPFileBaseTest {
/** /**
* XML file that used to be parsed. * XML file that used to be parsed.
*/ */
@ -55,71 +52,70 @@ public class XMLReaderNSTableTest {
/** /**
* namespace processing is enabled. namespace-prefix is also is enabled. * namespace processing is enabled. namespace-prefix is also is enabled.
* So it is a True-True combination. * So it is a True-True combination.
* The test is to test XMLReader with these conditions * The test is to test XMLReader with these conditions.
*
* @throws Exception If any errors occur.
*/ */
public void testWithTrueTrue() { public void testWithTrueTrue() throws Exception {
String outputFile = CLASS_DIR + "XRNSTableTT.out"; String outputFile = USER_DIR + "XRNSTableTT.out";
String goldFile = GOLDEN_DIR + "NSTableTTGF.out"; String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true); xmlReader.setFeature(NAMESPACE_PREFIXES, true);
xmlReader.setContentHandler(new MyNSContentHandler(outputFile)); try (FileInputStream fis = new FileInputStream(xmlFile);
xmlReader.parse(new InputSource(new FileInputStream(xmlFile))); MyNSContentHandler handler = new MyNSContentHandler(outputFile);) {
assertTrue(compareWithGold(goldFile, outputFile)); xmlReader.setContentHandler(handler);
} catch (ParserConfigurationException | SAXException | IOException ex) { xmlReader.parse(new InputSource(fis));
failUnexpected(ex);
} }
assertTrue(compareWithGold(goldFile, outputFile));
} }
/** /**
* Namespace processing is enabled. Hence namespace-prefix is * Namespace processing is enabled. Hence namespace-prefix is
* expected to be automaically off. So it is a True-False combination. * expected to be automatically off. So it is a True-False combination.
* The test is to test XMLReader with these conditions * The test is to test XMLReader with these conditions.
*
* @throws Exception If any errors occur.
*/ */
public void testWithTrueFalse() { public void testWithTrueFalse() throws Exception {
String outputFile = CLASS_DIR + "XRNSTableTF.out"; String outputFile = USER_DIR + "XRNSTableTF.out";
String goldFile = GOLDEN_DIR + "NSTableTFGF.out"; String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser(); SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader(); XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new MyNSContentHandler(outputFile)); try (FileInputStream fis = new FileInputStream(xmlFile);
xmlReader.parse(new InputSource(new FileInputStream(xmlFile))); MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
assertTrue(compareWithGold(goldFile, outputFile)); xmlReader.setContentHandler(handler);
} catch (ParserConfigurationException | SAXException | IOException ex) { xmlReader.parse(new InputSource(fis));
failUnexpected(ex);
} }
assertTrue(compareWithGold(goldFile, outputFile));
} }
/** /**
* namespace processing is not enabled. Hence namespace-prefix is * namespace processing is not enabled. Hence namespace-prefix is
* expected to be automaically on. So it is a False-True combination. * expected to be automaically on. So it is a False-True combination.
* The test is to test XMLReader with these conditions * The test is to test XMLReader with these conditions.
*
* @throws Exception If any errors occur.
*/ */
public void testWithFalseTrue() { public void testWithFalseTrue()throws Exception {
String outputFile = CLASS_DIR + "XRNSTableFT.out"; String outputFile = USER_DIR + "XRNSTableFT.out";
String goldFile = GOLDEN_DIR + "NSTableFTGF.out"; String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLReader xmlReader = saxParser.getXMLReader(); try (FileInputStream fis = new FileInputStream(xmlFile);
MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
xmlReader.setContentHandler(new MyNSContentHandler(outputFile)); xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new FileInputStream(xmlFile))); xmlReader.parse(new InputSource(fis));
assertTrue(compareWithGold(goldFile, outputFile));
} catch (ParserConfigurationException | SAXException | IOException ex) {
failUnexpected(ex);
} }
assertTrue(compareWithGold(goldFile, outputFile));
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,10 +23,9 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import static jaxp.library.JAXPTestUtilities.failUnexpected; import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
@ -45,42 +44,43 @@ import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
/** /**
* Class containing the test cases for SAXParser API * Class containing the test cases for SAXParser API
*/ */
public class XMLReaderTest { public class XMLReaderTest extends JAXPFileReadOnlyBaseTest {
/** /**
* XML namespaces. * XML namespaces.
*/ */
private static final String NAMESPACES = private static final String NAMESPACES
"http://xml.org/sax/features/namespaces"; = "http://xml.org/sax/features/namespaces";
/** /**
* XML namespaces prefixes. * XML namespaces prefixes.
*/ */
private static final String NAMESPACE_PREFIXES = private static final String NAMESPACE_PREFIXES
"http://xml.org/sax/features/namespace-prefixes"; = "http://xml.org/sax/features/namespace-prefixes";
/** /**
* A string intern name. * A string intern name.
*/ */
private static final String STRING_INTERNING = private static final String STRING_INTERNING
"http://xml.org/sax/features/string-interning"; = "http://xml.org/sax/features/string-interning";
/** /**
* Validation name. * Validation name.
*/ */
private static final String VALIDATION = private static final String VALIDATION
"http://xml.org/sax/features/validation"; = "http://xml.org/sax/features/validation";
/** /**
* A general external entities name * A general external entities name
*/ */
private static final String EXTERNAL_G_ENTITIES = private static final String EXTERNAL_G_ENTITIES
"http://xml.org/sax/features/external-general-entities"; = "http://xml.org/sax/features/external-general-entities";
/** /**
* A external parameter entities name * A external parameter entities name
*/ */
private static final String EXTERNAL_P_ENTITIES = private static final String EXTERNAL_P_ENTITIES
"http://xml.org/sax/features/external-parameter-entities"; = "http://xml.org/sax/features/external-parameter-entities";
/** /**
* XML DOM node name. * XML DOM node name.
@ -95,526 +95,444 @@ public class XMLReaderTest {
/** /**
* Declare handler name * Declare handler name
*/ */
private static final String DECL_HANDLER = private static final String DECL_HANDLER
"http://xml.org/sax/properties/declaration-handler"; = "http://xml.org/sax/properties/declaration-handler";
/** /**
* Lexical handler name * Lexical handler name
*/ */
private static final String LEXICAL_HANDLER = private static final String LEXICAL_HANDLER
"http://xml.org/sax/properties/lexical-handler"; = "http://xml.org/sax/properties/lexical-handler";
/** /**
* According to the SAX2 specs, All XMLReaders are required to recognize the * According to the SAX2 specs, All XMLReaders are required to recognize the
* http://xml.org/sax/features/namespaces feature names. * http://xml.org/sax/features/namespaces feature names. This test case is
* This test case is to test this. * to test this.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNS01() { public void featureNS01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertFalse(xmlReader.getFeature(NAMESPACES)); assertFalse(xmlReader.getFeature(NAMESPACES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* According to the SAX2 specs, All XMLReaders are required to recognize the * According to the SAX2 specs, All XMLReaders are required to recognize the
* http://xml.org/sax/features/namespaces feature names. * http://xml.org/sax/features/namespaces feature names. This test case is
* This test case is to test this. * to test this.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNS02() { public void featureNS02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(NAMESPACES)); assertTrue(xmlReader.getFeature(NAMESPACES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain http://xml.org/sax/features/namespaces feature name after it's * Obtain http://xml.org/sax/features/namespaces feature name after it's
* just set. Expect it's same as set value. * just set. Expect it's same as set value.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNS03() { public void featureNS03() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACES, true); xmlReader.setFeature(NAMESPACES, true);
assertTrue(xmlReader.getFeature(NAMESPACES)); assertTrue(xmlReader.getFeature(NAMESPACES));
xmlReader.setFeature(NAMESPACES, false); xmlReader.setFeature(NAMESPACES, false);
assertFalse(xmlReader.getFeature(NAMESPACES)); assertFalse(xmlReader.getFeature(NAMESPACES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* According to the SAX2 specs, All XMLReaders are required to recognize the * According to the SAX2 specs, All XMLReaders are required to recognize the
* http://xml.org/sax/features/namespace-prefixes feature names. * http://xml.org/sax/features/namespace-prefixes feature names. This test
* This test case is to test this. * case is to test this.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNSP01() { public void featureNSP01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* According to the SAX2 specs, All XMLReaders are required to recognize the * According to the SAX2 specs, All XMLReaders are required to recognize the
* http://xml.org/sax/features/namespace-prefixes feature names. * http://xml.org/sax/features/namespace-prefixes feature names. This test
* This test case is to test this. * case is to test this.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNSP02() { public void featureNSP02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* Obtain http://xml.org/sax/features/namespaces-prefixes feature name after * Obtain http://xml.org/sax/features/namespaces-prefixes feature name after
* it's just set. Expect it's same as set value. * it's just set. Expect it's same as set value.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureNSP03() { public void featureNSP03() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true); xmlReader.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
xmlReader.setFeature(NAMESPACE_PREFIXES, false); xmlReader.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
} catch (ParserConfigurationException | SAXException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns true if a feature has not been preset when namespace * getFeature returns true if a feature has not been preset when namespace
* awareness is set. * awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureSI01() { public void featureSI01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(STRING_INTERNING)); assertTrue(xmlReader.getFeature(STRING_INTERNING));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature with validation feature name returns the value that * getFeature with validation feature name returns the value that
* setValidation set. * setValidation set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureV01() { public void featureV01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION));
spf.setValidating(true); spf.setValidating(true);
assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns the value that a feature has been preset as when * getFeature returns the value that a feature has been preset as when
* namespace awareness is set. * namespace awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureV02() { public void featureV02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(VALIDATION, true); xmlReader.setFeature(VALIDATION, true);
assertTrue(xmlReader.getFeature(VALIDATION)); assertTrue(xmlReader.getFeature(VALIDATION));
xmlReader.setFeature(VALIDATION, false); xmlReader.setFeature(VALIDATION, false);
assertFalse(xmlReader.getFeature(VALIDATION)); assertFalse(xmlReader.getFeature(VALIDATION));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns true if a feature has not been preset when namespace * getFeature returns true if a feature has not been preset when namespace
* awareness is set. * awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureEGE01() { public void featureEGE01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns false if a feature has been preset as false when * getFeature returns false if a feature has been preset as false when
* namespace awareness is set. * namespace awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureEGE02() { public void featureEGE02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(EXTERNAL_G_ENTITIES, false); xmlReader.setFeature(EXTERNAL_G_ENTITIES, false);
assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns true if a feature has not been preset when namespace * getFeature returns true if a feature has not been preset when namespace
* awareness is set. * awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureEPE01() { public void featureEPE01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature returns false if a feature has been preset as false when * getFeature returns false if a feature has been preset as false when
* namespace awareness is set. * namespace awareness is set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void featureEPE02() { public void featureEPE02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(EXTERNAL_P_ENTITIES, false); xmlReader.setFeature(EXTERNAL_P_ENTITIES, false);
assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES));
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* getFeature with a unknown feature name throws SAXNotRecognizedException. * getFeature with a unknown feature name throws SAXNotRecognizedException.
* @throws SAXNotRecognizedException If the feature value can't be assigned *
* or retrieved. * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXNotRecognizedException.class) @Test(expectedExceptions = SAXNotRecognizedException.class)
public void featureNE01() throws SAXNotRecognizedException { public void featureNE01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature");
boolean noMeaningFeature = xmlReader.getFeature("no-meaning-feature");
} catch(SAXNotRecognizedException ex) {
throw ex;
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No exception expected when set entity resolver as simple entity resolver. * No exception expected when set entity resolver as simple entity resolver.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void entity01() { public void entity01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlReader.setEntityResolver(xmlFilter); xmlReader.setEntityResolver(xmlFilter);
assertNotNull(xmlReader.getEntityResolver()); assertEquals(xmlReader.getEntityResolver(), xmlFilter);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No NPE expected when set entity resolver as null. * No NPE expected when set entity resolver as null.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void entity02() { public void entity02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().setEntityResolver(null);
xmlReader.setEntityResolver(null);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No exception expected when set DTD handler as simple DTD handler. * No exception expected when set DTD handler as simple DTD handler.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void dtdhandler01() { public void dtdhandler01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlReader.setDTDHandler(xmlFilter); xmlReader.setDTDHandler(xmlFilter);
assertNotNull(xmlReader.getDTDHandler()); assertEquals(xmlReader.getDTDHandler(), xmlFilter);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No NPE expected when set DTD handler as null. * No NPE expected when set DTD handler as null.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void dtdhandler02() { public void dtdhandler02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().setDTDHandler(null);
xmlReader.setDTDHandler(null);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No exception expected when set content handler as simple content handler. * No exception expected when set content handler as simple content handler.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void contenthandler01() { public void contenthandler01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLFilterImpl xmlFilter = new XMLFilterImpl(); XMLFilterImpl xmlFilter = new XMLFilterImpl();
xmlReader.setContentHandler(xmlFilter); xmlReader.setContentHandler(xmlFilter);
assertNotNull(xmlReader.getContentHandler()); assertEquals(xmlReader.getContentHandler(), xmlFilter);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No NPE expected when set content handler as null. * No NPE expected when set content handler as null.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void contenthandler02() { public void contenthandler02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().setContentHandler(null);
xmlReader.setContentHandler(null);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No exception expected when set content handler as simple error handler. * No exception expected when set content handler as simple error handler.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void errorhandler01() { public void errorhandler01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setErrorHandler(new XMLFilterImpl()); xmlReader.setErrorHandler(new XMLFilterImpl());
assertNotNull(xmlReader.getErrorHandler()); assertNotNull(xmlReader.getErrorHandler());
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* No NPE expected when set error handler as null. * No NPE expected when set error handler as null.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void errorhandler02() { public void errorhandler02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setErrorHandler(null); xmlReader.setErrorHandler(null);
} catch (SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
}
} }
/** /**
* Parse a null input source throw NPE. * Parse a null input source throw NPE.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void parse01() { public void parse01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().parse((InputSource) null);
xmlReader.parse((InputSource)null);
} catch (SAXException | ParserConfigurationException | IOException ex) {
failUnexpected(ex);
}
} }
/** /**
* Unit test for parse a error-formatted file. SAXException is expected. * Unit test for parse a error-formatted file. SAXException is expected.
* @throws org.xml.sax.SAXException parsing failed. *
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXException.class) @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class)
public void parse02() throws SAXException { public void parse02() throws Exception {
try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")){ try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().parse(new InputSource(fis));
InputSource is = new InputSource(fis);
xmlReader.parse(is);
} catch (ParserConfigurationException | IOException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Unit test for parse a well-formatted file. No exception is expected. * Unit test for parse a well-formatted file. No exception is expected.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void parse03(){ public void parse03() throws Exception {
try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); spf.newSAXParser().getXMLReader().parse(new InputSource(fis));
InputSource is = new InputSource(fis);
xmlReader.parse(is);
} catch (IOException | SAXException | ParserConfigurationException ex) {
failUnexpected(ex);
} }
} }
/** /**
* Modified by IBM * Modified by IBM Xerces does not support this feature and it is not
* Xerces does not support this feature and it is not mandatory * mandatory.
* @throws org.xml.sax.SAXNotSupportedException *
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXNotSupportedException.class) @Test(expectedExceptions = SAXNotSupportedException.class)
public void xrProperty01() throws SAXNotSupportedException { public void xrProperty01() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.getProperty(XML_STRING); xmlReader.getProperty(XML_STRING);
} catch(SAXNotSupportedException ex) {
throw ex;
} catch (SAXException | ParserConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* SAXNotSupportedException thrown if property name is known but no value * SAXNotSupportedException thrown if property name is known but no value
* assigned to this property. * assigned to this property.
* @throws org.xml.sax.SAXNotSupportedException when XMLReader recognizes *
* the property name but cannot determine its value at this time. * @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXNotSupportedException.class) @Test(expectedExceptions = SAXNotSupportedException.class)
public void xrProperty02() throws SAXNotSupportedException { public void xrProperty02() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertNull(xmlReader.getProperty(DOM_NODE)); assertNull(xmlReader.getProperty(DOM_NODE));
} catch (SAXNotSupportedException ex) {
throw ex;
} catch (SAXException | ParserConfigurationException ex){
failUnexpected(ex);
} }
}
/** /**
* XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set. * XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void xrProperty03() { public void xrProperty03() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertNull(xmlReader.getProperty(LEXICAL_HANDLER)); assertNull(xmlReader.getProperty(LEXICAL_HANDLER));
} catch (SAXException | ParserConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* XMLReader.getProperty returns null if DECL_HANDLER wasn't set. * XMLReader.getProperty returns null if DECL_HANDLER wasn't set.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void xrProperty04() { public void xrProperty04() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
assertNull(xmlReader.getProperty(DECL_HANDLER)); assertNull(xmlReader.getProperty(DECL_HANDLER));
} catch (SAXException | ParserConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test. * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void xrProperty05() { public void xrProperty05() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler); xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler);
assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER)); assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER));
} catch (SAXException | ParserConfigurationException ex){
failUnexpected(ex);
}
} }
/** /**
* XMLReader.setProperty/getProperty for DECL_HANDLER unit test. * XMLReader.setProperty/getProperty for DECL_HANDLER unit test.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void xrProperty06() { public void xrProperty06() throws Exception {
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLReader xmlReader = spf.newSAXParser().getXMLReader();
MyDeclHandler myDeclHandler = new MyDeclHandler(); MyDeclHandler myDeclHandler = new MyDeclHandler();
xmlReader.setProperty(DECL_HANDLER, myDeclHandler); xmlReader.setProperty(DECL_HANDLER, myDeclHandler);
assertNotNull(xmlReader.getProperty(DECL_HANDLER)); assertNotNull(xmlReader.getProperty(DECL_HANDLER));
} catch (ParserConfigurationException | SAXException ex){
failUnexpected(ex);
}
} }
} }
@ -622,6 +540,7 @@ public class XMLReaderTest {
* Simple LexicalHandler that skips every lexical event. * Simple LexicalHandler that skips every lexical event.
*/ */
class MyLexicalHandler implements LexicalHandler { class MyLexicalHandler implements LexicalHandler {
/** /**
* Report an XML comment anywhere in the document. * Report an XML comment anywhere in the document.
* *
@ -667,8 +586,10 @@ class MyLexicalHandler implements LexicalHandler {
* Report the start of DTD declarations, if any. * Report the start of DTD declarations, if any.
* *
* @param name The document type name. * @param name The document type name.
* @param publicId The declared public identifier for the external DTD subset. * @param publicId The declared public identifier for the external DTD
* @param systemId The declared system identifier for the external DTD subset. * subset.
* @param systemId The declared system identifier for the external DTD
* subset.
*/ */
@Override @Override
public void startDTD(String name, String publicId, String systemId) { public void startDTD(String name, String publicId, String systemId) {
@ -688,16 +609,17 @@ class MyLexicalHandler implements LexicalHandler {
* Simple DeclHandler that skips every DTD declaration event. * Simple DeclHandler that skips every DTD declaration event.
*/ */
class MyDeclHandler implements DeclHandler { class MyDeclHandler implements DeclHandler {
/** /**
* Report an attribute type declaration. * Report an attribute type declaration.
*
* @param eName The name of the associated element. * @param eName The name of the associated element.
* @param aName The name of the attribute. * @param aName The name of the attribute.
* @param type A string representing the attribute type. * @param type A string representing the attribute type.
* @param mode A string representing the attribute defaulting mode * @param mode A string representing the attribute defaulting mode
* ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if none of these applies.
* none of these applies. * @param value A string representing the attribute's default value, or null
* @param value A string representing the attribute's default value, * if there is none.
* or null if there is none.
*/ */
@Override @Override
public void attributeDecl(String eName, String aName, String type, public void attributeDecl(String eName, String aName, String type,
@ -706,6 +628,7 @@ class MyDeclHandler implements DeclHandler {
/** /**
* Report an element type declaration. * Report an element type declaration.
*
* @param name The element type name. * @param name The element type name.
* @param model The content model as a normalized string. * @param model The content model as a normalized string.
*/ */
@ -715,10 +638,11 @@ class MyDeclHandler implements DeclHandler {
/** /**
* Report a parsed external entity declaration. * Report a parsed external entity declaration.
* @param name The name of the entity. If it is a parameter *
* entity, the name will begin with '%'. * @param name The name of the entity. If it is a parameter entity, the name
* @param publicId The entity's public identifier, or null if none * will begin with '%'.
* was given. * @param publicId The entity's public identifier, or null if none was
* given.
* @param systemId The entity's system identifier. * @param systemId The entity's system identifier.
*/ */
@Override @Override
@ -728,8 +652,9 @@ class MyDeclHandler implements DeclHandler {
/** /**
* Report an internal entity declaration. * Report an internal entity declaration.
* @param name The name of the entity. If it is a parameter *
* entity, the name will begin with '%'. * @param name The name of the entity. If it is a parameter entity, the name
* will begin with '%'.
* @param value The replacement text of the entity. * @param value The replacement text of the entity.
*/ */
@Override @Override

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,23 +27,18 @@ import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration; import javax.xml.datatype.Duration;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
@ -51,8 +46,8 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema; import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; import javax.xml.validation.Validator;
import jaxp.library.JAXPFileReadOnlyBaseTest;
import static jaxp.library.JAXPTestUtilities.bomStream; import static jaxp.library.JAXPTestUtilities.bomStream;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
import org.w3c.dom.DOMConfiguration; import org.w3c.dom.DOMConfiguration;
@ -63,23 +58,23 @@ import org.w3c.dom.TypeInfo;
import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS; import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
import static test.auctionportal.HiBidConstants.XML_DIR; import static test.auctionportal.HiBidConstants.XML_DIR;
/** /**
* This is the user controller class for the Auction portal HiBid.com. * This is the user controller class for the Auction portal HiBid.com.
*/ */
public class AuctionController { public class AuctionController extends JAXPFileReadOnlyBaseTest {
/** /**
* Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927 * Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927
* DOMConfiguration.setParameter("well-formed",true) throws an exception. * DOMConfiguration.setParameter("well-formed",true) throws an exception.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCreateNewItem2Sell() { public void testCreateNewItem2Sell() throws Exception {
String xmlFile = XML_DIR + "novelsInvalid.xml"; String xmlFile = XML_DIR + "novelsInvalid.xml";
try {
Document document = DocumentBuilderFactory.newInstance() Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(xmlFile); .newDocumentBuilder().parse(xmlFile);
@ -91,22 +86,18 @@ public class AuctionController {
domOutput.setByteStream(System.out); domOutput.setByteStream(System.out);
LSSerializer writer = impl.createLSSerializer(); LSSerializer writer = impl.createLSSerializer();
writer.write(document, domOutput); writer.write(document, domOutput);
} catch (ParserConfigurationException | SAXException | IOException
| ClassNotFoundException | InstantiationException
| IllegalAccessException | ClassCastException e) {
failUnexpected(e);
}
} }
/** /**
* Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132 * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132
* test throws DOM Level 1 node error. * test throws DOM Level 1 node error.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCreateNewItem2SellRetry() { public void testCreateNewItem2SellRetry() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
Document document = dbf.newDocumentBuilder().parse(xmlFile); Document document = dbf.newDocumentBuilder().parse(xmlFile);
@ -127,22 +118,18 @@ public class AuctionController {
document.normalizeDocument(); document.normalizeDocument();
writer.write(document, domoutput); writer.write(document, domoutput);
assertFalse(errHandler.isError()); assertFalse(errHandler.isError());
} catch (ParserConfigurationException | SAXException | IOException
| ClassNotFoundException | InstantiationException
| IllegalAccessException | ClassCastException e) {
failUnexpected(e);
}
} }
/** /**
* Check if setting the attribute to be of type ID works. This will affect * Check if setting the attribute to be of type ID works. This will affect
* the Attr.isID method according to the spec. * the Attr.isID method according to the spec.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCreateID() { public void testCreateID() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
@ -153,19 +140,17 @@ public class AuctionController {
account.setIdAttributeNS(PORTAL_ACCOUNT_NS, "accountID", true); account.setIdAttributeNS(PORTAL_ACCOUNT_NS, "accountID", true);
Attr aID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID"); Attr aID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
assertTrue(aID.isId()); assertTrue(aID.isId());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Check the user data on the node. * Check the user data on the node.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckingUserData() { public void testCheckingUserData() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
@ -190,46 +175,43 @@ public class AuctionController {
document.normalizeDocument(); document.normalizeDocument();
String data = (someName.getUserData("mykey")).toString(); String data = (someName.getUserData("mykey")).toString();
assertEquals(data, "dd"); assertEquals(data, "dd");
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Check the UTF-16 XMLEncoding xml file. * Check the UTF-16 XMLEncoding xml file.
*
* @throws Exception If any errors occur.
* @see <a href="content/movies.xml">movies.xml</a> * @see <a href="content/movies.xml">movies.xml</a>
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testCheckingEncoding() { public void testCheckingEncoding() throws Exception {
// Note since movies.xml is UTF-16 encoding. We're not using stanard XML // Note since movies.xml is UTF-16 encoding. We're not using stanard XML
// file suffix. // file suffix.
String xmlFile = XML_DIR + "movies.xml.data"; String xmlFile = XML_DIR + "movies.xml.data";
//try (FileInputStream is = new FileInputStream(xmlFile)) { try (InputStream source = bomStream("UTF-16", xmlFile)) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
InputStream source = bomStream("UTF-16", xmlFile);
Document document = dbf.newDocumentBuilder().parse(source); Document document = dbf.newDocumentBuilder().parse(source);
assertEquals(document.getXmlEncoding(), "UTF-16"); assertEquals(document.getXmlEncoding(), "UTF-16");
assertEquals(document.getXmlStandalone(), true); assertEquals(document.getXmlStandalone(), true);
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Check validation API features. A schema which is including in Bug 4909119 * Check validation API features. A schema which is including in Bug 4909119
* used to be testing for the functionalities. * used to be testing for the functionalities.
*
* @throws Exception If any errors occur.
* @see <a href="content/userDetails.xsd">userDetails.xsd</a> * @see <a href="content/userDetails.xsd">userDetails.xsd</a>
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testGetOwnerInfo() { public void testGetOwnerInfo() throws Exception {
String schemaFile = XML_DIR + "userDetails.xsd"; String schemaFile = XML_DIR + "userDetails.xsd";
String xmlFile = XML_DIR + "userDetails.xml"; String xmlFile = XML_DIR + "userDetails.xml";
try { try(FileInputStream fis = new FileInputStream(xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
@ -244,27 +226,27 @@ public class AuctionController {
DocumentBuilder docBuilder = dbf.newDocumentBuilder(); DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.setErrorHandler(eh); docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(new FileInputStream(xmlFile)); Document document = docBuilder.parse(fis);
DOMResult dResult = new DOMResult(); DOMResult dResult = new DOMResult();
DOMSource domSource = new DOMSource(document); DOMSource domSource = new DOMSource(document);
validator.validate(domSource, dResult); validator.validate(domSource, dResult);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (SAXException | ParserConfigurationException | IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Check grammar caching with imported schemas. * Check grammar caching with imported schemas.
*
* @throws Exception If any errors occur.
* @see <a href="content/coins.xsd">coins.xsd</a> * @see <a href="content/coins.xsd">coins.xsd</a>
* @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a> * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testGetOwnerItemList() { public void testGetOwnerItemList() throws Exception {
String xsdFile = XML_DIR + "coins.xsd"; String xsdFile = XML_DIR + "coins.xsd";
String xmlFile = XML_DIR + "coins.xml"; String xmlFile = XML_DIR + "coins.xml";
try { try(FileInputStream fis = new FileInputStream(xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
@ -278,11 +260,9 @@ public class AuctionController {
validator.setErrorHandler(eh); validator.setErrorHandler(eh);
DocumentBuilder docBuilder = dbf.newDocumentBuilder(); DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new FileInputStream(xmlFile)); Document document = docBuilder.parse(fis);
validator.validate(new DOMSource(document), new DOMResult()); validator.validate(new DOMSource(document), new DOMResult());
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (SAXException | ParserConfigurationException | IOException e) {
failUnexpected(e);
} }
} }
@ -291,16 +271,16 @@ public class AuctionController {
* Check for the same imported schemas but will use SAXParserFactory and try * Check for the same imported schemas but will use SAXParserFactory and try
* parsing using the SAXParser. SCHEMA_SOURCE attribute is using for this * parsing using the SAXParser. SCHEMA_SOURCE attribute is using for this
* test. * test.
*
* @throws Exception If any errors occur.
* @see <a href="content/coins.xsd">coins.xsd</a> * @see <a href="content/coins.xsd">coins.xsd</a>
* @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a> * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testGetOwnerItemList1() { public void testGetOwnerItemList1() throws Exception {
String xsdFile = XML_DIR + "coins.xsd"; String xsdFile = XML_DIR + "coins.xsd";
String xmlFile = XML_DIR + "coins.xml"; String xmlFile = XML_DIR + "coins.xml";
try {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
spf.setValidating(true); spf.setValidating(true);
@ -312,19 +292,17 @@ public class AuctionController {
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
sp.parse(new File(xmlFile), eh); sp.parse(new File(xmlFile), eh);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Check usage of javax.xml.datatype.Duration class. * Check usage of javax.xml.datatype.Duration class.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testGetItemDuration() { public void testGetItemDuration() throws Exception {
String xmlFile = XML_DIR + "itemsDuration.xml"; String xmlFile = XML_DIR + "itemsDuration.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
Document document = dbf.newDocumentBuilder().parse(xmlFile); Document document = dbf.newDocumentBuilder().parse(xmlFile);
@ -347,20 +325,17 @@ public class AuctionController {
Duration myDuration = sellDuration.add(duration); Duration myDuration = sellDuration.add(duration);
assertEquals(myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22)), assertEquals(myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22)),
DatatypeFactory.newInstance().newDuration("P730D")); DatatypeFactory.newInstance().newDuration("P730D"));
} catch (ParserConfigurationException | DatatypeConfigurationException
| SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Check usage of TypeInfo interface introduced in DOM L3. * Check usage of TypeInfo interface introduced in DOM L3.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testGetTypeInfo() { public void testGetTypeInfo() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(true); dbf.setValidating(true);
@ -379,8 +354,5 @@ public class AuctionController {
TypeInfo roletypeInfo = role.getSchemaTypeInfo(); TypeInfo roletypeInfo = role.getSchemaTypeInfo();
assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell")); assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell"));
assertTrue(roletypeInfo.getTypeNamespace().equals(PORTAL_ACCOUNT_NS)); assertTrue(roletypeInfo.getTypeNamespace().equals(PORTAL_ACCOUNT_NS));
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -30,39 +30,31 @@ import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.FilePermission;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING; import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold; import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import static test.auctionportal.HiBidConstants.CLASS_DIR;
import static test.auctionportal.HiBidConstants.GOLDEN_DIR; import static test.auctionportal.HiBidConstants.GOLDEN_DIR;
import static test.auctionportal.HiBidConstants.XML_DIR; import static test.auctionportal.HiBidConstants.XML_DIR;
/** /**
* This is a test class for the Auction portal HiBid.com. * This is a test class for the Auction portal HiBid.com.
*/ */
public class AuctionItemRepository { public class AuctionItemRepository extends JAXPFileBaseTest {
/** /**
* XML file for parsing. * XML file for parsing.
*/ */
@ -78,94 +70,92 @@ public class AuctionItemRepository {
* document that has more than two levels of entity expansion is parsed or * document that has more than two levels of entity expansion is parsed or
* not. Previous system property was changed to jdk.xml.entityExpansionLimit * not. Previous system property was changed to jdk.xml.entityExpansionLimit
* see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html. * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testEntityExpansionSAXPos() { public void testEntityExpansionSAXPos() throws Exception {
try {
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
// Secure processing will limit XML processing to conform to // Secure processing will limit XML processing to conform to
// implementation limits. // implementation limits.
factory.setFeature(FEATURE_SECURE_PROCESSING, true); factory.setFeature(FEATURE_SECURE_PROCESSING, true);
// Set entityExpansionLimit as 2 should expect fatalError // Set entityExpansionLimit as 2 should expect fatalError
System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000)); setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000));
SAXParser parser = factory.newSAXParser(); SAXParser parser = factory.newSAXParser();
MyErrorHandler fatalHandler = new MyErrorHandler(); MyErrorHandler fatalHandler = new MyErrorHandler();
setPermissions(new FilePermission(ENTITY_XML, "read"));
parser.parse(new File(ENTITY_XML), fatalHandler); parser.parse(new File(ENTITY_XML), fatalHandler);
assertFalse(fatalHandler.isAnyError()); assertFalse(fatalHandler.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Setting the EntityExpansion Limit to 2 and checks if the XML * Setting the EntityExpansion Limit to 2 and checks if the XML
* document that has more than two levels of entity expansion is parsed or * document that has more than two levels of entity expansion is parsed or
* not. Previous system property was changed to jdk.xml.entityExpansionLimit * not. Previous system property was changed to jdk.xml.entityExpansionLimit
* see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html. * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXParseException.class) @Test(expectedExceptions = SAXParseException.class)
public void testEntityExpansionSAXNeg() throws SAXParseException { public void testEntityExpansionSAXNeg() throws Exception {
//
try {
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
// Secure processing will limit XML processing to conform to // Secure processing will limit XML processing to conform to
// implementation limits. // implementation limits.
factory.setFeature(FEATURE_SECURE_PROCESSING, true); factory.setFeature(FEATURE_SECURE_PROCESSING, true);
// Set entityExpansionLimit as 2 should expect SAXParseException // Set entityExpansionLimit as 2 should expect SAXParseException.
System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
SAXParser parser = factory.newSAXParser();
SAXParser parser = factory.newSAXParser();
MyErrorHandler fatalHandler = new MyErrorHandler(); MyErrorHandler fatalHandler = new MyErrorHandler();
setPermissions(new FilePermission(ENTITY_XML, "read"));
parser.parse(new File(ENTITY_XML), fatalHandler); parser.parse(new File(ENTITY_XML), fatalHandler);
} catch (SAXParseException e) {
throw e;
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
* Testing set MaxOccursLimit to 10000 in the secure processing enabled for * Testing set MaxOccursLimit to 10000 in the secure processing enabled for
* SAXParserFactory. * SAXParserFactory.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testMaxOccurLimitPos() { public void testMaxOccurLimitPos() throws Exception {
String schema_file = XML_DIR + "toys.xsd"; String schema_file = XML_DIR + "toys.xsd";
String xml_file = XML_DIR + "toys.xml"; String xml_file = XML_DIR + "toys.xml";
try (InputStream is = new FileInputStream(xml_file)) {
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true); factory.setValidating(true);
factory.setFeature(FEATURE_SECURE_PROCESSING, true); factory.setFeature(FEATURE_SECURE_PROCESSING, true);
System.setProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000)); setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000));
SAXParser parser = factory.newSAXParser(); SAXParser parser = factory.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
setPermissions(new FilePermission(XML_DIR + "-", "read"));
parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file)); parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file));
try (InputStream is = new FileInputStream(xml_file)) {
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
parser.parse(is, eh); parser.parse(is, eh);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
} }
} }
/** /**
* Use a DocumentBuilder to create a DOM object and see if Secure Processing * Use a DocumentBuilder to create a DOM object and see if Secure Processing
* feature affects the entity expansion. * feature affects the entity expansion.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testEntityExpansionDOMPos() { public void testEntityExpansionDOMPos() throws Exception {
try {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setFeature(FEATURE_SECURE_PROCESSING, true); dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000)); setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000));
DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
dBuilder.setErrorHandler(eh); dBuilder.setErrorHandler(eh);
try {
setPermissions(new FilePermission(ENTITY_XML, "read"));
dBuilder.parse(ENTITY_XML); dBuilder.parse(ENTITY_XML);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | IOException | SAXException e) { } finally {
failUnexpected(e); setPermissions();
} }
} }
@ -173,34 +163,32 @@ public class AuctionItemRepository {
* Use a DocumentBuilder to create a DOM object and see how does the Secure * Use a DocumentBuilder to create a DOM object and see how does the Secure
* Processing feature and entityExpansionLimit value affects output. * Processing feature and entityExpansionLimit value affects output.
* Negative test that when entityExpansionLimit is too small. * Negative test that when entityExpansionLimit is too small.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = SAXParseException.class) @Test(expectedExceptions = SAXParseException.class)
public void testEntityExpansionDOMNeg() throws SAXParseException { public void testEntityExpansionDOMNeg() throws Exception {
try {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setFeature(FEATURE_SECURE_PROCESSING, true); dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
dBuilder.setErrorHandler(eh); dBuilder.setErrorHandler(eh);
setPermissions(new FilePermission(ENTITY_XML, "read"));
dBuilder.parse(ENTITY_XML); dBuilder.parse(ENTITY_XML);
} catch (SAXParseException e) {
throw e;
} catch (ParserConfigurationException | IOException | SAXException e) {
failUnexpected(e);
}
} }
/** /**
* Test xi:include with a SAXParserFactory. * Test xi:include with a SAXParserFactory.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeSAXPos() { public void testXIncludeSAXPos() throws Exception {
String resultFile = CLASS_DIR + "doc_xinclude.out"; String resultFile = USER_DIR + "doc_xinclude.out";
String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml"; String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
String xmlFile = XML_DIR + "doc_xinclude.xml"; String xmlFile = XML_DIR + "doc_xinclude.xml";
try {
try(FileOutputStream fos = new FileOutputStream(resultFile)) { try(FileOutputStream fos = new FileOutputStream(resultFile)) {
XInclHandler xh = new XInclHandler(fos, null); XInclHandler xh = new XInclHandler(fos, null);
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
@ -210,67 +198,42 @@ public class AuctionItemRepository {
spf.newSAXParser().parse(new File(xmlFile), xh); spf.newSAXParser().parse(new File(xmlFile), xh);
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test the simple case of including a document using xi:include using a * Test the simple case of including a document using xi:include using a
* DocumentBuilder. * DocumentBuilder.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeDOMPos() { public void testXIncludeDOMPos() throws Exception {
String resultFile = CLASS_DIR + "doc_xincludeDOM.out"; String resultFile = USER_DIR + "doc_xincludeDOM.out";
String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml"; String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
String xmlFile = XML_DIR + "doc_xinclude.xml"; String xmlFile = XML_DIR + "doc_xinclude.xml";
try {
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile)); Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
doc.setXmlStandalone(true); doc.setXmlStandalone(true);
TransformerFactory.newInstance().newTransformer(). TransformerFactory.newInstance().newTransformer().
transform(new DOMSource(doc), new StreamResult(fos)); transform(new DOMSource(doc), new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test the simple case of including a document using xi:include within a * Test the simple case of including a document using xi:include within a
* xi:fallback using a DocumentBuilder. * xi:fallback using a DocumentBuilder.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeFallbackDOMPos() { public void testXIncludeFallbackDOMPos() throws Exception {
String resultFile = CLASS_DIR + "doc_fallbackDOM.out"; String resultFile = USER_DIR + "doc_fallbackDOM.out";
String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml"; String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml";
String xmlFile = XML_DIR + "doc_fallback.xml"; String xmlFile = XML_DIR + "doc_fallback.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
@ -282,32 +245,19 @@ public class AuctionItemRepository {
.transform(new DOMSource(doc), new StreamResult(fos)); .transform(new DOMSource(doc), new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test for xi:fallback where the fall back text is parsed as text. This * Test for xi:fallback where the fall back text is parsed as text. This
* test uses a nested xi:include for the fallback test. * test uses a nested xi:include for the fallback test.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeFallbackTextPos() { public void testXIncludeFallbackTextPos() throws Exception {
String resultFile = CLASS_DIR + "doc_fallback_text.out"; String resultFile = USER_DIR + "doc_fallback_text.out";
String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml"; String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml";
String xmlFile = XML_DIR + "doc_fallback_text.xml"; String xmlFile = XML_DIR + "doc_fallback_text.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
@ -319,31 +269,18 @@ public class AuctionItemRepository {
.transform(new DOMSource(doc), new StreamResult(fos)); .transform(new DOMSource(doc), new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test the XPointer element() framework with XInclude. * Test the XPointer element() framework with XInclude.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXpointerElementPos() { public void testXpointerElementPos() throws Exception {
String resultFile = CLASS_DIR + "doc_xpointer_element.out"; String resultFile = USER_DIR + "doc_xpointer_element.out";
String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml"; String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
String xmlFile = XML_DIR + "doc_xpointer_element.xml"; String xmlFile = XML_DIR + "doc_xpointer_element.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
@ -356,31 +293,19 @@ public class AuctionItemRepository {
new StreamResult(fos)); new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test the XPointer framework with a SAX object. * Test the XPointer framework with a SAX object.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXPointerPos() { public void testXPointerPos() throws Exception {
String resultFile = CLASS_DIR + "doc_xpointer.out"; String resultFile = USER_DIR + "doc_xpointer.out";
String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml"; String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
String xmlFile = XML_DIR + "doc_xpointer.xml"; String xmlFile = XML_DIR + "doc_xpointer.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); spf.setNamespaceAware(true);
@ -390,31 +315,20 @@ public class AuctionItemRepository {
spf.newSAXParser().parse(new File(xmlFile), new XInclHandler(fos, null)); spf.newSAXParser().parse(new File(xmlFile), new XInclHandler(fos, null));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test if xi:include may reference the doc containing the include if the * Test if xi:include may reference the doc containing the include if the
* parse type is text. * parse type is text.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeLoopPos() { public void testXIncludeLoopPos() throws Exception {
String resultFile = CLASS_DIR + "doc_xinc_loops.out"; String resultFile = USER_DIR + "doc_xinc_loops.out";
String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml"; String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml";
String xmlFile = XML_DIR + "doc_xinc_loops.xml"; String xmlFile = XML_DIR + "doc_xinc_loops.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
@ -428,32 +342,20 @@ public class AuctionItemRepository {
.transform(new DOMSource(doc), new StreamResult(fos)); .transform(new DOMSource(doc), new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
/** /**
* Test if two non nested xi:include elements can include the same document * Test if two non nested xi:include elements can include the same document
* with an xi:include statement. * with an xi:include statement.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readWriteLocalFiles"})
public void testXIncludeNestedPos() { public void testXIncludeNestedPos() throws Exception {
String resultFile = CLASS_DIR + "schedule.out"; String resultFile = USER_DIR + "schedule.out";
String goldFile = GOLDEN_DIR + "scheduleGold.xml"; String goldFile = GOLDEN_DIR + "scheduleGold.xml";
String xmlFile = XML_DIR + "schedule.xml"; String xmlFile = XML_DIR + "schedule.xml";
try{
try (FileOutputStream fos = new FileOutputStream(resultFile)) { try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true); dbf.setXIncludeAware(true);
@ -465,18 +367,5 @@ public class AuctionItemRepository {
.transform(new DOMSource(doc), new StreamResult(fos)); .transform(new DOMSource(doc), new StreamResult(fos));
} }
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ParserConfigurationException | SAXException | IOException
| TransformerException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,21 +25,17 @@ package test.auctionportal;
import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE; import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold; import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;
import static jaxp.library.JAXPTestUtilities.failCleanup;
import static jaxp.library.JAXPTestUtilities.failUnexpected;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -50,8 +46,6 @@ import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSParser; import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
import static test.auctionportal.HiBidConstants.CLASS_DIR;
import static test.auctionportal.HiBidConstants.GOLDEN_DIR; import static test.auctionportal.HiBidConstants.GOLDEN_DIR;
import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS; import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
import static test.auctionportal.HiBidConstants.XML_DIR; import static test.auctionportal.HiBidConstants.XML_DIR;
@ -59,16 +53,17 @@ import static test.auctionportal.HiBidConstants.XML_DIR;
/** /**
* This is the user controller class for the Auction portal HiBid.com. * This is the user controller class for the Auction portal HiBid.com.
*/ */
public class UserController { public class UserController extends JAXPFileBaseTest {
/** /**
* Checking when creating an XML document using DOM Level 2 validating * Checking when creating an XML document using DOM Level 2 validating
* it without having a schema source or a schema location It must throw a * it without having a schema source or a schema location It must throw a
* sax parse exception. * sax parse exception.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testCreateNewUser() { public void testCreateNewUser() throws Exception {
String resultFile = CLASS_DIR + "accountInfoOut.xml"; String resultFile = USER_DIR + "accountInfoOut.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(true); dbf.setValidating(true);
@ -92,34 +87,29 @@ public class UserController {
.newInstance().getDOMImplementation("LS"); .newInstance().getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer(); LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
FileOutputStream output = new FileOutputStream(resultFile); try(FileOutputStream output = new FileOutputStream(resultFile)) {
MyDOMOutput domOutput = new MyDOMOutput(); MyDOMOutput domOutput = new MyDOMOutput();
domOutput.setByteStream(output); domOutput.setByteStream(output);
writer.write(account, domOutput); writer.write(account, domOutput);
docBuilder.parse(resultFile); docBuilder.parse(resultFile);
assertTrue(eh.isAnyError());
} catch (ParserConfigurationException | ClassNotFoundException |
InstantiationException | IllegalAccessException
| ClassCastException | SAXException | IOException e) {
failUnexpected(e);
} }
assertTrue(eh.isAnyError());
} }
/** /**
* Checking conflicting namespaces and use renameNode and normalizeDocument. * Checking conflicting namespaces and use renameNode and normalizeDocument.
* @see <a href="content/accountInfo.xml">accountInfo.xml</a> * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testAddUser() { public void testAddUser() throws Exception {
String resultFile = CLASS_DIR + "accountRole.out"; String resultFile = USER_DIR + "accountRole.out";
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
// Copy schema for outputfile // Copy schema for outputfile
Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"), Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"),
Paths.get(CLASS_DIR, "accountInfo.xsd"), Paths.get(USER_DIR, "accountInfo.xsd"),
StandardCopyOption.REPLACE_EXISTING); StandardCopyOption.REPLACE_EXISTING);
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@ -152,24 +142,17 @@ public class UserController {
docBuilder.parse(resultFile); docBuilder.parse(resultFile);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException
| ClassNotFoundException | InstantiationException
| IllegalAccessException | ClassCastException e) {
failUnexpected(e);
}
} }
/** /**
* Checking Text content in XML file. * Checking Text content in XML file.
* @see <a href="content/accountInfo.xml">accountInfo.xml</a> * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
*
* @throws Exception If any errors occur.
*/ */
@Test @Test(groups = {"readLocalFiles"})
public void testMoreUserInfo() { public void testMoreUserInfo() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml"; String xmlFile = XML_DIR + "accountInfo.xml";
try {
System.out.println("Checking additional user info");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
@ -191,9 +174,6 @@ public class UserController {
assertTrue(accountID.getTextContent().trim().equals("1")); assertTrue(accountID.getTextContent().trim().equals("1"));
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
@ -204,15 +184,14 @@ public class UserController {
* into an XML file which is validated by the schema This covers Row 5 * into an XML file which is validated by the schema This covers Row 5
* for the table * for the table
* http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed * http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed
* bug 4893745 because there was a difference in behavior * bug 4893745 because there was a difference in behavior.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testCreateUserAccount() { public void testCreateUserAccount() throws Exception {
System.out.println("Creating user account");
String userXmlFile = XML_DIR + "userInfo.xml"; String userXmlFile = XML_DIR + "userInfo.xml";
String accountXmlFile = XML_DIR + "accountInfo.xml"; String accountXmlFile = XML_DIR + "accountInfo.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(true); dbf.setValidating(true);
@ -248,26 +227,20 @@ public class UserController {
writer.write(accDocument, mydomoutput); writer.write(accDocument, mydomoutput);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException
| ClassNotFoundException | InstantiationException
| IllegalAccessException | ClassCastException e) {
failUnexpected(e);
}
} }
/** /**
* Checking for Row 8 from the schema table when setting the schemaSource * Checking for Row 8 from the schema table when setting the schemaSource
* without the schemaLanguage must report an error. * without the schemaLanguage must report an error.
*
* @throws Exception If any errors occur.
*/ */
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testUserError() throws IllegalArgumentException { public void testUserError() throws Exception {
System.out.println("Creating an error in user account");
String xmlFile = XML_DIR + "userInfo.xml"; String xmlFile = XML_DIR + "userInfo.xml";
String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource"; String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource";
String schemaValue = "http://dummy.com/dummy.xsd"; String schemaValue = "http://dummy.com/dummy.xsd";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(true); dbf.setValidating(true);
@ -276,11 +249,8 @@ public class UserController {
DocumentBuilder docBuilder = dbf.newDocumentBuilder(); DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler(); MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh); docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(xmlFile); docBuilder.parse(xmlFile);
assertFalse(eh.isAnyError()); assertFalse(eh.isAnyError());
} catch (ParserConfigurationException | SAXException | IOException e) {
failUnexpected(e);
}
} }
/** /**
@ -288,10 +258,12 @@ public class UserController {
* @see <a href="content/screenName.xml">screenName.xml</a> has prefix of * @see <a href="content/screenName.xml">screenName.xml</a> has prefix of
* userName is bound to "http://hibid.com/user" namespace normalization * userName is bound to "http://hibid.com/user" namespace normalization
* will create a namespace of prefix us and attach userEmail. * will create a namespace of prefix us and attach userEmail.
*
* @throws Exception If any errors occur.
*/ */
@Test @Test
public void testCheckScreenNameExists() { public void testCheckScreenNameExists() throws Exception {
String resultFile = CLASS_DIR + "screenName.out"; String resultFile = USER_DIR + "screenName.out";
String xmlFile = XML_DIR + "screenName.xml"; String xmlFile = XML_DIR + "screenName.xml";
String goldFile = GOLDEN_DIR + "screenNameGold.xml"; String goldFile = GOLDEN_DIR + "screenNameGold.xml";
@ -318,21 +290,7 @@ public class UserController {
MyDOMOutput domoutput = new MyDOMOutput(); MyDOMOutput domoutput = new MyDOMOutput();
domoutput.setByteStream(output); domoutput.setByteStream(output);
writer.write(document, domoutput); writer.write(document, domoutput);
}
assertTrue(compareDocumentWithGold(goldFile, resultFile)); assertTrue(compareDocumentWithGold(goldFile, resultFile));
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | ClassCastException | IOException
| ParserConfigurationException | SAXException e) {
failUnexpected(e);
} finally {
try {
Path resultPath = Paths.get(resultFile);
if (Files.exists(resultPath)) {
Files.delete(resultPath);
}
} catch (IOException ex) {
failCleanup(ex, resultFile);
}
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,37 +22,22 @@
*/ */
package javax.xml.parsers.ptests; package javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER;
import java.nio.file.StandardCopyOption;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.Locator; import org.xml.sax.Locator;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.LocatorImpl; import org.xml.sax.helpers.LocatorImpl;
/**
* Utility interface which includes final variables of xml, golden file
* directories.
*/
interface TestUtils {
final String XML_DIR = System.getProperty("test.src", ".") + FILE_SEP + "javax/xml/parsers/xmlfiles";
final String GOLDEN_DIR = XML_DIR + FILE_SEP + "out";
}
/** /**
* Customized DefaultHandler which writes output document when methods are * Customized DefaultHandler which writes output document when methods are
* called by Transformer. Test may use output document to compare with golden * called by Transformer. Test may use output document to compare with golden
* file for verification. * file for verification.
*/ */
class MyCHandler extends DefaultHandler { class MyCHandler extends DefaultHandler implements AutoCloseable {
private final BufferedWriter bWriter; private final BufferedWriter bWriter;
private final Locator locator = new LocatorImpl(); private final Locator locator = new LocatorImpl();
@ -66,6 +51,7 @@ class MyCHandler extends DefaultHandler {
return handler; return handler;
} }
@Override
public void characters(char[] ch, int start, int length) { public void characters(char[] ch, int start, int length) {
String s = new String(ch, start, length); String s = new String(ch, start, length);
String str = String.format("characters...length is:%d\n<%s>", s.length(), s); String str = String.format("characters...length is:%d\n<%s>", s.length(), s);
@ -77,19 +63,19 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void endDocument() { public void endDocument() {
String str = "endDocument..."; String str = "endDocument...";
try { try {
bWriter.write(str, 0, str.length()); bWriter.write(str, 0, str.length());
bWriter.newLine(); bWriter.newLine();
bWriter.flush(); bWriter.flush();
bWriter.close();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(ERROR_MSG_HEADER, e); throw new RuntimeException(ERROR_MSG_HEADER, e);
} }
} }
@Override
public void endElement(String namespaceURI, String localName, String qName) { public void endElement(String namespaceURI, String localName, String qName) {
String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName);
try { try {
@ -100,6 +86,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void endPrefixMapping(String prefix) { public void endPrefixMapping(String prefix) {
String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix);
try { try {
@ -110,6 +97,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void ignorableWhitespace(char[] ch, int start, int length) { public void ignorableWhitespace(char[] ch, int start, int length) {
String s = new String(ch, start, length); String s = new String(ch, start, length);
String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length());
@ -121,6 +109,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void processingInstruction(String target, String data) { public void processingInstruction(String target, String data) {
String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data);
try { try {
@ -131,6 +120,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void skippedEntity(String name) { public void skippedEntity(String name) {
String str = String.format("skippedEntity...\nname: <%s>", name); String str = String.format("skippedEntity...\nname: <%s>", name);
try { try {
@ -141,6 +131,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void startDocument() { public void startDocument() {
String str = "startDocument..."; String str = "startDocument...";
try { try {
@ -151,6 +142,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI,
localName, qName, atts.getLength(), locator.getLineNumber()); localName, qName, atts.getLength(), locator.getLineNumber());
@ -162,6 +154,7 @@ class MyCHandler extends DefaultHandler {
} }
} }
@Override
public void startPrefixMapping(String prefix, String uri) { public void startPrefixMapping(String prefix, String uri) {
String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri);
try { try {
@ -171,30 +164,10 @@ class MyCHandler extends DefaultHandler {
throw new RuntimeException(ERROR_MSG_HEADER, e); throw new RuntimeException(ERROR_MSG_HEADER, e);
} }
} }
}
/** @Override
* Customized DefaultHandler used for SAXParseException testing. public void close() throws IOException {
*/ if (bWriter != null)
class MyErrorHandler extends DefaultHandler { bWriter.close();
boolean errorOccured = false;
private MyErrorHandler() {
}
public static MyErrorHandler newInstance() {
return new MyErrorHandler();
}
public void error(SAXParseException e) {
errorOccured = true;
}
public void warning(SAXParseException e) {
errorOccured = true;
}
public void fatalError(SAXParseException e) {
errorOccured = true;
} }
} }

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 1999, 2015, 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 javax.xml.parsers.ptests;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Customized DefaultHandler used for SAXParseException testing.
*/
class MyErrorHandler extends DefaultHandler {
/**
* Flag whether any event was received.
*/
private volatile boolean errorOccured;
/**
* Set no event received on constructor.
*/
private MyErrorHandler() {
errorOccured = false;
}
/**
* Factory method to create a MyErrorHandler instance.
* @return a MyErrorHandler instance.
*/
public static MyErrorHandler newInstance() {
return new MyErrorHandler();
}
/**
* Receive notification of a recoverable error.
* @param e a recoverable parser exception error.
*/
@Override
public void error(SAXParseException e) {
errorOccured = true;
}
/**
* Receive notification of a parser warning.
* @param e a parser warning event.
*/
@Override
public void warning(SAXParseException e) {
errorOccured = true;
}
/**
* Report a fatal XML parsing error.
* @param e The error information encoded as an exception.
*/
@Override
public void fatalError(SAXParseException e) {
errorOccured = true;
}
/**
* Has any event been received.
*
* @return true if any event has been received.
* false if no event has been received.
*/
public boolean isErrorOccured() {
return errorOccured;
}
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2014, 2015, 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 javax.xml.parsers.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.getPathByClassName;
/**
* Utility interface which includes final variables of XML, golden file
* directories.
*/
public class ParserTestConst {
/**
* XML source file directory.
*/
public static final String XML_DIR = getPathByClassName(ParserTestConst.class,
".." + FILE_SEP + "xmlfiles");
/**
* Golden validation files directory.
*/
public static final String GOLDEN_DIR = getPathByClassName(ParserTestConst.class,
".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out");
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,40 +23,22 @@
package javax.xml.transform.ptests; package javax.xml.transform.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.getPathByClassName;
/** /**
* This is the Base test class provide basic support for JAXP functional test * This is the Base test class provide basic support for JAXP functional test
*/ */
public class TransformerTestConst { public class TransformerTestConst {
/** /**
* Current test directory. * XML source file directory.
*/ */
public static final String CLASS_DIR public static final String XML_DIR = getPathByClassName(TransformerTestConst.class,
= System.getProperty("test.classes", ".") + FILE_SEP; ".." + FILE_SEP + "xmlfiles");
/** /**
* Package name that separates by slash. * Golden validation files directory.
*/ */
public static final String PACKAGE_NAME = FILE_SEP + public static final String GOLDEN_DIR = getPathByClassName(TransformerTestConst.class,
TransformerTestConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP); ".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out");
/**
* Test base directory. Every package has its own test package directory.
*/
public static final String BASE_DIR
= System.getProperty("test.src", USER_DIR).replaceAll("\\" + System.getProperty("file.separator"), "/")
+ PACKAGE_NAME + FILE_SEP + "..";
/**
* Source XML file directory.
*/
public static final String XML_DIR = BASE_DIR + FILE_SEP + "xmlfiles" + FILE_SEP;
/**
* Golden output file directory. We pre-define all expected output in golden
* output file. Test verifies whether the standard output is same as content
* of golden file.
*/
public static final String GOLDEN_DIR = XML_DIR + FILE_SEP + "out" + FILE_SEP;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,27 +23,15 @@
package javax.xml.xpath.ptests; package javax.xml.xpath.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.getPathByClassName;
/** /**
* This is the Base test class provide basic support for XPath functional test * This is the Base test class provide basic support for XPath functional test
*/ */
public class XPathTestConst { public class XPathTestConst {
/** /**
* Package name that separates by slash. * XML source file directory.
*/ */
public static final String PACKAGE_NAME = FILE_SEP + public static final String XML_DIR = getPathByClassName(XPathTestConst.class,
XPathTestConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP); ".." + FILE_SEP + "xmlfiles");
/**
* Test base directory. Every package has its own test package directory.
*/
public static final String BASE_DIR
= System.getProperty("test.src", USER_DIR).replaceAll("\\" + System.getProperty("file.separator"), "/")
+ PACKAGE_NAME + FILE_SEP + "..";
/**
* Source XML file directory.
*/
public static final String XML_DIR = BASE_DIR + FILE_SEP + "xmlfiles" + FILE_SEP;
} }

View file

@ -0,0 +1,138 @@
/*
* Copyright (c) 2015, 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 jaxp.library;
import java.security.Permission;
import java.security.Permissions;
import java.security.Policy;
import java.util.PropertyPermission;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
/**
* This is a base class that every test class must extend if it needs to be run
* with security mode.
*/
public class JAXPBaseTest {
/**
* Backing up policy.
*/
protected static Policy policy;
/**
* Backing up security manager.
*/
private static SecurityManager sm;
/*
* Install a SecurityManager along with a base Policy to allow testNG to
* run when there is a security manager.
*/
@BeforeClass
public void setUpClass() throws Exception {
setPolicy(new TestPolicy());
System.setSecurityManager(new SecurityManager());
}
/*
* Install the original Policy and SecurityManager when there is a security
* manager.
*/
@AfterClass
public void tearDownClass() throws Exception {
System.setSecurityManager(sm);
setPolicy(policy);
}
/*
* Utility Method used to set the current Policy.
*/
protected static void setPolicy(Policy p) {
Policy.setPolicy(p);
}
/*
* Add the specified permission(s) to the test policy.
* Note there is no way to add permissions to current permissions. Reset
* test policy by setting minimal permmisons in addition to specified
* permissions when calling this method.
*/
protected static void setPermissions(Permission... ps) {
Policy.setPolicy(new TestPolicy(ps));
}
/*
* Add the specified permission(s) to the test policy.
* Note there is no way to add permissions to current permissions. Reset
* test policy by setting minimal permmisons in addition to specified
* permissions when calling this method.
*/
protected static void setPermissions(Permissions ps) {
Policy.setPolicy(new TestPolicy(ps));
}
/**
* Backing up policy and security manager for restore when there is a
* security manager.
*/
public JAXPBaseTest() {
policy = Policy.getPolicy();
sm = System.getSecurityManager();
}
/**
* Safety acquire a system property.
* Note invocation of this method will restore permission to limited
* minimal permission of tests. If there is additional permission set
* already, you need restore permission by yourself.
* @param propName System property name to be acquired.
* @return property value
*/
protected String getSystemProperty(final String propName) {
setPermissions(new PropertyPermission(propName, "read"));
try {
return System.getProperty(propName);
} finally {
setPermissions();
}
}
/**
* Safety set a system property by given system value.
*
* @param propName System property name to be set.
* @param propValue System property value to be set.
*/
protected void setSystemProperty(final String propName, final String propValue) {
setPermissions(new PropertyPermission(propName, "write"));
try {
if (propValue == null) {
System.clearProperty(propName);
} else {
System.setProperty(propName, propValue);
}
} finally {
setPermissions();
}
}
}

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 2015, 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 jaxp.library;
import java.io.FilePermission;
import java.security.Permission;
import java.security.Permissions;
import java.security.Policy;
import static jaxp.library.JAXPBaseTest.setPolicy;
import org.testng.annotations.BeforeClass;
/**
* This is a base class that every test class that need to access local XML
* files must extend if it needs to be run with security mode.
*/
public class JAXPFileBaseTest extends JAXPBaseTest {
/*
* Install a SecurityManager along with a base Policy to allow testNG to
* run when there is a security manager.
*/
@BeforeClass
@Override
public void setUpClass() throws Exception {
setPolicy(new FileTestPolicy());
System.setSecurityManager(new SecurityManager());
}
/*
* Add the specified permission(s) to the test policy.
* Note there is no way to add permissions to current permissions. Reset
* test policy by setting minimal permmisons in addition to specified
* permissions when calling this method.
*/
protected static void setPermissions(Permission... ps) {
Policy.setPolicy(new FileTestPolicy(ps));
}
/*
* Add the specified permission(s) to the test policy.
* Note there is no way to add permissions to current permissions. Reset
* test policy by setting minimal permmisons in addition to specified
* permissions when calling this method.
*/
protected static void setPermissions(Permissions ps) {
Policy.setPolicy(new FileTestPolicy(ps));
}
}
/**
* This policy is only given to tests that need access local files. Additional
* permissions for accessing local files have been granted by default.
* @author HaiboYan
*/
class FileTestPolicy extends TestPolicy {
/**
* Constructor which sets the minimum permissions by default allowing testNG
* to work with a SecurityManager.
* @param ps permissions to be added.
*/
public FileTestPolicy(Permissions ps) {
super(ps);
}
/**
* Constructor which sets the minimum permissions by default allowing testNG
* to work with a SecurityManager.
* @param ps permission array to be added.
*/
public FileTestPolicy(Permission... ps) {
super(ps);
}
/**
* Defines the minimal permissions required by testNG when running these
* tests
*/
@Override
protected void setMinimalPermissions() {
super.setMinimalPermissions();
permissions.add(new FilePermission(System.getProperty("user.dir") + "/-",
"read, write"));
permissions.add(new FilePermission(System.getProperty("test.src") + "/-",
"read"));
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015, 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 jaxp.library;
import java.io.FilePermission;
import static jaxp.library.JAXPBaseTest.setPermissions;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
/**
* This is a base class that every test class that need to reading local XML
* files must extend if it needs to be run with security mode.
*/
public class JAXPFileReadOnlyBaseTest extends JAXPBaseTest {
/**
* Source files/XML files directory.
*/
private final String SRC_DIR = getSystemProperty("test.src");
/**
* Allowing access local file system for this group.
*/
@BeforeGroups (groups = {"readLocalFiles"})
public void setFilePermissions() {
setPermissions(new FilePermission(SRC_DIR + "/-", "read"));
}
/**
* Restore the system property.
*/
@AfterGroups (groups = {"readLocalFiles"})
public void restoreFilePermissions() {
setPermissions();
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,21 +23,34 @@
package jaxp.library; package jaxp.library;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
@ -61,14 +74,17 @@ public class JAXPTestUtilities {
public static final String FILE_SEP = "/"; public static final String FILE_SEP = "/";
/** /**
* User home. * Current test directory.
*/ */
public static final String USER_DIR = System.getProperty("user.dir", "."); public static final String USER_DIR =
System.getProperty("user.dir", ".") + FILE_SEP;;
/** /**
* TEMP file directory. * A map storing every test's current test file pointer. File number should
* be incremental and it's a thread-safe reading on this file number.
*/ */
public static final String TEMP_DIR = System.getProperty("java.io.tmpdir", "."); private static final ConcurrentHashMap<Class, Integer> currentFileNumber
= new ConcurrentHashMap<>();
/** /**
* BOM table for storing BOM header. * BOM table for storing BOM header.
@ -94,12 +110,60 @@ public class JAXPTestUtilities {
* @return true if two files are identical. * @return true if two files are identical.
* false if two files are not identical. * false if two files are not identical.
* @throws IOException if an I/O error occurs reading from the file or a * @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read * malformed or unmappable byte sequence is read.
*/ */
public static boolean compareWithGold(String goldfile, String outputfile) public static boolean compareWithGold(String goldfile, String outputfile)
throws IOException { throws IOException {
return compareWithGold(goldfile, outputfile, StandardCharsets.UTF_8);
}
/**
* Compare contents of golden file with test output file line by line.
* return true if they're identical.
* @param goldfile Golden output file name.
* @param outputfile Test output file name.
* @param cs the charset to use for decoding.
* @return true if two files are identical.
* false if two files are not identical.
* @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read.
*/
public static boolean compareWithGold(String goldfile, String outputfile,
Charset cs) throws IOException {
return Files.readAllLines(Paths.get(goldfile)). return Files.readAllLines(Paths.get(goldfile)).
equals(Files.readAllLines(Paths.get(outputfile))); equals(Files.readAllLines(Paths.get(outputfile), cs));
}
/**
* Compare contents of golden file with test output list line by line.
* return true if they're identical.
* @param goldfile Golden output file name.
* @param lines test output list.
* @return true if file's content is identical to given list.
* false if file's content is not identical to given list.
* @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read
*/
public static boolean compareLinesWithGold(String goldfile, List<String> lines)
throws IOException {
return Files.readAllLines(Paths.get(goldfile)).equals(lines);
}
/**
* Compare contents of golden file with a test output string.
* return true if they're identical.
* @param goldfile Golden output file name.
* @param string test string.
* @return true if file's content is identical to given string.
* false if file's content is not identical to given string.
* @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read
*/
public static boolean compareStringWithGold(String goldfile, String string)
throws IOException {
return Files.readAllLines(Paths.get(goldfile)).stream().collect(
Collectors.joining(System.getProperty("line.separator")))
.equals(string);
} }
/** /**
@ -132,6 +196,35 @@ public class JAXPTestUtilities {
resultD.normalizeDocument(); resultD.normalizeDocument();
return goldD.isEqualNode(resultD); return goldD.isEqualNode(resultD);
} }
/**
* Compare contents of golden file with the serialization represent by given
* DOM node.
* Here we ignore the white space and comments. return true if they're
* lexical identical.
* @param goldfile Golden output file name.
* @param node A DOM node instance.
* @return true if file's content is identical to given node's serialization
* represent.
* false if file's content is not identical to given node's
* serialization represent.
* @throws TransformerException If an unrecoverable error occurs during the
* course of the transformation..
* @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read .
*/
public static boolean compareSerializeDOMWithGold(String goldfile, Node node)
throws TransformerException, IOException {
TransformerFactory factory = TransformerFactory.newInstance();
// Use identity transformer to serialize
Transformer identityTransformer = factory.newTransformer();
StringWriter sw = new StringWriter();
StreamResult streamResult = new StreamResult(sw);
DOMSource nodeSource = new DOMSource(node);
identityTransformer.transform(nodeSource, streamResult);
return compareStringWithGold(goldfile, sw.toString());
}
/** /**
* Convert stream to ByteArrayInputStream by given character set. * Convert stream to ByteArrayInputStream by given character set.
* @param charset target character set. * @param charset target character set.
@ -159,6 +252,36 @@ public class JAXPTestUtilities {
return new ByteArrayInputStream(bb.array()); return new ByteArrayInputStream(bb.array());
} }
/**
* Worker method to detect common absolute URLs.
*
* @param s String path\filename or URL (or any, really)
* @return true if s starts with a common URI scheme (namely
* the ones found in the examples of RFC2396); false otherwise
*/
protected static boolean isCommonURL(String s) {
if (null == s)
return false;
return Pattern.compile("^(file:|http:|ftp:|gopher:|mailto:|news:|telnet:)")
.matcher(s).matches();
}
/**
* Utility method to translate a String filename to URL.
*
* If the name starts with a common URI scheme (namely the ones
* found in the examples of RFC2396), then simply return the
* name as-is (the assumption is that it's already a URL).
* Otherwise we attempt (cheaply) to convert to a file:/ URL.
*
* @param filename local path/filename of a file.
* @return a file:/ URL if filename represent a file, the same string if
* it appears to already be a URL.
*/
public static String filenameToURL(String filename) {
return Paths.get(filename).toUri().toASCIIString();
}
/** /**
* Prints error message if an exception is thrown * Prints error message if an exception is thrown
* @param ex The exception is thrown by test. * @param ex The exception is thrown by test.
@ -175,4 +298,38 @@ public class JAXPTestUtilities {
public static void failCleanup(IOException ex, String name) { public static void failCleanup(IOException ex, String name) {
fail(String.format(ERROR_MSG_CLEANUP, name), ex); fail(String.format(ERROR_MSG_CLEANUP, name), ex);
} }
/**
* Retrieve next test output file name. This method is a thread-safe method.
* @param clazz test class.
* @return next test output file name.
*/
public static String getNextFile(Class clazz) {
int nextNumber = currentFileNumber.contains(clazz)
? currentFileNumber.get(clazz) + 1 : 1;
Integer i = currentFileNumber.putIfAbsent(clazz, nextNumber);
if (i != null && i != nextNumber) {
do {
nextNumber = currentFileNumber.get(clazz) + 1;
} while (currentFileNumber.replace(clazz, nextNumber -1, nextNumber));
}
return USER_DIR + clazz.getName() + nextNumber + ".out";
}
/**
* Acquire a full path string by given class name and relative path string.
* @param clazz Class name for the test.
* @param relativeDir relative path between java source file and expected
* path.
* @return a string represents the full path of accessing path.
*/
public static String getPathByClassName(Class clazz, String relativeDir) {
String packageName = FILE_SEP +
clazz.getPackage().getName().replaceAll("[.]", FILE_SEP);
String javaSourcePath = System.getProperty("test.src").replaceAll("\\" + File.separator, FILE_SEP)
+ packageName + FILE_SEP;
String normalizedPath = Paths.get(javaSourcePath, relativeDir).normalize().
toAbsolutePath().toString();
return normalizedPath.replace("\\", FILE_SEP) + FILE_SEP;
}
} }

View file

@ -0,0 +1,149 @@
/*
* Copyright (c) 2015, 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 jaxp.library;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.security.SecurityPermission;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.PropertyPermission;
import java.util.StringJoiner;
/*
* Simple Policy class that supports the required Permissions to validate the
* JAXP concrete classes.
* Note: permission can only be added. You may want to create a new TestPolicy
* instance if you need remove permissions.
*/
public class TestPolicy extends Policy {
protected final PermissionCollection permissions = new Permissions();
/**
* Constructor which sets the minimum permissions by default allowing testNG
* to work with a SecurityManager.
*/
public TestPolicy() {
setMinimalPermissions();
}
/**
* Construct an instance with the minimal permissions required by the test
* environment and additional permission(s) as specified.
* @param ps permissions to be added.
*/
public TestPolicy(Permissions ps) {
setMinimalPermissions();
TestPolicy.this.addPermissions(ps);
}
/**
* Construct an instance with the minimal permissions required by the test
* environment and additional permission(s) as specified.
* @param ps permission array to be added.
*/
public TestPolicy(Permission... ps) {
setMinimalPermissions();
addPermissions(ps);
}
/**
* Defines the minimal permissions required by testNG when running these
* tests
*/
protected void setMinimalPermissions() {
permissions.add(new SecurityPermission("getPolicy"));
permissions.add(new SecurityPermission("setPolicy"));
permissions.add(new RuntimePermission("getClassLoader"));
permissions.add(new RuntimePermission("setSecurityManager"));
permissions.add(new RuntimePermission("createSecurityManager"));
permissions.add(new PropertyPermission("testng.show.stack.frames",
"read"));
permissions.add(new PropertyPermission("user.dir", "read"));
permissions.add(new PropertyPermission("test.src", "read"));
permissions.add(new PropertyPermission("file.separator", "read"));
permissions.add(new PropertyPermission("line.separator", "read"));
permissions.add(new PropertyPermission("fileStringBuffer", "read"));
permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
}
/*
* Add permissions for your tests.
* @param permissions to be added.
*/
private void addPermissions(Permissions ps) {
Collections.list(ps.elements()).forEach(p -> permissions.add(p));
}
/*
* Add permissions for your tests.
* @param permissions to be added.
*/
private void addPermissions(Permission[] ps) {
Arrays.stream(ps).forEach(p -> permissions.add(p));
}
/**
* Set all permissions. Caution: this should not called carefully unless
* it's really needed.
*/
private void setAllPermissions() {
permissions.add(new AllPermission());
}
/*
* Overloaded methods from the Policy class.
*/
@Override
public String toString() {
StringJoiner sj = new StringJoiner("\n", "policy: ", "");
Enumeration<Permission> perms = permissions.elements();
while (perms.hasMoreElements()) {
sj.add(perms.nextElement().toString());
}
return sj.toString();
}
@Override
public PermissionCollection getPermissions(ProtectionDomain domain) {
return permissions;
}
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
return permissions;
}
@Override
public boolean implies(ProtectionDomain domain, Permission perm) {
return permissions.implies(perm);
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -31,11 +31,12 @@ import java.io.IOException;
import java.io.FileWriter; import java.io.FileWriter;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
class MyNSContentHandler extends DefaultHandler { class MyNSContentHandler extends DefaultHandler implements AutoCloseable{
/** /**
* Prefix for written string. * Prefix for written string.
*/ */
private final static String WRITE_ERROR = "bWrite error"; private final static String WRITE_ERROR = "bWrite error";
/** /**
* FileWriter to write output file. * FileWriter to write output file.
*/ */
@ -205,4 +206,14 @@ class MyNSContentHandler extends DefaultHandler {
throw new SAXException(WRITE_ERROR, ex); throw new SAXException(WRITE_ERROR, ex);
} }
} }
/**
* Close writer if it's initiated.
* @throws IOException if any I/O error when close writer.
*/
@Override
public void close() throws IOException {
if (bWriter != null)
bWriter.close();
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
package org.xml.sax.ptests; package org.xml.sax.ptests;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.FILE_SEP;
import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.getPathByClassName;
/** /**
* This is the Base test class provide basic support for JAXP SAX functional * This is the Base test class provide basic support for JAXP SAX functional
@ -32,33 +32,15 @@ import static jaxp.library.JAXPTestUtilities.USER_DIR;
*/ */
public class SAXTestConst { public class SAXTestConst {
/** /**
* Current test directory. * XML source file directory.
*/ */
public static final String CLASS_DIR public static final String XML_DIR = getPathByClassName(SAXTestConst.class,
= System.getProperty("test.classes", ".") + FILE_SEP; ".." + FILE_SEP + "xmlfiles");
/** /**
* Package name that separates by slash. * Golden validation files directory.
*/ */
public static final String PACKAGE_NAME = FILE_SEP + public static final String GOLDEN_DIR = getPathByClassName(SAXTestConst.class,
SAXTestConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP); ".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out");
/**
* Test base directory. Every package has its own test package directory.
*/
public static final String BASE_DIR
= System.getProperty("test.src", USER_DIR).replaceAll("\\" + System.getProperty("file.separator"), "/")
+ PACKAGE_NAME + FILE_SEP + "..";
/**
* Source XML file directory.
*/
public static final String XML_DIR = BASE_DIR + FILE_SEP + "xmlfiles" + FILE_SEP;
/**
* Golden output file directory. We pre-define all expected output in golden
* output file. Test verifies whether the standard output is same as content
* of golden file.
*/
public static final String GOLDEN_DIR = XML_DIR + FILE_SEP + "out" + FILE_SEP;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,44 +22,21 @@
*/ */
package test.auctionportal; package test.auctionportal;
import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.getPathByClassName;
import static jaxp.library.JAXPTestUtilities.USER_DIR;
/** /**
* This is the Base test class provide basic support for Auction portal test. * This is the Base test class provide basic support for Auction portal test.
*/ */
public class HiBidConstants { public class HiBidConstants {
/** /**
* Current test directory. * XML source file directory.
*/ */
public static final String CLASS_DIR public static final String XML_DIR = getPathByClassName(HiBidConstants.class, "content");
= System.getProperty("test.classes", ".") + FILE_SEP;
/** /**
* Package name that separates by slash. * Golden validation files directory.
*/ */
public static final String PACKAGE_NAME = FILE_SEP + public static final String GOLDEN_DIR = getPathByClassName(HiBidConstants.class, "golden");
HiBidConstants.class.getPackage().getName().replaceAll("[.]", FILE_SEP);
/**
* Java source directory.
*/
public static final String SRC_DIR = System.getProperty("test.src", USER_DIR)
.replaceAll("\\" + System.getProperty("file.separator"), "/")
+ PACKAGE_NAME + FILE_SEP;
/**
* Source XML file directory.
*/
public static final String XML_DIR = SRC_DIR + "content" + FILE_SEP;
/**
* Golden output file directory.
* We pre-define all expected output in golden output file. Test verifies
* whether the standard output is same as content of golden file.
*/
public static final String GOLDEN_DIR = SRC_DIR + "golden" + FILE_SEP;
/** /**
* Name space for account operation. * Name space for account operation.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -108,7 +108,7 @@ public class MyDOMOutput implements LSOutput {
/** /**
* Set 16 bits unit writable stream. * Set 16 bits unit writable stream.
* *
* @param bs a Writer instance * @param cs a Writer instance
*/ */
@Override @Override
public void setCharacterStream(Writer cs) { public void setCharacterStream(Writer cs) {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -57,6 +57,8 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
/** /**
* Sets whether output is canonical. * Sets whether output is canonical.
*
* @param canonical if the output is canonical format.
*/ */
public void setCanonical(boolean canonical) { public void setCanonical(boolean canonical) {
fCanonical = canonical; fCanonical = canonical;
@ -66,6 +68,8 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
* Sets the output stream for printing. * Sets the output stream for printing.
* @param stream OutputStream for message output. * @param stream OutputStream for message output.
* @param encoding File encoding for message output. * @param encoding File encoding for message output.
* @throws UnsupportedEncodingException if given encoding is an unsupported
* encoding name or invalid encoding name.
*/ */
public XInclHandler(OutputStream stream, String encoding) public XInclHandler(OutputStream stream, String encoding)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
@ -97,8 +101,8 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
* @param target The processing instruction target. * @param target The processing instruction target.
* @param data The processing instruction data, or null if * @param data The processing instruction data, or null if
* none is supplied. * none is supplied.
* @exception org.xml.sax.SAXException Any SAX exception, possibly * @exception SAXException Any SAX exception, possibly wrapping another
* wrapping another exception. * exception.
*/ */
@Override @Override
public void processingInstruction (String target, String data) public void processingInstruction (String target, String data)
@ -119,14 +123,16 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
* @param uri The Namespace URI, or the empty string if the * @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace * element has no Namespace URI or if Namespace
* processing is not being performed. * processing is not being performed.
* @param localName The local name (without prefix), or the * @param local The local name (without prefix), or the
* empty string if Namespace processing is not being * empty string if Namespace processing is not being
* performed. * performed.
* @param qName The qualified name (with prefix), or the * @param raw The qualified name (with prefix), or the
* empty string if qualified names are not available. * empty string if qualified names are not available.
* @param attributes The attributes attached to the element. If * @param attrs The attributes attached to the element. If
* there are no attributes, it shall be an empty * there are no attributes, it shall be an empty
* Attributes object. * Attributes object.
* @throws SAXException Any SAX exception, possibly wrapping another
* exception.
*/ */
@Override @Override
public void startElement(String uri, String local, String raw, public void startElement(String uri, String local, String raw,
@ -181,11 +187,13 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
* @param uri The Namespace URI, or the empty string if the * @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace * element has no Namespace URI or if Namespace
* processing is not being performed. * processing is not being performed.
* @param localName The local name (without prefix), or the * @param local The local name (without prefix), or the
* empty string if Namespace processing is not being * empty string if Namespace processing is not being
* performed. * performed.
* @param qName The qualified name (with prefix), or the * @param raw The qualified name (with prefix), or the
* empty string if qualified names are not available. * empty string if qualified names are not available.
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*/ */
@Override @Override
public void endElement(String uri, String local, String raw) public void endElement(String uri, String local, String raw)
@ -196,7 +204,7 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
/** /**
* Receive notification of a parser warning and print it out. * Receive notification of a parser warning and print it out.
* @param e The warning information encoded as an exception. * @param ex The warning information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly * @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception. * wrapping another exception.
*/ */
@ -207,10 +215,9 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
/** /**
* Receive notification of a parser error and print it out. * Receive notification of a parser error and print it out.
* @param e The error information encoded as an exception. * @param ex The error information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly * @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception. * wrapping another exception.
*/ */
@Override @Override
public void error(SAXParseException ex) throws SAXException { public void error(SAXParseException ex) throws SAXException {
@ -220,7 +227,7 @@ public class XInclHandler extends DefaultHandler implements LexicalHandler {
/** /**
* Receive notification of a parser fatal error. Throw out fatal error * Receive notification of a parser fatal error. Throw out fatal error
* following print fatal error message. * following print fatal error message.
* @param e The fatal error information encoded as an exception. * @param ex The fatal error information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly * @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception. * wrapping another exception.