KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > TestCase


1 //$Id: TestCase.java,v 1.2 2005/05/14 17:00:48 epbernard Exp $
2
package org.hibernate.test.annotations;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.Session;
6 import org.hibernate.SessionFactory;
7 import org.hibernate.cfg.AnnotationConfiguration;
8 import org.hibernate.cfg.Configuration;
9 import org.hibernate.cfg.Environment;
10 import org.hibernate.dialect.Dialect;
11
12 public abstract class TestCase extends junit.framework.TestCase {
13
14     private static SessionFactory sessions;
15     private static AnnotationConfiguration cfg;
16     private static Dialect dialect;
17     private static Class JavaDoc lastTestClass;
18     private Session session;
19
20     public TestCase(String JavaDoc x) {
21         super(x);
22     }
23
24     private void buildSessionFactory(Class JavaDoc[] classes, String JavaDoc[] packages) throws Exception JavaDoc {
25
26         if ( getSessions()!=null ) getSessions().close();
27         try {
28             setCfg( new AnnotationConfiguration() );
29             configure(cfg);
30             if ( recreateSchema() ) {
31                 cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
32             }
33             for (int i=0; i<packages.length; i++) {
34                 getCfg().addPackage( packages[i] );
35             }
36             for (int i=0; i<classes.length; i++) {
37                 getCfg().addAnnotatedClass( classes[i] );
38             }
39             setDialect( Dialect.getDialect() );
40             setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) );
41         }
42         catch (Exception JavaDoc e) {
43             e.printStackTrace();
44             throw e;
45         }
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         if ( getSessions()==null || lastTestClass!=getClass() ) {
50             buildSessionFactory( getMappings(), getAnnotatedPackages() );
51             lastTestClass = getClass();
52         }
53     }
54
55     protected void runTest() throws Throwable JavaDoc {
56         try {
57             super.runTest();
58             if ( session!=null && session.isOpen() ) {
59                 if ( session.isConnected() ) session.connection().rollback();
60                 session.close();
61                 session = null;
62                 fail("unclosed session");
63             }
64             else {
65                 session=null;
66             }
67         }
68         catch (Throwable JavaDoc e) {
69             try {
70                 if ( session!=null && session.isOpen() ) {
71                     if ( session.isConnected() ) session.connection().rollback();
72                     session.close();
73                 }
74             }
75             catch (Exception JavaDoc ignore) {}
76             try {
77                 if (sessions!=null) {
78                     sessions.close();
79                     sessions=null;
80                 }
81             }
82             catch (Exception JavaDoc ignore) {}
83             throw e;
84         }
85     }
86
87     public Session openSession() throws HibernateException {
88         session = getSessions().openSession();
89         return session;
90     }
91
92     protected abstract Class JavaDoc[] getMappings();
93     
94     protected String JavaDoc[] getAnnotatedPackages() {
95         return new String JavaDoc[] {};
96     }
97
98     private void setSessions(SessionFactory sessions) {
99         TestCase.sessions = sessions;
100     }
101
102     protected SessionFactory getSessions() {
103         return sessions;
104     }
105
106     private void setDialect(Dialect dialect) {
107         TestCase.dialect = dialect;
108     }
109
110     protected Dialect getDialect() {
111         return dialect;
112     }
113
114     protected static void setCfg(AnnotationConfiguration cfg) {
115         TestCase.cfg = cfg;
116     }
117
118     protected static AnnotationConfiguration getCfg() {
119         return cfg;
120     }
121
122     protected void configure(Configuration cfg) {
123         //cfg.getSessionEventListenerConfig().setFlushEventListener( new EJB3FlushEventListener() );
124
//cfg.getSessionEventListenerConfig().setAutoFlushEventListener( new EJB3AutoFlushEventListener() );
125
}
126
127     protected boolean recreateSchema() {
128         return true;
129     }
130
131 }
132
Popular Tags