--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/stream/StreamResultAPITest.java 2014-12-31 11:41:15.059141224 -0800 @@ -0,0 +1,154 @@ +/* + * 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.ByteArrayOutputStream; +import java.io.CharArrayWriter; +import java.io.FileInputStream; +import java.io.FilePermission; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.io.StringWriter; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +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.compareStringWithGold; +import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; +import static org.apache.qetest.trax.TraxConst.XML_DIR; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import org.testng.annotations.Test; + +/** + * API Coverage test for the StreamResult class of TRAX. + */ +public class StreamResultAPITest extends JAXPBaseTest { + /** + * Basic API coverage, constructor and set/get methods. + * @throws IOException if any I/O operation failed. + */ + @Test + public void testCase1() throws IOException { + // Default no-arg ctor sets nothing + StreamResult defaultStream = new StreamResult(); + assertNull(defaultStream.getOutputStream()); + assertNull(defaultStream.getWriter()); + assertNull(defaultStream.getSystemId()); + + try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); + StringWriter strWriter = new StringWriter();) { + StreamResult byteResult1 = new StreamResult(baos); + assertEquals(byteResult1.getOutputStream(), baos); + assertNull(byteResult1.getWriter()); + assertNull(byteResult1.getSystemId()); + + StreamResult readerResult1 = new StreamResult(strWriter); + assertNull(readerResult1.getOutputStream()); + assertEquals(readerResult1.getWriter(), strWriter); + assertNull(readerResult1.getSystemId()); + + StreamResult wackyStream = new StreamResult(); + wackyStream.setOutputStream(baos); + assertEquals(wackyStream.getOutputStream(), baos); + + wackyStream.setWriter(strWriter); + assertEquals(wackyStream.getWriter(), strWriter); + + wackyStream.setSystemId("new-system-id"); + assertEquals(wackyStream.getSystemId(), "new-system-id"); + } + } + + /** + * Basic functionality of StreamResults. + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + * @throws IOException if any I/O operation failed. + */ + @Test + public void testCase2() throws TransformerException, IOException { + setPermissions(new FilePermission(XML_DIR + "/-", "read"), + new FilePermission(GOLDEN_DIR + "/-", "read"), + new FilePermission(CLASS_DIR + "-", "read, write")); + String xsltFile = XML_DIR + "StreamOutputFormat.xsl"; + String xmlFile = XML_DIR + "StreamOutputFormat.xml"; + String goldFile = GOLDEN_DIR + "StreamOutputFormat.out"; + + TransformerFactory factory = TransformerFactory.newInstance(); + try (InputStream xslis = new FileInputStream(xsltFile); + InputStream xmlis1 = new FileInputStream(xmlFile); + InputStream xmlis2 = new FileInputStream(xmlFile); + InputStream xmlis3 = new FileInputStream(xmlFile); + InputStream xmlis4 = new FileInputStream(xmlFile); + InputStream xmlis5 = new FileInputStream(xmlFile); + InputStream xmlis6 = new FileInputStream(xmlFile);) { + // Create re-useable sources + Source xslSource = new StreamSource(xslis); + Templates templates = factory.newTemplates(xslSource); + + // Test some OutputStreams + // Simple FileOutputStream is tested in numerous other tests + Transformer transformer = templates.newTransformer(); + + // Note: must get a new xmlSource for each transform for now. + // FileInputStreams don't just get 'reset' for you, + // But it would be nice to reuse the StreamSources + try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos2 = new ByteArrayOutputStream();) { + transformer.transform(new StreamSource(xmlis1), + new StreamResult(baos)); + transformer.transform(new StreamSource(xmlis2), + new StreamResult(new PrintStream(baos2))); + assertEquals(baos.toString(), baos2.toString()); + compareStringWithGold(goldFile, baos.toString("UTF-8")); + } + + // Test wrap on StringWriter and CharArrayWriter + try(StringWriter sw = new StringWriter(); + CharArrayWriter cw = new CharArrayWriter();) { + transformer.transform(new StreamSource(xmlis3), new StreamResult(sw)); + transformer.transform(new StreamSource(xmlis4), new StreamResult(cw)); + assertEquals(sw.toString(), cw.toString()); + compareStringWithGold(goldFile, sw.toString()); + } + + // Test with systemId set + try (StringWriter sw1 = new StringWriter(); + StringWriter sw2 = new StringWriter();) { + transformer.transform(new StreamSource(xmlis5), new StreamResult(sw1)); + Result swResult2 = new StreamResult(sw2); + swResult2.setSystemId("random-system-id"); + transformer.transform(new StreamSource(xmlis6), swResult2); + assertEquals(sw1.toString(), sw2.toString()); + assertEquals(swResult2.getSystemId(), "random-system-id"); + compareStringWithGold(goldFile, sw1.toString()); + } + } + setPermissions(); + } +}