--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/EmbeddedStylesheetTest.java 2014-12-31 11:40:29.258069950 -0800 @@ -0,0 +1,143 @@ +/* + * 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.IOException; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import jaxp.library.JAXPFileBaseTest; +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 org.testng.Assert.assertNull; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class EmbeddedStylesheetTest extends JAXPFileBaseTest { + /** + * Simple XML documents with xml-stylesheet PI's. + * + * @throws IOException if any I/O operation error. + * @throws TransformerException Any SAX exception, possibly wrapping another + * exception. + */ + @Test + public void testCase1() throws IOException, TransformerException { + setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"), + new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects")); + String xmlName = XML_DIR + "embeddedIdentity.xml"; + String goldFile = GOLDEN_DIR + "embeddedIdentity.out"; + String outputFile = CLASS_DIR + "embeddedIdentity.out"; + + TransformerFactory factory = TransformerFactory.newInstance(); + Source stylesheet = factory.getAssociatedStylesheet( + new StreamSource(filenameToURL(xmlName)), null, null, null); + Templates embedTemplates = factory.newTemplates(stylesheet); + Transformer embedTransformer = embedTemplates.newTransformer(); + embedTransformer.transform( + new StreamSource(filenameToURL(xmlName)), + new StreamResult(outputFile)); + compareWithGold(goldFile, outputFile); + } + + /** + * Data provider for testCase2. + * @return two dimensional array that provide parameters as output file, + * golden file, title, media. + */ + @DataProvider + public static Object[][] titleMediaProvider() { + return new Object[][]{ + {"EmbeddedRelative0.out", "EmbeddedRelative0.out", null, "bar/media"}, + {"EmbeddedRelative1.out", "EmbeddedRelative1.out", null, "foo/media"}, + {"EmbeddedRelative0.out", "EmbeddedRelative0.out", "bar-title", null}, + {"EmbeddedRelative1.out", "EmbeddedRelative1.out", "foo-title", null}, + {"EmbeddedRelative2.out", "EmbeddedRelative2.out", null, "alt/media"} + }; + } + + /** + * Test media, title, character set types of xml-stylesheet PI's. + * + * @param outputFileName output file name. + * @param goldFileName golden validation file name. + * @param media The media attribute to be matched. May be null, in which + * case the prefered templates will be used (i.e. alternate = no). + * @param title The value of the title attribute to match. May be null. + * @throws TransformerException Any SAX exception, possibly wrapping another + * exception. + * @throws IOException if any I/O operation error. + */ + @Test(dataProvider = "titleMediaProvider") + public void testCase2(String outputFileName, String goldFileName, + String title, String media) throws TransformerException, IOException { + String mediaTitleName = XML_DIR + "EmbeddedMediaTitle.xml"; + String outputFile = CLASS_DIR + outputFileName; + String goldFile = GOLDEN_DIR + goldFileName; + + TransformerFactory factory = TransformerFactory.newInstance(); + Source xslSrc = factory.getAssociatedStylesheet( + new StreamSource(filenameToURL(mediaTitleName)), + media, title, null); + factory.newTransformer(xslSrc).transform( + new StreamSource(filenameToURL(mediaTitleName)), + new StreamResult(outputFile)); + compareWithGold(goldFile, outputFile); + } + + @DataProvider + public Object[][] invalidTitleMedia() { + return new Object[][] { + {"title-not-found", null}, + {null, "media/notfound"}, + {"alt-title", "media/notfound"}, + {"title-not-found", "alt/media"} + }; + } + + /** + * Expect null if invalid title or media. + * + * @param media The media attribute to be matched. May be null, in which + * case the prefered templates will be used (i.e. alternate = no). + * @param title The value of the title attribute to match. May be null. + * @throws TransformerConfigurationException Thrown in case of {@linkplain + * java.util.ServiceConfigurationError service configuration error} or if + * the implementation is not available or cannot be instantiated. + */ + @Test(dataProvider = "invalidTitleMedia") + public void checkNull(String title, String media) + throws TransformerConfigurationException { + String mediaTitleName = XML_DIR + "EmbeddedMediaTitle.xml"; + TransformerFactory factory = TransformerFactory.newInstance(); + Source xslSrc = factory.getAssociatedStylesheet( + new StreamSource(filenameToURL(mediaTitleName)), + media, title, null); + assertNull(xslSrc); + } +}