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