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