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