1 /* 2 * Copyright (c) 2015, 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; 21 22 import javax.xml.transform.Source; 23 import javax.xml.transform.Templates; 24 import javax.xml.transform.Transformer; 25 import javax.xml.transform.TransformerConfigurationException; 26 import javax.xml.transform.TransformerFactory; 27 import javax.xml.transform.stream.StreamResult; 28 import javax.xml.transform.stream.StreamSource; 29 import jaxp.library.JAXPFileBaseTest; 30 import static jaxp.library.JAXPTestUtilities.USER_DIR; 31 import static jaxp.library.JAXPTestUtilities.compareWithGold; 32 import static jaxp.library.JAXPTestUtilities.filenameToURL; 33 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 34 import static org.apache.qetest.trax.TraxConst.XML_DIR; 35 import static org.testng.Assert.assertNull; 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 public class EmbeddedStylesheetTest extends JAXPFileBaseTest { 40 /** 41 * Simple XML documents with xml-stylesheet PI's. 42 * 43 * @throws Exception If any errors occur. 44 */ 45 @Test 46 public void testCase1() throws Exception { 47 setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"), 48 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects")); 49 String xmlName = XML_DIR + "embeddedIdentity.xml"; 50 String goldFile = GOLDEN_DIR + "embeddedIdentity.out"; 51 String outputFile = USER_DIR + "embeddedIdentity.out"; 52 53 TransformerFactory factory = TransformerFactory.newInstance(); 54 Source stylesheet = factory.getAssociatedStylesheet( 55 new StreamSource(filenameToURL(xmlName)), null, null, null); 56 Templates embedTemplates = factory.newTemplates(stylesheet); 57 Transformer embedTransformer = embedTemplates.newTransformer(); 58 embedTransformer.transform( 59 new StreamSource(filenameToURL(xmlName)), 60 new StreamResult(outputFile)); 61 compareWithGold(goldFile, outputFile); 62 } 63 64 /** 65 * Data provider for testCase2. 66 * @return two dimensional array that provide parameters as output file, 67 * golden file, title, media. 68 */ 69 @DataProvider 70 public static Object[][] titleMediaProvider() { 71 return new Object[][]{ 72 {"EmbeddedRelative0.out", "EmbeddedRelative0.out", null, "bar/media"}, 73 {"EmbeddedRelative1.out", "EmbeddedRelative1.out", null, "foo/media"}, 74 {"EmbeddedRelative0.out", "EmbeddedRelative0.out", "bar-title", null}, 75 {"EmbeddedRelative1.out", "EmbeddedRelative1.out", "foo-title", null}, 76 {"EmbeddedRelative2.out", "EmbeddedRelative2.out", null, "alt/media"} 77 }; 78 } 79 80 /** 81 * Test media, title, character set types of xml-stylesheet PI's. 82 * 83 * @param outputFileName output file name. 84 * @param goldFileName golden validation file name. 85 * @param media The media attribute to be matched. May be null, in which 86 * case the prefered templates will be used (i.e. alternate = no). 87 * @param title The value of the title attribute to match. May be null. 88 * @throws Exception If any errors occur. 89 */ 90 @Test(dataProvider = "titleMediaProvider") 91 public void testCase2(String outputFileName, String goldFileName, 92 String title, String media) throws Exception { 93 String mediaTitleName = XML_DIR + "EmbeddedMediaTitle.xml"; 94 String outputFile = USER_DIR + outputFileName; 95 String goldFile = GOLDEN_DIR + goldFileName; 96 97 TransformerFactory factory = TransformerFactory.newInstance(); 98 Source xslSrc = factory.getAssociatedStylesheet( 99 new StreamSource(filenameToURL(mediaTitleName)), 100 media, title, null); 101 factory.newTransformer(xslSrc).transform( 102 new StreamSource(filenameToURL(mediaTitleName)), 103 new StreamResult(outputFile)); 104 compareWithGold(goldFile, outputFile); 105 } 106 107 @DataProvider 108 public Object[][] invalidTitleMedia() { 109 return new Object[][] { 110 {"title-not-found", null}, 111 {null, "media/notfound"}, 112 {"alt-title", "media/notfound"}, 113 {"title-not-found", "alt/media"} 114 }; 115 } 116 117 /** 118 * Expect null if invalid title or media. 119 * 120 * @param media The media attribute to be matched. May be null, in which 121 * case the prefered templates will be used (i.e. alternate = no). 122 * @param title The value of the title attribute to match. May be null. 123 * @throws TransformerConfigurationException Thrown in case of {@linkplain 124 * java.util.ServiceConfigurationError service configuration error} or if 125 * the implementation is not available or cannot be instantiated. 126 */ 127 @Test(dataProvider = "invalidTitleMedia") 128 public void checkNull(String title, String media) 129 throws TransformerConfigurationException { 130 String mediaTitleName = XML_DIR + "EmbeddedMediaTitle.xml"; 131 TransformerFactory factory = TransformerFactory.newInstance(); 132 Source xslSrc = factory.getAssociatedStylesheet( 133 new StreamSource(filenameToURL(mediaTitleName)), 134 media, title, null); 135 assertNull(xslSrc); 136 } 137 }