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.Templates;
  23 import javax.xml.transform.Transformer;
  24 import javax.xml.transform.TransformerFactory;
  25 import javax.xml.transform.stream.StreamResult;
  26 import javax.xml.transform.stream.StreamSource;
  27 import jaxp.library.JAXPFileBaseTest;
  28 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  29 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  30 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR;
  31 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  32 import static jaxp.library.JAXPTestUtilities.getNextFile;
  33 import static org.testng.Assert.assertEquals;
  34 import static org.testng.Assert.assertTrue;
  35 import org.testng.annotations.Test;
  36 
  37 /**
  38  * Verify that URIResolvers are called properly.
  39  */
  40 public class URIResolverTest extends JAXPFileBaseTest {
  41     /**
  42      * Build a style-sheet/do a transform with lots of URIs to resolve.
  43      * Verify that the URIResolver is called properly.
  44      * 
  45      * @throws Exception If any errors occur.
  46      */
  47     @Test
  48     public void testCase1() throws Exception {
  49         setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
  50                 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"));
  51         String xslFile = XML_DIR + "URIResolverTest.xsl";
  52         String xmlFile = XML_DIR + "URIResolverTest.xml";
  53         String goldFile = GOLDEN_DIR + "URIResolverTest.out";
  54 
  55         // Validate various URI's to be resolved during stylesheet
  56         //  build with the loggingURIResolver
  57         String[] expXslUris = {
  58                     "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl",
  59                     "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdInclude.xsl"
  60                 };
  61         CheckingURIResolver chkURIResolver = new CheckingURIResolver(expXslUris);
  62         TransformerFactory factory = TransformerFactory.newInstance();
  63         // Set the URIResolver and validate it
  64         factory.setURIResolver(chkURIResolver);
  65         Templates templates = factory.newTemplates(new StreamSource(xslFile));
  66         Transformer transformer = templates.newTransformer();
  67         assertEquals(factory.getURIResolver(), chkURIResolver);
  68         
  69         // Validate various URI's to be resolved during transform
  70         //  time with the loggingURIResolver
  71         String[] expectedXmlUris = {
  72             "{" + filenameToURL(xslFile) + "}" + "../impincl/SystemIdImport.xsl",
  73             "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl",
  74             "{" + filenameToURL(xslFile) + "}" + "systemid/impincl/SystemIdImport.xsl",
  75         };
  76         CheckingURIResolver transfomerResolver = new CheckingURIResolver(expectedXmlUris);
  77         transformer.setURIResolver(transfomerResolver);
  78         String outputFile = getNextFile(this.getClass());
  79         transformer.transform(new StreamSource(filenameToURL(xmlFile)),
  80                                   new StreamResult(outputFile));
  81         assertTrue(compareWithGold(goldFile, outputFile));
  82     }   
  83 }