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.stream; 21 22 import java.io.ByteArrayInputStream; 23 import java.io.FileInputStream; 24 import java.io.FileOutputStream; 25 import java.io.FilePermission; 26 import java.io.InputStream; 27 import java.io.OutputStream; 28 import java.io.StringReader; 29 import javax.xml.transform.Source; 30 import javax.xml.transform.TransformerFactory; 31 import javax.xml.transform.stream.StreamResult; 32 import javax.xml.transform.stream.StreamSource; 33 import jaxp.library.JAXPBaseTest; 34 import static jaxp.library.JAXPTestUtilities.USER_DIR; 35 import static jaxp.library.JAXPTestUtilities.compareWithGold; 36 import static jaxp.library.JAXPTestUtilities.filenameToURL; 37 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 38 import static org.apache.qetest.trax.TraxConst.XML_DIR; 39 import static jaxp.library.JAXPTestUtilities.getNextFile; 40 import static org.testng.Assert.assertEquals; 41 import static org.testng.Assert.assertNull; 42 import org.testng.annotations.Test; 43 44 /** 45 * API Coverage test for the StreamSource class of TRAX. 46 */ 47 public class StreamSourceAPITest extends JAXPBaseTest { 48 /** 49 * Basic API coverage, constructor and set/get methods. 50 */ 51 @Test 52 public void testCase1() { 53 // Default StreamSource contains empty source. 54 StreamSource defaultStream = new StreamSource(); 55 assertNull(defaultStream.getInputStream()); 56 assertNull(defaultStream.getReader()); 57 assertNull(defaultStream.getPublicId()); 58 assertNull(defaultStream.getSystemId()); 59 60 byte[] bytes = {0, 0, 0, 0}; // just a few zeroes, not really needed 61 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 62 StreamSource byteSource1 = new StreamSource(bais); 63 assertEquals(byteSource1.getInputStream(), bais); 64 assertNull(byteSource1.getReader()); 65 assertNull(byteSource1.getPublicId()); 66 assertNull(byteSource1.getSystemId()); 67 68 StreamSource byteSource2 = new StreamSource(bais, "some-system-id"); 69 assertEquals(byteSource2.getInputStream(), bais); 70 assertNull(byteSource2.getReader()); 71 assertNull(byteSource2.getPublicId()); 72 assertEquals(byteSource2.getSystemId(), "some-system-id"); 73 74 try(StringReader strReader = 75 new StringReader("this is not your parent's XML data")) { 76 StreamSource readerSource1 = new StreamSource(strReader); 77 assertNull(readerSource1.getInputStream()); 78 assertEquals(readerSource1.getReader(), strReader); 79 assertNull(readerSource1.getPublicId()); 80 assertNull(readerSource1.getSystemId()); 81 82 StreamSource readerSource2 = new StreamSource(strReader, "some-system-id"); 83 assertNull(readerSource2.getInputStream()); 84 assertEquals(readerSource2.getReader(), strReader); 85 assertNull(readerSource2.getPublicId()); 86 assertEquals(readerSource2.getSystemId(), "some-system-id"); 87 88 StreamSource sysIDStream = new StreamSource("real-system-id"); 89 assertNull(sysIDStream.getInputStream()); 90 assertNull(sysIDStream.getReader()); 91 assertNull(sysIDStream.getPublicId()); 92 assertEquals(sysIDStream.getSystemId(), "real-system-id"); 93 94 StreamSource wackyStream = new StreamSource(); 95 wackyStream.setInputStream(bais); 96 assertEquals(wackyStream.getInputStream(), bais); 97 98 wackyStream.setReader(strReader); 99 assertEquals(wackyStream.getReader(), strReader); 100 101 wackyStream.setSystemId("new-system-id"); 102 assertEquals(wackyStream.getSystemId(), "new-system-id"); 103 104 wackyStream.setPublicId("new-public-id"); 105 assertEquals(wackyStream.getPublicId(), "new-public-id"); 106 } 107 } 108 109 /** 110 * Basic functionality of StreamResource. 111 * 112 * @throws Exception If any errors occur. 113 */ 114 @Test 115 public void testCase2() throws Exception { 116 setPermissions(new FilePermission(XML_DIR + "/-", "read"), 117 new FilePermission(GOLDEN_DIR + "/-", "read"), 118 new FilePermission(USER_DIR + "-", "read, write")); 119 String xsltFile = XML_DIR + "impincl/StreamImpIncl.xsl"; 120 String xmlFile = XML_DIR + "impincl/StreamImpIncl.xml"; 121 String goldFile = GOLDEN_DIR + "StreamImpIncl.out"; 122 String xslID = filenameToURL(xsltFile); 123 String xmlID = filenameToURL(xmlFile); 124 125 TransformerFactory factory = TransformerFactory.newInstance(); 126 try (InputStream xslStream1 = new FileInputStream(xsltFile); 127 InputStream xmlStream1 = new FileInputStream(xmlFile); 128 InputStream xslStream2 = new FileInputStream(xsltFile); 129 InputStream xmlStream2 = new FileInputStream(xmlFile); 130 InputStream xslStream3 = new FileInputStream(xsltFile); 131 InputStream xmlStream3 = new FileInputStream(xmlFile);) { 132 String outputFile1 = getNextFile(this.getClass()); 133 try(OutputStream fos1 = new FileOutputStream(outputFile1)) { 134 Source xslSource1 = new StreamSource(xslStream1); 135 xslSource1.setSystemId(xslID); 136 Source xmlSource1 = new StreamSource(xmlStream1); 137 xmlSource1.setSystemId(xmlID); 138 factory.newTemplates(xslSource1).newTransformer() 139 .transform(xmlSource1, new StreamResult(fos1)); 140 } 141 compareWithGold(goldFile, outputFile1); 142 143 String outputFile2 = getNextFile(this.getClass()); 144 try(OutputStream fos = new FileOutputStream(outputFile2)) { 145 factory.newTemplates(new StreamSource(xslStream2, xslID)) 146 .newTransformer().transform( 147 new StreamSource(xmlStream2, xmlID), new StreamResult(fos)); 148 } 149 compareWithGold(goldFile, outputFile2); 150 151 String outputFile3 = getNextFile(this.getClass()); 152 try(OutputStream fos = new FileOutputStream(outputFile3)) { 153 StreamSource source = new StreamSource(xslStream3); 154 source.setSystemId(xslID); 155 factory.newTemplates(source).newTransformer() 156 .transform(new StreamSource(xmlStream3), new StreamResult(fos)); 157 } 158 compareWithGold(goldFile, outputFile3); 159 } 160 setPermissions(); 161 } 162 }