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