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 org.testng.Assert.assertFalse;

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

  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import static javax.xml.parsers.ptests.TestUtils.XML_DIR;
  36 import jaxp.library.JAXPBaseTest;
  37 import org.testng.annotations.AfterGroups;
  38 import org.testng.annotations.BeforeGroups;
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;

  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 extends JAXPBaseTest implements EntityResolver {

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

  71     }
  72 
  73     /**
  74      * Test the default functionality of isNamespaceAware method.
  75      * @param docBuilder document builder instance.
  76      */
  77     @Test(dataProvider = "builder-provider")
  78     public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
  79         assertFalse(docBuilder.isNamespaceAware());
  80     }
  81     
  82     /**
  83      * Save system property for restoring.
  84      */
  85     @BeforeGroups (groups = {"readLocalFiles"})
  86     public void setFilePermissions() {
  87         setPermissions(new FilePermission(XML_DIR + "/-", "read"));



  88     }
  89     
  90     /**
  91      * Restore the system property.
  92      */
  93     @AfterGroups (groups = {"readLocalFiles"})
  94     public void restoreFilePermissions() {
  95         setPermissions();
  96     }
  97 
  98     /**
  99      * Test the parse(InputStream).
 100      * @param docBuilder document builder instance.
 101      * @throws SAXException If any parse errors occur.
 102      * @throws IOException if the file exists but is a directory rather than
 103      *         a regular file, does not exist but cannot be created, or cannot 
 104      *         be opened for any other reason.
 105      */
 106     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
 107     public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) 
 108             throws SAXException, IOException {
 109         try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
 110                 "DocumentBuilderImpl01.xml"))) {
 111             docBuilder.parse(fis);
 112         }
 113     }
 114 
 115     /**
 116      * Test the parse(File).
 117      * 
 118      * @param docBuilder document builder instance.
 119      * @throws SAXException If any parse errors occur.
 120      * @throws IOException if the file exists but is a directory rather than
 121      *         a regular file, does not exist but cannot be created, or cannot 
 122      *         be opened for any other reason.
 123      */
 124     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
 125     public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) 
 126             throws SAXException, IOException {
 127         docBuilder.parse(new File(XML_DIR, "DocumentBuilderImpl01.xml"));
 128     }
 129 
 130     /**
 131      * Test the parse(InputStream,systemId).
 132      * @param docBuilder document builder instance.
 133      * @throws SAXException If any parse errors occur.
 134      * @throws IOException if the file exists but is a directory rather than
 135      *         a regular file, does not exist but cannot be created, or cannot 
 136      *         be opened for any other reason.
 137      */
 138     @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
 139     public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
 140             throws SAXException, IOException {
 141         setPermissions(new FilePermission(XML_DIR + "../-",
 142                 "read"));
 143         try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
 144                 "DocumentBuilderImpl02.xml"))) {
 145             docBuilder.parse(fis, new File(XML_DIR).toURI()
 146                     .toASCIIString() + FILE_SEP);


 147         }
 148     }
 149 
 150     /**
 151      * Test the setEntityResolver.
 152      * @param docBuilder document builder instance.
 153      */
 154     @Test(dataProvider = "builder-provider")
 155     public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
 156         docBuilder.setEntityResolver(this);
 157         resolveEntity("publicId", "http://www.myhost.com/today");
 158     }
 159 
 160     /**
 161      * Allow the application to resolve external entities.
 162      * 
 163      * @param publicId The public identifier of the external entity
 164      *        being referenced, or null if none was supplied.
 165      * @param systemId The system identifier of the external entity
 166      *        being referenced.
 167      * @return An InputSource object describing the new input source,
 168      *         or null to request that the parser open a regular
 169      *         URI connection to the system identifier. 
 170      */
 171     @Override
 172     public InputSource resolveEntity(String publicId, String systemId) {
 173         if (systemId.equals("http://www.myhost.com/today"))
 174             return new InputSource(systemId);
 175         else
 176             return null;
 177     }
 178 }
--- EOF ---