1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.math.BigDecimal;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import junit.framework.TestCase;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33
34 /**
35 * Test accessing ResultSets via DynaBeans.
36 *
37 * @author Craig R. McClanahan
38 * @version $Revision: 1.5 $ $Date: 2004/02/28 13:18:36 $
39 */
40
41 public class DynaResultSetTestCase extends TestCase {
42
43
44
45
46
47 /**
48 * The mock result set DynaClass to be tested.
49 */
50 protected ResultSetDynaClass dynaClass = null;
51
52
53 /**
54 * Names of the columns for this test. Must match the order they are
55 * defined in {@link TestResultSetMetaData}, and must be all lower case.
56 */
57 protected String columns[] =
58 { "bigdecimalproperty", "booleanproperty",
59 "byteproperty", "dateproperty",
60 "doubleproperty", "floatproperty",
61 "intproperty", "longproperty",
62 "nullproperty", "shortproperty",
63 "stringproperty", "timeproperty",
64 "timestampproperty" };
65
66
67
68
69
70 /**
71 * Construct a new instance of this test case.
72 *
73 * @param name Name of the test case
74 */
75 public DynaResultSetTestCase(String name) {
76
77 super(name);
78
79 }
80
81
82
83
84
85 /**
86 * Set up instance variables required by this test case.
87 */
88 public void setUp() throws Exception {
89
90 dynaClass = new ResultSetDynaClass(new TestResultSet());
91
92 }
93
94
95 /**
96 * Return the tests included in this test suite.
97 */
98 public static Test suite() {
99
100 return (new TestSuite(DynaResultSetTestCase.class));
101
102 }
103
104
105 /**
106 * Tear down instance variables required by this test case.
107 */
108 public void tearDown() {
109
110 dynaClass = null;
111
112 }
113
114
115
116
117
118
119 public void testGetName() {
120
121 assertEquals("DynaClass name",
122 "org.apache.commons.beanutils.ResultSetDynaClass",
123 dynaClass.getName());
124
125
126 }
127
128
129 public void testGetDynaProperty() {
130
131
132 try {
133 dynaClass.getDynaProperty(null);
134 fail("Did not throw IllegaArgumentException");
135 } catch (IllegalArgumentException e) {
136 ;
137 }
138
139
140 DynaProperty dynaProp = dynaClass.getDynaProperty("unknownProperty");
141 assertTrue("unknown property returns null",
142 (dynaProp == null));
143
144
145 dynaProp = dynaClass.getDynaProperty("stringproperty");
146 assertNotNull("string property exists", dynaProp);
147 assertEquals("string property name", "stringproperty",
148 dynaProp.getName());
149 assertEquals("string property class", String.class,
150 dynaProp.getType());
151
152 }
153
154
155 public void testGetDynaProperties() {
156
157 DynaProperty dynaProps[] = dynaClass.getDynaProperties();
158 assertNotNull("dynaProps exists", dynaProps);
159 assertEquals("dynaProps length", columns.length, dynaProps.length);
160 for (int i = 0; i < columns.length; i++) {
161 assertEquals("Property " + columns[i],
162 columns[i], dynaProps[i].getName());
163 }
164
165 }
166
167
168 public void testNewInstance() {
169
170 try {
171 dynaClass.newInstance();
172 fail("Did not throw UnsupportedOperationException()");
173 } catch (UnsupportedOperationException e) {
174 ;
175 } catch (Exception e) {
176 fail("Threw exception " + e);
177 }
178
179 }
180
181
182 public void testIteratorCount() {
183
184 Iterator rows = dynaClass.iterator();
185 assertNotNull("iterator exists", rows);
186 int n = 0;
187 while (rows.hasNext()) {
188 rows.next();
189 n++;
190 if (n > 10) {
191 fail("Returned too many rows");
192 }
193 }
194 assertEquals("iterator rows", 5, n);
195
196 }
197
198
199 public void testIteratorResults() {
200
201
202 Iterator rows = dynaClass.iterator();
203 rows.next();
204 rows.next();
205 DynaBean row = (DynaBean) rows.next();
206
207
208 try {
209 row.get("unknownProperty");
210 fail("Did not throw IllegalArgumentException");
211 } catch (IllegalArgumentException e) {
212 ;
213 }
214
215
216
217 Object bigDecimalProperty = row.get("bigdecimalproperty");
218 assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
219 assertTrue("bigDecimalProperty type",
220 bigDecimalProperty instanceof BigDecimal);
221 assertEquals("bigDecimalProperty value",
222 123.45,
223 ((BigDecimal) bigDecimalProperty).doubleValue(),
224 0.005);
225
226 Object intProperty = row.get("intproperty");
227 assertNotNull("intProperty exists", intProperty);
228 assertTrue("intProperty type",
229 intProperty instanceof Integer);
230 assertEquals("intProperty value",
231 103,
232 ((Integer) intProperty).intValue());
233
234 Object nullProperty = row.get("nullproperty");
235 assertNull("nullProperty null", nullProperty);
236
237 Object stringProperty = row.get("stringproperty");
238 assertNotNull("stringProperty exists", stringProperty);
239 assertTrue("stringProperty type",
240 stringProperty instanceof String);
241 assertEquals("stringProperty value",
242 "This is a string",
243 (String) stringProperty);
244
245
246 }
247
248
249 }