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 */ | 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 */ |