< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2014, 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 jaxp.library.JAXPTestUtilities.FILE_SEP;
  27 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  28 import static org.testng.Assert.assertFalse;
  29 
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.IOException;
  33 
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 


  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 import org.w3c.dom.Document;
  41 import org.xml.sax.EntityResolver;
  42 import org.xml.sax.InputSource;
  43 import org.xml.sax.SAXException;
  44 
  45 /**
  46  * This checks for the methods of DocumentBuilder
  47  */
  48 public class DocumentBuilderImpl01 implements EntityResolver {
  49 
  50     /**
  51      * Provide DocumentBuilder.
  52      *
  53      * @throws ParserConfigurationException


  54      */
  55     @DataProvider(name = "builder-provider")
  56     public Object[][] getBuilder() throws ParserConfigurationException {
  57         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  58         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  59         return new Object[][] { { docBuilder } };
  60     }
  61 
  62     /**
  63      * Testcase to test the default functionality of isValidation method. Expect
  64      * to return false because not setting the validation.

  65      */
  66     @Test(dataProvider = "builder-provider")
  67     public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
  68         assertFalse(docBuilder.isValidating());
  69 
  70     }
  71 
  72     /**
  73      * Testcase to test the default functionality of isNamespaceAware method.

  74      */
  75     @Test(dataProvider = "builder-provider")
  76     public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
  77         assertFalse(docBuilder.isNamespaceAware());
  78     }
  79 
  80     /**
  81      * Testcase to test the parse(InputStream).


  82      */
  83     @Test(dataProvider = "builder-provider")
  84     public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) {
  85         try {
  86             Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml")));
  87         } catch (SAXException | IOException e) {
  88             failUnexpected(e);
  89         }
  90     }
  91 
  92     /**
  93      * Testcase to test the parse(File).



  94      */
  95     @Test(dataProvider = "builder-provider")
  96     public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) {
  97         try {
  98             Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml"));
  99         } catch (SAXException | IOException e) {
 100             failUnexpected(e);
 101         }
 102     }
 103 
 104     /**
 105      * Testcase to test the parse(InputStream,systemId).


 106      */
 107     @Test(dataProvider = "builder-provider")
 108     public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder) {
 109         try {
 110             Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl02.xml")), new File(TestUtils.XML_DIR).toURI()
 111                     .toASCIIString() + FILE_SEP);
 112         } catch (SAXException | IOException e) {
 113             failUnexpected(e);


 114         }
 115     }
 116 
 117     /**
 118      * Testcase to test the setEntityResolver.

 119      */
 120     @Test(dataProvider = "builder-provider")
 121     public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
 122         docBuilder.setEntityResolver(this);
 123         resolveEntity("publicId", "http://www.myhost.com/today");
 124     }
 125 












 126     public InputSource resolveEntity(String publicId, String systemId) {
 127         if (systemId.equals("http://www.myhost.com/today"))
 128             return new InputSource(systemId);
 129         else
 130             return null;
 131     }
 132 }
   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 jaxp.library.JAXPTestUtilities.FILE_SEP;

  27 import static org.testng.Assert.assertFalse;

  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.FilePermission;

  31 import javax.xml.parsers.DocumentBuilder;
  32 import javax.xml.parsers.DocumentBuilderFactory;
  33 import javax.xml.parsers.ParserConfigurationException;
  34 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
  35 import jaxp.library.JAXPFileReadOnlyBaseTest;
  36 import static org.testng.Assert.assertNotNull;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;

  39 import org.xml.sax.EntityResolver;
  40 import org.xml.sax.InputSource;

  41 
  42 /**
  43  * This checks for the methods of DocumentBuilder
  44  */
  45 public class DocumentBuilderImpl01 extends JAXPFileReadOnlyBaseTest 
  46             implements EntityResolver {
  47     /**
  48      * Provide DocumentBuilder.
  49      *
  50      * @return data provider has single DocumentBuilder.
  51      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  52      *         created which satisfies the configuration requested.
  53      */
  54     @DataProvider(name = "builder-provider")
  55     public Object[][] getBuilder() throws ParserConfigurationException {
  56         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  57         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  58         return new Object[][] { { docBuilder } };
  59     }
  60 
  61     /**
  62      * Test the default functionality of isValidation method. Expect
  63      * to return false because not setting the validation.
  64      * @param docBuilder document builder instance.
  65      */
  66     @Test(dataProvider = "builder-provider")
  67     public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
  68         assertFalse(docBuilder.isValidating());

  69     }
  70 
  71     /**
  72      * Test the default functionality of isNamespaceAware method.
  73      * @param docBuilder document builder instance.
  74      */
  75     @Test(dataProvider = "builder-provider")
  76     public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
  77         assertFalse(docBuilder.isNamespaceAware());
  78     }
  79 
  80     /**
  81      * Test the parse(InputStream).
  82      * @param docBuilder document builder instance.
  83      * @throws Exception If any errors occur.
  84      */
  85     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
  86     public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) 
  87             throws Exception {
  88         try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
  89                 "DocumentBuilderImpl01.xml"))) {
  90             assertNotNull(docBuilder.parse(fis));
  91         }
  92     }
  93 
  94     /**
  95      * Test the parse(File).
  96      * 
  97      * @param docBuilder document builder instance.
  98      * @throws Exception If any errors occur.
  99      */
 100     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
 101     public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) 
 102             throws Exception {
 103         assertNotNull(docBuilder.parse(new File(XML_DIR,
 104                 "DocumentBuilderImpl01.xml")));


 105     }
 106 
 107     /**
 108      * Test the parse(InputStream,systemId).
 109      * @param docBuilder document builder instance.
 110      * @throws Exception If any errors occur.
 111      */
 112     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
 113     public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
 114             throws Exception {
 115         setPermissions(new FilePermission(XML_DIR + "../-",
 116                 "read"));
 117         try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
 118                 "DocumentBuilderImpl02.xml"))) {
 119             assertNotNull(docBuilder.parse(fis, new File(XML_DIR).toURI()
 120                     .toASCIIString() + FILE_SEP));
 121         }
 122     }
 123 
 124     /**
 125      * Test the setEntityResolver.
 126      * @param docBuilder document builder instance.
 127      */
 128     @Test(dataProvider = "builder-provider")
 129     public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
 130         docBuilder.setEntityResolver(this);
 131         assertNotNull(resolveEntity("publicId", "http://www.myhost.com/today"));
 132     }
 133 
 134     /**
 135      * Allow the application to resolve external entities.
 136      * 
 137      * @param publicId The public identifier of the external entity
 138      *        being referenced, or null if none was supplied.
 139      * @param systemId The system identifier of the external entity
 140      *        being referenced.
 141      * @return An InputSource object describing the new input source,
 142      *         or null to request that the parser open a regular
 143      *         URI connection to the system identifier. 
 144      */
 145     @Override
 146     public InputSource resolveEntity(String publicId, String systemId) {
 147         if (systemId.equals("http://www.myhost.com/today"))
 148             return new InputSource(systemId);
 149         else
 150             return null;
 151     }
 152 }
< prev index next >