< prev index next >

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

Print this page


   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 package javax.xml.transform.ptests;
  24 
  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileNotFoundException;
  28 import java.io.IOException;
  29 import java.util.Properties;
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.transform.ErrorListener;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.dom.DOMSource;
  39 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  40 import javax.xml.transform.sax.SAXSource;
  41 import javax.xml.transform.stream.StreamSource;
  42 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  43 import static org.testng.Assert.assertEquals;
  44 import static org.testng.Assert.assertNotNull;
  45 import static org.testng.Assert.assertTrue;
  46 import org.testng.annotations.Test;
  47 import org.w3c.dom.Document;
  48 import org.xml.sax.InputSource;
  49 import org.xml.sax.SAXException;
  50 
  51 /**
  52  * Basic test cases for Transformer API
  53  */
  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      */
  63     @Test
  64     public void transformer01() {
  65         try {
  66             TransformerFactory tfactory = TransformerFactory.newInstance();
  67             StreamSource streamSource = new StreamSource(
  68                                         new File(TEST_XSL));
  69             Transformer transformer = tfactory.newTransformer(streamSource);
  70             assertNotNull(transformer);
  71         } catch (TransformerConfigurationException ex){
  72             failUnexpected(ex);
  73         }
  74     }
  75 
  76     /**
  77      * This tests if newTransformer(SAXSource) method returns Transformer

  78      */
  79     @Test
  80     public void transformer02() {
  81         try {
  82             TransformerFactory tfactory = TransformerFactory.newInstance();
  83             InputSource is = new InputSource(
  84                         new FileInputStream(TEST_XSL));
  85             SAXSource saxSource = new SAXSource(is);
  86             Transformer transformer = tfactory.newTransformer(saxSource);
  87             assertNotNull(transformer);
  88         } catch (TransformerConfigurationException | FileNotFoundException ex){
  89             failUnexpected(ex);
  90         }
  91     }
  92 
  93     /**
  94      * This tests if newTransformer(DOMSource) method returns Transformer


  95      */
  96     @Test
  97     public void transformer03() {
  98         try {
  99             TransformerFactory tfactory = TransformerFactory.newInstance();
 100 
 101             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 102             dbf.setNamespaceAware(true);
 103             DocumentBuilder db = dbf.newDocumentBuilder();
 104             Document document = db.parse(new File(TEST_XSL));
 105             DOMSource domSource = new DOMSource(document);
 106 
 107             Transformer transformer = tfactory.newTransformer(domSource);
 108             assertNotNull(transformer);
 109         } catch (TransformerConfigurationException | IOException
 110                 | ParserConfigurationException | SAXException ex){
 111             failUnexpected(ex);
 112         }
 113     }
 114 
 115     /**
 116      * This tests set/get ErrorListener methods of Transformer


 117      */
 118     @Test
 119     public void transformer04() {
 120         try {
 121             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 122             dbf.setNamespaceAware(true);
 123             DocumentBuilder db = dbf.newDocumentBuilder();
 124             Document document = db.parse(new File(TEST_XSL));
 125             DOMSource domSource = new DOMSource(document);
 126 
 127             Transformer transformer = TransformerFactory.newInstance()
 128                     .newTransformer(domSource);
 129             transformer.setErrorListener(new MyErrorListener());
 130             assertNotNull(transformer.getErrorListener());
 131             assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
 132         } catch (IOException | IllegalArgumentException | ParserConfigurationException
 133                 | TransformerConfigurationException | SAXException ex){
 134             failUnexpected(ex);
 135         }
 136     }
 137 
 138     /**
 139      * This tests getOutputProperties() method of Transformer


 140      */
 141     @Test
 142     public void transformer05() {
 143         try {
 144             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 145             dbf.setNamespaceAware(true);
 146             DocumentBuilder db = dbf.newDocumentBuilder();
 147             Document document = db.parse(new File(TEST_XSL));
 148             DOMSource domSource = new DOMSource(document);
 149 
 150             Transformer transformer = TransformerFactory.newInstance().
 151                     newTransformer(domSource);
 152             Properties prop = transformer.getOutputProperties();
 153 
 154             assertEquals(prop.getProperty("indent"), "yes");
 155             assertEquals(prop.getProperty("method"), "xml");
 156             assertEquals(prop.getProperty("encoding"), "UTF-8");
 157             assertEquals(prop.getProperty("standalone"), "no");
 158             assertEquals(prop.getProperty("version"), "1.0");
 159             assertEquals(prop.getProperty("omit-xml-declaration"), "no");
 160         } catch (ParserConfigurationException | SAXException | IOException
 161                 | TransformerConfigurationException ex){
 162             failUnexpected(ex);
 163         }
 164     }
 165 
 166     /**
 167      * This tests getOutputProperty() method of Transformer


 168      */
 169     @Test
 170     public void transformer06() {
 171         try {
 172             TransformerFactory tfactory = TransformerFactory.newInstance();
 173 
 174             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 175             dbf.setNamespaceAware(true);
 176             DocumentBuilder db = dbf.newDocumentBuilder();
 177             Document document = db.parse(new File(TEST_XSL));
 178             DOMSource domSource = new DOMSource(document);
 179 
 180             Transformer transformer = tfactory.newTransformer(domSource);
 181             assertEquals(transformer.getOutputProperty("method"), "xml");
 182         } catch (ParserConfigurationException | SAXException | IOException
 183                 | TransformerConfigurationException | IllegalArgumentException ex){
 184             failUnexpected(ex);
 185         }
 186     }
 187 }
 188 
 189 /**
 190  * Simple ErrorListener print out all exception.
 191  */
 192 class MyErrorListener implements ErrorListener {
 193     /**
 194      * Prints exception when notification of a recoverable error.
 195      * @param e exception of a recoverable error.
 196      */
 197     @Override
 198     public void error (TransformerException e) {
 199         System.out.println(" In error" + e);
 200     }
 201 
 202     /**
 203      * Prints exception when notification of a warning.
 204      * @param e exception of a warning.
 205      */
   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.
 174      * @param e exception of a recoverable error.
 175      */
 176     @Override
 177     public void error (TransformerException e) {
 178         System.out.println(" In error" + e);
 179     }
 180 
 181     /**
 182      * Prints exception when notification of a warning.
 183      * @param e exception of a warning.
 184      */
< prev index next >