1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package javax.xml.transform.ptests;
  24 
  25 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertNull;
  28 
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 
  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.dom.DOMSource;
  38 import javax.xml.transform.sax.SAXSource;
  39 import javax.xml.transform.stream.StreamSource;
  40 
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 import org.w3c.dom.Document;
  44 import org.w3c.dom.Node;
  45 import org.xml.sax.InputSource;
  46 
  47 /**
  48  * Class containing the test cases for SAXParserFactory API
  49  */
  50 /*
  51  * @test
  52  * @library /javax/xml/jaxp/libs
  53  * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.TfClearParamTest
  54  * @run testng/othervm javax.xml.transform.ptests.TfClearParamTest
  55  */
  56 @Listeners({jaxp.library.FilePolicy.class})
  57 public class TfClearParamTest {
  58     /**
  59      * Test style-sheet file name.
  60      */
  61     private final String XSL_FILE = XML_DIR + "cities.xsl";
  62 
  63     /**
  64      * Long parameter name embedded with a URI.
  65      */
  66     private final String LONG_PARAM_NAME = "{http://xyz.foo.com/yada/baz.html}foo";
  67 
  68     /**
  69      * Short parameter name.
  70      */
  71     private final String SHORT_PARAM_NAME = "foo";
  72 
  73     /**
  74      * Parameter value.
  75      */
  76     private final String PARAM_VALUE = "xyz";
  77 
  78     /**
  79      * Obtains transformer's parameter with the same name that set before. Value
  80      * should be same as set one.
  81      * @throws TransformerConfigurationException If for some reason the
  82      *         TransformerHandler can not be created.
  83      */
  84     @Test
  85     public void clear01() throws TransformerConfigurationException {
  86         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  87         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
  88         assertEquals(transformer.getParameter(LONG_PARAM_NAME).toString(), PARAM_VALUE);
  89     }
  90 
  91     /**
  92      * Obtains transformer's parameter with the a name that wasn't set before.
  93      * Null is expected.
  94      * @throws TransformerConfigurationException If for some reason the
  95      *         TransformerHandler can not be created.
  96      */
  97     @Test
  98     public void clear02() throws TransformerConfigurationException {
  99         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 100         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 101         transformer.clearParameters();
 102         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 103     }
 104 
 105     /**
 106      * Obtains transformer's parameter with a short name that set before. Value
 107      * should be same as set one.
 108      * @throws TransformerConfigurationException If for some reason the
 109      *         TransformerHandler can not be created.
 110      */
 111     @Test
 112     public void clear03() throws TransformerConfigurationException {
 113         TransformerFactory tfactory = TransformerFactory.newInstance();
 114         Transformer transformer = tfactory.newTransformer();
 115 
 116         transformer.setParameter(SHORT_PARAM_NAME, PARAM_VALUE);
 117         assertEquals(transformer.getParameter(SHORT_PARAM_NAME).toString(), PARAM_VALUE);
 118     }
 119 
 120     /**
 121      * Obtains transformer's parameter with a short name that set with an integer
 122      * object before. Value should be same as the set integer object.
 123      * @throws TransformerConfigurationException If for some reason the
 124      *         TransformerHandler can not be created.
 125      */
 126     @Test
 127     public void clear04() throws TransformerConfigurationException {
 128         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 129 
 130         int intObject = 5;
 131         transformer.setParameter(SHORT_PARAM_NAME, intObject);
 132         assertEquals(transformer.getParameter(SHORT_PARAM_NAME), intObject);
 133     }
 134 
 135     /**
 136      * Obtains transformer's parameter whose initiated with a stream source with
 137      * the a name that set before. Value should be same as set one.
 138      * @throws TransformerConfigurationException If for some reason the
 139      *         TransformerHandler can not be created.
 140      */
 141     @Test
 142     public void clear05() throws TransformerConfigurationException {
 143         Transformer transformer = TransformerFactory.newInstance().
 144                 newTransformer(new StreamSource(new File(XSL_FILE)));
 145 
 146         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 147         assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 148     }
 149 
 150     /**
 151      * Obtains transformer's parameter whose initiated with a stream source with
 152      * the a name that wasn't set before. Null is expected.
 153      * @throws TransformerConfigurationException If for some reason the
 154      *         TransformerHandler can not be created.
 155      */
 156     @Test
 157     public void clear06() throws TransformerConfigurationException {
 158         Transformer transformer = TransformerFactory.newInstance().
 159                 newTransformer(new StreamSource(new File(XSL_FILE)));
 160         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 161         transformer.clearParameters();
 162         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 163     }
 164 
 165     /**
 166      * Obtains transformer's parameter whose initiated with a sax source with
 167      * the a name that set before. Value should be same as set one.
 168      * @throws Exception If any errors occur.
 169      */
 170     @Test
 171     public void clear07() throws Exception {
 172         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 173             SAXSource saxSource = new SAXSource();
 174             saxSource.setInputSource(new InputSource(fis));
 175 
 176             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 177             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 178             assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 179         }
 180     }
 181 
 182     /**
 183      * Obtains transformer's parameter whose initiated with a sax source with
 184      * the a name that wasn't set before. Null is expected.
 185      * @throws Exception If any errors occur.
 186      */
 187     @Test
 188     public void clear08() throws Exception {
 189         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 190             SAXSource saxSource = new SAXSource();
 191             saxSource.setInputSource(new InputSource(fis));
 192 
 193             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 194             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 195             transformer.clearParameters();
 196             assertNull(transformer.getParameter(LONG_PARAM_NAME));
 197         }
 198     }
 199 
 200     /**
 201      * Obtains transformer's parameter whose initiated with a dom source with
 202      * the a name that set before. Value should be same as set one.
 203      * @throws Exception If any errors occur.
 204      */
 205     @Test
 206     public void clear09() throws Exception {
 207         TransformerFactory tfactory = TransformerFactory.newInstance();
 208 
 209         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 210         dbf.setNamespaceAware(true);
 211         DocumentBuilder db = dbf.newDocumentBuilder();
 212         Document document = db.parse(new File(XSL_FILE));
 213         DOMSource domSource = new DOMSource((Node)document);
 214 
 215         Transformer transformer = tfactory.newTransformer(domSource);
 216 
 217         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 218         assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 219     }
 220 
 221     /**
 222      * Obtains transformer's parameter whose initiated with a dom source with
 223      * the a name that wasn't set before. Null is expected.
 224      * @throws Exception If any errors occur.
 225      */
 226     @Test
 227     public void clear10() throws Exception {
 228         TransformerFactory tfactory = TransformerFactory.newInstance();
 229 
 230         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 231         dbf.setNamespaceAware(true);
 232         DocumentBuilder db = dbf.newDocumentBuilder();
 233         Document document = db.parse(new File(XSL_FILE));
 234         DOMSource domSource = new DOMSource((Node)document);
 235 
 236         Transformer transformer = tfactory.newTransformer(domSource);
 237         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 238         transformer.clearParameters();
 239         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 240     }
 241 }
 242 
 243