1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.math.BigDecimal;
23 import java.util.Iterator;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29
30
31
32
33
34
35
36 public class DynaResultSetTestCase extends TestCase {
37
38
39
40
41
42
43
44
45 protected ResultSetDynaClass dynaClass = null;
46
47
48
49
50
51
52 protected String columns[] =
53 { "bigdecimalproperty", "booleanproperty",
54 "byteproperty", "dateproperty",
55 "doubleproperty", "floatproperty",
56 "intproperty", "longproperty",
57 "nullproperty", "shortproperty",
58 "stringproperty", "timeproperty",
59 "timestampproperty" };
60
61
62
63
64
65
66
67
68
69
70 public DynaResultSetTestCase(final String name) {
71
72 super(name);
73
74 }
75
76
77
78
79
80
81
82
83 @Override
84 public void setUp() throws Exception {
85
86 dynaClass = new ResultSetDynaClass(TestResultSet.createProxy());
87
88 }
89
90
91
92
93
94 public static Test suite() {
95
96 return (new TestSuite(DynaResultSetTestCase.class));
97
98 }
99
100
101
102
103
104 @Override
105 public void tearDown() {
106
107 dynaClass = null;
108
109 }
110
111
112
113
114
115
116 public void testGetName() {
117
118 assertEquals("DynaClass name",
119 "org.apache.commons.beanutils.ResultSetDynaClass",
120 dynaClass.getName());
121
122
123 }
124
125
126 public void testGetDynaProperty() {
127
128
129 try {
130 dynaClass.getDynaProperty(null);
131 fail("Did not throw IllegaArgumentException");
132 } catch (final IllegalArgumentException e) {
133
134 }
135
136
137 DynaProperty dynaProp = dynaClass.getDynaProperty("unknownProperty");
138 assertTrue("unknown property returns null",
139 (dynaProp == null));
140
141
142 dynaProp = dynaClass.getDynaProperty("stringproperty");
143 assertNotNull("string property exists", dynaProp);
144 assertEquals("string property name", "stringproperty",
145 dynaProp.getName());
146 assertEquals("string property class", String.class,
147 dynaProp.getType());
148
149 }
150
151
152 public void testGetDynaProperties() {
153
154 final DynaProperty dynaProps[] = dynaClass.getDynaProperties();
155 assertNotNull("dynaProps exists", dynaProps);
156 assertEquals("dynaProps length", columns.length, dynaProps.length);
157 for (int i = 0; i < columns.length; i++) {
158 assertEquals("Property " + columns[i],
159 columns[i], dynaProps[i].getName());
160 }
161
162 }
163
164
165 public void testNewInstance() {
166
167 try {
168 dynaClass.newInstance();
169 fail("Did not throw UnsupportedOperationException()");
170 } catch (final UnsupportedOperationException e) {
171
172 } catch (final Exception e) {
173 fail("Threw exception " + e);
174 }
175
176 }
177
178
179 public void testIteratorCount() {
180
181 final Iterator<?> rows = dynaClass.iterator();
182 assertNotNull("iterator exists", rows);
183 int n = 0;
184 while (rows.hasNext()) {
185 rows.next();
186 n++;
187 if (n > 10) {
188 fail("Returned too many rows");
189 }
190 }
191 assertEquals("iterator rows", 5, n);
192
193 }
194
195
196 public void testIteratorResults() {
197
198
199 final Iterator<DynaBean> rows = dynaClass.iterator();
200 rows.next();
201 rows.next();
202 final DynaBean row = rows.next();
203
204
205 try {
206 row.get("unknownProperty");
207 fail("Did not throw IllegalArgumentException");
208 } catch (final IllegalArgumentException e) {
209
210 }
211
212
213
214 final Object bigDecimalProperty = row.get("bigdecimalproperty");
215 assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
216 assertTrue("bigDecimalProperty type",
217 bigDecimalProperty instanceof BigDecimal);
218 assertEquals("bigDecimalProperty value",
219 123.45,
220 ((BigDecimal) bigDecimalProperty).doubleValue(),
221 0.005);
222
223 final Object intProperty = row.get("intproperty");
224 assertNotNull("intProperty exists", intProperty);
225 assertTrue("intProperty type",
226 intProperty instanceof Integer);
227 assertEquals("intProperty value",
228 103,
229 ((Integer) intProperty).intValue());
230
231 final Object nullProperty = row.get("nullproperty");
232 assertNull("nullProperty null", nullProperty);
233
234 final Object stringProperty = row.get("stringproperty");
235 assertNotNull("stringProperty exists", stringProperty);
236 assertTrue("stringProperty type",
237 stringProperty instanceof String);
238 assertEquals("stringProperty value",
239 "This is a string",
240 (String) stringProperty);
241
242
243 }
244
245
246
247
248
249 public void testIteratorResultsNormalCase() {
250 ResultSetDynaClass dynaClass = null;
251 try {
252 dynaClass = new ResultSetDynaClass(TestResultSet.createProxy(), false);
253 } catch (final Exception e) {
254 fail("Error creating ResultSetDynaClass: " + e);
255 }
256
257
258 final Iterator<DynaBean> rows = dynaClass.iterator();
259 rows.next();
260 rows.next();
261 final DynaBean row = rows.next();
262
263
264 try {
265 row.get("unknownProperty");
266 fail("Did not throw IllegalArgumentException");
267 } catch (final IllegalArgumentException e) {
268
269 }
270
271
272
273 final Object bigDecimalProperty = row.get("bigDecimalProperty");
274 assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
275 assertTrue("bigDecimalProperty type",
276 bigDecimalProperty instanceof BigDecimal);
277 assertEquals("bigDecimalProperty value",
278 123.45,
279 ((BigDecimal) bigDecimalProperty).doubleValue(),
280 0.005);
281
282 final Object intProperty = row.get("intProperty");
283 assertNotNull("intProperty exists", intProperty);
284 assertTrue("intProperty type",
285 intProperty instanceof Integer);
286 assertEquals("intProperty value",
287 103,
288 ((Integer) intProperty).intValue());
289
290 final Object nullProperty = row.get("nullProperty");
291 assertNull("nullProperty null", nullProperty);
292
293 final Object stringProperty = row.get("stringProperty");
294 assertNotNull("stringProperty exists", stringProperty);
295 assertTrue("stringProperty type",
296 stringProperty instanceof String);
297 assertEquals("stringProperty value",
298 "This is a string",
299 (String) stringProperty);
300
301
302 }
303
304
305 }