< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2003, 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 package javax.xml.transform.ptests;
  24 





  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 import java.util.Properties;

  28 import javax.xml.parsers.DocumentBuilder;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.transform.ErrorListener;
  31 import javax.xml.transform.Transformer;
  32 import javax.xml.transform.TransformerConfigurationException;
  33 import javax.xml.transform.TransformerException;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMSource;
  36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  37 import javax.xml.transform.sax.SAXSource;
  38 import javax.xml.transform.stream.StreamSource;
  39 import jaxp.library.JAXPFileReadOnlyBaseTest;
  40 import static org.testng.Assert.assertEquals;
  41 import static org.testng.Assert.assertNotNull;
  42 import static org.testng.Assert.assertTrue;
  43 import org.testng.annotations.Test;
  44 import org.w3c.dom.Document;
  45 import org.xml.sax.InputSource;
  46 
  47 /**
  48  * Basic test cases for Transformer API
  49  */
  50 public class TransformerTest extends JAXPFileReadOnlyBaseTest {

  51     /**
  52      * XSLT file serves every test method.
  53      */
  54     private final static String TEST_XSL = XML_DIR + "cities.xsl";
  55 
  56     /**
  57      * This tests if newTransformer(StreamSource) method returns Transformer.
  58      * @throws TransformerConfigurationException If for some reason the
  59      *         TransformerHandler can not be created.
  60      */
  61     @Test (groups = {"readLocalFiles"})
  62     public void transformer01() throws TransformerConfigurationException {
  63         TransformerFactory tfactory = TransformerFactory.newInstance();
  64         StreamSource streamSource = new StreamSource(
  65                                     new File(TEST_XSL));
  66         Transformer transformer = tfactory.newTransformer(streamSource);
  67         assertNotNull(transformer);
  68     }
  69 
  70     /**
  71      * This tests if newTransformer(SAXSource) method returns Transformer.
  72      * @throws Exception If any errors occur.
  73      */
  74     @Test (groups = {"readLocalFiles"})
  75     public void transformer02() throws Exception {
  76         try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
  77             TransformerFactory tfactory = TransformerFactory.newInstance();
  78             SAXSource saxSource = new SAXSource(new InputSource(fis));
  79             Transformer transformer = tfactory.newTransformer(saxSource);
  80             assertNotNull(transformer);
  81         }
  82     }
  83 
  84     /**
  85      * This tests if newTransformer(DOMSource) method returns Transformer.
  86      *
  87      * @throws Exception If any errors occur.
  88      */
  89     @Test (groups = {"readLocalFiles"})
  90     public void transformer03() throws Exception {
  91         TransformerFactory tfactory = TransformerFactory.newInstance();
  92 
  93         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  94         dbf.setNamespaceAware(true);
  95         DocumentBuilder db = dbf.newDocumentBuilder();
  96         Document document = db.parse(new File(TEST_XSL));
  97         DOMSource domSource = new DOMSource(document);
  98 
  99         Transformer transformer = tfactory.newTransformer(domSource);
 100         assertNotNull(transformer);
 101     }
 102 
 103     /**
 104      * This tests set/get ErrorListener methods of Transformer.
 105      *
 106      * @throws Exception If any errors occur.
 107      */
 108     @Test (groups = {"readLocalFiles"})
 109     public void transformer04() throws Exception {
 110         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 111         dbf.setNamespaceAware(true);
 112         DocumentBuilder db = dbf.newDocumentBuilder();
 113         Document document = db.parse(new File(TEST_XSL));
 114         DOMSource domSource = new DOMSource(document);
 115 
 116         Transformer transformer = TransformerFactory.newInstance()
 117                 .newTransformer(domSource);
 118         transformer.setErrorListener(new MyErrorListener());
 119         assertNotNull(transformer.getErrorListener());
 120         assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
 121     }
 122 
 123     /**
 124      * This tests getOutputProperties() method of Transformer.
 125      *
 126      * @throws Exception If any errors occur.
 127      */
 128     @Test (groups = {"readLocalFiles"})
 129     public void transformer05() throws Exception {
 130         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 131         dbf.setNamespaceAware(true);
 132         DocumentBuilder db = dbf.newDocumentBuilder();
 133         Document document = db.parse(new File(TEST_XSL));
 134         DOMSource domSource = new DOMSource(document);
 135 
 136         Transformer transformer = TransformerFactory.newInstance().
 137                 newTransformer(domSource);
 138         Properties prop = transformer.getOutputProperties();
 139 
 140         assertEquals(prop.getProperty("indent"), "yes");
 141         assertEquals(prop.getProperty("method"), "xml");
 142         assertEquals(prop.getProperty("encoding"), "UTF-8");
 143         assertEquals(prop.getProperty("standalone"), "no");
 144         assertEquals(prop.getProperty("version"), "1.0");
 145         assertEquals(prop.getProperty("omit-xml-declaration"), "no");
 146     }
 147 
 148     /**
 149      * This tests getOutputProperty() method of Transformer.
 150      *
 151      * @throws Exception If any errors occur.
 152      */
 153     @Test (groups = {"readLocalFiles"})
 154     public void transformer06() throws Exception {
 155         TransformerFactory tfactory = TransformerFactory.newInstance();
 156 
 157         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 158         dbf.setNamespaceAware(true);
 159         DocumentBuilder db = dbf.newDocumentBuilder();
 160         Document document = db.parse(new File(TEST_XSL));
 161         DOMSource domSource = new DOMSource(document);
 162 
 163         Transformer transformer = tfactory.newTransformer(domSource);
 164         assertEquals(transformer.getOutputProperty("method"), "xml");
 165     }
 166 }
 167 
 168 /**
 169  * Simple ErrorListener print out all exception.
 170  */
 171 class MyErrorListener implements ErrorListener {
 172     /**
 173      * Prints exception when notification of a recoverable error.


   1 /*
   2  * Copyright (c) 2003, 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 package javax.xml.transform.ptests;
  24 
  25 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertNotNull;
  28 import static org.testng.Assert.assertTrue;
  29 
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.util.Properties;
  33 
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.transform.ErrorListener;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerConfigurationException;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMSource;

  42 import javax.xml.transform.sax.SAXSource;
  43 import javax.xml.transform.stream.StreamSource;
  44 
  45 import org.testng.annotations.Listeners;


  46 import org.testng.annotations.Test;
  47 import org.w3c.dom.Document;
  48 import org.xml.sax.InputSource;
  49 
  50 /**
  51  * Basic test cases for Transformer API
  52  */
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class TransformerTest {
  55     /**
  56      * XSLT file serves every test method.
  57      */
  58     private final static String TEST_XSL = XML_DIR + "cities.xsl";
  59 
  60     /**
  61      * This tests if newTransformer(StreamSource) method returns Transformer.
  62      * @throws TransformerConfigurationException If for some reason the
  63      *         TransformerHandler can not be created.
  64      */
  65     @Test 
  66     public void transformer01() throws TransformerConfigurationException {
  67         TransformerFactory tfactory = TransformerFactory.newInstance();
  68         StreamSource streamSource = new StreamSource(
  69                                     new File(TEST_XSL));
  70         Transformer transformer = tfactory.newTransformer(streamSource);
  71         assertNotNull(transformer);
  72     }
  73 
  74     /**
  75      * This tests if newTransformer(SAXSource) method returns Transformer.
  76      * @throws Exception If any errors occur.
  77      */
  78     @Test 
  79     public void transformer02() throws Exception {
  80         try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
  81             TransformerFactory tfactory = TransformerFactory.newInstance();
  82             SAXSource saxSource = new SAXSource(new InputSource(fis));
  83             Transformer transformer = tfactory.newTransformer(saxSource);
  84             assertNotNull(transformer);
  85         }
  86     }
  87 
  88     /**
  89      * This tests if newTransformer(DOMSource) method returns Transformer.
  90      *
  91      * @throws Exception If any errors occur.
  92      */
  93     @Test 
  94     public void transformer03() throws Exception {
  95         TransformerFactory tfactory = TransformerFactory.newInstance();
  96 
  97         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  98         dbf.setNamespaceAware(true);
  99         DocumentBuilder db = dbf.newDocumentBuilder();
 100         Document document = db.parse(new File(TEST_XSL));
 101         DOMSource domSource = new DOMSource(document);
 102 
 103         Transformer transformer = tfactory.newTransformer(domSource);
 104         assertNotNull(transformer);
 105     }
 106 
 107     /**
 108      * This tests set/get ErrorListener methods of Transformer.
 109      *
 110      * @throws Exception If any errors occur.
 111      */
 112     @Test 
 113     public void transformer04() throws Exception {
 114         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 115         dbf.setNamespaceAware(true);
 116         DocumentBuilder db = dbf.newDocumentBuilder();
 117         Document document = db.parse(new File(TEST_XSL));
 118         DOMSource domSource = new DOMSource(document);
 119 
 120         Transformer transformer = TransformerFactory.newInstance()
 121                 .newTransformer(domSource);
 122         transformer.setErrorListener(new MyErrorListener());
 123         assertNotNull(transformer.getErrorListener());
 124         assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
 125     }
 126 
 127     /**
 128      * This tests getOutputProperties() method of Transformer.
 129      *
 130      * @throws Exception If any errors occur.
 131      */
 132     @Test 
 133     public void transformer05() throws Exception {
 134         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 135         dbf.setNamespaceAware(true);
 136         DocumentBuilder db = dbf.newDocumentBuilder();
 137         Document document = db.parse(new File(TEST_XSL));
 138         DOMSource domSource = new DOMSource(document);
 139 
 140         Transformer transformer = TransformerFactory.newInstance().
 141                 newTransformer(domSource);
 142         Properties prop = transformer.getOutputProperties();
 143 
 144         assertEquals(prop.getProperty("indent"), "yes");
 145         assertEquals(prop.getProperty("method"), "xml");
 146         assertEquals(prop.getProperty("encoding"), "UTF-8");
 147         assertEquals(prop.getProperty("standalone"), "no");
 148         assertEquals(prop.getProperty("version"), "1.0");
 149         assertEquals(prop.getProperty("omit-xml-declaration"), "no");
 150     }
 151 
 152     /**
 153      * This tests getOutputProperty() method of Transformer.
 154      *
 155      * @throws Exception If any errors occur.
 156      */
 157     @Test 
 158     public void transformer06() throws Exception {
 159         TransformerFactory tfactory = TransformerFactory.newInstance();
 160 
 161         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 162         dbf.setNamespaceAware(true);
 163         DocumentBuilder db = dbf.newDocumentBuilder();
 164         Document document = db.parse(new File(TEST_XSL));
 165         DOMSource domSource = new DOMSource(document);
 166 
 167         Transformer transformer = tfactory.newTransformer(domSource);
 168         assertEquals(transformer.getOutputProperty("method"), "xml");
 169     }
 170 }
 171 
 172 /**
 173  * Simple ErrorListener print out all exception.
 174  */
 175 class MyErrorListener implements ErrorListener {
 176     /**
 177      * Prints exception when notification of a recoverable error.


< prev index next >