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