001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
015:
016: import junit.framework.TestCase;
017:
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.Set;
021:
022: public abstract class AbstractXMLReaderTest extends TestCase {
023:
024: // factory method
025: protected abstract HierarchicalStreamReader createReader(String xml)
026: throws Exception;
027:
028: public void testStartsAtRootTag() throws Exception {
029: HierarchicalStreamReader xmlReader = createReader("<hello/>");
030: assertEquals("hello", xmlReader.getNodeName());
031: }
032:
033: public void testCanNavigateDownChildTagsByIndex() throws Exception {
034: HierarchicalStreamReader xmlReader = createReader("<a><b><ooh/></b><b><aah/></b></a>");
035:
036: assertEquals("a", xmlReader.getNodeName());
037:
038: assertTrue(xmlReader.hasMoreChildren());
039:
040: xmlReader.moveDown(); // /a/b
041:
042: assertEquals("b", xmlReader.getNodeName());
043:
044: assertTrue(xmlReader.hasMoreChildren());
045:
046: xmlReader.moveDown(); // a/b/ooh
047: assertEquals("ooh", xmlReader.getNodeName());
048: assertFalse(xmlReader.hasMoreChildren());
049: xmlReader.moveUp(); // a/b
050:
051: assertFalse(xmlReader.hasMoreChildren());
052:
053: xmlReader.moveUp(); // /a
054:
055: assertTrue(xmlReader.hasMoreChildren());
056:
057: xmlReader.moveDown(); // /a/b[2]
058:
059: assertEquals("b", xmlReader.getNodeName());
060:
061: assertTrue(xmlReader.hasMoreChildren());
062:
063: xmlReader.moveDown(); // a/b[2]/aah
064:
065: assertEquals("aah", xmlReader.getNodeName());
066: assertFalse(xmlReader.hasMoreChildren());
067:
068: xmlReader.moveUp(); // a/b[2]
069:
070: assertFalse(xmlReader.hasMoreChildren());
071:
072: xmlReader.moveUp(); // a
073:
074: assertFalse(xmlReader.hasMoreChildren());
075: }
076:
077: public void testChildTagsCanBeMixedWithOtherNodes()
078: throws Exception {
079: HierarchicalStreamReader xmlReader = createReader("<!-- xx --><a> <hello/> <!-- x --> getValue <world/></a>");
080:
081: assertTrue(xmlReader.hasMoreChildren());
082: xmlReader.moveDown();
083: assertEquals("hello", xmlReader.getNodeName());
084: xmlReader.moveUp();
085:
086: assertTrue(xmlReader.hasMoreChildren());
087: xmlReader.moveDown();
088: assertEquals("world", xmlReader.getNodeName());
089: xmlReader.moveUp();
090:
091: assertFalse(xmlReader.hasMoreChildren());
092: }
093:
094: public void testAttributesCanBeFetchedFromTags() throws Exception {
095: HierarchicalStreamReader xmlReader = createReader(""
096: + "<hello one=\"1\" two=\"2\">"
097: + " <child three=\"3\"/>" + "</hello>"); // /hello
098:
099: assertEquals("1", xmlReader.getAttribute("one"));
100: assertEquals("2", xmlReader.getAttribute("two"));
101: assertNull(xmlReader.getAttribute("three"));
102:
103: xmlReader.moveDown(); // /hello/child
104: assertNull(xmlReader.getAttribute("one"));
105: assertNull(xmlReader.getAttribute("two"));
106: assertEquals("3", xmlReader.getAttribute("three"));
107:
108: }
109:
110: public void testTextCanBeExtractedFromTag() throws Exception {
111: HierarchicalStreamReader xmlReader = createReader("<root><a>some<!-- ignore me --> getValue!</a><b>more</b></root>");
112:
113: xmlReader.moveDown();
114: assertEquals("some getValue!", xmlReader.getValue());
115: xmlReader.moveUp();
116:
117: xmlReader.moveDown();
118: assertEquals("more", xmlReader.getValue());
119: xmlReader.moveUp();
120: }
121:
122: public void testDoesNotIgnoreWhitespaceAroundText()
123: throws Exception {
124: HierarchicalStreamReader xmlReader = createReader("<root> hello world </root>");
125:
126: assertEquals(" hello world ", xmlReader.getValue());
127: }
128:
129: public void testReturnsEmptyStringForEmptyTags() throws Exception {
130: HierarchicalStreamReader xmlReader = createReader("<root></root>");
131:
132: String text = xmlReader.getValue();
133: assertNotNull(text);
134: assertEquals("", text);
135: }
136:
137: public void testReturnsLastResultForHasMoreChildrenIfCalledRepeatedlyWithoutMovingNode()
138: throws Exception {
139: HierarchicalStreamReader xmlReader = createReader("<row><cells></cells></row>");
140:
141: assertEquals("row", xmlReader.getNodeName());
142: assertTrue(xmlReader.hasMoreChildren()); // this is OK
143: assertTrue(xmlReader.hasMoreChildren()); // this fails
144: }
145:
146: public void testExposesAttributesKeysAndValuesByIndex()
147: throws Exception {
148: HierarchicalStreamReader xmlReader = createReader("<node hello='world' a='b' c='d'><empty/></node>");
149:
150: assertEquals(3, xmlReader.getAttributeCount());
151:
152: assertEquals("hello", xmlReader.getAttributeName(0));
153: assertEquals("a", xmlReader.getAttributeName(1));
154: assertEquals("c", xmlReader.getAttributeName(2));
155:
156: assertEquals("world", xmlReader.getAttribute(0));
157: assertEquals("b", xmlReader.getAttribute(1));
158: assertEquals("d", xmlReader.getAttribute(2));
159:
160: xmlReader.moveDown();
161: assertEquals("empty", xmlReader.getNodeName());
162: assertEquals(0, xmlReader.getAttributeCount());
163: }
164:
165: public void testExposesAttributesKeysAsIterator() throws Exception {
166: HierarchicalStreamReader xmlReader = createReader("<node hello='world' a='b' c='d'><empty/></node>");
167:
168: Set expected = new HashSet();
169: expected.add("hello");
170: expected.add("a");
171: expected.add("c");
172:
173: Set actual = new HashSet();
174: Iterator iterator;
175:
176: iterator = xmlReader.getAttributeNames();
177: while (iterator.hasNext()) {
178: actual.add(iterator.next());
179: }
180: assertEquals(expected, actual);
181:
182: // again, to check iteration is repeatable
183: iterator = xmlReader.getAttributeNames();
184: while (iterator.hasNext()) {
185: actual.add(iterator.next());
186: }
187: assertEquals(expected, actual);
188: }
189:
190: public void testAllowsValueToBeReadWithoutDisturbingChildren()
191: throws Exception {
192: HierarchicalStreamReader xmlReader = createReader("<root><child></child><sibling>text2</sibling></root>"); // at: /root
193:
194: assertEquals("root", xmlReader.getNodeName());
195: assertEquals("", xmlReader.getValue());
196: assertTrue(xmlReader.hasMoreChildren());
197:
198: xmlReader.moveDown(); // at: /root/child
199: assertEquals("child", xmlReader.getNodeName());
200: assertEquals(null, xmlReader.getAttribute("something"));
201: assertEquals("", xmlReader.getValue());
202:
203: assertFalse(xmlReader.hasMoreChildren()); // <--- This is an awkward one for pull parsers
204:
205: xmlReader.moveUp(); // at: /root
206:
207: assertTrue(xmlReader.hasMoreChildren());
208:
209: xmlReader.moveDown(); // at: /root/sibling
210: assertEquals("sibling", xmlReader.getNodeName());
211: assertEquals("text2", xmlReader.getValue());
212: assertFalse(xmlReader.hasMoreChildren());
213: xmlReader.moveUp(); // at: /root
214:
215: assertFalse(xmlReader.hasMoreChildren());
216: }
217:
218: public void testExposesTextValueOfCurrentElementButNotChildren()
219: throws Exception {
220: HierarchicalStreamReader xmlReader = createReader("<root>hello<child>FNARR</child></root>");
221:
222: assertEquals("hello", xmlReader.getValue());
223: xmlReader.moveDown();
224: assertEquals("FNARR", xmlReader.getValue());
225: }
226:
227: // TODO: See XSTR-473
228: public void todoTestCanReadNullValueInString() throws Exception {
229: HierarchicalStreamReader xmlReader = createReader("<string>�</string>");
230: assertEquals("\u0000", xmlReader.getValue());
231: }
232: }
|