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;
  21 
  22 import java.io.FilePermission;
  23 import java.util.Properties;
  24 import javax.xml.transform.OutputKeys;
  25 import javax.xml.transform.Templates;
  26 import javax.xml.transform.Transformer;
  27 import javax.xml.transform.TransformerConfigurationException;
  28 import javax.xml.transform.TransformerFactory;
  29 import javax.xml.transform.stream.StreamSource;
  30 import jaxp.library.JAXPBaseTest;
  31 import static org.apache.qetest.trax.TraxConst.XML_DIR;
  32 import static org.testng.Assert.*;
  33 import org.testng.annotations.Test;
  34 
  35 /**
  36  * Basic API coverage test for the Templates class of TRAX.
  37  */
  38 public class TemplateAPITest extends JAXPBaseTest{
  39     /**
  40      * TRAX Templates: cover newTransformer(),getOutputProperties() APIs and 
  41      * basic functionality.
  42      * @throws javax.xml.transform.TransformerConfigurationException
  43      */
  44     @Test
  45     public void test() throws TransformerConfigurationException {
  46         setPermissions(new FilePermission(XML_DIR + "-", "read"));
  47         final String xslFile = XML_DIR + "TransformerAPIParam.xsl";
  48         final String outputFormatXSL = XML_DIR + "TransformerAPIOutputFormat.xsl";
  49         
  50         TransformerFactory factory = TransformerFactory.newInstance();
  51         Templates templates1 = factory.newTemplates(new StreamSource(xslFile));
  52         
  53         // Cover APIs newTransformer(), getOutputProperties()
  54         Transformer transformer = templates1.newTransformer();
  55         assertNotNull(transformer);
  56         Properties outputFormat = templates1.getOutputProperties();
  57         assertNotNull(outputFormat);
  58         
  59         // Check that the local stylesheet.getProperty has default set, cf. 
  60         // getOutputProperties javadoc
  61         assertEquals(outputFormat.getProperty(OutputKeys.METHOD), "xml");
  62         assertNull(outputFormat.get(OutputKeys.METHOD));
  63         assertEquals(outputFormat.getProperty(OutputKeys.INDENT), "no");
  64         assertNull(outputFormat.get(OutputKeys.INDENT));
  65         
  66         Templates templates2 
  67                 = factory.newTemplates(new StreamSource(outputFormatXSL));
  68         Properties outputFormat2 = templates2.getOutputProperties();
  69         assertNotNull(outputFormat2);
  70                
  71         // Known outputFormat property name/value from outputFormatXSL.
  72         final String OUTPUT_FORMAT_NAME = OutputKeys.CDATA_SECTION_ELEMENTS;
  73         final String OUTPUT_FORMAT_VALUE = "cdataHere ";
  74 
  75         // Test known properties
  76         outputFormat2.getProperty(OUTPUT_FORMAT_NAME);
  77         assertEquals(outputFormat2.getProperty(OUTPUT_FORMAT_NAME), OUTPUT_FORMAT_VALUE);
  78         assertEquals(outputFormat2.getProperty("omit-xml-declaration"), "yes");
  79     }
  80 }