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 RowSets 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 DynaRowSetTestCase extends TestCase {
42
43
44
45
46
47 /**
48 * The mock result set DynaClass to be tested.
49 */
50 protected RowSetDynaClass 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 DynaRowSetTestCase(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 RowSetDynaClass(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(DynaRowSetTestCase.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.RowSetDynaClass",
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 testListCount() {
183
184 List rows = dynaClass.getRows();
185 assertNotNull("list exists", rows);
186 assertEquals("list row count", 5, rows.size());
187
188 }
189
190
191 public void testListResults() {
192
193
194 List rows = dynaClass.getRows();
195 DynaBean row = (DynaBean) rows.get(2);
196
197
198 try {
199 row.get("unknownProperty");
200 fail("Did not throw IllegalArgumentException");
201 } catch (IllegalArgumentException e) {
202 ;
203 }
204
205
206
207 Object bigDecimalProperty = row.get("bigdecimalproperty");
208 assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
209 assertTrue("bigDecimalProperty type",
210 bigDecimalProperty instanceof BigDecimal);
211 assertEquals("bigDecimalProperty value",
212 123.45,
213 ((BigDecimal) bigDecimalProperty).doubleValue(),
214 0.005);
215
216 Object intProperty = row.get("intproperty");
217 assertNotNull("intProperty exists", intProperty);
218 assertTrue("intProperty type",
219 intProperty instanceof Integer);
220 assertEquals("intProperty value",
221 103,
222 ((Integer) intProperty).intValue());
223
224 Object nullProperty = row.get("nullproperty");
225 assertNull("nullProperty null", nullProperty);
226
227 Object stringProperty = row.get("stringproperty");
228 assertNotNull("stringProperty exists", stringProperty);
229 assertTrue("stringProperty type",
230 stringProperty instanceof String);
231 assertEquals("stringProperty value",
232 "This is a string",
233 (String) stringProperty);
234
235
236 }
237
238 public void testLimitedRows() throws Exception {
239
240
241 RowSetDynaClass limitedDynaClass = new RowSetDynaClass(new TestResultSet(), 3);
242 List rows = limitedDynaClass.getRows();
243 assertNotNull("list exists", rows);
244 assertEquals("limited row count", 3, rows.size());
245
246 }
247 }