1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.trax.sax; 21 22 import java.io.IOException; 23 import javax.xml.transform.Result; 24 import javax.xml.transform.Source; 25 import javax.xml.transform.Templates; 26 import javax.xml.transform.Transformer; 27 import javax.xml.transform.TransformerConfigurationException; 28 import javax.xml.transform.TransformerException; 29 import javax.xml.transform.TransformerFactory; 30 import javax.xml.transform.sax.SAXTransformerFactory; 31 import javax.xml.transform.sax.TransformerHandler; 32 import javax.xml.transform.stream.StreamResult; 33 import javax.xml.transform.stream.StreamSource; 34 import jaxp.library.JAXPFileBaseTest; 35 import static jaxp.library.JAXPTestUtilities.CLASS_DIR; 36 import static jaxp.library.JAXPTestUtilities.compareWithGold; 37 import static jaxp.library.JAXPTestUtilities.filenameToURL; 38 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 39 import static org.apache.qetest.trax.TraxConst.XML_DIR; 40 import static jaxp.library.JAXPTestUtilities.getNextFile; 41 import static org.testng.Assert.assertEquals; 42 import static org.testng.Assert.assertNotNull; 43 import static org.testng.Assert.assertNull; 44 import org.testng.annotations.Test; 45 46 /** 47 * API Coverage test for the TransformerHandler class of TRAX. 48 */ 49 public class TransformerHandlerAPITest extends JAXPFileBaseTest { 50 /** 51 * Nonsense systemId for various tests. 52 */ 53 private static final String NONSENSE_SYSTEMID = "file:///nonsense/system/id/"; 54 55 /** 56 * IllegalArgumentExceptionis thrown if result set as null. 57 * 58 * @throws TransformerConfigurationException Thrown in case of 59 * ServiceConfigurationError service configuration error or if the 60 * implementation is not available or cannot be instantiated. 61 */ 62 @Test(expectedExceptions = IllegalArgumentException.class) 63 public void negativeCase1() throws TransformerConfigurationException { 64 String outputFile = CLASS_DIR + "TransformerHandlerAPITest01.out"; 65 // Validate API's for an identity transformer 66 // No public constructor available: you must always ask 67 // a SAXTransformerFactory to give you one 68 SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); 69 70 // Basic construction of identity transformer 71 TransformerHandler tHandler = saxFactory.newTransformerHandler(); 72 73 // set/getSystemId API coverage 74 tHandler.setSystemId(NONSENSE_SYSTEMID); 75 tHandler.setSystemId(null); 76 77 // setResult API coverage 78 Result unusedResult = new StreamResult(outputFile); 79 tHandler.setResult(unusedResult); 80 tHandler.setResult(null); 81 } 82 83 /** 84 * IllegalArgumentExceptionis thrown if result set as null. 85 * 86 * @throws TransformerConfigurationException Thrown in case of 87 * ServiceConfigurationError service configuration error or if the 88 * implementation is not available or cannot be instantiated. 89 */ 90 @Test(expectedExceptions = IllegalArgumentException.class) 91 public void negativeCase2() throws TransformerConfigurationException { 92 String outputFile = getNextFile(this.getClass()); 93 94 // Validate API's for an identity transformer 95 // No public constructor available: you must always ask 96 // a SAXTransformerFactory to give you one 97 SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); 98 99 // Basic construction of identity transformer 100 TransformerHandler tHandler = saxFactory.newTransformerHandler(); 101 102 // set/getSystemId API coverage 103 tHandler.setSystemId(NONSENSE_SYSTEMID); 104 tHandler.setSystemId(null); 105 106 // setResult API coverage 107 Result unusedResult = new StreamResult(outputFile); 108 tHandler.setResult(unusedResult); 109 tHandler.setResult(null); 110 } 111 112 /** 113 * IllegalArgumentExceptionis thrown if result set as null. 114 * 115 * @throws TransformerConfigurationException Thrown in case of 116 * ServiceConfigurationError service configuration error or if the 117 * implementation is not available or cannot be instantiated. 118 */ 119 @Test(expectedExceptions = IllegalArgumentException.class) 120 public void negativeCase3() throws TransformerConfigurationException { 121 String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); 122 String outputFile = getNextFile(this.getClass()); 123 124 // Validate API's for a 'real' transformer, which is different code 125 SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); 126 // Basic construction of identity transformer 127 TransformerHandler tHandler = saxFactory.newTransformerHandler(new StreamSource(xslURI)); 128 assertNotNull(tHandler); 129 // getTemplates API coverage - simple 130 Transformer transformer = tHandler.getTransformer(); 131 assertNotNull(transformer); 132 // set/getSystemId API coverage 133 tHandler.setSystemId(NONSENSE_SYSTEMID); 134 assertEquals(tHandler.getSystemId(), NONSENSE_SYSTEMID); 135 tHandler.setSystemId(null); 136 assertNull(tHandler.getSystemId()); 137 138 // setResult API coverage 139 Result unusedResult = new StreamResult(outputFile); 140 tHandler.setResult(unusedResult); 141 tHandler.setResult(null); 142 } 143 144 /** 145 * Basic functionality of TransformerHandler. 146 * 147 * @throws TransformerException If an unrecoverable error occurs during the 148 * course of the transformation. 149 * @throws IOException if any I/O operation error. 150 */ 151 public void testCase2() throws TransformerException, IOException { 152 String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); 153 String xmlURI = XML_DIR + "SAXTest.xml"; 154 String goldFile = GOLDEN_DIR + "SAXTest.out"; 155 String outputURI = filenameToURL(CLASS_DIR + "SAXTest.out"); 156 157 SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); 158 // Validate an identity transformerHandler is valid and performs as an 159 // identity stylesheet 160 TransformerHandler transformerHandler = saxFactory.newTransformerHandler(); 161 Transformer transformer = transformerHandler.getTransformer(); 162 assertNotNull(transformer); 163 transformer.transform(new StreamSource(xmlURI), new StreamResult(xslURI)); 164 compareWithGold(xmlURI, outputURI); 165 166 167 // Validate newTransformerHandler(Source) works 168 transformer = transformerHandler.getTransformer(); 169 assertNotNull(transformer); 170 transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI)); 171 compareWithGold(goldFile, outputURI); 172 173 // Validate newTransformerHandler(Templates) works 174 Source xslSource = new StreamSource(xslURI); 175 Templates otherTemplates = saxFactory.newTemplates(xslSource); 176 transformerHandler = saxFactory.newTransformerHandler(otherTemplates); 177 transformer = transformerHandler.getTransformer(); 178 assertNotNull(transformer); 179 transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI)); 180 compareWithGold(goldFile, outputURI); 181 } 182 }