/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.qetest.trax; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilePermission; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import jaxp.library.JAXPBaseTest; import static jaxp.library.JAXPTestUtilities.CLASS_DIR; import static jaxp.library.JAXPTestUtilities.compareWithGold; import static jaxp.library.JAXPTestUtilities.filenameToURL; import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; import static org.apache.qetest.trax.TraxConst.XML_DIR; import static jaxp.library.JAXPTestUtilities.getNextFile; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import org.xml.sax.SAXException; /** * API Coverage test for TransformerFactory class of TRAX. */ public class TransformerFactoryAPITest extends JAXPBaseTest { /** * Cached system property. */ private final String cachedSysProp = null; /** * System property name, from TransformerFactory. */ private static final String DEFAULT_PROP_NAME = "javax.xml.transform.TransformerFactory"; /** * Stored system property name for TransformerFactory. */ private String factory_prop_value; /** * System property name for Xalan-J 2.x impl. */ private static final String XALAN_CLASSNAME = "org.apache.xalan.processor.TransformerFactoryImpl"; /** * Test XSLT file name. */ private static final String XSL_URI = filenameToURL(XML_DIR + "identity.xsl"); /** * Test XML file name. */ private static final String EMBEDDED_XML = XML_DIR + "embeddedIdentity.xml"; /** * Test XML uri. */ private static final String EMBEDDED_XML_URI = filenameToURL(EMBEDDED_XML); /** * Golden file name. */ private static final String EMBEDDED_GOLD_FILE = GOLDEN_DIR + "embeddedIdentity.out"; /** * invalid feature/attribute name for negative test. */ private static final String BOGUS_NAME = "fnord:this/feature/does/not/exist"; /** * Coverage tests for factory pattern API's. */ @Test public void testCase1() { // Reset the system property to what was cached previously // test when system property is null setSystemProperty(DEFAULT_PROP_NAME, null); assertNotNull(TransformerFactory.newInstance()); // This should come last so it will stay set for the rest of the test // Note: this needs review, since in the future we may // not guaruntee order of testCase execution! setSystemProperty(DEFAULT_PROP_NAME, cachedSysProp); assertNotNull(TransformerFactory.newInstance()); } /** * Save system property for restoring. */ @BeforeGroups (groups = {"systemProp"}) public void saveProp() { factory_prop_value = getSystemProperty(DEFAULT_PROP_NAME); } /** * Restore the system property. */ @AfterGroups (groups = {"systemProp"}) public void restoreProp() { setSystemProperty(DEFAULT_PROP_NAME, factory_prop_value); } /** * TransformerFactoryConfigurationError is thrown when system property is a * bogus name. */ @Test(groups = {"systemProp"}, expectedExceptions = TransformerFactoryConfigurationError.class) public void negativeTestCase1() { setSystemProperty(DEFAULT_PROP_NAME, "this.class.does.not.exist"); TransformerFactory.newInstance(); } /** * TransformerFactoryConfigurationError is thrown when system property is * another kind of class name. */ @Test(groups = {"systemProp"}, expectedExceptions = TransformerFactoryConfigurationError.class) public void negativeTestCase2() { setSystemProperty(DEFAULT_PROP_NAME, "java.lang.String"); TransformerFactory.newInstance(); } /** * Coverage tests for newTransformer() API's. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test public void testCase2() throws TransformerConfigurationException { setPermissions(new FilePermission(XML_DIR + "/-", "read")); TransformerFactory factory = TransformerFactory.newInstance(); Transformer identityTransformer = factory.newTransformer(); assertNotNull(identityTransformer); assertTrue(factory.getFeature(StreamSource.FEATURE)); assertNotNull(factory.newTransformer(new StreamSource(XSL_URI))); setPermissions(); } /** * Coverage tests for newTemplates() API's. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test public void testCase3() throws TransformerConfigurationException { setPermissions(new FilePermission(XML_DIR + "/-", "read")); TransformerFactory factory = TransformerFactory.newInstance(); assertNotNull(factory.getFeature(StreamSource.FEATURE)); assertNotNull(factory.newTemplates(new StreamSource(XSL_URI))); setPermissions(); } /** * Coverage tests for getAssociatedStylesheet() API's. * * @throws IOException if any I/O operation error. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void testCase4() throws IOException, TransformerException{ setPermissions(new FilePermission(XML_DIR + "/-", "read"), new FilePermission(GOLDEN_DIR + "/-", "read"), new FilePermission(CLASS_DIR + "-", "read, write")); String outputFile = getNextFile(this.getClass()); try (FileOutputStream resultStream = new FileOutputStream(getNextFile(this.getClass()))){ TransformerFactory factory = TransformerFactory.newInstance(); // Get the xml-stylesheet and process it Source stylesheet = factory.getAssociatedStylesheet( new StreamSource(EMBEDDED_XML_URI), null, null, null); assertTrue(stylesheet instanceof Source); assertNotNull(stylesheet); Transformer transformer = factory.newTransformer(stylesheet); transformer.transform(new StreamSource(EMBEDDED_XML_URI), new StreamResult(resultStream)); } assert(compareWithGold(EMBEDDED_GOLD_FILE, outputFile)); setPermissions(); } /** * Coverage tests for get/setURIResolver(), get/setErrorListener() API's. */ @Test public void testCase5() { TransformerFactory factory = TransformerFactory.newInstance(); assertNull(factory.getURIResolver()); CheckingURIResolver loggingURIRes = new CheckingURIResolver(); factory.setURIResolver(loggingURIRes); assertEquals(factory.getURIResolver(), loggingURIRes); factory.setURIResolver(null); assertNull(factory.getURIResolver()); assertNotNull(factory.getErrorListener()); CheckingErrorListener loggingErrListener = new CheckingErrorListener(); factory.setErrorListener(loggingErrListener); assertEquals(factory.getErrorListener(), loggingErrListener); } /** * IAE is thrown when setErrorListener(null). */ @Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeCase5() { TransformerFactory factory = TransformerFactory.newInstance(); factory.setErrorListener(null); } /** * Miscellaneous tests. * @throws TransformerConfigurationException An Exception is thrown if an * error occurs during parsing of the source. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException If any parse errors occur. * @throws IOException If any I/O errors occur. */ @Test public void associateSourceTest() throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException { setPermissions(new FilePermission(XML_DIR + "/-", "read")); TransformerFactory factory = TransformerFactory.newInstance(); DOMSource domsource = new DOMSource(DocumentBuilderFactory. newInstance().newDocumentBuilder().parse(EMBEDDED_XML)); assertNull(factory.getAssociatedStylesheet(domsource,"screen","Modern",null)); try(FileInputStream fis = new FileInputStream(EMBEDDED_XML)) { StreamSource ss = new StreamSource(fis); assertNull(factory.getAssociatedStylesheet(ss,"screen","Modern",null)); } try(FileInputStream fis = new FileInputStream(EMBEDDED_XML)) { StreamSource ss = new StreamSource(fis); ss.setSystemId(EMBEDDED_XML_URI); assertNull(factory.getAssociatedStylesheet(ss,"screen","Modern",null)); } setPermissions(); } /** * Test if factory supports valid features and doesn't support invalid * features. */ @Test public void getFeaturesTest() { TransformerFactory factory = TransformerFactory.newInstance(); // Expected no NPE here. assertFalse(factory.getFeature(BOGUS_NAME)); assertTrue(factory.getFeature(StreamSource.FEATURE)); assertTrue(factory.getFeature(StreamResult.FEATURE)); } /** * IllegalArgumentException is thrown when implementation does not recognize * the attribute. */ @Test(expectedExceptions = IllegalArgumentException.class) public void bogusSetFeatures() { TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute(BOGUS_NAME, "on"); } }