< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/URIResolverTest.java

Print this page

        

@@ -22,11 +22,11 @@
  */
 package javax.xml.transform.ptests;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.FilePermission;
 import java.io.IOException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Source;

@@ -38,22 +38,23 @@
 import javax.xml.transform.dom.DOMSource;
 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
-import static jaxp.library.JAXPTestUtilities.FILE_SEP;
-import static jaxp.library.JAXPTestUtilities.failUnexpected;
+import jaxp.library.JAXPBaseTest;
 import static org.testng.Assert.assertEquals;
+import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeGroups;
 import org.testng.annotations.Test;
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 /**
  * URIResolver should be invoked when transform happens.
  */
-public class URIResolverTest implements URIResolver {
+public class URIResolverTest extends JAXPBaseTest implements URIResolver {
     /**
      * System ID constant.
      */
     private final static String SYSTEM_ID = "file:///" + XML_DIR;
 

@@ -70,22 +71,29 @@
     /**
      * TEMP XML file.
      */
     private final static String XSL_TEMP_FILE = "temp/cities.xsl";
 
-
     /**
-     * expected Href.
+     * expected HREF.
      */
     private final String validateHref;
 
     /**
      * expected Base URI.
      */
     private final String validateBase;
 
     /**
+     * Default constructor for testng invocation.
+     */
+    public URIResolverTest(){
+        validateHref = null;
+        validateBase = null;
+    }
+
+    /**
      * Constructor for setting expected Href and expected Base URI.
      * @param validateHref expected Href
      * @param validateBase expected Base URI
      */
     public URIResolverTest(String validateHref, String validateBase){

@@ -107,169 +115,205 @@
         assertEquals(base, validateBase);
         return null;
     }
 
     /**
-     * This is to test the URIResolver.resolve() method when a transformer is
-     * created using StreamSource. xsl file has xsl:include in it
+     * Save system property for restoring.
      */
-    @Test
-    public static void resolver01() {
-        try {
+    @BeforeGroups (groups = {"readLocalFiles"})
+    public void setFilePermissions() {
+        setPermissions(new FilePermission(XML_DIR  + "-", "read"));
+    }
+    
+    /**
+     * Restore the system property.
+     */
+    @AfterGroups (groups = {"readLocalFiles"})
+    public void restoreFilePermissions() {
+        setPermissions();
+    }
+
+    /**
+     * This is to test the URIResolver.resolve() method when a transformer is
+     * created using StreamSource. style-sheet file has xsl:include in it.
+     * 
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     * @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"})
+    public static void resolver01() throws TransformerConfigurationException,
+            IOException {
+        try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)) {
             TransformerFactory tfactory = TransformerFactory.newInstance();
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             tfactory.setURIResolver(resolver);
 
-            StreamSource streamSource = new StreamSource(new FileInputStream(XSL_INCLUDE_FILE));
+            StreamSource streamSource = new StreamSource(fis);
             streamSource.setSystemId(SYSTEM_ID);
-
-            Transformer transformer = tfactory.newTransformer(streamSource);
-        } catch (FileNotFoundException | TransformerConfigurationException ex){
-            failUnexpected(ex);
+            tfactory.newTransformer(streamSource);
         }
     }
 
     /**
      * This is to test the URIResolver.resolve() method when a transformer is
-     * created using DOMSource. xsl file has xsl:include in it
-     */
-    @Test
-    public static void resolver02() {
-        try {
+     * created using DOMSource. style-sheet file has xsl:include in it.
+     * 
+     * @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.
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     * @throws ParserConfigurationException if a DocumentBuilder cannot be 
+     *         created which satisfies the configuration requested.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void resolver02() throws ParserConfigurationException, 
+            SAXException, TransformerConfigurationException, IOException {
             TransformerFactory tfactory = TransformerFactory.newInstance();
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             tfactory.setURIResolver(resolver);
 
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document document = db.parse(XSL_INCLUDE_FILE);
             DOMSource domSource = new DOMSource(document, SYSTEM_ID);
 
-            Transformer transformer = tfactory.newTransformer(domSource);
-        } catch (IOException | ParserConfigurationException
-                | TransformerConfigurationException | SAXException ex){
-            failUnexpected(ex);
-        }
+        tfactory.newTransformer(domSource);
     }
 
     /**
      * This is to test the URIResolver.resolve() method when a transformer is
-     * created using SAXSource. xsl file has xsl:include in it
-     */
-    @Test
-    public static void resolver03() {
-        try {
+     * created using SAXSource. style-sheet file has xsl:include in it.
+     * 
+     * @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.
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void resolver03() throws TransformerConfigurationException, 
+            IOException {
+        try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)){
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             TransformerFactory tfactory = TransformerFactory.newInstance();
             tfactory.setURIResolver(resolver);
-            InputSource is = new InputSource(new FileInputStream(XSL_INCLUDE_FILE));
+            InputSource is = new InputSource(fis);
             is.setSystemId(SYSTEM_ID);
             SAXSource saxSource = new SAXSource(is);
-
-            Transformer transformer = tfactory.newTransformer(saxSource);
-        } catch (FileNotFoundException |  TransformerConfigurationException ex){
-            failUnexpected(ex);
+            tfactory.newTransformer(saxSource);
         }
     }
 
     /**
      * This is to test the URIResolver.resolve() method when a transformer is
-     * created using StreamSource. xsl file has xsl:import in it
-     */
-    @Test
-    public static void resolver04() {
-        try {
+     * created using StreamSource. style-sheet file has xsl:import in it.
+     * 
+     * @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.
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void resolver04() throws TransformerConfigurationException, 
+            IOException {
+        try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)) {
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             TransformerFactory tfactory = TransformerFactory.newInstance();
             tfactory.setURIResolver(resolver);
-
-            StreamSource streamSource = new StreamSource(new FileInputStream(XSL_IMPORT_FILE));
+            StreamSource streamSource = new StreamSource(fis);
             streamSource.setSystemId(SYSTEM_ID);
-
-            Transformer transformer = tfactory.newTransformer(streamSource);
-        } catch (FileNotFoundException | TransformerConfigurationException ex){
-            failUnexpected(ex);
+            tfactory.newTransformer(streamSource);
         }
     }
 
     /**
      * This is to test the URIResolver.resolve() method when a transformer is
-     * created using DOMSource. xsl file has xsl:import in it
-     */
-    @Test
-    public static void resolver05() {
-        try {
+     * created using DOMSource. style-sheet file has xsl:import in it.
+     * 
+     * @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.
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     * @throws ParserConfigurationException if a DocumentBuilder cannot be 
+     *         created which satisfies the configuration requested.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void resolver05() throws TransformerConfigurationException, 
+            IOException, SAXException, ParserConfigurationException {
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             TransformerFactory tfactory = TransformerFactory.newInstance();
             tfactory.setURIResolver(resolver);
-
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document document = db.parse(new File(XSL_IMPORT_FILE));
             DOMSource domSource = new DOMSource(document, SYSTEM_ID);
-
-            Transformer transformer = tfactory.newTransformer(domSource);
-        } catch (ParserConfigurationException | SAXException | IOException
-                | TransformerConfigurationException ex){
-            failUnexpected(ex);
-        }
-
+        tfactory.newTransformer(domSource);
     }
 
     /**
      * This is to test the URIResolver.resolve() method when a transformer is
-     * created using SAXSource. xsl file has xsl:import in it
-     */
-    @Test
-    public static void resolver06() {
-        try {
+     * created using SAXSource. style-sheet file has xsl:import in it.
+     * 
+     * @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.
+     * @throws TransformerConfigurationException If for some reason the
+     *         TransformerHandler can not be created.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void resolver06() throws IOException, TransformerConfigurationException {
+        try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)){
             URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
             TransformerFactory tfactory = TransformerFactory.newInstance();
             tfactory.setURIResolver(resolver);
-
-            InputSource is = new InputSource(new FileInputStream(XSL_IMPORT_FILE));
+            InputSource is = new InputSource(fis);
             is.setSystemId(SYSTEM_ID);
             SAXSource saxSource = new SAXSource(is);
-
-            Transformer transformer = tfactory.newTransformer(saxSource);
-        } catch (FileNotFoundException | TransformerConfigurationException ex){
-            failUnexpected(ex);
+            tfactory.newTransformer(saxSource);
         }
-
     }
 
     /**
      * This is to test the URIResolver.resolve() method when there is an error
      * in the file.
-     */
-    @Test
-    public static void docResolver01() {
-        try {
+     * 
+     * @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.
+     * @throws TransformerException If an unrecoverable error occurs during the
+     *         course of the transformation..
+     * @throws ParserConfigurationException if a DocumentBuilder cannot be 
+     *         created which satisfies the configuration requested.
+     */
+    @Test (groups = {"readLocalFiles"})
+    public static void docResolver01() throws TransformerException, 
+            IOException, SAXException, ParserConfigurationException {
+        try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) {
             URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID);
-            TransformerFactory tfactory = TransformerFactory.newInstance();
-
-            StreamSource streamSource = new StreamSource(
-                    new FileInputStream(XML_DIR + FILE_SEP + "doctest.xsl"));
+            StreamSource streamSource = new StreamSource(fis);
             streamSource.setSystemId(SYSTEM_ID);
-            System.err.println(streamSource.getSystemId());
 
-            Transformer transformer = tfactory.newTransformer(streamSource);
+            Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);
             transformer.setURIResolver(resolver);
 
-            File f = new File(XML_DIR + FILE_SEP + "myFake.xml");
-            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder builder = factory.newDocumentBuilder();
-            Document document = builder.parse(f);
+            File f = new File(XML_DIR + "myFake.xml");
+            Document document = DocumentBuilderFactory.newInstance().
+                    newDocumentBuilder().parse(f);
 
             // Use a Transformer for output
             DOMSource source = new DOMSource(document);
-            System.err.println("Ignore the following output -- just dumping it here");
             StreamResult result = new StreamResult(System.err);
             transformer.transform(source, result);
-        } catch (IOException | ParserConfigurationException | SAXException
-                | TransformerException ex) {
-            failUnexpected(ex);
         }
     }
 }
< prev index next >