KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > AbstractStreamReaderTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 import javax.xml.namespace.QName JavaDoc;
20 import javax.xml.stream.XMLStreamReader;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @author <a HREF="mailto:dan@envoisolutions.com">Dan Diephouse</a>
26  * @since Oct 26, 2004
27  */

28 public abstract class AbstractStreamReaderTest
29     extends TestCase
30 {
31     public void testSingleElement(XMLStreamReader reader) throws Exception JavaDoc
32     {
33         assertTrue(reader.hasNext());
34         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
35         assertTrue(reader.hasNext());
36         assertEquals("root", reader.getLocalName());
37         assertEquals(1, reader.getNamespaceCount());
38         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
39         
40         assertEquals(1, reader.getNamespaceCount());
41         assertEquals("", reader.getNamespacePrefix(0));
42         assertEquals("urn:test", reader.getNamespaceURI(0));
43         
44         assertTrue(reader.hasNext());
45         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
46     }
47     
48     public void testTextChild(XMLStreamReader reader) throws Exception JavaDoc
49     {
50         assertTrue(reader.hasNext());
51         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
52         assertTrue(reader.hasNext());
53         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
54         
55         assertEquals(1, reader.getNamespaceCount());
56         assertEquals("", reader.getNamespacePrefix(0));
57         assertEquals("urn:test", reader.getNamespaceURI(0));
58         
59         assertTrue(reader.hasNext());
60         assertEquals(XMLStreamReader.CHARACTERS, reader.next());
61         
62         assertEquals("Hello World", reader.getText());
63         
64         assertTrue(reader.hasNext());
65         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
66     }
67
68     public void testMixedContent(XMLStreamReader reader) throws Exception JavaDoc
69     {
70         assertTrue(reader.hasNext());
71         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
72         assertTrue(reader.hasNext());
73         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
74         
75         assertEquals("root", reader.getLocalName());
76         assertEquals(1, reader.getNamespaceCount());
77         assertEquals("", reader.getNamespacePrefix(0));
78         assertEquals("urn:test", reader.getNamespaceURI(0));
79         
80         assertTrue(reader.hasNext());
81         assertEquals(XMLStreamReader.CHARACTERS, reader.next());
82         assertEquals("Hello World", reader.getText());
83
84         assertTrue(reader.hasNext());
85         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
86         assertEquals("element", reader.getLocalName());
87
88         assertTrue(reader.hasNext());
89         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
90         
91         assertTrue(reader.hasNext());
92         assertEquals(XMLStreamReader.CHARACTERS, reader.next());
93         assertEquals(" more text", reader.getText());
94
95         assertTrue(reader.hasNext());
96         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
97     }
98
99     public void testAttributes(XMLStreamReader reader) throws Exception JavaDoc
100     {
101         assertTrue(reader.hasNext());
102         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
103         
104         assertTrue(reader.hasNext());
105         assertEquals(XMLStreamReader.ATTRIBUTE, reader.next());
106         
107         // first attribute
108
assertEquals(2, reader.getAttributeCount());
109         assertTrue(reader.getAttributePrefix(0) == null || reader.getAttributePrefix(0).equals(""));
110         assertEquals("att1", reader.getAttributeLocalName(0));
111         assertTrue(reader.getAttributeNamespace(0) == null ||
112                    reader.getAttributeNamespace(0).equals(""));
113         assertEquals("value1", reader.getAttributeValue(0));
114         assertEquals("value1", reader.getAttributeValue("", "att1"));
115         
116         QName JavaDoc q = reader.getAttributeName(0);
117         assertEquals("", q.getNamespaceURI());
118         assertEquals("", q.getPrefix());
119         assertEquals("att1", q.getLocalPart());
120
121         // second attribute
122
assertEquals("p", reader.getAttributePrefix(1));
123         assertEquals("att2", reader.getAttributeLocalName(1));
124         assertEquals("urn:test2", reader.getAttributeNamespace(1));
125         assertEquals("value2", reader.getAttributeValue(1));
126         assertEquals("value2", reader.getAttributeValue("urn:test2", "att2"));
127         
128         q = reader.getAttributeName(1);
129         assertEquals("urn:test2", q.getNamespaceURI());
130         assertEquals("p", q.getPrefix());
131         assertEquals("att2", q.getLocalPart());
132
133         assertTrue(reader.hasNext());
134         assertEquals(XMLStreamReader.ATTRIBUTE, reader.next());
135         
136         assertTrue(reader.hasNext());
137         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
138         
139         assertEquals(2, reader.getNamespaceCount());
140         assertEquals("", reader.getNamespacePrefix(0));
141         assertEquals("urn:test", reader.getNamespaceURI(0));
142         assertEquals("p", reader.getNamespacePrefix(1));
143         assertEquals("urn:test2", reader.getNamespaceURI(1));
144         
145         assertTrue(reader.hasNext());
146         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
147         
148         assertTrue(reader.hasNext());
149         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
150     }
151     
152     public void testElementChild(XMLStreamReader reader) throws Exception JavaDoc
153     {
154         assertTrue(reader.hasNext());
155         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
156         
157         assertEquals("root", reader.getLocalName());
158         assertEquals("urn:test", reader.getNamespaceURI());
159         assertEquals("", reader.getPrefix());
160         
161         assertTrue(reader.hasNext());
162         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
163         
164         assertTrue(reader.hasNext());
165         assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
166         
167         assertEquals("child", reader.getLocalName());
168         assertEquals("urn:test2", reader.getNamespaceURI());
169         assertEquals("a", reader.getPrefix());
170         
171         assertTrue(reader.hasNext());
172         assertEquals(XMLStreamReader.NAMESPACE, reader.next());
173         
174         assertTrue(reader.hasNext());
175         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
176         
177         assertTrue(reader.hasNext());
178         assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
179     }
180 }
181
Popular Tags