KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > path > PathTrackerTest


1 package com.thoughtworks.xstream.io.path;
2
3 import junit.framework.TestCase;
4
5 public class PathTrackerTest extends TestCase {
6
7     private PathTracker pathTracker;
8
9     protected void setUp() throws Exception JavaDoc {
10         super.setUp();
11         // small initial capacity to ensure resizing works
12
pathTracker = new PathTracker(1);
13     }
14
15     public void testExposesXpathLikeExpressionOfLocationInWriter() {
16
17         assertEquals("", pathTracker.getCurrentPath());
18
19         // <root>
20
pathTracker.pushElement("root");
21         assertEquals("/root", pathTracker.getCurrentPath());
22
23         // <childA>
24
pathTracker.pushElement("childA");
25         assertEquals("/root/childA", pathTracker.getCurrentPath());
26         // </childA>
27
pathTracker.popElement();
28         assertEquals("/root", pathTracker.getCurrentPath());
29
30         // <childB>
31
pathTracker.pushElement("childB");
32         assertEquals("/root/childB", pathTracker.getCurrentPath());
33
34         // <grandchild>
35
pathTracker.pushElement("grandchild");
36         assertEquals("/root/childB/grandchild", pathTracker.getCurrentPath());
37         // </grandchild>
38
pathTracker.popElement();
39         assertEquals("/root/childB", pathTracker.getCurrentPath());
40
41         // </childB>
42
pathTracker.popElement();
43         assertEquals("/root", pathTracker.getCurrentPath());
44
45         // </root>
46
pathTracker.popElement();
47         assertEquals("", pathTracker.getCurrentPath());
48
49     }
50
51     public void testAddsIndexIfSiblingOfSameTypeAlreadyExists() {
52
53         // <root>
54
pathTracker.pushElement("root");
55
56         // <child>
57
pathTracker.pushElement("child");
58         assertEquals("/root/child", pathTracker.getCurrentPath());
59         // </child>
60
pathTracker.popElement();
61
62         // <child>
63
pathTracker.pushElement("child");
64         assertEquals("/root/child[2]", pathTracker.getCurrentPath());
65         // </child>
66
pathTracker.popElement();
67
68         // <another>
69
pathTracker.pushElement("another");
70         assertEquals("/root/another", pathTracker.getCurrentPath());
71         // </another>
72
pathTracker.popElement();
73
74         // <child>
75
pathTracker.pushElement("child");
76         assertEquals("/root/child[3]", pathTracker.getCurrentPath());
77         // </child>
78
pathTracker.popElement();
79
80         // ...
81
}
82
83     public void testAssociatesIndexOnlyWithDirectParent() {
84
85         // <root>
86
pathTracker.pushElement("root");
87
88         // <child>
89
pathTracker.pushElement("child");
90
91         // <child>
92
pathTracker.pushElement("child");
93         assertEquals("/root/child/child", pathTracker.getCurrentPath());
94         // </child>
95
pathTracker.popElement();
96
97         // <child>
98
pathTracker.pushElement("child");
99         assertEquals("/root/child/child[2]", pathTracker.getCurrentPath());
100         // </child>
101
pathTracker.popElement();
102
103         // </child>
104
pathTracker.popElement();
105
106         // <child>
107
pathTracker.pushElement("child");
108
109         // <child>
110
pathTracker.pushElement("child");
111         assertEquals("/root/child[2]/child", pathTracker.getCurrentPath());
112         // </child>
113
pathTracker.popElement();
114
115         // <child>
116
pathTracker.pushElement("child");
117         assertEquals("/root/child[2]/child[2]", pathTracker.getCurrentPath());
118
119         // ...
120
}
121
122 }
123
Popular Tags