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