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.FilePermission;
  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 jaxp.library.JAXPBaseTest;
  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.AfterGroups;
  47 import org.testng.annotations.BeforeGroups;
  48 import org.testng.annotations.Test;
  49 import org.w3c.dom.Document;
  50 import org.xml.sax.InputSource;
  51 import org.xml.sax.SAXException;
  52 
  53 /**
  54  * Basic test cases for Transformer API
  55  */
  56 public class TransformerTest extends JAXPBaseTest {
  57     /**
  58      * XSLT file serves every test method.
  59      */
  60     private final static String TEST_XSL = XML_DIR + "cities.xsl";
  61 
  62 
  63     /**
  64      * Save system property for restoring.
  65      */
  66     @BeforeGroups (groups = {"readLocalFiles"})
  67     public void setFilePermissions() {
  68         setPermissions(new FilePermission(TEST_XSL, "read"));
  69     }
  70     
  71     /**
  72      * Restore the system property.
  73      */
  74     @AfterGroups (groups = {"readLocalFiles"})
  75     public void restoreFilePermissions() {
  76         setPermissions();
  77     }
  78 
  79     /**
  80      * This tests if newTransformer(StreamSource) method returns Transformer.
  81      * @throws TransformerConfigurationException If for some reason the
  82      *         TransformerHandler can not be created.
  83      */
  84     @Test (groups = {"readLocalFiles"})
  85     public void transformer01() throws TransformerConfigurationException {
  86         TransformerFactory tfactory = TransformerFactory.newInstance();
  87         StreamSource streamSource = new StreamSource(
  88                                     new File(TEST_XSL));
  89         Transformer transformer = tfactory.newTransformer(streamSource);
  90         assertNotNull(transformer);



  91     }
  92 
  93     /**
  94      * This tests if newTransformer(SAXSource) method returns Transformer.
  95      * @throws IOException if the file exists but is a directory rather than
  96      *         a regular file, does not exist but cannot be created, or cannot 
  97      *         be opened for any other reason.
  98      * @throws TransformerConfigurationException If for some reason the
  99      *         TransformerHandler can not be created.
 100      */
 101     @Test (groups = {"readLocalFiles"})
 102     public void transformer02() throws IOException, TransformerConfigurationException {
 103         try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
 104             TransformerFactory tfactory = TransformerFactory.newInstance();
 105             SAXSource saxSource = new SAXSource(new InputSource(fis));


 106             Transformer transformer = tfactory.newTransformer(saxSource);
 107             assertNotNull(transformer);


 108         }
 109     }
 110 
 111     /**
 112      * This tests if newTransformer(DOMSource) method returns Transformer.
 113      * 
 114      * @throws SAXException If any parse errors occur.
 115      * @throws IOException if the file exists but is a directory rather than
 116      *         a regular file, does not exist but cannot be created, or cannot 
 117      *         be opened for any other reason.
 118      * @throws TransformerConfigurationException If for some reason the
 119      *         TransformerHandler can not be created.
 120      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 121      *         created which satisfies the configuration requested.
 122      */
 123     @Test (groups = {"readLocalFiles"})
 124     public void transformer03() throws ParserConfigurationException, SAXException,
 125             IOException, TransformerConfigurationException {
 126         TransformerFactory tfactory = TransformerFactory.newInstance();
 127 
 128         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 129         dbf.setNamespaceAware(true);
 130         DocumentBuilder db = dbf.newDocumentBuilder();
 131         Document document = db.parse(new File(TEST_XSL));
 132         DOMSource domSource = new DOMSource(document);
 133 
 134         Transformer transformer = tfactory.newTransformer(domSource);
 135         assertNotNull(transformer);




 136     }
 137 
 138     /**
 139      * This tests set/get ErrorListener methods of Transformer.
 140      * 
 141      * @throws SAXException If any parse errors occur.
 142      * @throws IOException if the file exists but is a directory rather than
 143      *         a regular file, does not exist but cannot be created, or cannot 
 144      *         be opened for any other reason.
 145      * @throws TransformerConfigurationException If for some reason the
 146      *         TransformerHandler can not be created.
 147      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 148      *         created which satisfies the configuration requested.
 149      */
 150     @Test (groups = {"readLocalFiles"})
 151     public void transformer04() throws ParserConfigurationException, 
 152             SAXException, IOException, TransformerConfigurationException {
 153         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 154         dbf.setNamespaceAware(true);
 155         DocumentBuilder db = dbf.newDocumentBuilder();
 156         Document document = db.parse(new File(TEST_XSL));
 157         DOMSource domSource = new DOMSource(document);
 158 
 159         Transformer transformer = TransformerFactory.newInstance()
 160                 .newTransformer(domSource);
 161         transformer.setErrorListener(new MyErrorListener());
 162         assertNotNull(transformer.getErrorListener());
 163         assertTrue(transformer.getErrorListener() instanceof MyErrorListener);




 164     }
 165 
 166     /**
 167      * This tests getOutputProperties() method of Transformer.
 168      * 
 169      * @throws SAXException If any parse errors occur.
 170      * @throws IOException if the file exists but is a directory rather than
 171      *         a regular file, does not exist but cannot be created, or cannot 
 172      *         be opened for any other reason.
 173      * @throws TransformerConfigurationException If for some reason the
 174      *         TransformerHandler can not be created.
 175      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 176      *         created which satisfies the configuration requested.
 177      */
 178     @Test (groups = {"readLocalFiles"})
 179     public void transformer05() throws ParserConfigurationException,
 180             SAXException, IOException, TransformerConfigurationException {
 181         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 182         dbf.setNamespaceAware(true);
 183         DocumentBuilder db = dbf.newDocumentBuilder();
 184         Document document = db.parse(new File(TEST_XSL));
 185         DOMSource domSource = new DOMSource(document);
 186 
 187         Transformer transformer = TransformerFactory.newInstance().
 188                 newTransformer(domSource);
 189         Properties prop = transformer.getOutputProperties();
 190 
 191         assertEquals(prop.getProperty("indent"), "yes");
 192         assertEquals(prop.getProperty("method"), "xml");
 193         assertEquals(prop.getProperty("encoding"), "UTF-8");
 194         assertEquals(prop.getProperty("standalone"), "no");
 195         assertEquals(prop.getProperty("version"), "1.0");
 196         assertEquals(prop.getProperty("omit-xml-declaration"), "no");




 197     }
 198 
 199     /**
 200      * This tests getOutputProperty() method of Transformer.
 201      * 
 202      * @throws SAXException If any parse errors occur.
 203      * @throws IOException if the file exists but is a directory rather than
 204      *         a regular file, does not exist but cannot be created, or cannot 
 205      *         be opened for any other reason.
 206      * @throws TransformerConfigurationException If for some reason the
 207      *         TransformerHandler can not be created.
 208      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 209      *         created which satisfies the configuration requested.
 210      */
 211     @Test (groups = {"readLocalFiles"})
 212     public void transformer06() throws ParserConfigurationException, 
 213             SAXException, IOException, TransformerConfigurationException {
 214         TransformerFactory tfactory = TransformerFactory.newInstance();
 215 
 216         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 217         dbf.setNamespaceAware(true);
 218         DocumentBuilder db = dbf.newDocumentBuilder();
 219         Document document = db.parse(new File(TEST_XSL));
 220         DOMSource domSource = new DOMSource(document);
 221 
 222         Transformer transformer = tfactory.newTransformer(domSource);
 223         assertEquals(transformer.getOutputProperty("method"), "xml");




 224     }
 225 }
 226 
 227 /**
 228  * Simple ErrorListener print out all exception.
 229  */
 230 class MyErrorListener implements ErrorListener {
 231     /**
 232      * Prints exception when notification of a recoverable error.
 233      * @param e exception of a recoverable error.
 234      */
 235     @Override
 236     public void error (TransformerException e) {
 237         System.out.println(" In error" + e);
 238     }
 239 
 240     /**
 241      * Prints exception when notification of a warning.
 242      * @param e exception of a warning.
 243      */
 244     @Override
 245     public void warning (TransformerException e) {
 246         System.out.println(" In warning");
 247     }
 248 
 249     /**
 250      * Prints exception when notification of a fatal error.
 251      * @param e exception of a fatal error.
 252      */
 253     @Override
 254     public void fatalError (TransformerException e) throws
 255                 TransformerException {
 256         System.out.println(" In fatal");
 257     }
 258 }
--- EOF ---