1 /* 2 * Copyright (c) 2002, 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 test.astro; 24 25 import static jaxp.library.JAXPTestUtilities.filenameToURL; 26 import static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertNotNull; 28 import static org.testng.Assert.assertNull; 29 import static org.testng.Assert.assertTrue; 30 import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; 31 import static test.astro.AstroConstants.ASTROCAT; 32 33 import java.io.FileInputStream; 34 import java.io.FileOutputStream; 35 import java.io.InputStream; 36 import java.io.OutputStream; 37 import java.io.Writer; 38 39 import javax.xml.parsers.DocumentBuilder; 40 import javax.xml.parsers.DocumentBuilderFactory; 41 import javax.xml.parsers.ParserConfigurationException; 42 43 import org.testng.annotations.Listeners; 44 import org.testng.annotations.Test; 45 import org.w3c.dom.Document; 46 import org.w3c.dom.Element; 47 import org.w3c.dom.ls.DOMImplementationLS; 48 import org.w3c.dom.ls.LSInput; 49 import org.w3c.dom.ls.LSOutput; 50 import org.w3c.dom.ls.LSParser; 51 import org.w3c.dom.ls.LSSerializer; 52 53 /* 54 * @test 55 * @library /javax/xml/jaxp/libs 56 * @run testng/othervm -DrunSecMngr=true test.astro.DocumentLSTest 57 * @run testng/othervm test.astro.DocumentLSTest 58 * @summary org.w3c.dom.ls tests 59 */ 60 @Listeners({jaxp.library.FilePolicy.class}) 61 public class DocumentLSTest { 62 /* 63 * Test creating an empty Document 64 */ 65 @Test 66 public void testNewDocument() throws ParserConfigurationException { 67 Document doc = getDocumentBuilder().newDocument(); 68 assertNull(doc.getDocumentElement()); 69 } 70 71 /* 72 * Test creating an LSInput instance, and parsing ByteStream 73 */ 74 @Test 75 public void testLSInputParsingByteStream() throws Exception { 76 DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation(); 77 LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null); 78 LSInput src = impl.createLSInput(); 79 80 try (InputStream is = new FileInputStream(ASTROCAT)) { 81 src.setByteStream(is); 82 assertNotNull(src.getByteStream()); 83 // set certified accessor methods 84 boolean origCertified = src.getCertifiedText(); 85 src.setCertifiedText(true); 86 assertTrue(src.getCertifiedText()); 87 src.setCertifiedText(origCertified); // set back to orig 88 89 src.setSystemId(filenameToURL(ASTROCAT)); 90 91 Document doc = domParser.parse(src); 92 Element result = doc.getDocumentElement(); 93 assertEquals(result.getTagName(), "stardb"); 94 } 95 } 96 97 /* 98 * Test creating an LSInput instance, and parsing String 99 */ 100 @Test 101 public void testLSInputParsingString() throws Exception { 102 DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation(); 103 String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>"; 104 105 LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null); 106 LSSerializer domSerializer = impl.createLSSerializer(); 107 // turn off xml decl in serialized string for comparison 108 domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); 109 LSInput src = impl.createLSInput(); 110 src.setStringData(xml); 111 assertEquals(src.getStringData(), xml); 112 113 Document doc = domParser.parse(src); 114 String result = domSerializer.writeToString(doc); 115 116 assertEquals(result, "<test>runDocumentLS_Q6</test>"); 117 } 118 119 /* 120 * Test writing with an LSOutput instance 121 */ 122 @Test 123 public void testLSOutput() throws Exception { 124 DocumentBuilder db = getDocumentBuilder(); 125 // Create the Document using the supplied builder... 126 Document doc = db.parse(ASTROCAT); 127 128 DOMImplementationLS impl = null; 129 130 impl = (DOMImplementationLS) db.getDOMImplementation(); 131 LSSerializer domSerializer = impl.createLSSerializer(); 132 MyDOMOutput mydomoutput = new MyDOMOutput(); 133 try (OutputStream os = new FileOutputStream("test.out")) { 134 mydomoutput.setByteStream(os); 135 mydomoutput.setEncoding("UTF-8"); 136 assertTrue(domSerializer.write(doc, mydomoutput)); 137 } 138 } 139 140 private static class MyDOMOutput implements LSOutput { 141 private OutputStream bytestream = null; 142 private String encoding = null; 143 private String sysId = null; 144 private Writer writer = null; 145 146 public OutputStream getByteStream() { 147 return bytestream; 148 } 149 150 public Writer getCharacterStream() { 151 return writer; 152 } 153 154 public String getEncoding() { 155 return encoding; 156 } 157 158 public String getSystemId() { 159 return sysId; 160 } 161 162 public void setByteStream(OutputStream bs) { 163 bytestream = bs; 164 } 165 166 public void setCharacterStream(Writer cs) { 167 writer = cs; 168 } 169 170 public void setEncoding(String enc) { 171 encoding = enc; 172 } 173 174 public void setSystemId(String sysId) { 175 this.sysId = sysId; 176 } 177 } 178 179 private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 180 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 181 dbf.setNamespaceAware(true); 182 return dbf.newDocumentBuilder(); 183 } 184 }