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.sax;
  21 
  22 import java.io.StringReader;
  23 import java.nio.file.Files;
  24 import java.nio.file.Paths;
  25 import java.util.stream.Collectors;
  26 import javax.xml.parsers.SAXParser;
  27 import javax.xml.parsers.SAXParserFactory;
  28 import javax.xml.transform.Transformer;
  29 import javax.xml.transform.TransformerException;
  30 import javax.xml.transform.TransformerFactory;
  31 import javax.xml.transform.sax.SAXTransformerFactory;
  32 import javax.xml.transform.sax.TransformerHandler;
  33 import javax.xml.transform.stream.StreamResult;
  34 import javax.xml.transform.stream.StreamSource;
  35 import jaxp.library.JAXPFileBaseTest;
  36 import static jaxp.library.JAXPTestUtilities.getNextFile;
  37 import org.apache.qetest.xsl.CheckingSAXErrorHandler;
  38 import static org.testng.Assert.assertEquals;
  39 import org.testng.annotations.Test;
  40 import org.xml.sax.SAXParseException;
  41 
  42 /**
  43  * Basic functionality test for the TransformerHandler class of TRAX.
  44  */
  45 public class TransformerHandlerTest extends JAXPFileBaseTest {
  46 
  47     /**
  48      * Un-formed XML string(note mismatched tags).
  49      */
  50     private static final String XML_ERROR_STR
  51             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  52             + "<root1></root2>";
  53 
  54     /**
  55      * Well-formed XML string.
  56      */
  57     private static final String XML_GOOD_STR
  58             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  59             + "<root>Hello world</root>";
  60 
  61     /**
  62      * XSLT string.
  63      */
  64     private static final String XSL_STR
  65             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  66             + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
  67             + "<xsl:template match=\"/\">"
  68             + "<xsl:copy-of select=\".\"/>"
  69             + "</xsl:template>"
  70             + "</xsl:stylesheet>";
  71     
  72     /**
  73      * Golden validation string.
  74      */
  75     private static final String GOLDEN_STR
  76             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>Hello world</root>";
  77 
  78     /**
  79      * Transformer.transform on a not well-formed XML throws TransformerException.
  80      * 
  81      * @throws TransformerException If an unrecoverable error occurs during the 
  82      *         course of the transformation. 
  83      */
  84     @Test(expectedExceptions = TransformerException.class)
  85     public void negativeCase1() throws TransformerException {
  86         // No public constructor available: you must always ask
  87         //  a SAXTransformerFactory to give you one
  88         SAXTransformerFactory saxFactory
  89                 = (SAXTransformerFactory) TransformerFactory.newInstance();
  90         try (StringReader sr1 = new StringReader(XSL_STR);
  91                 StringReader sr2 = new StringReader(XML_ERROR_STR)) {
  92             Transformer reusedTransformer = saxFactory.newTransformer(
  93                     new StreamSource(sr1));
  94             reusedTransformer.transform(new StreamSource(sr2),
  95                     new StreamResult(getNextFile(TransformerHandlerTest.class)));
  96         }
  97     }
  98 
  99     /**
 100      * Reader.read on a not well-formed XML throws TransformerException. 
 101      * 
 102      * @throws Exception If any errors occur.
 103      */
 104     @Test(expectedExceptions = SAXParseException.class)
 105     public void testCase2() throws Exception {
 106         SAXTransformerFactory saxFactory
 107                 = (SAXTransformerFactory) TransformerFactory.newInstance();
 108         try(StringReader sr1 = new StringReader(XSL_STR);
 109                 StringReader sr2 = new StringReader(XML_ERROR_STR);) {
 110             TransformerHandler thandler = saxFactory.newTransformerHandler(
 111                     new StreamSource(sr1));
 112             SAXParserFactory spf1 = SAXParserFactory.newInstance();
 113             spf1.setNamespaceAware(true);
 114             SAXParser parser1 = spf1.newSAXParser();
 115             org.xml.sax.XMLReader reader = parser1.getXMLReader();
 116             reader.setContentHandler(thandler);
 117             reader.setErrorHandler(new CheckingSAXErrorHandler());
 118             thandler.setResult(new StreamResult(getNextFile(TransformerHandlerTest.class)));
 119             reader.parse(new org.xml.sax.InputSource(sr2));
 120         }
 121     }
 122 
 123     /**
 124      * Re-transform several times with XMLReader and Transformer. No exception 
 125      * should be thrown.
 126      * 
 127      * @throws Exception If any errors occur. 
 128      */
 129     @Test
 130     public void testCase3() throws Exception {
 131         SAXTransformerFactory saxFactory
 132                 = (SAXTransformerFactory) TransformerFactory.newInstance();
 133         String outputFile1 = getNextFile(this.getClass());
 134         String outputFile2 = getNextFile(this.getClass());
 135         try(StringReader sr1 = new StringReader(XSL_STR);
 136                 StringReader sr2 = new StringReader(XML_GOOD_STR);
 137                 StringReader sr3 = new StringReader(XSL_STR);
 138                 StringReader sr4 = new StringReader(XML_GOOD_STR)) {
 139             TransformerHandler thandler = saxFactory.newTransformerHandler(
 140                     new StreamSource(sr1));
 141             SAXParserFactory spf = SAXParserFactory.newInstance();
 142             spf.setNamespaceAware(true);
 143             SAXParser parser = spf.newSAXParser();
 144             org.xml.sax.XMLReader reader = parser.getXMLReader();
 145             thandler.setResult(new StreamResult(outputFile1));
 146             reader.setContentHandler(thandler);
 147             reader.setErrorHandler(new CheckingSAXErrorHandler());
 148             reader.parse(new org.xml.sax.InputSource(sr2));
 149 
 150             Transformer reusedTransformer = TransformerFactory.newInstance()
 151                     .newTransformer(new StreamSource(sr3));
 152             reusedTransformer.transform(new StreamSource(sr4),
 153                     new StreamResult(outputFile2));
 154         }
 155         String outputStr1 = Files.readAllLines(Paths.get(outputFile1)).stream()
 156                 .collect(Collectors.joining(System.getProperty("line.separator")));
 157         String outputStr2 = Files.readAllLines(Paths.get(outputFile1)).stream()
 158                 .collect(Collectors.joining(System.getProperty("line.separator")));
 159         assertEquals(outputStr1, GOLDEN_STR);
 160         assertEquals(outputStr2, GOLDEN_STR);
 161     }
 162 }