1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io.read;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 import org.apache.commons.betwixt.AbstractTestCase;
23 import org.apache.commons.betwixt.BindingConfiguration;
24 import org.apache.commons.betwixt.LibraryBeanWithArraySetter;
25
26 /***
27 * Test harness for ReadContext
28 *
29 * @author Robert Burrell Donkin
30 * @version $Id: TestReadContext.java 155402 2005-02-26 12:52:00Z dirkv $
31 */
32 public class TestReadContext extends AbstractTestCase {
33
34 public TestReadContext(String name) {
35 super(name);
36 }
37
38 public static Test suite() {
39 return new TestSuite(TestReadContext.class);
40 }
41
42 public void testElementStackPushPop() throws Exception {
43 ReadContext context = new ReadContext(
44 new BindingConfiguration(),
45 new ReadConfiguration());
46 context.pushElement("alpha");
47 assertEquals("Push then pop", "alpha", context.popElement());
48 assertEquals("Push then pop at bottom", null, context.popElement());
49
50 context.pushElement("beta");
51 context.pushElement("delta");
52 context.pushElement("gamma");
53 assertEquals("Triple push (1)", "gamma", context.popElement());
54 assertEquals("Triple push (2)", "delta", context.popElement());
55 assertEquals("Triple push (3)", "beta", context.popElement());
56 assertEquals("Triple push at bottom", null, context.popElement());
57
58 }
59
60 public void testElementStackMarkedPushPop() throws Exception {
61 ReadContext context = new ReadContext(
62 new BindingConfiguration(),
63 new ReadConfiguration());
64
65 context.pushElement("beta");
66 context.pushElement("delta");
67 context.markClassMap(Object.class);
68 context.pushElement("gamma");
69 assertEquals("One mark (1)", "gamma", context.popElement());
70 assertEquals("One mark (2)", "delta", context.popElement());
71 assertEquals("One mark (3)", "beta", context.popElement());
72 assertEquals("One mark at bottom", null, context.popElement());
73
74 context.markClassMap(Object.class);
75 context.pushElement("beta");
76 context.pushElement("delta");
77 context.markClassMap(Object.class);
78 context.pushElement("gamma");
79 context.markClassMap(Object.class);
80 assertEquals("Three marks (1)", "gamma", context.popElement());
81 assertEquals("Three marks (2)", "delta", context.popElement());
82 assertEquals("Three marks (3)", "beta", context.popElement());
83 assertEquals("Three marks at bottom", null, context.popElement());
84 }
85
86 public void testLastMappedClassNoClass() throws Exception
87 {
88 ReadContext context = new ReadContext(
89 new BindingConfiguration(),
90 new ReadConfiguration());
91 context.pushElement("beta");
92 context.pushElement("delta");
93 context.pushElement("gamma");
94 assertEquals("No class", null, context.getLastMappedClass());
95 }
96
97 public void testGetCurrentElement() throws Exception {
98 ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
99 context.pushElement("element");
100 context.markClassMap(String.class);
101 assertEquals("Current element: ", "element", context.getCurrentElement());
102 }
103
104 public void testLastMappedClassBottomClass() throws Exception
105 {
106 ReadContext context = new ReadContext(
107 new BindingConfiguration(),
108 new ReadConfiguration());
109
110 context.markClassMap(Object.class);
111 context.pushElement("beta");
112 context.pushElement("delta");
113 context.pushElement("gamma");
114 assertEquals("One classes", Object.class, context.getLastMappedClass());
115 }
116
117 public void testLastMappedClassTwoClasses() throws Exception
118 {
119
120 ReadContext context = new ReadContext(
121 new BindingConfiguration(),
122 new ReadConfiguration());
123 context.markClassMap(Object.class);
124 context.pushElement("beta");
125 context.pushElement("delta");
126 context.markClassMap(String.class);
127 context.pushElement("gamma");
128 assertEquals("Two classes", String.class, context.getLastMappedClass());
129 }
130
131 public void testLastMappedClassTopClass() throws Exception
132 {
133 ReadContext context = new ReadContext(
134 new BindingConfiguration(),
135 new ReadConfiguration());
136 context.markClassMap(Object.class);
137 context.pushElement("beta");
138 context.pushElement("delta");
139 context.markClassMap(String.class);
140 context.pushElement("gamma");
141 context.markClassMap(Integer.class);
142 assertEquals("Top class", Integer.class, context.getLastMappedClass());
143 }
144
145
146 public void testNullElementNameMatchesAll() throws Exception {
147
148 ReadContext context = new ReadContext(
149 new BindingConfiguration(),
150 new ReadConfiguration());
151
152 context.pushElement("LibraryBeanWithArraySetter");
153 context.markClassMap(LibraryBeanWithArraySetter.class);
154 context.pushElement("books");
155 context.pushElement("whatever");
156 assertNotNull("Null name should match any new element", context.getCurrentDescriptor());
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 }