Hibernate::
|
Testing environment.Last update: Oct-10-2005 Every test case has a class that is responsible for DB schema creation and initialization, in every package the class is named DBSetterImpl because it implements DBSetter interface. The class gets executed by TestUtilities ( line #42) . The TestUtilities method sequentially executes _dropTables() method, then createTables(), and finally generateTestData() method. public class DBSetterImpl extends AbstractDBSetter { 16 17 18 public void createTables( Connection c ) throws IOException, SQLException{ 19 execScript( c, this.getClass().getResourceAsStream( "data/create-db.sql")); 20 } 21 22 public void _dropTables( Connection c ) throws Exception{ 23 execScriptTolerantly( c, this.getClass().getResourceAsStream( "data/drop-db.sql")); 24 } 25 26 public void generateTestData( Connection c ) throws SQLException{ 27 for( int main_id = 0; main_id < 5; main_id++){ 28 exec( c, "INSERT INTO sql_addresses (street, zip ) VALUES ( 'Street "+ main_id +"','98034-"+ main_id + "')"); 29 } 30 } 31 32 public static void main( String[] args ){ 33 try{ 34 TestUtilities.run( new DBSetterImpl() ); 35 } catch( Exception e ){ 36 e.printStackTrace(); 37 } 38 } 39 } 40 Test environment utilizes Spring AOP facilities programmatically to wrap our test POJOs into tracing and other services if necessary. 47 48 public static Object wrap( Object target, MethodInterceptor[] interceptors ){ 49 debugInterceptor.setLoggerName( "com.sourcelabs.hibernate.bhw.utils" ); 50 ProxyFactoryBean pf = new ProxyFactoryBean(); 51 pf.setTarget( target ); 52 if( interceptors != null ){ 53 for (int i = 0; i < interceptors.length; i++) { 54 MethodInterceptor interceptor = interceptors[i]; 55 pf.addAdvice( interceptor ); 56 } 57 } 58 //pf.addAdvice( debugInterceptor ); 59 return pf.getObject(); 60 } Execution of test methods of our test classes is the reflection based (line #97): 73 74 public static void _run( Object testTarget, String methodName, SessionFactory hibernateSessionFactory, int n ) 75 throws Exception, IllegalAccessException, InvocationTargetException{ 76 Method m = testTarget.getClass().getMethod( methodName, null); 77 Object wrappedTarget = wrap( testTarget ); 78 long start = System.currentTimeMillis(); 79 for( int i = 0; i < n;i++){ 80 System.out.println( "Pass #" + (i+1) ); 81 long startIt = System.currentTimeMillis(); 82 runOnce( hibernateSessionFactory, m, wrappedTarget ); 83 System.out.println( m.getName() + "\t" + (System.currentTimeMillis() - startIt ) + " ms" ); 84 } 85 System.out.println( "Average::" + ((1.0* (System.currentTimeMillis() - start ))/ n ) + " ms"); 86 } 87 88 private static void runOnce( SessionFactory hibernateSessionFactory, Method m, Object target ) 89 throws Exception{ 90 Connection c = createConnection(); 91 Session s = null; 92 try{ 93 System.out.println( "hibernateSessionFactory = " + hibernateSessionFactory ); 94 s = hibernateSessionFactory.openSession( c ); 95 hsession.set( s ); 96 tx.set( s.beginTransaction() ); 97 m.invoke( target, null ); 98 (( Transaction )tx.get()).commit(); 99 100 } finally{ 101 s.close(); 102 c.close(); 103 hsession.set( null); 104 tx.set( null ); 105 } 106 } Therefore typical test invocation looks like the following: We supply our testing utilities with test class, name of test method, number of invocations of the method and Hibernate session factory to use. Utility classes do the rest. |
© Copyright 2003-2006 SourceLabs, Inc. All Rights Reserved.
411 First Ave S. Ste 403, Seattle, WA 98104 | 206.322.0099 | info@sourcelabs.com