1 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 40 41 public class DynaResultSetTestCase extends TestCase { 42 43 44 46 47 50 protected ResultSetDynaClass dynaClass = null; 51 52 53 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 69 70 75 public DynaResultSetTestCase(String name) { 76 77 super(name); 78 79 } 80 81 82 84 85 88 public void setUp() throws Exception { 89 90 dynaClass = new ResultSetDynaClass(new TestResultSet()); 91 92 } 93 94 95 98 public static Test suite() { 99 100 return (new TestSuite(DynaResultSetTestCase.class)); 101 102 } 103 104 105 108 public void tearDown() { 109 110 dynaClass = null; 111 112 } 113 114 115 116 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 try { 133 dynaClass.getDynaProperty(null); 134 fail("Did not throw IllegaArgumentException"); 135 } catch (IllegalArgumentException e) { 136 ; } 138 139 DynaProperty dynaProp = dynaClass.getDynaProperty("unknownProperty"); 141 assertTrue("unknown property returns null", 142 (dynaProp == null)); 143 144 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 ; } 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 Iterator rows = dynaClass.iterator(); 203 rows.next(); 204 rows.next(); 205 DynaBean row = (DynaBean) rows.next(); 206 207 try { 209 row.get("unknownProperty"); 210 fail("Did not throw IllegalArgumentException"); 211 } catch (IllegalArgumentException e) { 212 ; } 214 215 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 } 250 | Popular Tags |