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.parsers.ptests.ParserTestConst.XML_DIR;
  27 import static jaxp.library.JAXPTestUtilities.FILE_SEP;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertNotNull;
  30 
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FilePermission;
  34 
  35 import javax.xml.parsers.DocumentBuilder;
  36 import javax.xml.parsers.DocumentBuilderFactory;
  37 import javax.xml.parsers.ParserConfigurationException;
  38 
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Listeners;
  41 import org.testng.annotations.Test;
  42 import org.xml.sax.EntityResolver;
  43 import org.xml.sax.InputSource;
  44 
  45 /**
  46  * This checks for the methods of DocumentBuilder
  47  */
  48 @Listeners({jaxp.library.FilePolicy.class})
  49 public class DocumentBuilderImpl01 implements EntityResolver {
  50     /**
  51      * Provide DocumentBuilder.
  52      *
  53      * @return data provider has single DocumentBuilder.
  54      * @throws ParserConfigurationException if a DocumentBuilder cannot be
  55      *         created which satisfies the configuration requested.
  56      */
  57     @DataProvider(name = "builder-provider")
  58     public Object[][] getBuilder() throws ParserConfigurationException {
  59         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  60         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  61         return new Object[][] { { docBuilder } };
  62     }
  63 
  64     /**
  65      * Test the default functionality of isValidation method. Expect
  66      * to return false because not setting the validation.
  67      * @param docBuilder document builder instance.
  68      */
  69     @Test(dataProvider = "builder-provider")
  70     public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
  71         assertFalse(docBuilder.isValidating());
  72     }
  73 
  74     /**
  75      * Test the default functionality of isNamespaceAware method.
  76      * @param docBuilder document builder instance.
  77      */
  78     @Test(dataProvider = "builder-provider")
  79     public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
  80         assertFalse(docBuilder.isNamespaceAware());
  81     }
  82 
  83     /**
  84      * Test the parse(InputStream).
  85      * @param docBuilder document builder instance.
  86      * @throws Exception If any errors occur.
  87      */
  88     @Test(dataProvider = "builder-provider")
  89     public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder)
  90             throws Exception {
  91         try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
  92                 "DocumentBuilderImpl01.xml"))) {
  93             assertNotNull(docBuilder.parse(fis));
  94         }
  95     }
  96 
  97     /**
  98      * Test the parse(File).
  99      *
 100      * @param docBuilder document builder instance.
 101      * @throws Exception If any errors occur.
 102      */
 103     @Test(dataProvider = "builder-provider")
 104     public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder)
 105             throws Exception {
 106         assertNotNull(docBuilder.parse(new File(XML_DIR,
 107                 "DocumentBuilderImpl01.xml")));
 108     }
 109 
 110     /**
 111      * Test the parse(InputStream,systemId).
 112      * @param docBuilder document builder instance.
 113      * @throws Exception If any errors occur.
 114      */
 115     @Test(dataProvider = "builder-provider")
 116     public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
 117             throws Exception {
 118         try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
 119                 "DocumentBuilderImpl02.xml"))) {
 120             assertNotNull(docBuilder.parse(fis, new File(XML_DIR).toURI()
 121                     .toASCIIString() + FILE_SEP));
 122         }
 123     }
 124 
 125     /**
 126      * Test the setEntityResolver.
 127      * @param docBuilder document builder instance.
 128      */
 129     @Test(dataProvider = "builder-provider")
 130     public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
 131         docBuilder.setEntityResolver(this);
 132         assertNotNull(resolveEntity("publicId", "http://www.myhost.com/today"));
 133     }
 134 
 135     /**
 136      * Allow the application to resolve external entities.
 137      *
 138      * @param publicId The public identifier of the external entity
 139      *        being referenced, or null if none was supplied.
 140      * @param systemId The system identifier of the external entity
 141      *        being referenced.
 142      * @return An InputSource object describing the new input source,
 143      *         or null to request that the parser open a regular
 144      *         URI connection to the system identifier.
 145      */
 146     @Override
 147     public InputSource resolveEntity(String publicId, String systemId) {
 148         if (systemId.equals("http://www.myhost.com/today"))
 149             return new InputSource(systemId);
 150         else
 151             return null;
 152     }
 153 }