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.ByteArrayOutputStream;
  23 import java.io.CharArrayWriter;
  24 import java.io.FileInputStream;
  25 import java.io.FilePermission;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.PrintStream;
  29 import java.io.StringWriter;
  30 import javax.xml.transform.Result;
  31 import javax.xml.transform.Source;
  32 import javax.xml.transform.Templates;
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerException;
  35 import javax.xml.transform.TransformerFactory;
  36 import javax.xml.transform.stream.StreamResult;
  37 import javax.xml.transform.stream.StreamSource;
  38 import jaxp.library.JAXPBaseTest;
  39 import static jaxp.library.JAXPTestUtilities.CLASS_DIR;
  40 import static jaxp.library.JAXPTestUtilities.compareStringWithGold;
  41 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR;
  42 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  43 import static org.testng.Assert.assertEquals;
  44 import static org.testng.Assert.assertNull;
  45 import org.testng.annotations.Test;
  46 
  47 /**
  48  * API Coverage test for the StreamResult class of TRAX.
  49  */
  50 public class StreamResultAPITest extends JAXPBaseTest {
  51     /**
  52      * Basic API coverage, constructor and set/get methods.
  53      * @throws IOException if any I/O operation failed.
  54      */
  55     @Test
  56     public void testCase1() throws IOException {
  57         // Default no-arg ctor sets nothing
  58         StreamResult defaultStream = new StreamResult();
  59         assertNull(defaultStream.getOutputStream());
  60         assertNull(defaultStream.getWriter());
  61         assertNull(defaultStream.getSystemId());
  62 
  63         try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64                 StringWriter strWriter = new StringWriter();) {
  65             StreamResult byteResult1 = new StreamResult(baos);
  66             assertEquals(byteResult1.getOutputStream(), baos);
  67             assertNull(byteResult1.getWriter());
  68             assertNull(byteResult1.getSystemId());
  69 
  70             StreamResult readerResult1 = new StreamResult(strWriter);
  71             assertNull(readerResult1.getOutputStream());
  72             assertEquals(readerResult1.getWriter(), strWriter);
  73             assertNull(readerResult1.getSystemId());
  74 
  75             StreamResult wackyStream = new StreamResult();
  76             wackyStream.setOutputStream(baos);
  77             assertEquals(wackyStream.getOutputStream(), baos);
  78 
  79             wackyStream.setWriter(strWriter);
  80             assertEquals(wackyStream.getWriter(), strWriter);
  81 
  82             wackyStream.setSystemId("new-system-id");
  83             assertEquals(wackyStream.getSystemId(), "new-system-id");
  84         }
  85     }
  86     
  87     /**
  88      * Basic functionality of StreamResults.
  89      * @throws TransformerException If an unrecoverable error occurs during the 
  90      *         course of the transformation.
  91      * @throws IOException if any I/O operation failed.
  92      */
  93     @Test
  94     public void testCase2() throws TransformerException, IOException {
  95         setPermissions(new FilePermission(XML_DIR + "/-", "read"),
  96                 new FilePermission(GOLDEN_DIR + "/-", "read"),
  97                 new FilePermission(CLASS_DIR + "-", "read, write"));
  98         String xsltFile = XML_DIR + "StreamOutputFormat.xsl";
  99         String xmlFile = XML_DIR + "StreamOutputFormat.xml";
 100         String goldFile = GOLDEN_DIR + "StreamOutputFormat.out"; 
 101         
 102         TransformerFactory factory = TransformerFactory.newInstance();
 103         try (InputStream xslis = new FileInputStream(xsltFile);
 104              InputStream xmlis1 = new FileInputStream(xmlFile);
 105              InputStream xmlis2 = new FileInputStream(xmlFile);
 106              InputStream xmlis3 = new FileInputStream(xmlFile);
 107              InputStream xmlis4 = new FileInputStream(xmlFile);
 108              InputStream xmlis5 = new FileInputStream(xmlFile);
 109              InputStream xmlis6 = new FileInputStream(xmlFile);) {
 110             // Create re-useable sources
 111             Source xslSource = new StreamSource(xslis);
 112             Templates templates = factory.newTemplates(xslSource);
 113             
 114             // Test some OutputStreams
 115             // Simple FileOutputStream is tested in numerous other tests
 116             Transformer transformer = templates.newTransformer();
 117   
 118             // Note: must get a new xmlSource for each transform for now. 
 119             // FileInputStreams don't just get 'reset' for you,
 120             // But it would be nice to reuse the StreamSources
 121             try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
 122                 ByteArrayOutputStream baos2 = new ByteArrayOutputStream();) {
 123                 transformer.transform(new StreamSource(xmlis1), 
 124                         new StreamResult(baos));
 125                 transformer.transform(new StreamSource(xmlis2), 
 126                         new StreamResult(new PrintStream(baos2)));
 127                 assertEquals(baos.toString(), baos2.toString());
 128                 compareStringWithGold(goldFile, baos.toString("UTF-8"));
 129             }
 130             
 131             // Test wrap on StringWriter and CharArrayWriter
 132             try(StringWriter sw = new StringWriter();
 133                     CharArrayWriter cw = new CharArrayWriter();) {
 134                 transformer.transform(new StreamSource(xmlis3), new StreamResult(sw));
 135                 transformer.transform(new StreamSource(xmlis4), new StreamResult(cw));
 136                 assertEquals(sw.toString(), cw.toString());
 137                 compareStringWithGold(goldFile, sw.toString());
 138             }
 139             
 140             // Test with systemId set
 141             try (StringWriter sw1 = new StringWriter();
 142                 StringWriter sw2 = new StringWriter();) {
 143                 transformer.transform(new StreamSource(xmlis5), new StreamResult(sw1));
 144                 Result swResult2 = new StreamResult(sw2);
 145                 swResult2.setSystemId("random-system-id");
 146                 transformer.transform(new StreamSource(xmlis6), swResult2);
 147                 assertEquals(sw1.toString(), sw2.toString());
 148                 assertEquals(swResult2.getSystemId(), "random-system-id");
 149                 compareStringWithGold(goldFile, sw1.toString());
 150             }
 151         }
 152         setPermissions();
 153     }
 154 }