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.io.InputStream; 27 28 import javax.xml.namespace.NamespaceContext; 29 import javax.xml.namespace.QName; 30 import javax.xml.stream.XMLInputFactory; 31 import javax.xml.stream.XMLStreamConstants; 32 import javax.xml.stream.XMLStreamReader; 33 34 import org.testng.Assert; 35 import org.testng.annotations.Listeners; 36 import org.testng.annotations.Test; 37 38 /* 39 * @summary Test StAX parser processes namespace. 40 */ 41 @Listeners({jaxp.library.BasePolicy.class}) 42 public class NamespaceTest { 43 44 String namespaceURI = "foobar.com"; 45 String rootElement = "foo"; 46 String childElement = "foochild"; 47 String prefix = "a"; 48 49 // Add test methods here, they have to start with 'test' name. 50 // for example: 51 // public void testHello() {} 52 53 String getXML() { 54 StringBuffer sbuffer = new StringBuffer(); 55 sbuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 56 sbuffer.append("<" + rootElement + " xmlns:"); 57 sbuffer.append(prefix); 58 sbuffer.append("=\"" + namespaceURI + "\">"); 59 sbuffer.append("<" + prefix + ":" + childElement + ">"); 60 sbuffer.append("blahblah"); 61 sbuffer.append("</" + prefix + ":" + childElement + ">"); 62 sbuffer.append("</" + rootElement + ">"); 63 // System.out.println("XML = " + sbuffer.toString()) ; 64 return sbuffer.toString(); 65 } 66 67 @Test 68 public void testRootElementNamespace() { 69 try { 70 XMLInputFactory xif = XMLInputFactory.newInstance(); 71 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); 72 InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes()); 73 XMLStreamReader sr = xif.createXMLStreamReader(is); 74 while (sr.hasNext()) { 75 int eventType = sr.next(); 76 if (eventType == XMLStreamConstants.START_ELEMENT) { 77 if (sr.getLocalName().equals(rootElement)) { 78 Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefix) && sr.getNamespaceURI(0).equals(namespaceURI)); 79 } 80 } 81 } 82 } catch (Exception ex) { 83 ex.printStackTrace(); 84 } 85 } 86 87 @Test 88 public void testChildElementNamespace() { 89 try { 90 XMLInputFactory xif = XMLInputFactory.newInstance(); 91 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); 92 InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes()); 93 XMLStreamReader sr = xif.createXMLStreamReader(is); 94 while (sr.hasNext()) { 95 int eventType = sr.next(); 96 if (eventType == XMLStreamConstants.START_ELEMENT) { 97 if (sr.getLocalName().equals(childElement)) { 98 QName qname = sr.getName(); 99 Assert.assertTrue(qname.getPrefix().equals(prefix) && qname.getNamespaceURI().equals(namespaceURI) 100 && qname.getLocalPart().equals(childElement)); 101 } 102 } 103 } 104 } catch (Exception ex) { 105 ex.printStackTrace(); 106 } 107 } 108 109 @Test 110 public void testNamespaceContext() { 111 try { 112 XMLInputFactory xif = XMLInputFactory.newInstance(); 113 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); 114 InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes()); 115 XMLStreamReader sr = xif.createXMLStreamReader(is); 116 while (sr.hasNext()) { 117 int eventType = sr.next(); 118 if (eventType == XMLStreamConstants.START_ELEMENT) { 119 if (sr.getLocalName().equals(childElement)) { 120 NamespaceContext context = sr.getNamespaceContext(); 121 Assert.assertTrue(context.getPrefix(namespaceURI).equals(prefix)); 122 } 123 } 124 } 125 } catch (Exception ex) { 126 ex.printStackTrace(); 127 } 128 } 129 130 @Test 131 public void testNamespaceCount() { 132 try { 133 XMLInputFactory xif = XMLInputFactory.newInstance(); 134 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); 135 InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes()); 136 XMLStreamReader sr = xif.createXMLStreamReader(is); 137 while (sr.hasNext()) { 138 int eventType = sr.next(); 139 if (eventType == XMLStreamConstants.START_ELEMENT) { 140 if (sr.getLocalName().equals(rootElement)) { 141 int count = sr.getNamespaceCount(); 142 Assert.assertTrue(count == 1); 143 } 144 } 145 } 146 } catch (Exception ex) { 147 ex.printStackTrace(); 148 } 149 } 150 151 }