KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > AbstractXMLReaderTest


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
4 import junit.framework.TestCase;
5
6 public abstract class AbstractXMLReaderTest extends TestCase {
7
8     // factory method
9
protected abstract HierarchicalStreamReader createReader(String JavaDoc xml) throws Exception JavaDoc;
10
11     public void testStartsAtRootTag() throws Exception JavaDoc {
12         HierarchicalStreamReader xmlReader = createReader("<hello/>");
13         assertEquals("hello", xmlReader.getNodeName());
14     }
15
16     public void testCanNavigateDownChildTagsByIndex() throws Exception JavaDoc {
17         HierarchicalStreamReader xmlReader = createReader("<a><b><ooh/></b><b><aah/></b></a>");
18
19         assertEquals("a", xmlReader.getNodeName());
20
21         assertTrue(xmlReader.hasMoreChildren());
22
23         xmlReader.moveDown(); // /a/b
24

25         assertEquals("b", xmlReader.getNodeName());
26
27         assertTrue(xmlReader.hasMoreChildren());
28
29         xmlReader.moveDown(); // a/b/ooh
30
assertEquals("ooh", xmlReader.getNodeName());
31         assertFalse(xmlReader.hasMoreChildren());
32         xmlReader.moveUp(); // a/b
33

34         assertFalse(xmlReader.hasMoreChildren());
35
36         xmlReader.moveUp(); // /a
37

38         assertTrue(xmlReader.hasMoreChildren());
39
40         xmlReader.moveDown(); // /a/b[2]
41

42         assertEquals("b", xmlReader.getNodeName());
43
44         assertTrue(xmlReader.hasMoreChildren());
45
46         xmlReader.moveDown(); // a/b[2]/aah
47

48         assertEquals("aah", xmlReader.getNodeName());
49         assertFalse(xmlReader.hasMoreChildren());
50
51         xmlReader.moveUp(); // a/b[2]
52

53         assertFalse(xmlReader.hasMoreChildren());
54
55         xmlReader.moveUp(); // a
56

57         assertFalse(xmlReader.hasMoreChildren());
58     }
59
60     public void testChildTagsCanBeMixedWithOtherNodes() throws Exception JavaDoc {
61         HierarchicalStreamReader xmlReader = createReader("<!-- xx --><a> <hello/> <!-- x --> getValue <world/></a>");
62
63         assertTrue(xmlReader.hasMoreChildren());
64         xmlReader.moveDown();
65         assertEquals("hello", xmlReader.getNodeName());
66         xmlReader.moveUp();
67
68         assertTrue(xmlReader.hasMoreChildren());
69         xmlReader.moveDown();
70         assertEquals("world", xmlReader.getNodeName());
71         xmlReader.moveUp();
72
73         assertFalse(xmlReader.hasMoreChildren());
74     }
75
76     public void testAttributesCanBeFetchedFromTags() throws Exception JavaDoc {
77         HierarchicalStreamReader xmlReader = createReader("" +
78                 "<hello one=\"1\" two=\"2\">" +
79                 " <child three=\"3\"/>" +
80                 "</hello>"); // /hello
81

82         assertEquals("1", xmlReader.getAttribute("one"));
83         assertEquals("2", xmlReader.getAttribute("two"));
84         assertNull(xmlReader.getAttribute("three"));
85
86         xmlReader.moveDown(); // /hello/child
87
assertNull(xmlReader.getAttribute("one"));
88         assertNull(xmlReader.getAttribute("two"));
89         assertEquals("3", xmlReader.getAttribute("three"));
90
91     }
92
93     public void testTextCanBeExtractedFromTag() throws Exception JavaDoc {
94         HierarchicalStreamReader xmlReader = createReader("<root><a>some<!-- ignore me --> getValue!</a><b>more</b></root>");
95
96         xmlReader.moveDown();
97         assertEquals("some getValue!", xmlReader.getValue());
98         xmlReader.moveUp();
99
100         xmlReader.moveDown();
101         assertEquals("more", xmlReader.getValue());
102         xmlReader.moveUp();
103     }
104
105     public void testDoesNotIgnoreWhitespaceAroundText() throws Exception JavaDoc {
106         HierarchicalStreamReader xmlReader = createReader("<root> hello world </root>");
107
108         assertEquals(" hello world ", xmlReader.getValue());
109     }
110
111     public void testReturnsEmptyStringForEmptyTags() throws Exception JavaDoc {
112         HierarchicalStreamReader xmlReader = createReader("<root></root>");
113
114         String JavaDoc text = xmlReader.getValue();
115         assertNotNull(text);
116         assertEquals("", text);
117     }
118
119 }
120
Popular Tags