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