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