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.Templates;
  24 import javax.xml.transform.Transformer;
  25 import javax.xml.transform.TransformerConfigurationException;
  26 import javax.xml.transform.TransformerException;
  27 import javax.xml.transform.TransformerFactory;
  28 import javax.xml.transform.stream.StreamResult;
  29 import javax.xml.transform.stream.StreamSource;
  30 import jaxp.library.JAXPFileBaseTest;
  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 jaxp.library.JAXPTestUtilities.getNextFile;
  36 import static org.testng.Assert.assertEquals;
  37 import static org.testng.Assert.assertTrue;
  38 import org.testng.annotations.Test;
  39 
  40 /**
  41  * Verify that URIResolvers are called properly.
  42  */
  43 public class URIResolverTest extends JAXPFileBaseTest {
  44     /**
  45      * Build a style-sheet/do a transform with lots of URIs to resolve.
  46      * Verify that the URIResolver is called properly.
  47      * 
  48      * @throws TransformerConfigurationException Thrown in case of {@linkplain
  49      * java.util.ServiceConfigurationError service configuration error} or if
  50      * the implementation is not available or cannot be instantiated.
  51      * @throws IOException if an I/O error occurs reading from the file or a 
  52      * malformed or unmappable byte sequence is read.
  53      */
  54     @Test
  55     public void testCase1() throws TransformerException, IOException {
  56         setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
  57                 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"));
  58         String xslFile = XML_DIR + "URIResolverTest.xsl";
  59         String xmlFile = XML_DIR + "URIResolverTest.xml";
  60         String goldFile = GOLDEN_DIR + "URIResolverTest.out";
  61 
  62         // Validate various URI's to be resolved during stylesheet
  63         //  build with the loggingURIResolver
  64         String[] expXslUris = {
  65                     "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl",
  66                     "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdInclude.xsl"
  67                 };
  68         CheckingURIResolver chkURIResolver = new CheckingURIResolver(expXslUris);
  69         TransformerFactory factory = TransformerFactory.newInstance();
  70         // Set the URIResolver and validate it
  71         factory.setURIResolver(chkURIResolver);
  72         Templates templates = factory.newTemplates(new StreamSource(xslFile));
  73         Transformer transformer = templates.newTransformer();
  74         assertEquals(factory.getURIResolver(), chkURIResolver);
  75         
  76         // Validate various URI's to be resolved during transform
  77         //  time with the loggingURIResolver
  78         String[] expectedXmlUris = {
  79             "{" + filenameToURL(xslFile) + "}" + "../impincl/SystemIdImport.xsl",
  80             "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl",
  81             "{" + filenameToURL(xslFile) + "}" + "systemid/impincl/SystemIdImport.xsl",
  82         };
  83         CheckingURIResolver transfomerResolver = new CheckingURIResolver(expectedXmlUris);
  84         transformer.setURIResolver(transfomerResolver);
  85         String outputFile = getNextFile(this.getClass());
  86         transformer.transform(new StreamSource(filenameToURL(xmlFile)),
  87                                   new StreamResult(outputFile));
  88         assertTrue(compareWithGold(goldFile, outputFile));
  89     }   
  90 }