View Javadoc

1   package org.controlhaus.hibernate.util;
2   
3   import java.lang.reflect.Method;
4   import java.sql.Connection;
5   
6   import junit.framework.TestCase;
7   
8   import net.sf.hibernate.Session;
9   
10  import org.apache.beehive.controls.api.bean.Control;
11  import org.apache.beehive.controls.api.context.ControlBeanContext;
12  import org.apache.beehive.controls.runtime.bean.ControlContainerContext;
13  import org.controlhaus.hibernate.HibernateControl;
14  
15  /***
16   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
17   * @since Nov 5, 2004
18   */
19  public class AbstractHibernateTest
20      extends TestCase
21  {
22      public final static String SETUP_SQL = "hibernate.setupSql";
23      public final static String TEARDOWN_SQL = "hibernate.teardownSql";
24  
25      private ControlContainerContext context;
26      private Connection conn;
27  
28      @Control HibernateControl hibernate;
29      
30      public void setUp() throws Exception
31      {
32          super.setUp();
33          
34          context = new ControlContainerContext();
35          
36          Class top = getClass();
37          while ( top != null )
38          {
39              initializeClass( top );
40              top = top.getSuperclass();
41          }
42  
43          String filename = System.getProperty(SETUP_SQL);
44  
45          if ( filename != null )
46          {
47  	        try
48  	        {
49  	            insertSqlFile(filename);
50  	        }
51  	        catch ( Exception e )
52  	        {
53  	            e.printStackTrace();
54  	        }
55          }
56      }
57  
58      protected void initializeClass( Class clazz )
59      	throws Exception
60      {
61          context.beginContext();
62          
63          try
64          {
65              Class init = getClass().getClassLoader().loadClass( 
66                      clazz.getName() + "ClientInitializer" );
67              
68              Method m = init.getMethod("initialize", 
69                                        new Class[] 
70                                        { 
71                                              ControlBeanContext.class, 
72                                              clazz
73                                        } );
74              
75              m.invoke( null, new Object[] { context, this } );
76          }
77          catch ( ClassNotFoundException cnfe )
78          {
79              // do nothing.
80          }
81      }
82      
83      protected void insertSqlFile( String filename ) throws Exception
84      {
85          if ( hibernate == null )
86              throw new RuntimeException("AbstractHibernateTest was not initialized!");
87          
88          Session sess = hibernate.getSessionFactory().openSession();
89          conn = sess.connection();
90          System.out.println("Loading SQL " + filename);
91          BatchSqlCommandRunner runner = new BatchSqlCommandRunner(conn);
92          runner.runCommands( filename );
93  
94          sess.close();
95      }
96  
97      public void tearDown() throws Exception
98      {
99          context.endContext();
100         
101         String filename = System.getProperty(TEARDOWN_SQL);
102 
103         if ( filename != null )
104         {
105 	        try
106 	        {
107 	            insertSqlFile(filename);
108 	        }
109 	        catch ( Exception e )
110 	        {
111 	            e.printStackTrace();
112 	        }
113         }
114 
115         conn = null;
116 
117         super.tearDown();
118     }
119     
120     public ControlContainerContext getContext()
121     {
122         return context;
123     }
124 }