KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmlpull > v1 > tests > TestSimpleWithNs


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
3

4 package org.xmlpull.v1.tests;
5
6 //import junit.framework.Test;
7
import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.StringReader JavaDoc;
12
13 import org.xmlpull.v1.XmlPullParser;
14 import org.xmlpull.v1.XmlPullParserFactory;
15 import org.xmlpull.v1.XmlPullParserException;
16
17 /**
18  * Simple test for minimal XML parsing with namespaces
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestSimpleWithNs extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public TestSimpleWithNs(String JavaDoc name) {
26         super(name);
27     }
28
29     protected void setUp() throws XmlPullParserException {
30         factory = factoryNewInstance();
31         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
32         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
33     }
34
35     protected void tearDown() {
36     }
37
38     public void testSimpleWithNs() throws Exception JavaDoc {
39         XmlPullParser xpp = factory.newPullParser();
40         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
41
42         // check setInput semantics
43
assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
44         try {
45             xpp.next();
46             fail("exception was expected of next() if no input was set on parser");
47         } catch(XmlPullParserException ex) {}
48
49         xpp.setInput(null);
50         assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
51         try {
52             xpp.next();
53             fail("exception was expected of next() if no input was set on parser");
54         } catch(XmlPullParserException ex) {}
55
56
57         // check the simplest possible XML document - just one root element
58
xpp.setInput(new StringReader JavaDoc("<foo></foo>"));
59         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
60         xpp.next();
61         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false/*empty*/, 0);
62         xpp.next();
63         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
64         xpp.next();
65         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
66
67         xpp.setInput(new StringReader JavaDoc("<foo/>"));
68         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
69         xpp.next();
70         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, true/*empty*/, 0);
71         xpp.next();
72         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
73         xpp.next();
74         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
75
76
77         // one step further - it has content ...
78

