001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.math.BigDecimal;
021: import java.util.Iterator;
022:
023: import junit.framework.TestCase;
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * Test accessing ResultSets via DynaBeans.
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 556221 $ $Date: 2007-07-14 05:19:21 +0100 (Sat, 14 Jul 2007) $
032: */
033:
034: public class DynaResultSetTestCase extends TestCase {
035:
036: // ----------------------------------------------------- Instance Variables
037:
038: /**
039: * The mock result set DynaClass to be tested.
040: */
041: protected ResultSetDynaClass dynaClass = null;
042:
043: /**
044: * Names of the columns for this test. Must match the order they are
045: * defined in {@link TestResultSetMetaData}, and must be all lower case.
046: */
047: protected String columns[] = { "bigdecimalproperty",
048: "booleanproperty", "byteproperty", "dateproperty",
049: "doubleproperty", "floatproperty", "intproperty",
050: "longproperty", "nullproperty", "shortproperty",
051: "stringproperty", "timeproperty", "timestampproperty" };
052:
053: // ----------------------------------------------------------- Constructors
054:
055: /**
056: * Construct a new instance of this test case.
057: *
058: * @param name Name of the test case
059: */
060: public DynaResultSetTestCase(String name) {
061:
062: super (name);
063:
064: }
065:
066: // --------------------------------------------------- Overall Test Methods
067:
068: /**
069: * Set up instance variables required by this test case.
070: */
071: public void setUp() throws Exception {
072:
073: dynaClass = new ResultSetDynaClass(TestResultSet.createProxy());
074:
075: }
076:
077: /**
078: * Return the tests included in this test suite.
079: */
080: public static Test suite() {
081:
082: return (new TestSuite(DynaResultSetTestCase.class));
083:
084: }
085:
086: /**
087: * Tear down instance variables required by this test case.
088: */
089: public void tearDown() {
090:
091: dynaClass = null;
092:
093: }
094:
095: // ------------------------------------------------ Individual Test Methods
096:
097: public void testGetName() {
098:
099: assertEquals("DynaClass name",
100: "org.apache.commons.beanutils.ResultSetDynaClass",
101: dynaClass.getName());
102:
103: }
104:
105: public void testGetDynaProperty() {
106:
107: // Invalid argument test
108: try {
109: dynaClass.getDynaProperty(null);
110: fail("Did not throw IllegaArgumentException");
111: } catch (IllegalArgumentException e) {
112: // Expected result
113: }
114:
115: // Negative test
116: DynaProperty dynaProp = dynaClass
117: .getDynaProperty("unknownProperty");
118: assertTrue("unknown property returns null", (dynaProp == null));
119:
120: // Positive test
121: dynaProp = dynaClass.getDynaProperty("stringproperty");
122: assertNotNull("string property exists", dynaProp);
123: assertEquals("string property name", "stringproperty", dynaProp
124: .getName());
125: assertEquals("string property class", String.class, dynaProp
126: .getType());
127:
128: }
129:
130: public void testGetDynaProperties() {
131:
132: DynaProperty dynaProps[] = dynaClass.getDynaProperties();
133: assertNotNull("dynaProps exists", dynaProps);
134: assertEquals("dynaProps length", columns.length,
135: dynaProps.length);
136: for (int i = 0; i < columns.length; i++) {
137: assertEquals("Property " + columns[i], columns[i],
138: dynaProps[i].getName());
139: }
140:
141: }
142:
143: public void testNewInstance() {
144:
145: try {
146: dynaClass.newInstance();
147: fail("Did not throw UnsupportedOperationException()");
148: } catch (UnsupportedOperationException e) {
149: // Expected result
150: } catch (Exception e) {
151: fail("Threw exception " + e);
152: }
153:
154: }
155:
156: public void testIteratorCount() {
157:
158: Iterator rows = dynaClass.iterator();
159: assertNotNull("iterator exists", rows);
160: int n = 0;
161: while (rows.hasNext()) {
162: rows.next();
163: n++;
164: if (n > 10) {
165: fail("Returned too many rows");
166: }
167: }
168: assertEquals("iterator rows", 5, n);
169:
170: }
171:
172: public void testIteratorResults() {
173:
174: // Grab the third row
175: Iterator rows = dynaClass.iterator();
176: rows.next();
177: rows.next();
178: DynaBean row = (DynaBean) rows.next();
179:
180: // Invalid argument test
181: try {
182: row.get("unknownProperty");
183: fail("Did not throw IllegalArgumentException");
184: } catch (IllegalArgumentException e) {
185: // Expected result
186: }
187:
188: // Verify property values
189:
190: Object bigDecimalProperty = row.get("bigdecimalproperty");
191: assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
192: assertTrue("bigDecimalProperty type",
193: bigDecimalProperty instanceof BigDecimal);
194: assertEquals("bigDecimalProperty value", 123.45,
195: ((BigDecimal) bigDecimalProperty).doubleValue(), 0.005);
196:
197: Object intProperty = row.get("intproperty");
198: assertNotNull("intProperty exists", intProperty);
199: assertTrue("intProperty type", intProperty instanceof Integer);
200: assertEquals("intProperty value", 103, ((Integer) intProperty)
201: .intValue());
202:
203: Object nullProperty = row.get("nullproperty");
204: assertNull("nullProperty null", nullProperty);
205:
206: Object stringProperty = row.get("stringproperty");
207: assertNotNull("stringProperty exists", stringProperty);
208: assertTrue("stringProperty type",
209: stringProperty instanceof String);
210: assertEquals("stringProperty value", "This is a string",
211: (String) stringProperty);
212:
213: }
214:
215: /**
216: * Test normal case column names (i.e. not converted to lower case)
217: */
218: public void testIteratorResultsNormalCase() {
219: ResultSetDynaClass dynaClass = null;
220: try {
221: dynaClass = new ResultSetDynaClass(TestResultSet
222: .createProxy(), false);
223: } catch (Exception e) {
224: fail("Error creating ResultSetDynaClass: " + e);
225: }
226:
227: // Grab the third row
228: Iterator rows = dynaClass.iterator();
229: rows.next();
230: rows.next();
231: DynaBean row = (DynaBean) rows.next();
232:
233: // Invalid argument test
234: try {
235: row.get("unknownProperty");
236: fail("Did not throw IllegalArgumentException");
237: } catch (IllegalArgumentException e) {
238: // Expected result
239: }
240:
241: // Verify property values
242:
243: Object bigDecimalProperty = row.get("bigDecimalProperty");
244: assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
245: assertTrue("bigDecimalProperty type",
246: bigDecimalProperty instanceof BigDecimal);
247: assertEquals("bigDecimalProperty value", 123.45,
248: ((BigDecimal) bigDecimalProperty).doubleValue(), 0.005);
249:
250: Object intProperty = row.get("intProperty");
251: assertNotNull("intProperty exists", intProperty);
252: assertTrue("intProperty type", intProperty instanceof Integer);
253: assertEquals("intProperty value", 103, ((Integer) intProperty)
254: .intValue());
255:
256: Object nullProperty = row.get("nullProperty");
257: assertNull("nullProperty null", nullProperty);
258:
259: Object stringProperty = row.get("stringProperty");
260: assertNotNull("stringProperty exists", stringProperty);
261: assertTrue("stringProperty type",
262: stringProperty instanceof String);
263: assertEquals("stringProperty value", "This is a string",
264: (String) stringProperty);
265:
266: }
267:
268: }
|