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,v 1.2 2004/06/13 21:32:48 rdonkin Exp $
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 testLastMappedClassBottomClass() throws Exception
98 {
99 ReadContext context = new ReadContext(
100 new BindingConfiguration(),
101 new ReadConfiguration());
102
103 context.markClassMap(Object.class);
104 context.pushElement("beta");
105 context.pushElement("delta");
106 context.pushElement("gamma");
107 assertEquals("One classes", Object.class, context.getLastMappedClass());
108 }
109
110 public void testLastMappedClassTwoClasses() throws Exception
111 {
112
113 ReadContext context = new ReadContext(
114 new BindingConfiguration(),
115 new ReadConfiguration());
116 context.markClassMap(Object.class);
117 context.pushElement("beta");
118 context.pushElement("delta");
119 context.markClassMap(String.class);
120 context.pushElement("gamma");
121 assertEquals("Two classes", String.class, context.getLastMappedClass());
122 }
123
124 public void testLastMappedClassTopClass() throws Exception
125 {
126 ReadContext context = new ReadContext(
127 new BindingConfiguration(),
128 new ReadConfiguration());
129 context.markClassMap(Object.class);
130 context.pushElement("beta");
131 context.pushElement("delta");
132 context.markClassMap(String.class);
133 context.pushElement("gamma");
134 context.markClassMap(Integer.class);
135 assertEquals("Top class", Integer.class, context.getLastMappedClass());
136 }
137
138
139 public void testNullElementNameMatchesAll() throws Exception {
140
141 ReadContext context = new ReadContext(
142 new BindingConfiguration(),
143 new ReadConfiguration());
144
145 context.pushElement("LibraryBeanWithArraySetter");
146 context.markClassMap(LibraryBeanWithArraySetter.class);
147 context.pushElement("books");
148 context.pushElement("whatever");
149 assertNotNull("Null name should match any new element", context.getCurrentDescriptor());
150 }
151
152
153
154
155
156
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 }