1 /*
   2  * Copyright (c) 2003, 2015, 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.IOException;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.transform.dom.DOMSource;
  31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  32 import javax.xml.transform.sax.SAXSource;
  33 import javax.xml.transform.stream.StreamSource;
  34 import jaxp.library.JAXPFileReadOnlyBaseTest;
  35 import static org.testng.Assert.assertEquals;
  36 import static org.testng.Assert.assertNotNull;
  37 import static org.testng.Assert.assertNull;
  38 import org.testng.annotations.Test;
  39 import org.xml.sax.InputSource;
  40 
  41 
  42 /**
  43  * Unit test for SAXSource sourceToInputSource API.
  44  */
  45 public class SAXSourceTest extends JAXPFileReadOnlyBaseTest {
  46     /**
  47      * Test style-sheet file name
  48      */
  49     private final String TEST_FILE = XML_DIR + "cities.xsl";
  50 
  51     /**
  52      * Test obtaining a SAX InputSource object from a Source object.
  53      * 
  54      * @throws IOException reading file error.
  55      */
  56     @Test(groups = {"readLocalFiles"})
  57     public void source2inputsource01() throws IOException {
  58         try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
  59             StreamSource streamSource = new StreamSource(fis);
  60             assertNotNull(SAXSource.sourceToInputSource(streamSource));
  61         }
  62     }
  63 
  64     /**
  65      * This test case tries to get InputSource from DOMSource using
  66      * sourceToInputSource method. It is not possible and hence null is
  67      * expected. This is a negative test case,
  68      * 
  69      * @throws Exception If any errors occur.
  70      */
  71     @Test(groups = {"readLocalFiles"})
  72     public void source2inputsource02() throws Exception {
  73         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  74         dbf.setNamespaceAware(true);
  75         dbf.newDocumentBuilder().parse(new File(TEST_FILE));
  76         assertNull(SAXSource.sourceToInputSource(new DOMSource(null)));
  77     }
  78 
  79     /**
  80      * This test case tries to get InputSource from SAXSource using
  81      * sourceToInputSource method. This will also check if the systemId
  82      * remained the same. This is a positive test case.
  83      * 
  84      * @throws IOException reading file error.
  85      */
  86     @Test(groups = {"readLocalFiles"})
  87     public void source2inputsource03() throws IOException {
  88         String SYSTEM_ID = "file:///" + XML_DIR;
  89         try (FileInputStream fis = new FileInputStream(TEST_FILE)) {
  90             SAXSource saxSource =
  91                     new SAXSource(new InputSource(fis));
  92             saxSource.setSystemId(SYSTEM_ID);
  93             assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(),
  94                     SYSTEM_ID);
  95         }
  96     }
  97 }