1 /* 2 * Copyright (c) 2014, 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.XMLStreamReaderTest; 25 26 import java.util.Iterator; 27 28 import javax.xml.stream.XMLEventReader; 29 import javax.xml.stream.XMLInputFactory; 30 import javax.xml.stream.XMLOutputFactory; 31 import javax.xml.stream.XMLStreamConstants; 32 import javax.xml.stream.XMLStreamReader; 33 import javax.xml.stream.events.StartElement; 34 import javax.xml.stream.events.XMLEvent; 35 36 import org.testng.Assert; 37 import org.testng.annotations.Listeners; 38 import org.testng.annotations.Test; 39 40 /* 41 * @summary Test StAX parses namespace and attribute. 42 */ 43 @Listeners({jaxp.library.FilePolicy.class}) 44 public class DefaultAttributeTest { 45 46 private static final String INPUT_FILE = "ExternalDTD.xml"; 47 48 @Test 49 public void testStreamReader() { 50 XMLInputFactory ifac = XMLInputFactory.newInstance(); 51 XMLOutputFactory ofac = XMLOutputFactory.newInstance(); 52 53 try { 54 ifac.setProperty(ifac.IS_REPLACING_ENTITY_REFERENCES, new Boolean(false)); 55 56 XMLStreamReader re = ifac.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(), 57 this.getClass().getResourceAsStream(INPUT_FILE)); 58 59 while (re.hasNext()) { 60 int event = re.next(); 61 if (event == XMLStreamConstants.START_ELEMENT && re.getLocalName().equals("bookurn")) { 62 Assert.assertTrue(re.getAttributeCount() == 0, "No attributes are expected for <bookurn> "); 63 Assert.assertTrue(re.getNamespaceCount() == 2, "Two namespaces are expected for <bookurn> "); 64 } 65 } 66 } catch (Exception e) { 67 e.printStackTrace(); 68 Assert.fail("Exception occured: " + e.getMessage()); 69 } 70 } 71 72 @Test 73 public void testEventReader() { 74 try { 75 XMLInputFactory ifac = XMLInputFactory.newInstance(); 76 XMLEventReader read = ifac.createXMLEventReader(this.getClass().getResource(INPUT_FILE).toExternalForm(), 77 this.getClass().getResourceAsStream(INPUT_FILE)); 78 while (read.hasNext()) { 79 XMLEvent event = read.nextEvent(); 80 if (event.isStartElement()) { 81 StartElement startElement = event.asStartElement(); 82 if (startElement.getName().getLocalPart().equals("bookurn")) { 83 Iterator iterator = startElement.getNamespaces(); 84 int count = 0; 85 while (iterator.hasNext()) { 86 iterator.next(); 87 count++; 88 } 89 Assert.assertTrue(count == 2, "Two namespaces are expected for <bookurn> "); 90 91 Iterator attributes = startElement.getAttributes(); 92 count = 0; 93 while (attributes.hasNext()) { 94 iterator.next(); 95 count++; 96 } 97 Assert.assertTrue(count == 0, "Zero attributes are expected for <bookurn> "); 98 } 99 } 100 } 101 } catch (Exception e) { 102 e.printStackTrace(); 103 Assert.fail("Exception occured: " + e.getMessage()); 104 } 105 } 106 }