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 */ 185 @Override 186 public void warning (TransformerException e) { 187 System.out.println(" In warning"); 188 } 189 190 /** 191 * Prints exception when notification of a fatal error. 192 * @param e exception of a fatal error. 193 */ 194 @Override 195 public void fatalError (TransformerException e) throws 196 TransformerException { 197 System.out.println(" In fatal"); 198 } 199 }