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 javax.xml.transform.Result;
  23 import javax.xml.transform.Source;
  24 import javax.xml.transform.Templates;
  25 import javax.xml.transform.Transformer;
  26 import javax.xml.transform.TransformerConfigurationException;
  27 import javax.xml.transform.TransformerFactory;
  28 import javax.xml.transform.sax.SAXTransformerFactory;
  29 import javax.xml.transform.sax.TransformerHandler;
  30 import javax.xml.transform.stream.StreamResult;
  31 import javax.xml.transform.stream.StreamSource;
  32 import jaxp.library.JAXPFileBaseTest;
  33 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  34 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  35 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  36 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR;
  37 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  38 import static jaxp.library.JAXPTestUtilities.getNextFile;
  39 import static org.testng.Assert.assertEquals;
  40 import static org.testng.Assert.assertNotNull;
  41 import static org.testng.Assert.assertNull;
  42 import org.testng.annotations.Test;
  43 
  44 /**
  45  * API Coverage test for the TransformerHandler class of TRAX.
  46  */
  47 public class TransformerHandlerAPITest extends JAXPFileBaseTest {
  48     /**
  49      * Nonsense systemId for various tests.
  50      */
  51     private static final String NONSENSE_SYSTEMID = "file:///nonsense/system/id/";
  52 
  53     /**
  54      * IllegalArgumentException is thrown if result set as null.
  55      * 
  56      * @throws TransformerConfigurationException Thrown in case of 
  57      * ServiceConfigurationError service configuration error or if the
  58      *  implementation is not available or cannot be instantiated. 
  59      */
  60     @Test(expectedExceptions = IllegalArgumentException.class)
  61     public void negativeCase1() throws TransformerConfigurationException {
  62         String outputFile = USER_DIR + "TransformerHandlerAPITest01.out";
  63         // Validate API's for an identity transformer
  64         // No public constructor available: you must always ask
  65         //  a SAXTransformerFactory to give you one
  66         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
  67 
  68         // Basic construction of identity transformer
  69         TransformerHandler tHandler = saxFactory.newTransformerHandler();
  70 
  71         // set/getSystemId API coverage
  72         tHandler.setSystemId(NONSENSE_SYSTEMID);
  73         tHandler.setSystemId(null);
  74 
  75         // setResult API coverage
  76         Result unusedResult = new StreamResult(outputFile);
  77         tHandler.setResult(unusedResult);
  78         tHandler.setResult(null);
  79     }
  80 
  81     /**
  82      * IllegalArgumentException is thrown if result set as null.
  83      * 
  84      * @throws TransformerConfigurationException Thrown in case of 
  85      * ServiceConfigurationError service configuration error or if the
  86      * implementation is not available or cannot be instantiated.
  87      */
  88     @Test(expectedExceptions = IllegalArgumentException.class)
  89     public void negativeCase2() throws TransformerConfigurationException {
  90         String outputFile = getNextFile(this.getClass());
  91 
  92         // Validate API's for an identity transformer
  93         // No public constructor available: you must always ask
  94         //  a SAXTransformerFactory to give you one
  95         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
  96 
  97         // Basic construction of identity transformer
  98         TransformerHandler tHandler = saxFactory.newTransformerHandler();
  99 
 100         // set/getSystemId API coverage
 101         tHandler.setSystemId(NONSENSE_SYSTEMID);
 102         tHandler.setSystemId(null);
 103 
 104         // setResult API coverage
 105         Result unusedResult = new StreamResult(outputFile);
 106         tHandler.setResult(unusedResult);
 107         tHandler.setResult(null);
 108     }
 109 
 110     /**
 111      * IllegalArgumentExceptionis thrown  if result set as null.
 112      * 
 113      * @throws TransformerConfigurationException Thrown in case of 
 114      * ServiceConfigurationError service configuration error or if the
 115      * implementation is not available or cannot be instantiated. 
 116      */
 117     @Test(expectedExceptions = IllegalArgumentException.class)
 118     public void negativeCase3() throws TransformerConfigurationException {
 119         String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl");
 120         String outputFile = getNextFile(this.getClass());
 121 
 122         // Validate API's for a 'real' transformer, which is different code
 123         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
 124         // Basic construction of identity transformer
 125         TransformerHandler tHandler = saxFactory.newTransformerHandler(new StreamSource(xslURI));
 126         assertNotNull(tHandler);
 127         // getTemplates API coverage - simple
 128         Transformer transformer = tHandler.getTransformer();
 129         assertNotNull(transformer);
 130         // set/getSystemId API coverage
 131         tHandler.setSystemId(NONSENSE_SYSTEMID);
 132         assertEquals(tHandler.getSystemId(), NONSENSE_SYSTEMID);
 133         tHandler.setSystemId(null);
 134         assertNull(tHandler.getSystemId());
 135 
 136         // setResult API coverage
 137         Result unusedResult = new StreamResult(outputFile);
 138         tHandler.setResult(unusedResult);
 139         tHandler.setResult(null);
 140     }
 141 
 142     /**
 143      * Basic functionality of TransformerHandler.
 144      * 
 145      * @throws Exception If any errors occur.
 146      */
 147     public void testCase2() throws Exception {
 148         String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl");
 149         String xmlURI = XML_DIR + "SAXTest.xml";
 150         String goldFile = GOLDEN_DIR + "SAXTest.out";
 151         String outputURI = filenameToURL(USER_DIR + "SAXTest.out");
 152 
 153         SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
 154         // Validate an identity transformerHandler is valid and performs as an
 155         // identity stylesheet
 156         TransformerHandler transformerHandler = saxFactory.newTransformerHandler();
 157         Transformer transformer = transformerHandler.getTransformer();
 158         assertNotNull(transformer);
 159         transformer.transform(new StreamSource(xmlURI), new StreamResult(xslURI));
 160         compareWithGold(xmlURI, outputURI);
 161 
 162 
 163         // Validate newTransformerHandler(Source) works
 164         transformer = transformerHandler.getTransformer();
 165         assertNotNull(transformer);
 166         transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI));
 167         compareWithGold(goldFile, outputURI);
 168 
 169         // Validate newTransformerHandler(Templates) works
 170         Source  xslSource = new StreamSource(xslURI);
 171         Templates otherTemplates = saxFactory.newTemplates(xslSource);
 172         transformerHandler = saxFactory.newTransformerHandler(otherTemplates);
 173         transformer = transformerHandler.getTransformer();
 174         assertNotNull(transformer);
 175         transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI));
 176         compareWithGold(goldFile, outputURI);
 177     }
 178 }