79
80         xpp.setInput(new StringReader JavaDoc("<foo attrName='attrVal'>bar</foo>"));
81         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
82         xpp.next();
83         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 1);
84         checkAttribNs(xpp, 0, null, "", "attrName", "attrVal");
85         xpp.next();
86         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, "bar", false, -1);
87         xpp.next();
88         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
89         xpp.next();
90         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
91
92
93         byte[] binput = ("<foo xmlns='n' xmlns:ns1='n1' xmlns:ns2='n2'>"+
94                              "<ns1:bar xmlns:ns1='x1' xmlns:ns3='n3' xmlns='n1'>"+
95                              "<ns2:gugu a1='v1' ns2:a2='v2' xml:lang='en' ns1:a3=\"v3\"/>"+
96                              "<baz xmlns:ns1='y1'></baz>"+
97                              "</ns1:bar></foo>").getBytes("US-ASCII");
98         xpp.setInput(new ByteArrayInputStream JavaDoc( binput ), "US-ASCII" );
99         assertEquals("US-ASCII", xpp.getInputEncoding());
100
101         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
102
103         xpp.next();
104         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 3, "n", "foo", null, false, 0);
105         assertEquals(0, xpp.getNamespaceCount(0));
106         assertEquals(3, xpp.getNamespaceCount(1));
107         checkNamespace(xpp, 0, null, "n", true);
108         checkNamespace(xpp, 1, "ns1", "n1", true);
109         checkNamespace(xpp, 2, "ns2", "n2", true);
110
111         xpp.next();
112         checkParserStateNs(xpp, 2, xpp.START_TAG, "ns1", 6, "x1", "bar", null, false, 0);
113         assertEquals(0, xpp.getNamespaceCount(0));
114         assertEquals(3, xpp.getNamespaceCount(1));
115         assertEquals(6, xpp.getNamespaceCount(2));
116         checkNamespace(xpp, 3, "ns1", "x1", true);
117         checkNamespace(xpp, 4, "ns3", "n3", true);
118         checkNamespace(xpp, 5, null, "n1", true);
119
120         xpp.next();
121         checkParserStateNs(xpp, 3, xpp.START_TAG, "ns2", 6, "n2", "gugu", null, true, 4);
122         assertEquals(6, xpp.getNamespaceCount(2));
123         assertEquals(6, xpp.getNamespaceCount(3));
124         assertEquals("x1", xpp.getNamespace("ns1"));
125         assertEquals("n2", xpp.getNamespace("ns2"));
126         assertEquals("n3", xpp.getNamespace("ns3"));
127         checkAttribNs(xpp, 0, null, "", "a1", "v1");
128         checkAttribNs(xpp, 1, "ns2", "n2", "a2", "v2");
129         checkAttribNs(xpp, 2, "xml", "http://www.w3.org/XML/1998/namespace", "lang", "en");
130         checkAttribNs(xpp, 3, "ns1", "x1", "a3", "v3");
131
132         xpp.next();
133         checkParserStateNs(xpp, 3, xpp.END_TAG, "ns2", 6, "n2", "gugu", null, false, -1);
134
135         xpp.next();
136         checkParserStateNs(xpp, 3, xpp.START_TAG, null, 7, "n1", "baz", null, false, 0);
137         assertEquals(0, xpp.getNamespaceCount(0));
138         assertEquals(3, xpp.getNamespaceCount(1));
139         assertEquals(6, xpp.getNamespaceCount(2));
140         assertEquals(7, xpp.getNamespaceCount(3));
141         checkNamespace(xpp, 6, "ns1", "y1", true);
142         assertEquals("y1", xpp.getNamespace("ns1"));
143         assertEquals("n2", xpp.getNamespace("ns2"));
144         assertEquals("n3", xpp.getNamespace("ns3"));
145
146         xpp.next();
147         checkParserStateNs(xpp, 3, xpp.END_TAG, null, 7, "n1", "baz", null, false, -1);
148         assertEquals("y1", xpp.getNamespace("ns1"));
149         assertEquals("n2", xpp.getNamespace("ns2"));
150         assertEquals("n3", xpp.getNamespace("ns3"));
151
152         // check that declared namespaces can be accessed for current end tag
153
assertEquals(3, xpp.getDepth());
154         assertEquals(6, xpp.getNamespaceCount(2));
155         assertEquals(7, xpp.getNamespaceCount(3));
156
157         // chekc that namespace is accessible by direct addresssing
158
assertEquals(null, xpp.getNamespacePrefix(0));
159         assertEquals("n", xpp.getNamespaceUri(0));
160         assertEquals("ns1", xpp.getNamespacePrefix(1));
161         assertEquals("n1", xpp.getNamespaceUri(1));
162         assertEquals("ns1", xpp.getNamespacePrefix(3));
163         assertEquals("x1", xpp.getNamespaceUri(3));
164         assertEquals("ns1", xpp.getNamespacePrefix(6));
165         assertEquals("y1", xpp.getNamespaceUri(6));
166
167
168         xpp.next();
169         checkParserStateNs(xpp, 2, xpp.END_TAG, "ns1", 6, "x1", "bar", null, false, -1);
170         // check that namespace is undelcared
171
assertEquals("x1", xpp.getNamespace("ns1"));
172
173         xpp.next();
174         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 3, "n", "foo", null, false, -1);
175
176         assertEquals("n1", xpp.getNamespace("ns1"));
177         assertEquals("n2", xpp.getNamespace("ns2"));
178         assertEquals(null, xpp.getNamespace("ns3"));
179
180         xpp.next();
181         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
182         assertEquals(null, xpp.getNamespace("ns1"));
183         assertEquals(null, xpp.getNamespace("ns2"));
184         assertEquals(null, xpp.getNamespace("ns3"));
185
186     }
187
188     public static void main (String JavaDoc[] args) {
189         junit.textui.TestRunner.run (new TestSuite(TestSimpleWithNs.class));
190     }
191
192 }
193
194
Popular Tags