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 @Listeners({jaxp.library.FilePolicy.class})
  51 public class TfClearParamTest {
  52     /**
  53      * Test style-sheet file name.
  54      */
  55     private final String XSL_FILE = XML_DIR + "cities.xsl";
  56 
  57     /**
  58      * Long parameter name embedded with a URI.
  59      */
  60     private final String LONG_PARAM_NAME = "{http://xyz.foo.com/yada/baz.html}foo";
  61 
  62     /**
  63      * Short parameter name.
  64      */
  65     private final String SHORT_PARAM_NAME = "foo";
  66 
  67     /**
  68      * Parameter value.
  69      */
  70     private final String PARAM_VALUE = "xyz";
  71 
  72     /**
  73      * Obtains transformer's parameter with the same name that set before. Value
  74      * should be same as set one.
  75      * @throws TransformerConfigurationException If for some reason the
  76      *         TransformerHandler can not be created.
  77      */
  78     @Test
  79     public void clear01() throws TransformerConfigurationException {
  80         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  81         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
  82         assertEquals(transformer.getParameter(LONG_PARAM_NAME).toString(), PARAM_VALUE);
  83     }
  84 
  85     /**
  86      * Obtains transformer's parameter with the a name that wasn't set before.
  87      * Null is expected.
  88      * @throws TransformerConfigurationException If for some reason the
  89      *         TransformerHandler can not be created.
  90      */
  91     @Test
  92     public void clear02() throws TransformerConfigurationException {
  93         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  94         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
  95         transformer.clearParameters();
  96         assertNull(transformer.getParameter(LONG_PARAM_NAME));
  97     }
  98 
  99     /**
 100      * Obtains transformer's parameter with a short name that set before. Value
 101      * should be same as set one.
 102      * @throws TransformerConfigurationException If for some reason the
 103      *         TransformerHandler can not be created.
 104      */
 105     @Test
 106     public void clear03() throws TransformerConfigurationException {
 107         TransformerFactory tfactory = TransformerFactory.newInstance();
 108         Transformer transformer = tfactory.newTransformer();
 109 
 110         transformer.setParameter(SHORT_PARAM_NAME, PARAM_VALUE);
 111         assertEquals(transformer.getParameter(SHORT_PARAM_NAME).toString(), PARAM_VALUE);
 112     }
 113 
 114     /**
 115      * Obtains transformer's parameter with a short name that set with an integer
 116      * object before. Value should be same as the set integer object.
 117      * @throws TransformerConfigurationException If for some reason the
 118      *         TransformerHandler can not be created.
 119      */
 120     @Test
 121     public void clear04() throws TransformerConfigurationException {
 122         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 123 
 124         int intObject = 5;
 125         transformer.setParameter(SHORT_PARAM_NAME, intObject);
 126         assertEquals(transformer.getParameter(SHORT_PARAM_NAME), intObject);
 127     }
 128 
 129     /**
 130      * Obtains transformer's parameter whose initiated with a stream source with
 131      * the a name that set before. Value should be same as set one.
 132      * @throws TransformerConfigurationException If for some reason the
 133      *         TransformerHandler can not be created.
 134      */
 135     @Test 
 136     public void clear05() throws TransformerConfigurationException {
 137         Transformer transformer = TransformerFactory.newInstance().
 138                 newTransformer(new StreamSource(new File(XSL_FILE)));
 139 
 140         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 141         assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 142     }
 143 
 144     /**
 145      * Obtains transformer's parameter whose initiated with a stream source with
 146      * the a name that wasn't set before. Null is expected.
 147      * @throws TransformerConfigurationException If for some reason the
 148      *         TransformerHandler can not be created.
 149      */
 150     @Test 
 151     public void clear06() throws TransformerConfigurationException {
 152         Transformer transformer = TransformerFactory.newInstance().
 153                 newTransformer(new StreamSource(new File(XSL_FILE)));
 154         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 155         transformer.clearParameters();
 156         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 157     }
 158 
 159     /**
 160      * Obtains transformer's parameter whose initiated with a sax source with
 161      * the a name that set before. Value should be same as set one.
 162      * @throws Exception If any errors occur.
 163      */
 164     @Test 
 165     public void clear07() throws Exception {
 166         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 167             SAXSource saxSource = new SAXSource();
 168             saxSource.setInputSource(new InputSource(fis));
 169 
 170             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 171             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 172             assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 173         }
 174     }
 175 
 176     /**
 177      * Obtains transformer's parameter whose initiated with a sax source with
 178      * the a name that wasn't set before. Null is expected.
 179      * @throws Exception If any errors occur.
 180      */
 181     @Test 
 182     public void clear08() throws Exception {
 183         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 184             SAXSource saxSource = new SAXSource();
 185             saxSource.setInputSource(new InputSource(fis));
 186 
 187             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 188             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 189             transformer.clearParameters();
 190             assertNull(transformer.getParameter(LONG_PARAM_NAME));
 191         }
 192     }
 193 
 194     /**
 195      * Obtains transformer's parameter whose initiated with a dom source with
 196      * the a name that set before. Value should be same as set one.
 197      * @throws Exception If any errors occur.
 198      */
 199     @Test 
 200     public void clear09() throws Exception {
 201         TransformerFactory tfactory = TransformerFactory.newInstance();
 202 
 203         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 204         dbf.setNamespaceAware(true);
 205         DocumentBuilder db = dbf.newDocumentBuilder();
 206         Document document = db.parse(new File(XSL_FILE));
 207         DOMSource domSource = new DOMSource((Node)document);
 208 
 209         Transformer transformer = tfactory.newTransformer(domSource);
 210 
 211         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 212         assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 213     }
 214 
 215     /**
 216      * Obtains transformer's parameter whose initiated with a dom source with
 217      * the a name that wasn't set before. Null is expected.
 218      * @throws Exception If any errors occur.
 219      */
 220     @Test 
 221     public void clear10() throws Exception {
 222         TransformerFactory tfactory = TransformerFactory.newInstance();
 223 
 224         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 225         dbf.setNamespaceAware(true);
 226         DocumentBuilder db = dbf.newDocumentBuilder();
 227         Document document = db.parse(new File(XSL_FILE));
 228         DOMSource domSource = new DOMSource((Node)document);
 229 
 230         Transformer transformer = tfactory.newTransformer(domSource);
 231         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 232         transformer.clearParameters();
 233         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 234     }
 235 }