/* * Copyright (c) 2014, 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.stream; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilePermission; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import jaxp.library.JAXPBaseTest; import static jaxp.library.JAXPTestUtilities.CLASS_DIR; 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.assertNull; import org.testng.annotations.Test; /** * API Coverage test for the StreamSource class of TRAX. */ public class StreamSourceAPITest extends JAXPBaseTest { /** * Basic API coverage, constructor and set/get methods. */ @Test public void testCase1() { // Default StreamSource contains empty source. StreamSource defaultStream = new StreamSource(); assertNull(defaultStream.getInputStream()); assertNull(defaultStream.getReader()); assertNull(defaultStream.getPublicId()); assertNull(defaultStream.getSystemId()); byte[] bytes = {0, 0, 0, 0}; // just a few zeroes, not really needed ByteArrayInputStream bais = new ByteArrayInputStream(bytes); StreamSource byteSource1 = new StreamSource(bais); assertEquals(byteSource1.getInputStream(), bais); assertNull(byteSource1.getReader()); assertNull(byteSource1.getPublicId()); assertNull(byteSource1.getSystemId()); StreamSource byteSource2 = new StreamSource(bais, "some-system-id"); assertEquals(byteSource2.getInputStream(), bais); assertNull(byteSource2.getReader()); assertNull(byteSource2.getPublicId()); assertEquals(byteSource2.getSystemId(), "some-system-id"); try(StringReader strReader = new StringReader("this is not your parent's XML data")) { StreamSource readerSource1 = new StreamSource(strReader); assertNull(readerSource1.getInputStream()); assertEquals(readerSource1.getReader(), strReader); assertNull(readerSource1.getPublicId()); assertNull(readerSource1.getSystemId()); StreamSource readerSource2 = new StreamSource(strReader, "some-system-id"); assertNull(readerSource2.getInputStream()); assertEquals(readerSource2.getReader(), strReader); assertNull(readerSource2.getPublicId()); assertEquals(readerSource2.getSystemId(), "some-system-id"); StreamSource sysIDStream = new StreamSource("real-system-id"); assertNull(sysIDStream.getInputStream()); assertNull(sysIDStream.getReader()); assertNull(sysIDStream.getPublicId()); assertEquals(sysIDStream.getSystemId(), "real-system-id"); StreamSource wackyStream = new StreamSource(); wackyStream.setInputStream(bais); assertEquals(wackyStream.getInputStream(), bais); wackyStream.setReader(strReader); assertEquals(wackyStream.getReader(), strReader); wackyStream.setSystemId("new-system-id"); assertEquals(wackyStream.getSystemId(), "new-system-id"); wackyStream.setPublicId("new-public-id"); assertEquals(wackyStream.getPublicId(), "new-public-id"); } } /** * Basic functionality of StreamResource. * @throws IOException if any I/O operation failed. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void testCase2() throws IOException, TransformerException { setPermissions(new FilePermission(XML_DIR + "/-", "read"), new FilePermission(GOLDEN_DIR + "/-", "read"), new FilePermission(CLASS_DIR + "-", "read, write")); String xsltFile = XML_DIR + "impincl/StreamImpIncl.xsl"; String xmlFile = XML_DIR + "impincl/StreamImpIncl.xml"; String goldFile = GOLDEN_DIR + "StreamImpIncl.out"; String xslID = filenameToURL(xsltFile); String xmlID = filenameToURL(xmlFile); TransformerFactory factory = TransformerFactory.newInstance(); try (InputStream xslStream1 = new FileInputStream(xsltFile); InputStream xmlStream1 = new FileInputStream(xmlFile); InputStream xslStream2 = new FileInputStream(xsltFile); InputStream xmlStream2 = new FileInputStream(xmlFile); InputStream xslStream3 = new FileInputStream(xsltFile); InputStream xmlStream3 = new FileInputStream(xmlFile);) { String outputFile1 = getNextFile(this.getClass()); try(OutputStream fos1 = new FileOutputStream(outputFile1)) { Source xslSource1 = new StreamSource(xslStream1); xslSource1.setSystemId(xslID); Source xmlSource1 = new StreamSource(xmlStream1); xmlSource1.setSystemId(xmlID); factory.newTemplates(xslSource1).newTransformer() .transform(xmlSource1, new StreamResult(fos1)); } compareWithGold(goldFile, outputFile1); String outputFile2 = getNextFile(this.getClass()); try(OutputStream fos = new FileOutputStream(outputFile2)) { factory.newTemplates(new StreamSource(xslStream2, xslID)) .newTransformer().transform( new StreamSource(xmlStream2, xmlID), new StreamResult(fos)); } compareWithGold(goldFile, outputFile2); String outputFile3 = getNextFile(this.getClass()); try(OutputStream fos = new FileOutputStream(outputFile3)) { StreamSource source = new StreamSource(xslStream3); source.setSystemId(xslID); factory.newTemplates(source).newTransformer() .transform(new StreamSource(xmlStream3), new StreamResult(fos)); } compareWithGold(goldFile, outputFile3); } setPermissions(); } }