< prev index next >

test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package javax.xml.parsers.ptests;
  25 
  26 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  27 import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
  28 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
  29 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  30 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  31 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  32 import static org.testng.Assert.assertEquals;
  33 import static org.testng.Assert.assertFalse;
  34 import static org.testng.Assert.assertNotNull;
  35 import static org.testng.Assert.assertNull;
  36 import static org.testng.Assert.assertTrue;
  37 
  38 import java.io.BufferedReader;
  39 import java.io.Closeable;
  40 import java.io.File;
  41 import java.io.FileInputStream;
  42 import java.io.FileNotFoundException;
  43 import java.io.FilePermission;
  44 import java.io.FileReader;
  45 
  46 import javax.xml.parsers.DocumentBuilder;
  47 import javax.xml.parsers.DocumentBuilderFactory;
  48 import javax.xml.parsers.FactoryConfigurationError;
  49 import javax.xml.parsers.ParserConfigurationException;
  50 import javax.xml.parsers.SAXParser;
  51 import javax.xml.parsers.SAXParserFactory;
  52 import javax.xml.transform.Transformer;
  53 import javax.xml.transform.TransformerFactory;
  54 import javax.xml.transform.dom.DOMSource;
  55 import javax.xml.transform.sax.SAXResult;
  56 
  57 import jaxp.library.JAXPDataProvider;
  58 import jaxp.library.JAXPFileBaseTest;
  59 
  60 import org.testng.annotations.DataProvider;

  61 import org.testng.annotations.Test;
  62 import org.w3c.dom.Document;
  63 import org.w3c.dom.Element;
  64 import org.w3c.dom.NodeList;
  65 import org.xml.sax.InputSource;
  66 import org.xml.sax.SAXException;
  67 import org.xml.sax.helpers.DefaultHandler;
  68 
  69 /**
  70  * @bug 8080907
  71  * This checks the methods of DocumentBuilderFactoryImpl.
  72  */
  73 public class DocumentBuilderFactoryTest extends JAXPFileBaseTest {

  74     /**
  75      * DocumentBuilderFactory implementation class name.
  76      */
  77     private static final String DOCUMENT_BUILDER_FACTORY_CLASSNAME = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
  78 
  79     /**
  80      * Provide valid DocumentBuilderFactory instantiation parameters.
  81      *
  82      * @return a data provider contains DocumentBuilderFactory instantiation parameters.
  83      */
  84     @DataProvider(name = "parameters")
  85     public Object[][] getValidateParameters() {
  86         return new Object[][] { { DOCUMENT_BUILDER_FACTORY_CLASSNAME, null }, { DOCUMENT_BUILDER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } };
  87     }
  88 
  89     /**
  90      * Test for DocumentBuilderFactory.newInstance(java.lang.String
  91      * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName
  92      * points to correct implementation of
  93      * javax.xml.parsers.DocumentBuilderFactory , should return newInstance of


 402      * @throws Exception If any errors occur.
 403      */
 404     @Test
 405     public void testCheckDocumentBuilderFactory12() throws Exception {
 406         try (FileInputStream fis = new FileInputStream(new File(
 407                 XML_DIR, "dbf10import.xsl"))) {
 408             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 409             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 410             Document doc = docBuilder.parse(fis, " ");
 411             assertNotNull(doc);
 412         }
 413     }
 414 
 415     /**
 416      * This tests for the parse(uri).
 417      * @throws Exception If any errors occur.
 418      */
 419     @Test
 420     public void testCheckDocumentBuilderFactory13() throws Exception {
 421         // Accesing default working directory.
 422         String workingDir = getSystemProperty("user.dir");
 423         setPermissions(new FilePermission(workingDir + "/*", "read"));
 424         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 425         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 426         Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
 427                 .toURI().toASCIIString());
 428         assertNotNull(doc);
 429     }
 430 
 431     /**
 432      * This tests for the parse(uri) with empty string as parameter should
 433      * throw Sax Exception.
 434      * @throws Exception If any errors occur.
 435      */
 436     @Test(expectedExceptions = SAXException.class)
 437     public void testCheckDocumentBuilderFactory14() throws Exception {
 438         // Accesing default working directory.
 439         String workingDir = getSystemProperty("user.dir");
 440         setPermissions(new FilePermission(workingDir, "read"));
 441         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 442         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 443         docBuilder.parse("");
 444     }
 445 
 446     /**
 447      * This tests for the parse (uri) with null uri as parameter should throw
 448      * IllegalArgumentException.
 449      * @throws Exception If any errors occur.
 450      *
 451      */
 452     @Test(expectedExceptions = IllegalArgumentException.class)
 453     public void testCheckDocumentBuilderFactory15() throws Exception {
 454         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 455         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 456         String uri = null;
 457         docBuilder.parse(uri);
 458     }
 459 
 460     /**


   1 /*
   2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package javax.xml.parsers.ptests;
  25 
  26 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  27 import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
  28 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
  29 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  30 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  31 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  32 import static org.testng.Assert.assertEquals;
  33 import static org.testng.Assert.assertFalse;
  34 import static org.testng.Assert.assertNotNull;
  35 import static org.testng.Assert.assertNull;
  36 import static org.testng.Assert.assertTrue;
  37 
  38 import java.io.BufferedReader;
  39 import java.io.Closeable;
  40 import java.io.File;
  41 import java.io.FileInputStream;
  42 import java.io.FileNotFoundException;

  43 import java.io.FileReader;
  44 
  45 import javax.xml.parsers.DocumentBuilder;
  46 import javax.xml.parsers.DocumentBuilderFactory;
  47 import javax.xml.parsers.FactoryConfigurationError;
  48 import javax.xml.parsers.ParserConfigurationException;
  49 import javax.xml.parsers.SAXParser;
  50 import javax.xml.parsers.SAXParserFactory;
  51 import javax.xml.transform.Transformer;
  52 import javax.xml.transform.TransformerFactory;
  53 import javax.xml.transform.dom.DOMSource;
  54 import javax.xml.transform.sax.SAXResult;
  55 
  56 import jaxp.library.JAXPDataProvider;

  57 
  58 import org.testng.annotations.DataProvider;
  59 import org.testng.annotations.Listeners;
  60 import org.testng.annotations.Test;
  61 import org.w3c.dom.Document;
  62 import org.w3c.dom.Element;
  63 import org.w3c.dom.NodeList;
  64 import org.xml.sax.InputSource;
  65 import org.xml.sax.SAXException;
  66 import org.xml.sax.helpers.DefaultHandler;
  67 
  68 /**
  69  * @bug 8080907
  70  * This checks the methods of DocumentBuilderFactoryImpl.
  71  */
  72 @Listeners({jaxp.library.FilePolicy.class})
  73 public class DocumentBuilderFactoryTest {
  74     /**
  75      * DocumentBuilderFactory implementation class name.
  76      */
  77     private static final String DOCUMENT_BUILDER_FACTORY_CLASSNAME = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
  78 
  79     /**
  80      * Provide valid DocumentBuilderFactory instantiation parameters.
  81      *
  82      * @return a data provider contains DocumentBuilderFactory instantiation parameters.
  83      */
  84     @DataProvider(name = "parameters")
  85     public Object[][] getValidateParameters() {
  86         return new Object[][] { { DOCUMENT_BUILDER_FACTORY_CLASSNAME, null }, { DOCUMENT_BUILDER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } };
  87     }
  88 
  89     /**
  90      * Test for DocumentBuilderFactory.newInstance(java.lang.String
  91      * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName
  92      * points to correct implementation of
  93      * javax.xml.parsers.DocumentBuilderFactory , should return newInstance of


 402      * @throws Exception If any errors occur.
 403      */
 404     @Test
 405     public void testCheckDocumentBuilderFactory12() throws Exception {
 406         try (FileInputStream fis = new FileInputStream(new File(
 407                 XML_DIR, "dbf10import.xsl"))) {
 408             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 409             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 410             Document doc = docBuilder.parse(fis, " ");
 411             assertNotNull(doc);
 412         }
 413     }
 414 
 415     /**
 416      * This tests for the parse(uri).
 417      * @throws Exception If any errors occur.
 418      */
 419     @Test
 420     public void testCheckDocumentBuilderFactory13() throws Exception {
 421         // Accesing default working directory.


 422         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 423         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 424         Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
 425                 .toURI().toASCIIString());
 426         assertNotNull(doc);
 427     }
 428 
 429     /**
 430      * This tests for the parse(uri) with empty string as parameter should
 431      * throw Sax Exception.
 432      * @throws Exception If any errors occur.
 433      */
 434     @Test(expectedExceptions = SAXException.class)
 435     public void testCheckDocumentBuilderFactory14() throws Exception {
 436         // Accesing default working directory.


 437         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 438         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 439         docBuilder.parse("");
 440     }
 441 
 442     /**
 443      * This tests for the parse (uri) with null uri as parameter should throw
 444      * IllegalArgumentException.
 445      * @throws Exception If any errors occur.
 446      *
 447      */
 448     @Test(expectedExceptions = IllegalArgumentException.class)
 449     public void testCheckDocumentBuilderFactory15() throws Exception {
 450         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 451         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 452         String uri = null;
 453         docBuilder.parse(uri);
 454     }
 455 
 456     /**


< prev index next >