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 
  24 package javax.xml.transform.ptests;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FilePermission;
  29 import java.io.IOException;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 import javax.xml.transform.dom.DOMSource;
  33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  34 import javax.xml.transform.sax.SAXSource;
  35 import javax.xml.transform.stream.StreamSource;
  36 import jaxp.library.JAXPBaseTest;
  37 import static org.testng.Assert.assertEquals;
  38 import static org.testng.Assert.assertNotNull;
  39 import static org.testng.Assert.assertNull;
  40 import org.testng.annotations.AfterGroups;
  41 import org.testng.annotations.BeforeGroups;
  42 import org.testng.annotations.Test;
  43 import org.xml.sax.InputSource;
  44 import org.xml.sax.SAXException;
  45 
  46 
  47 /**
  48  * Unit test for SAXSource sourceToInputSource API.
  49  */
  50 public class SAXSourceTest01 extends JAXPBaseTest {
  51     /**
  52      * Test style-sheet file name
  53      */
  54     private final String TEST_FILE = XML_DIR + "cities.xsl";
  55     
  56     /**
  57      * Save system property for restoring.
  58      */
  59     @BeforeGroups (groups = {"readLocalFiles"})
  60     public void setFilePermissions() {
  61         setPermissions(new FilePermission(TEST_FILE, "read"));
  62     }
  63     
  64     /**
  65      * Restore the system property.
  66      */
  67     @AfterGroups (groups = {"readLocalFiles"})
  68     public void restoreFilePermissions() {
  69         setPermissions();
  70     }
  71 
  72     /**
  73      * Test obtaining a SAX InputSource object from a Source object.
  74      * 
  75      * @throws IOException reading file error.
  76      */
  77     @Test(groups = {"readLocalFiles"})
  78     public void source2inputsource01() throws IOException {
  79         try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
  80             StreamSource streamSource = new StreamSource(fis);
  81             assertNotNull(SAXSource.sourceToInputSource(streamSource));
  82         }
  83     }
  84 
  85     /**
  86      * This test case tries to get InputSource from DOMSource using
  87      * sourceToInputSource method. It is not possible and hence null is
  88      * expected. This is a negative test case,
  89      * 
  90      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  91      *         created which satisfies the configuration requested.
  92      * @throws SAXException If any parse errors occur.
  93      * @throws IOException if the file exists but is a directory rather than
  94      *         a regular file, does not exist but cannot be created, or cannot 
  95      *         be opened for any other reason.
  96      */
  97     @Test(groups = {"readLocalFiles"})
  98     public void source2inputsource02() throws ParserConfigurationException, 
  99             SAXException, IOException {
 100         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 101         dbf.setNamespaceAware(true);
 102         dbf.newDocumentBuilder().parse(new File(TEST_FILE));
 103         assertNull(SAXSource.sourceToInputSource(new DOMSource(null)));
 104     }
 105 
 106     /**
 107      * This test case tries to get InputSource from SAXSource using
 108      * sourceToInputSource method. This will also check if the systemId
 109      * remained the same. This is a positive test case.
 110      * 
 111      * @throws IOException reading file error.
 112      */
 113     @Test(groups = {"readLocalFiles"})
 114     public void source2inputsource03() throws IOException {
 115         String SYSTEM_ID = "file:///" + XML_DIR;
 116         try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
 117             SAXSource saxSource =
 118                     new SAXSource(new InputSource(fis));
 119             saxSource.setSystemId(SYSTEM_ID);
 120             assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(),
 121                     SYSTEM_ID);
 122         }
 123     }
 124 }