1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.io.read;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.betwixt.AbstractTestCase;
24 import org.apache.commons.betwixt.BindingConfiguration;
25 import org.apache.commons.betwixt.LibraryBeanWithArraySetter;
26
27 /***
28 * Test harness for ReadContext
29 *
30 * @author Robert Burrell Donkin
31 * @version $Id: TestReadContext.java 438373 2006-08-30 05:17:21Z bayard $
32 */
33 public class TestReadContext extends AbstractTestCase {
34
35 public TestReadContext(String name) {
36 super(name);
37 }
38
39 public static Test suite() {
40 return new TestSuite(TestReadContext.class);
41 }
42
43 public void testElementStackPushPop() throws Exception {
44 ReadContext context = new ReadContext(
45 new BindingConfiguration(),
46 new ReadConfiguration());
47 context.pushElement("alpha");
48 assertEquals("Push then pop", "alpha", context.popElement());
49 assertEquals("Push then pop at bottom", null, context.popElement());
50
51 context.pushElement("beta");
52 context.pushElement("delta");
53 context.pushElement("gamma");
54 assertEquals("Triple push (1)", "gamma", context.popElement());
55 assertEquals("Triple push (2)", "delta", context.popElement());
56 assertEquals("Triple push (3)", "beta", context.popElement());
57 assertEquals("Triple push at bottom", null, context.popElement());
58
59 }
60
61 public void testElementStackMarkedPushPop() throws Exception {
62 ReadContext context = new ReadContext(
63 new BindingConfiguration(),
64 new ReadConfiguration());
65
66 context.pushElement("beta");
67 context.pushElement("delta");
68 context.markClassMap(Object.class);
69 context.pushElement("gamma");
70 assertEquals("One mark (1)", "gamma", context.popElement());
71 assertEquals("One mark (2)", "delta", context.popElement());
72 assertEquals("One mark (3)", "beta", context.popElement());
73 assertEquals("One mark at bottom", null, context.popElement());
74
75 context.markClassMap(Object.class);
76 context.pushElement("beta");
77 context.pushElement("delta");
78 context.markClassMap(Object.class);
79 context.pushElement("gamma");
80 context.markClassMap(Object.class);
81 assertEquals("Three marks (1)", "gamma", context.popElement());
82 assertEquals("Three marks (2)", "delta", context.popElement());
83 assertEquals("Three marks (3)", "beta", context.popElement());
84 assertEquals("Three marks at bottom", null, context.popElement());
85 }
86
87 public void testLastMappedClassNoClass() throws Exception
88 {
89 ReadContext context = new ReadContext(
90 new BindingConfiguration(),
91 new ReadConfiguration());
92 context.pushElement("beta");
93 context.pushElement("delta");
94 context.pushElement("gamma");
95 assertEquals("No class", null, context.getLastMappedClass());
96 }
97
98 public void testGetCurrentElement() throws Exception {
99 ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
100 context.pushElement("element");
101 context.markClassMap(String.class);
102 assertEquals("Current element: ", "element", context.getCurrentElement());
103 }
104
105 public void testLastMappedClassBottomClass() throws Exception
106 {
107 ReadContext context = new ReadContext(
108 new BindingConfiguration(),
109 new ReadConfiguration());
110
111 context.markClassMap(Object.class);
112 context.pushElement("beta");
113 context.pushElement("delta");
114 context.pushElement("gamma");
115 assertEquals("One classes", Object.class, context.getLastMappedClass());
116 }
117
118 public void testLastMappedClassTwoClasses() throws Exception
119 {
120
121 ReadContext context = new ReadContext(
122 new BindingConfiguration(),
123 new ReadConfiguration());
124 context.markClassMap(Object.class);
125 context.pushElement("beta");
126 context.pushElement("delta");
127 context.markClassMap(String.class);
128 context.pushElement("gamma");
129 assertEquals("Two classes", String.class, context.getLastMappedClass());
130 }
131
132 public void testLastMappedClassTopClass() throws Exception
133 {
134 ReadContext context = new ReadContext(
135 new BindingConfiguration(),
136 new ReadConfiguration());
137 context.markClassMap(Object.class);
138 context.pushElement("beta");
139 context.pushElement("delta");
140 context.markClassMap(String.class);
141 context.pushElement("gamma");
142 context.markClassMap(Integer.class);
143 assertEquals("Top class", Integer.class, context.getLastMappedClass());
144 }
145
146
147 public void testNullElementNameMatchesAll() throws Exception {
148
149 ReadContext context = new ReadContext(
150 new BindingConfiguration(),
151 new ReadConfiguration());
152
153 context.pushElement("LibraryBeanWithArraySetter");
154 context.markClassMap(LibraryBeanWithArraySetter.class);
155 context.pushElement("books");
156 context.pushElement("whatever");
157 assertNotNull("Null name should match any new element", context.getCurrentDescriptor());
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
264 }