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.*; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import javax.xml.parsers.*; 32 import javax.xml.transform.*; 33 import javax.xml.transform.dom.*; 34 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 35 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 37 import javax.xml.transform.stream.*; 38 import static jaxp.library.JAXPTestUtilities.compareWithGold; 39 import static jaxp.library.JAXPTestUtilities.failCleanup; 40 import static jaxp.library.JAXPTestUtilities.failUnexpected; 41 import static org.testng.Assert.assertTrue; 42 import org.testng.annotations.Test; 43 import org.w3c.dom.*; 44 import org.xml.sax.SAXException; 45 46 /** 47 * Class containing the test cases for TransformerFactory API's 48 * getAssociatedStyleSheet method. 49 */ 50 public class TransformerFactoryTest { 51 /** 52 * This test case checks for the getAssociatedStylesheet method 53 * of TransformerFactory. 54 * The style sheet returned is then copied to an tfactory01.out 55 * It will then be verified to see if it matches the golden files 56 */ 57 @Test 58 public void tfactory01() { 59 String outputFile = CLASS_DIR + "tfactory01.out"; 60 String goldFile = GOLDEN_DIR + "tfactory01GF.out"; 61 String xmlFile = XML_DIR + "TransformerFactoryTest.xml"; 62 String xmlURI = "file:///" + XML_DIR; 63 64 try (FileInputStream fis = new FileInputStream(xmlFile); 65 FileOutputStream fos = new FileOutputStream(outputFile);) { 66 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 67 68 DocumentBuilder db = dbf.newDocumentBuilder(); 69 Document doc = db.parse(fis, xmlURI); 70 DOMSource domSource = new DOMSource(doc); 71 domSource.setSystemId(xmlURI); 72 StreamResult streamResult = new StreamResult(fos); 73 TransformerFactory tFactory = TransformerFactory.newInstance(); 74 75 Source s = tFactory.getAssociatedStylesheet(domSource, "screen", 76 "Modern", null); 77 Transformer t = tFactory.newTransformer(); 78 t.transform(s, streamResult); 79 assertTrue(compareWithGold(goldFile, outputFile)); 80 } catch (IOException | ParserConfigurationException 81 | TransformerException | SAXException ex) { 82 failUnexpected(ex); 83 } 84 } 85 } | 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.*; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import javax.xml.parsers.*; 29 import javax.xml.transform.*; 30 import javax.xml.transform.dom.*; 31 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 32 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 34 import javax.xml.transform.stream.*; 35 import jaxp.library.JAXPFileBaseTest; 36 import static jaxp.library.JAXPTestUtilities.compareWithGold; 37 import static org.testng.Assert.assertTrue; 38 import org.testng.annotations.Test; 39 import org.w3c.dom.*; 40 import org.xml.sax.SAXException; 41 42 /** 43 * Class containing the test cases for TransformerFactory API's 44 * getAssociatedStyleSheet method. 45 */ 46 public class TransformerFactoryTest extends JAXPFileBaseTest { 47 /** 48 * This test case checks for the getAssociatedStylesheet method 49 * of TransformerFactory. 50 * The style sheet returned is then copied to an tfactory01.out 51 * It will then be verified to see if it matches the golden files. 52 * 53 * @throws SAXException If any parse errors occur. 54 * @throws IOException if the file exists but is a directory rather than 55 * a regular file, does not exist but cannot be created, or cannot 56 * be opened for any other reason. 57 * @throws TransformerException If an unrecoverable error occurs during the 58 * course of the transformation. 59 * @throws ParserConfigurationException if a DocumentBuilder cannot be 60 * created which satisfies the configuration requested. 61 */ 62 @Test 63 public void tfactory01() throws ParserConfigurationException, SAXException, 64 IOException, TransformerException { 65 String outputFile = CLASS_DIR + "tfactory01.out"; 66 String goldFile = GOLDEN_DIR + "tfactory01GF.out"; 67 String xmlFile = XML_DIR + "TransformerFactoryTest.xml"; 68 String xmlURI = "file:///" + XML_DIR; 69 70 try (FileInputStream fis = new FileInputStream(xmlFile); 71 FileOutputStream fos = new FileOutputStream(outputFile);) { 72 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 73 74 DocumentBuilder db = dbf.newDocumentBuilder(); 75 Document doc = db.parse(fis, xmlURI); 76 DOMSource domSource = new DOMSource(doc); 77 domSource.setSystemId(xmlURI); 78 StreamResult streamResult = new StreamResult(fos); 79 TransformerFactory tFactory = TransformerFactory.newInstance(); 80 81 Source s = tFactory.getAssociatedStylesheet(domSource, "screen", 82 "Modern", null); 83 Transformer t = tFactory.newTransformer(); 84 t.transform(s, streamResult); 85 } 86 assertTrue(compareWithGold(goldFile, outputFile)); 87 } 88 } |