1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 */ 54 package net.sf.cglib.beans; 55 56 import java.lang.reflect.Method; 57 import junit.framework.*; 58 59 /*** 60 * 61 * @author baliuka 62 */ 63 public class TestBulkBean extends TestCase { 64 private String getters[] = { 65 "getIntP", 66 "getLongP", 67 "getByteP", 68 "getShortP", 69 "getFloatP", 70 "isBooleanP", 71 "getCharP", 72 "getDoubleP", 73 "getStringP", 74 "getId", 75 "getName", 76 "getPrivateName" 77 } ; 78 79 private String setters[] = { 80 "setIntP", 81 "setLongP", 82 "setByteP", 83 "setShortP", 84 "setFloatP", 85 "setBooleanP", 86 "setCharP", 87 "setDoubleP", 88 "setStringP", 89 "setId", 90 "setName", 91 "setPrivateName" 92 }; 93 94 private Class types[] = { 95 int.class, 96 long.class, 97 byte.class, 98 short.class, 99 float.class, 100 boolean.class, 101 char.class, 102 double.class, 103 String.class, 104 Long.class, 105 String.class, 106 String.class 107 }; 108 109 private Object values[] = { 110 new Integer(2) , 111 new Long(4) , 112 new Byte((byte)8), 113 new Short((short)4), 114 new Float(1.2), 115 Boolean.TRUE, 116 new Character('S'), 117 new Double(5.6), 118 "test", 119 new Long(88), 120 "test2", 121 "private" 122 }; 123 124 125 126 public TestBulkBean(java.lang.String testName) { 127 super(testName); 128 } 129 130 public static void main(java.lang.String[] args) { 131 junit.textui.TestRunner.run(suite()); 132 } 133 134 public static Test suite() { 135 return new TestSuite(TestBulkBean.class); 136 } 137 138 /*** Test of create method, of class net.sf.cglib.BulkBean. */ 139 public void testGetInstance() throws Throwable { 140 BulkBean mClass = BulkBean.create(MA.class, getters, setters, types); 141 142 MA bean = new MA(); 143 144 mClass.setPropertyValues( bean, values ); 145 Object values1[] = mClass.getPropertyValues( bean ); 146 147 for( int i = 0; i < types.length; i++ ){ 148 assertEquals(" property " + getters[i] + "/" + setters[i] , values[i] , values1[i] ); 149 } 150 } 151 152 public void testEmpty() throws Throwable { 153 BulkBean.create(MA.class, new String[0], new String[0], new Class[0]); 154 } 155 156 public void testBadTypes() throws Throwable { 157 Class[] types2 = (Class[])types.clone(); 158 types2[2] = String.class; 159 try { 160 BulkBean.create(MA.class, getters, setters, types2); 161 fail("expected exception"); 162 } catch (BulkBeanException e) { 163 assertTrue(e.getIndex() == 2); 164 } 165 } 166 167 public void testMismatchedLengths() throws Throwable { 168 try { 169 BulkBean.create(MA.class, getters, setters, new Class[0]); 170 fail("expected exception"); 171 } catch (BulkBeanException e) { 172 assertTrue(e.getIndex() == -1); 173 } 174 } 175 176 public void testMissingProperty() throws Throwable { 177 String[] getters2 = (String[])getters.clone(); 178 getters2[3] = "getChris"; 179 try { 180 BulkBean.create(MA.class, getters2, setters, types); 181 fail("expected exception"); 182 } catch (BulkBeanException e) { 183 assertTrue(e.getIndex() == 3); 184 } 185 } 186 187 public void testSetWrongType() throws Throwable { 188 BulkBean mClass = BulkBean.create(MA.class, getters, setters, types); 189 MA bean = new MA(); 190 Object[] values2 = (Object[])values.clone(); 191 values2[4] = new Object(); 192 try { 193 mClass.setPropertyValues(bean, values2); 194 fail("expected exception"); 195 } catch (BulkBeanException e) { 196 assertTrue(e.getIndex() == 4); 197 } 198 } 199 200 public void testBulkBeanPerformance() throws Throwable{ 201 202 int iterations = 100000; 203 204 System.out.println(); 205 System.out.println("iteration count: " + iterations); 206 System.out.println(); 207 208 BulkBean mClass = new BulkBeanReflectImpl( MA.class,getters,setters,types ); 209 210 System.out.println( mClass.getClass().getName() + ": " ); 211 int b = performanceTest( mClass, iterations ); 212 System.out.println( b + " ms. " + ( b/(float)iterations) + " per iteration" ); 213 System.out.println(); 214 215 216 mClass = BulkBean.create(MA.class, getters, setters, types); 217 218 219 System.out.println( mClass.getClass().getName() + ": " ); 220 int a = performanceTest( mClass, iterations ); 221 System.out.println( a + " ms. " + ( a/(float)iterations) + " per iteration" ); 222 223 224 System.out.println( "factor: " + b/(float)a ); 225 } 226 227 public int performanceTest( BulkBean mc, int iterations ) throws Throwable{ 228 229 230 231 long start = System.currentTimeMillis(); 232 for( int i = 0; i< iterations; i++ ){ 233 MA bean = new MA(); // (MA)mc.newInstance(); 234 mc.setPropertyValues( bean, values ); 235 mc.getPropertyValues( bean, values ); 236 } 237 238 return (int)( System.currentTimeMillis() - start ); 239 } 240 241 /*** Generated implementation of abstract class net.sf.cglib.BulkBean. Please fill dummy bodies of generated methods. */ 242 private static class BulkBeanReflectImpl extends BulkBean { 243 244 private Method gets[]; 245 private Method sets[]; 246 private int size ; 247 public BulkBeanReflectImpl(Class target, String[] getters, String[] setters, Class[] types) { 248 this.target = target; 249 this.types = types; 250 this.getters = getters; 251 this.setters = setters; 252 253 size = this.types.length; 254 gets = new Method [size]; 255 sets = new Method [size]; 256 257 try{ 258 259 for( int i = 0; i< size; i++ ) { 260 261 if( getters[i] != null ){ 262 gets[i] = target.getDeclaredMethod( getters[i], new Class[]{} ); 263 gets[i].setAccessible(true); 264 } 265 if( setters[i] != null ) { 266 sets[i] = target.getDeclaredMethod( setters[i], new Class[]{ types[i] } ); 267 sets[i].setAccessible(true); 268 } 269 270 271 } 272 }catch( Exception e ){ 273 throw new Error(e.getClass().getName() + ":" + e.getMessage() ); 274 } 275 } 276 277 public void getPropertyValues(Object bean, Object[] values) { 278 279 try{ 280 for( int i = 0; i < size ; i++ ){ 281 if( this.gets[i] != null ){ 282 values[i] = gets[i].invoke(bean, null ); 283 } 284 } 285 }catch( Exception e ){ 286 throw new Error( e.getMessage() ); 287 } 288 } 289 290 public void setPropertyValues(Object bean, Object[] values) { 291 try{ 292 293 for( int i = 0; i < size ; i++ ){ 294 if( this.sets[i] != null ){ 295 sets[i].invoke(bean, new Object[]{ values[i] } ); 296 } 297 } 298 299 300 }catch( Exception e ){ 301 e.printStackTrace(); 302 throw new Error( e.getMessage() ); 303 } 304 } 305 306 } 307 308 // Add test methods here, they have to start with 'test' name. 309 // for example: 310 // public void testHello() {} 311 312 313 }

This page was automatically generated by Maven