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 */ 206 @Override 207 public void warning (TransformerException e) { 208 System.out.println(" In warning"); 209 } 210 211 /** 212 * Prints exception when notification of a fatal error. 213 * @param e exception of a fatal error. 214 */ 215 @Override 216 public void fatalError (TransformerException e) throws 217 TransformerException { 218 System.out.println(" In fatal"); 219 } 220 }