< prev index next >

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

Print this page

        

@@ -22,109 +22,155 @@
  */
 
 package javax.xml.parsers.ptests;
 
 import static jaxp.library.JAXPTestUtilities.FILE_SEP;
-import static jaxp.library.JAXPTestUtilities.failUnexpected;
 import static org.testng.Assert.assertFalse;
-
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FilePermission;
 import java.io.IOException;
-
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
-
+import static javax.xml.parsers.ptests.TestUtils.XML_DIR;
+import jaxp.library.JAXPBaseTest;
+import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeGroups;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
-import org.w3c.dom.Document;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 /**
  * This checks for the methods of DocumentBuilder
  */
-public class DocumentBuilderImpl01 implements EntityResolver {
-
+public class DocumentBuilderImpl01 extends JAXPBaseTest implements EntityResolver {
     /**
      * 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")
     public Object[][] getBuilder() throws ParserConfigurationException {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
         return new Object[][] { { docBuilder } };
     }
 
     /**
-     * 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.
+     * @param docBuilder document builder instance.
      */
     @Test(dataProvider = "builder-provider")
     public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
         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")
     public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
         assertFalse(docBuilder.isNamespaceAware());
     }
 
     /**
-     * Testcase to test the parse(InputStream).
+     * Save system property for restoring.
      */
-    @Test(dataProvider = "builder-provider")
-    public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) {
-        try {
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml")));
-        } catch (SAXException | IOException e) {
-            failUnexpected(e);
+    @BeforeGroups (groups = {"readLocalFiles"})
+    public void setFilePermissions() {
+        setPermissions(new FilePermission(XML_DIR + "/-", "read"));
         }
+    
+    /**
+     * Restore the system property.
+     */
+    @AfterGroups (groups = {"readLocalFiles"})
+    public void restoreFilePermissions() {
+        setPermissions();
     }
 
     /**
-     * Testcase to test the parse(File).
+     * Test the parse(InputStream).
+     * @param docBuilder document builder instance.
+     * @throws SAXException If any parse errors occur.
+     * @throws IOException if the file exists but is a directory rather than
+     *         a regular file, does not exist but cannot be created, or cannot 
+     *         be opened for any other reason.
      */
-    @Test(dataProvider = "builder-provider")
-    public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) {
-        try {
-            Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderImpl01.xml"));
-        } catch (SAXException | IOException e) {
-            failUnexpected(e);
+    @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
+    public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder) 
+            throws SAXException, IOException {
+        try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
+                "DocumentBuilderImpl01.xml"))) {
+            docBuilder.parse(fis);
         }
     }
 
     /**
-     * Testcase to test the parse(InputStream,systemId).
-     */
-    @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()
+     * Test the parse(File).
+     * 
+     * @param docBuilder document builder instance.
+     * @throws SAXException If any parse errors occur.
+     * @throws IOException if the file exists but is a directory rather than
+     *         a regular file, does not exist but cannot be created, or cannot 
+     *         be opened for any other reason.
+     */
+    @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
+    public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder) 
+            throws SAXException, IOException {
+        docBuilder.parse(new File(XML_DIR, "DocumentBuilderImpl01.xml"));
+    }
+
+    /**
+     * Test the parse(InputStream,systemId).
+     * @param docBuilder document builder instance.
+     * @throws SAXException If any parse errors occur.
+     * @throws IOException if the file exists but is a directory rather than
+     *         a regular file, does not exist but cannot be created, or cannot 
+     *         be opened for any other reason.
+     */
+    @Test(groups = {"readLocalFiles"}, dataProvider = "builder-provider")
+    public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
+            throws SAXException, IOException {
+        setPermissions(new FilePermission(XML_DIR + "../-",
+                "read"));
+        try (FileInputStream fis = new FileInputStream(new File(XML_DIR, 
+                "DocumentBuilderImpl02.xml"))) {
+            docBuilder.parse(fis, new File(XML_DIR).toURI()
                     .toASCIIString() + FILE_SEP);
-        } catch (SAXException | IOException e) {
-            failUnexpected(e);
         }
     }
 
     /**
-     * Testcase to test the setEntityResolver.
+     * Test the setEntityResolver.
+     * @param docBuilder document builder instance.
      */
     @Test(dataProvider = "builder-provider")
     public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
         docBuilder.setEntityResolver(this);
         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) {
         if (systemId.equals("http://www.myhost.com/today"))
             return new InputSource(systemId);
         else
             return null;
< prev index next >