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