1 /*
   2  * Copyright (c) 2014, 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 java.io.File;
  26 import java.io.FileInputStream;
  27 import java.io.FilePermission;
  28 import java.io.IOException;
  29 import javax.xml.parsers.DocumentBuilder;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 import javax.xml.transform.Transformer;
  33 import javax.xml.transform.TransformerConfigurationException;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMSource;
  36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  37 import javax.xml.transform.sax.SAXSource;
  38 import javax.xml.transform.stream.StreamSource;
  39 import jaxp.library.JAXPBaseTest;
  40 import static org.testng.Assert.assertEquals;
  41 import static org.testng.Assert.assertNull;
  42 import org.testng.annotations.AfterGroups;
  43 import org.testng.annotations.BeforeGroups;
  44 import org.testng.annotations.Test;
  45 import org.w3c.dom.Document;
  46 import org.w3c.dom.Node;
  47 import org.xml.sax.InputSource;
  48 import org.xml.sax.SAXException;
  49 
  50 /**
  51  * Class containing the test cases for SAXParserFactory API
  52  */
  53 public class TfClearParamTest extends JAXPBaseTest {
  54     /**
  55      * Test style-sheet file name.
  56      */
  57     private final String XSL_FILE = XML_DIR + "cities.xsl";
  58 
  59     /**
  60      * Long parameter name embedded with a URI.
  61      */
  62     private final String LONG_PARAM_NAME = "{http://xyz.foo.com/yada/baz.html}foo";
  63 
  64     /**
  65      * Short parameter name.
  66      */
  67     private final String SHORT_PARAM_NAME = "foo";
  68 
  69     /**
  70      * Parameter value.
  71      */
  72     private final String PARAM_VALUE = "xyz";
  73 
  74     /**
  75      * Obtains transformer's parameter with the same name that set before. Value
  76      * should be same as set one.
  77      * @throws TransformerConfigurationException If for some reason the
  78      *         TransformerHandler can not be created.
  79      */
  80     @Test
  81     public void clear01() throws TransformerConfigurationException {
  82         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  83         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
  84         assertEquals(transformer.getParameter(LONG_PARAM_NAME).toString(), PARAM_VALUE);
  85     }
  86 
  87     /**
  88      * Obtains transformer's parameter with the a name that wasn't set before.
  89      * Null is expected.
  90      * @throws TransformerConfigurationException If for some reason the
  91      *         TransformerHandler can not be created.
  92      */
  93     @Test
  94     public void clear02() throws TransformerConfigurationException {
  95         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  96         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
  97         transformer.clearParameters();
  98         assertNull(transformer.getParameter(LONG_PARAM_NAME));
  99     }
 100 
 101     /**
 102      * Save system property for restoring.
 103      */
 104     @BeforeGroups (groups = {"readLocalFiles"})
 105     public void setFilePermissions() {
 106         setPermissions(new FilePermission(XSL_FILE, "read"));
 107     }
 108     
 109     /**
 110      * Restore the system property.
 111      */
 112     @AfterGroups (groups = {"readLocalFiles"})
 113     public void restoreFilePermissions() {
 114         setPermissions();
 115     }
 116 
 117     /**
 118      * Obtains transformer's parameter whose initiated with a stream source with
 119      * the a name that set before. Value should be same as set one.
 120      * @throws TransformerConfigurationException If for some reason the
 121      *         TransformerHandler can not be created.
 122      */
 123     @Test (groups = {"readLocalFiles"})
 124     public void clear03() throws TransformerConfigurationException {
 125         Transformer transformer = TransformerFactory.newInstance().
 126                 newTransformer(new StreamSource(new File(XSL_FILE)));
 127 
 128         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 129         assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 130     }
 131 
 132     /**
 133      * Obtains transformer's parameter whose initiated with a stream source with
 134      * the a name that wasn't set before. Null is expected.
 135      * @throws TransformerConfigurationException If for some reason the
 136      *         TransformerHandler can not be created.
 137      */
 138     @Test (groups = {"readLocalFiles"})
 139     public void clear04() throws TransformerConfigurationException {
 140         Transformer transformer = TransformerFactory.newInstance().
 141                 newTransformer(new StreamSource(new File(XSL_FILE)));
 142         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 143         transformer.clearParameters();
 144         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 145     }
 146 
 147     /**
 148      * Obtains transformer's parameter whose initiated with a sax source with
 149      * the a name that set before. Value should be same as set one.
 150      * @throws IOException if the file does not exist, is a directory rather 
 151      *         than a regular file, or for some other reason cannot be opened 
 152      *         for reading.
 153      * @throws TransformerConfigurationException If for some reason the
 154      *         TransformerHandler can not be created.
 155      */
 156     @Test (groups = {"readLocalFiles"})
 157     public void clear05() throws IOException, TransformerConfigurationException {
 158         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 159             SAXSource saxSource = new SAXSource();
 160             saxSource.setInputSource(new InputSource(fis));
 161 
 162             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 163             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 164             assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 165         }
 166     }
 167 
 168     /**
 169      * Obtains transformer's parameter whose initiated with a sax source with
 170      * the a name that wasn't set before. Null is expected.
 171      * @throws IOException if the file does not exist, is a directory rather 
 172      *         than a regular file, or for some other reason cannot be opened 
 173      *         for reading.
 174      * @throws TransformerConfigurationException If for some reason the
 175      *         TransformerHandler can not be created.
 176      */
 177     @Test (groups = {"readLocalFiles"})
 178     public void clear06() throws IOException, TransformerConfigurationException {
 179         try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
 180             SAXSource saxSource = new SAXSource();
 181             saxSource.setInputSource(new InputSource(fis));
 182 
 183             Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
 184             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 185             transformer.clearParameters();
 186             assertNull(transformer.getParameter(LONG_PARAM_NAME));
 187         }
 188     }
 189 
 190     /**
 191      * Obtains transformer's parameter whose initiated with a dom source with
 192      * the a name that set before. Value should be same as set one.
 193      * @throws IOException if the file does not exist, is a directory rather 
 194      *         than a regular file, or for some other reason cannot be opened 
 195      *         for reading.
 196      * @throws TransformerConfigurationException If for some reason the
 197      *         TransformerHandler can not be created.
 198      * @throws SAXException If any parse errors occur.
 199      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 200      *         created which satisfies the configuration requested.
 201      */
 202     @Test (groups = {"readLocalFiles"})
 203     public void clear07() throws ParserConfigurationException, SAXException, 
 204             IOException, TransformerConfigurationException {
 205             TransformerFactory tfactory = TransformerFactory.newInstance();
 206 
 207             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 208             dbf.setNamespaceAware(true);
 209             DocumentBuilder db = dbf.newDocumentBuilder();
 210             Document document = db.parse(new File(XSL_FILE));
 211             DOMSource domSource = new DOMSource((Node)document);
 212 
 213             Transformer transformer = tfactory.newTransformer(domSource);
 214 
 215             transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 216             assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
 217     }
 218 
 219     /**
 220      * Obtains transformer's parameter whose initiated with a dom source with
 221      * the a name that wasn't set before. Null is expected.
 222      * @throws IOException if the file does not exist, is a directory rather 
 223      *         than a regular file, or for some other reason cannot be opened 
 224      *         for reading.
 225      * @throws TransformerConfigurationException If for some reason the
 226      *         TransformerHandler can not be created.
 227      * @throws SAXException If any parse errors occur.
 228      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 229      *         created which satisfies the configuration requested.
 230      */
 231     @Test (groups = {"readLocalFiles"})
 232     public void clear08() throws ParserConfigurationException, SAXException, 
 233             IOException, TransformerConfigurationException {
 234         TransformerFactory tfactory = TransformerFactory.newInstance();
 235 
 236         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 237         dbf.setNamespaceAware(true);
 238         DocumentBuilder db = dbf.newDocumentBuilder();
 239         Document document = db.parse(new File(XSL_FILE));
 240         DOMSource domSource = new DOMSource((Node)document);
 241 
 242         Transformer transformer = tfactory.newTransformer(domSource);
 243         transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
 244         transformer.clearParameters();
 245         assertNull(transformer.getParameter(LONG_PARAM_NAME));
 246     }
 247 
 248     /**
 249      * Obtains transformer's parameter with a short name that set before. Value
 250      * should be same as set one.
 251      * @throws TransformerConfigurationException If for some reason the
 252      *         TransformerHandler can not be created.
 253      */
 254     @Test
 255     public void clear09() throws TransformerConfigurationException {
 256         TransformerFactory tfactory = TransformerFactory.newInstance();
 257         Transformer transformer = tfactory.newTransformer();
 258 
 259         transformer.setParameter(SHORT_PARAM_NAME, PARAM_VALUE);
 260         assertEquals(transformer.getParameter(SHORT_PARAM_NAME).toString(), PARAM_VALUE);
 261     }
 262 
 263     /**
 264      * Obtains transformer's parameter with a short name that set with an integer
 265      * object before. Value should be same as the set integer object.
 266      * @throws TransformerConfigurationException If for some reason the
 267      *         TransformerHandler can not be created.
 268      */
 269     @Test
 270     public void clear10() throws TransformerConfigurationException {
 271         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 272 
 273         int intObject = 5;
 274         transformer.setParameter(SHORT_PARAM_NAME, intObject);
 275         assertEquals(transformer.getParameter(SHORT_PARAM_NAME), intObject);
 276     }
 277 }