1 16 package net.sf.cglib.beans; 17 18 import java.lang.reflect.Method ; 19 import junit.framework.*; 20 21 25 public class TestBulkBean extends TestCase { 26 private String getters[] = { 27 "getIntP", 28 "getLongP", 29 "getByteP", 30 "getShortP", 31 "getFloatP", 32 "isBooleanP", 33 "getCharP", 34 "getDoubleP", 35 "getStringP", 36 "getId", 37 "getName", 38 "getPrivateName" 39 } ; 40 41 private String setters[] = { 42 "setIntP", 43 "setLongP", 44 "setByteP", 45 "setShortP", 46 "setFloatP", 47 "setBooleanP", 48 "setCharP", 49 "setDoubleP", 50 "setStringP", 51 "setId", 52 "setName", 53 "setPrivateName" 54 }; 55 56 private Class types[] = { 57 int.class, 58 long.class, 59 byte.class, 60 short.class, 61 float.class, 62 boolean.class, 63 char.class, 64 double.class, 65 String .class, 66 Long .class, 67 String .class, 68 String .class 69 }; 70 71 private Object values[] = { 72 new Integer (2) , 73 new Long (4) , 74 new Byte ((byte)8), 75 new Short ((short)4), 76 new Float (1.2), 77 Boolean.TRUE, 78 new Character ('S'), 79 new Double (5.6), 80 "test", 81 new Long (88), 82 "test2", 83 "private" 84 }; 85 86 87 88 public TestBulkBean(java.lang.String testName) { 89 super(testName); 90 } 91 92 public static void main(java.lang.String [] args) { 93 junit.textui.TestRunner.run(suite()); 94 } 95 96 public static Test suite() { 97 return new TestSuite(TestBulkBean.class); 98 } 99 100 101 public void testGetInstance() throws Throwable { 102 BulkBean mClass = BulkBean.create(MA.class, getters, setters, types); 103 104 MA bean = new MA(); 105 106 mClass.setPropertyValues( bean, values ); 107 Object values1[] = mClass.getPropertyValues( bean ); 108 109 for( int i = 0; i < types.length; i++ ){ 110 assertEquals(" property " + getters[i] + "/" + setters[i] , values[i] , values1[i] ); 111 } 112 } 113 114 public void testEmpty() throws Throwable { 115 BulkBean.create(MA.class, new String [0], new String [0], new Class [0]); 116 } 117 118 public void testBadTypes() throws Throwable { 119 Class [] types2 = (Class [])types.clone(); 120 types2[2] = String .class; 121 try { 122 BulkBean.create(MA.class, getters, setters, types2); 123 fail("expected exception"); 124 } catch (BulkBeanException e) { 125 assertTrue(e.getIndex() == 2); 126 } 127 } 128 129 public void testMismatchedLengths() throws Throwable { 130 try { 131 BulkBean.create(MA.class, getters, setters, new Class [0]); 132 fail("expected exception"); 133 } catch (BulkBeanException e) { 134 assertTrue(e.getIndex() == -1); 135 } 136 } 137 138 public void testMissingProperty() throws Throwable { 139 String [] getters2 = (String [])getters.clone(); 140 getters2[3] = "getChris"; 141 try { 142 BulkBean.create(MA.class, getters2, setters, types); 143 fail("expected exception"); 144 } catch (BulkBeanException e) { 145 assertTrue(e.getIndex() == 3); 146 } 147 } 148 149 public void testSetWrongType() throws Throwable { 150 BulkBean mClass = BulkBean.create(MA.class, getters, setters, types); 151 MA bean = new MA(); 152 Object [] values2 = (Object [])values.clone(); 153 values2[4] = new Object (); 154 try { 155 mClass.setPropertyValues(bean, values2); 156 fail("expected exception"); 157 } catch (BulkBeanException e) { 158 assertTrue(e.getIndex() == 4); 159 } 160 } 161 162 public void testBulkBeanPerformance() throws Throwable { 163 164 int iterations = 100000; 165 166 System.out.println(); 167 System.out.println("iteration count: " + iterations); 168 System.out.println(); 169 170 BulkBean mClass = new BulkBeanReflectImpl( MA.class,getters,setters,types ); 171 172 System.out.println( mClass.getClass().getName() + ": " ); 173 int b = performanceTest( mClass, iterations ); 174 System.out.println( b + " ms. " + ( b/(float)iterations) + " per iteration" ); 175 System.out.println(); 176 177 178 mClass = BulkBean.create(MA.class, getters, setters, types); 179 180 181 System.out.println( mClass.getClass().getName() + ": " ); 182 int a = performanceTest( mClass, iterations ); 183 System.out.println( a + " ms. " + ( a/(float)iterations) + " per iteration" ); 184 185 186 System.out.println( "factor: " + b/(float)a ); 187 188 mClass = new BulkBeanPlainImpl(); 189 190 System.out.println( mClass.getClass().getName() + ": " ); 191 a = performanceTest( mClass, iterations ); 192 System.out.println( a + " ms. " + ( a/(float)iterations) + " per iteration" ); 193 194 195 } 196 197 public int performanceTest( BulkBean mc, int iterations ) throws Throwable { 198 199 200 201 long start = System.currentTimeMillis(); 202 for( int i = 0; i< iterations; i++ ){ 203 MA bean = new MA(); mc.setPropertyValues( bean, values ); 205 mc.getPropertyValues( bean, values ); 206 } 207 208 return (int)( System.currentTimeMillis() - start ); 209 } 210 211 private static class BulkBeanPlainImpl extends BulkBean { 212 213 214 public void getPropertyValues(Object bean, Object [] values) { 215 216 int i = 0; 217 MA ma = (MA) bean; 218 219 values[i++] = new Integer (ma.getIntP()); 220 values[i++] = new Long (ma.getLongP()); 221 values[i++] = new Byte (ma.getByteP()); 222 values[i++] = new Short (ma.getShortP()); 223 values[i++] = new Float (ma.getFloatP()); 224 values[i++] = new Boolean (ma.isBooleanP()); 225 values[i++] = new Character ( ma.getCharP() ); 226 values[i++] = new Double (ma.getDoubleP()); 227 values[i++] = ma.getStringP(); 228 values[i++] = ma.getId(); 229 values[i++] = ma.getName(); 230 values[i++] = ma.getPrivateName(); 231 232 233 } 234 235 236 public void setPropertyValues(Object bean, Object [] values) { 237 238 int i = 0; 239 MA ma = (MA) bean; 240 241 ma.setIntP(((Number )values[i++]).intValue()); 242 ma.setLongP(((Number )values[i++]).longValue()); 243 ma.setByteP(((Number )values[i++]).byteValue()); 244 ma.setShortP(((Number )values[i++]).shortValue()); 245 ma.setFloatP(((Number )values[i++]).floatValue()); 246 ma.setBooleanP(((Boolean )values[i++]).booleanValue()); 247 ma.setCharP( ((Character )values[i++]).charValue()) ; 248 ma.setDoubleP(((Number )values[i++]).doubleValue()); 249 ma.setStringP((String ) values[i++]); 250 ma.setId((Long ) values[i++]); 251 ma.setName((String ) values[i++]); 252 ma.setPrivateName((String ) values[i++]); 253 254 255 } 256 257 258 } 259 260 261 private static class BulkBeanReflectImpl extends BulkBean { 262 263 private Method gets[]; 264 private Method sets[]; 265 private int size ; 266 public BulkBeanReflectImpl(Class target, String [] getters, String [] setters, Class [] types) { 267 this.target = target; 268 this.types = types; 269 this.getters = getters; 270 this.setters = setters; 271 272 size = this.types.length; 273 gets = new Method [size]; 274 sets = new Method [size]; 275 276 try{ 277 278 for( int i = 0; i< size; i++ ) { 279 280 if( getters[i] != null ){ 281 gets[i] = target.getDeclaredMethod( getters[i], new Class []{} ); 282 gets[i].setAccessible(true); 283 } 284 if( setters[i] != null ) { 285 sets[i] = target.getDeclaredMethod( setters[i], new Class []{ types[i] } ); 286 sets[i].setAccessible(true); 287 } 288 289 290 } 291 }catch( Exception e ){ 292 throw new Error (e.getClass().getName() + ":" + e.getMessage() ); 293 } 294 } 295 296 public void getPropertyValues(Object bean, Object [] values) { 297 298 try{ 299 for( int i = 0; i < size ; i++ ){ 300 if( this.gets[i] != null ){ 301 values[i] = gets[i].invoke(bean, null ); 302 } 303 } 304 }catch( Exception e ){ 305 throw new Error ( e.getMessage() ); 306 } 307 } 308 309 public void setPropertyValues(Object bean, Object [] values) { 310 try{ 311 312 for( int i = 0; i < size ; i++ ){ 313 if( this.sets[i] != null ){ 314 sets[i].invoke(bean, new Object []{ values[i] } ); 315 } 316 } 317 318 319 }catch( Exception e ){ 320 e.printStackTrace(); 321 throw new Error ( e.getMessage() ); 322 } 323 } 324 325 } 326 327 331 332 } 333
| Popular Tags
|