1 /* 2 * Copyright (c) 2014, 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 stream.XMLStreamWriterTest; 24 25 import java.io.StringWriter; 26 import javax.xml.parsers.DocumentBuilder; 27 import javax.xml.parsers.DocumentBuilderFactory; 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.stream.XMLEventFactory; 30 import javax.xml.stream.XMLEventWriter; 31 32 import javax.xml.stream.XMLOutputFactory; 33 import javax.xml.stream.XMLStreamException; 34 import javax.xml.stream.XMLStreamWriter; 35 import javax.xml.stream.events.XMLEvent; 36 import javax.xml.transform.dom.DOMResult; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 import org.w3c.dom.Document; 42 43 /* 44 * @test 45 * @bug 6347190 8139584 46 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 47 * @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.XMLStreamWriterTest 48 * @run testng/othervm stream.XMLStreamWriterTest.XMLStreamWriterTest 49 * @summary Test StAX Writer won't insert comment into element inside. 50 */ 51 @Listeners({jaxp.library.BasePolicy.class}) 52 public class XMLStreamWriterTest { 53 /** 54 * @bug 8139584 55 * Verifies that the resulting XML contains the standalone setting. 56 */ 57 @Test 58 public void testCreateStartDocument() throws XMLStreamException { 59 60 StringWriter stringWriter = new StringWriter(); 61 XMLOutputFactory out = XMLOutputFactory.newInstance(); 62 XMLEventFactory eventFactory = XMLEventFactory.newInstance(); 63 64 XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter); 65 66 XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true); 67 eventWriter.add(event); 68 eventWriter.flush(); 69 Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\"")); 70 Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\"")); 71 Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\"")); 72 } 73 74 /** 75 * @bug 8139584 76 * Verifies that the resulting XML contains the standalone setting. 77 */ 78 @Test 79 public void testCreateStartDocument_DOMWriter() 80 throws ParserConfigurationException, XMLStreamException { 81 82 XMLOutputFactory xof = XMLOutputFactory.newInstance(); 83 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 84 DocumentBuilder db = dbf.newDocumentBuilder(); 85 Document doc = db.newDocument(); 86 XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc)); 87 XMLEventFactory eventFactory = XMLEventFactory.newInstance(); 88 XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true); 89 eventWriter.add(event); 90 eventWriter.flush(); 91 Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15"); 92 Assert.assertEquals(doc.getXmlVersion(), "1.0"); 93 Assert.assertTrue(doc.getXmlStandalone()); 94 } 95 96 /** 97 * Test of main method, of class TestXMLStreamWriter. 98 */ 99 @Test 100 public void testWriteComment() { 101 try { 102 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html href=\"http://java.sun.com\"><!--This is comment-->java.sun.com</a:html>"; 103 XMLOutputFactory f = XMLOutputFactory.newInstance(); 104 // f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, 105 // Boolean.TRUE); 106 StringWriter sw = new StringWriter(); 107 XMLStreamWriter writer = f.createXMLStreamWriter(sw); 108 writer.writeStartDocument("UTF-8", "1.0"); 109 writer.writeStartElement("a", "html", "http://www.w3.org/TR/REC-html40"); 110 writer.writeAttribute("href", "http://java.sun.com"); 111 writer.writeComment("This is comment"); 112 writer.writeCharacters("java.sun.com"); 113 writer.writeEndElement(); 114 writer.writeEndDocument(); 115 writer.flush(); 116 sw.flush(); 117 StringBuffer sb = sw.getBuffer(); 118 System.out.println("sb:" + sb.toString()); 119 Assert.assertTrue(sb.toString().equals(xml)); 120 } catch (Exception ex) { 121 Assert.fail("Exception: " + ex.getMessage()); 122 } 123 } 124 125 }