/* * Copyright (c) 2015, 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 javax.xml.transform.Templates; import javax.xml.transform.Transformer; 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.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 jaxp.library.JAXPTestUtilities.getNextFile; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; /** * Verify that URIResolvers are called properly. */ public class URIResolverTest extends JAXPFileBaseTest { /** * Build a style-sheet/do a transform with lots of URIs to resolve. * Verify that the URIResolver is called properly. * * @throws Exception If any errors occur. */ @Test public void testCase1() throws Exception { setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"), new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects")); String xslFile = XML_DIR + "URIResolverTest.xsl"; String xmlFile = XML_DIR + "URIResolverTest.xml"; String goldFile = GOLDEN_DIR + "URIResolverTest.out"; // Validate various URI's to be resolved during stylesheet // build with the loggingURIResolver String[] expXslUris = { "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl", "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdInclude.xsl" }; CheckingURIResolver chkURIResolver = new CheckingURIResolver(expXslUris); TransformerFactory factory = TransformerFactory.newInstance(); // Set the URIResolver and validate it factory.setURIResolver(chkURIResolver); Templates templates = factory.newTemplates(new StreamSource(xslFile)); Transformer transformer = templates.newTransformer(); assertEquals(factory.getURIResolver(), chkURIResolver); // Validate various URI's to be resolved during transform // time with the loggingURIResolver String[] expectedXmlUris = { "{" + filenameToURL(xslFile) + "}" + "../impincl/SystemIdImport.xsl", "{" + filenameToURL(xslFile) + "}" + "impincl/SystemIdImport.xsl", "{" + filenameToURL(xslFile) + "}" + "systemid/impincl/SystemIdImport.xsl", }; CheckingURIResolver transfomerResolver = new CheckingURIResolver(expectedXmlUris); transformer.setURIResolver(transfomerResolver); String outputFile = getNextFile(this.getClass()); transformer.transform(new StreamSource(filenameToURL(xmlFile)), new StreamResult(outputFile)); assertTrue(compareWithGold(goldFile, outputFile)); } }