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 24 package stream.EventsTest; 25 26 import java.io.StringReader; 27 import java.io.StringWriter; 28 import java.util.Iterator; 29 import java.util.List; 30 31 import javax.xml.namespace.QName; 32 import javax.xml.stream.XMLEventFactory; 33 import javax.xml.stream.XMLEventReader; 34 import javax.xml.stream.XMLInputFactory; 35 import javax.xml.stream.XMLOutputFactory; 36 import javax.xml.stream.XMLStreamConstants; 37 import javax.xml.stream.XMLStreamException; 38 import javax.xml.stream.events.Attribute; 39 import javax.xml.stream.events.Characters; 40 import javax.xml.stream.events.Comment; 41 import javax.xml.stream.events.DTD; 42 import javax.xml.stream.events.EndDocument; 43 import javax.xml.stream.events.EndElement; 44 import javax.xml.stream.events.Namespace; 45 import javax.xml.stream.events.ProcessingInstruction; 46 import javax.xml.stream.events.StartDocument; 47 import javax.xml.stream.events.StartElement; 48 import javax.xml.stream.events.XMLEvent; 49 50 import org.testng.Assert; 51 import org.testng.annotations.Listeners; 52 import org.testng.annotations.Test; 53 54 /* 55 * @test 56 * @bug 6631268 57 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 58 * @run testng/othervm -DrunSecMngr=true stream.EventsTest.Issue41Test 59 * @run testng/othervm stream.EventsTest.Issue41Test 60 * @summary Test XMLEvent.writeAsEncodedUnicode can output the event content. 61 */ 62 @Listeners({jaxp.library.BasePolicy.class}) 63 public class Issue41Test { 64 65 public java.io.File input; 66 public final String filesDir = "./"; 67 protected XMLInputFactory inputFactory; 68 protected XMLOutputFactory outputFactory; 69 70 @Test 71 public void testEvents() { 72 XMLEventFactory f = XMLEventFactory.newInstance(); 73 final String contents = "test <some> text & more! [[]] --"; 74 final String prefix = "prefix"; 75 final String uri = "http://foo"; 76 final String localName = "elem"; 77 78 try { 79 StartDocument sd = f.createStartDocument(); 80 writeAsEncodedUnicode(sd); 81 82 Comment c = f.createComment("some comments"); 83 writeAsEncodedUnicode(c); 84 85 StartElement se = f.createStartElement(prefix, uri, localName); 86 87 ProcessingInstruction pi = f.createProcessingInstruction("target", "data"); 88 writeAsEncodedUnicode(pi); 89 90 Namespace ns = f.createNamespace(prefix, uri); 91 writeAsEncodedUnicode(ns); 92 93 Characters characters = f.createCharacters(contents); 94 writeAsEncodedUnicode(characters); 95 // CData 96 Characters cdata = f.createCData(contents); 97 writeAsEncodedUnicode(cdata); 98 99 // Attribute 100 QName attrName = new QName("http://test.com", "attr", "ns"); 101 Attribute attr = f.createAttribute(attrName, "value"); 102 writeAsEncodedUnicode(attr); 103 104 // prefix, uri, localName 105 EndElement ee = f.createEndElement(prefix, uri, localName); 106 writeAsEncodedUnicode(ee); 107 108 EndDocument ed = f.createEndDocument(); 109 writeAsEncodedUnicode(ed); 110 } catch (Exception e) { 111 Assert.fail(e.getMessage()); 112 } 113 114 } 115 116 /** 117 * DTDEvent instances constructed via event reader are missing the notation 118 * and entity declaration information 119 */ 120 @Test 121 public void testDTDEvent() { 122 String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [\n" + "<!ENTITY intEnt 'internal'>\n" + "<!ENTITY extParsedEnt SYSTEM 'url:dummy'>\n" 123 + "<!NOTATION notation PUBLIC 'notation-public-id'>\n" + "<!NOTATION notation2 SYSTEM 'url:dummy'>\n" 124 + "<!ENTITY extUnparsedEnt SYSTEM 'url:dummy2' NDATA notation>\n" + "]>" + "<root />"; 125 126 try { 127 XMLEventReader er = getReader(XML); 128 XMLEvent evt = er.nextEvent(); // StartDocument 129 evt = er.nextEvent(); // DTD 130 if (evt.getEventType() != XMLStreamConstants.DTD) { 131 Assert.fail("Expected DTD event"); 132 } 133 DTD dtd = (DTD) evt; 134 writeAsEncodedUnicode(dtd); 135 List entities = dtd.getEntities(); 136 if (entities == null) { 137 Assert.fail("No entity found. Expected 3."); 138 } else { 139 writeAsEncodedUnicode((XMLEvent) entities.get(0)); 140 writeAsEncodedUnicode((XMLEvent) entities.get(1)); 141 writeAsEncodedUnicode((XMLEvent) entities.get(2)); 142 } 143 144 List notations = dtd.getNotations(); 145 if (notations == null) { 146 Assert.fail("No notation found. Expected 2."); 147 } else { 148 writeAsEncodedUnicode((XMLEvent) notations.get(0)); 149 writeAsEncodedUnicode((XMLEvent) notations.get(1)); 150 } 151 } catch (Exception e) { 152 Assert.fail(e.getMessage()); 153 } 154 } 155 156 private XMLEventReader getReader(String XML) throws Exception { 157 inputFactory = XMLInputFactory.newInstance(); 158 159 // Check if event reader returns the correct event 160 XMLEventReader er = inputFactory.createXMLEventReader(new StringReader(XML)); 161 return er; 162 } 163 164 165 166 /** 167 * The return of XMLEvent writeAsEncodedUnicode method is not defined This 168 * method merely tests that the output exists 169 */ 170 public void writeAsEncodedUnicode(XMLEvent evt) throws XMLStreamException { 171 if (evt.getEventType() == XMLStreamConstants.END_DOCUMENT) { 172 return; 173 } 174 StringWriter sw = new StringWriter(); 175 evt.writeAsEncodedUnicode(sw); 176 177 Assert.assertTrue(sw.toString().length() > 0); 178 System.out.println(sw.toString()); 179 } 180 } 181