KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > test > EJB3TestCase


1 //$Id: EJB3TestCase.java,v 1.3 2005/07/09 20:04:44 epbernard Exp $
2
package org.hibernate.ejb.test;
3
4 import java.sql.Blob JavaDoc;
5 import java.sql.Clob JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.Interceptor;
11 import org.hibernate.SessionFactory;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Environment;
14 import org.hibernate.dialect.Dialect;
15 import org.hibernate.ejb.EJB3AutoFlushEventListener;
16 import org.hibernate.ejb.EJB3FlushEventListener;
17 import org.hibernate.engine.SessionFactoryImplementor;
18 import org.hibernate.mapping.Collection;
19 import org.hibernate.mapping.PersistentClass;
20 import org.hibernate.mapping.Property;
21 import org.hibernate.mapping.SimpleValue;
22
23 /**
24  * @author Gavin King
25  */

26 public abstract class EJB3TestCase extends junit.framework.TestCase {
27     private static SessionFactory sessions;
28     private static Configuration cfg;
29     private static Dialect dialect;
30     private static Class JavaDoc lastTestClass;
31     private org.hibernate.classic.Session session;
32
33     protected boolean recreateSchema() {
34         return true;
35     }
36
37     public EJB3TestCase(String JavaDoc x) {
38         super( x );
39     }
40
41     private void buildSessionFactory(String JavaDoc[] files) throws Exception JavaDoc {
42
43         if ( getSessions() != null ) getSessions().close();
44
45         try {
46
47             setCfg( new Configuration() );
48
49             cfg.addProperties( getExtraProperties() );
50
51             if ( recreateSchema() ) {
52                 cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
53             }
54
55             for ( int i = 0 ; i < files.length ; i++ ) {
56                 if ( !files[i].startsWith( "net/" ) ) files[i] = getBaseForMappings() + files[i];
57                 getCfg().addResource( files[i], TestCase.class.getClassLoader() );
58             }
59
60             setDialect( Dialect.getDialect() );
61
62             configure( cfg );
63
64             if ( getCacheConcurrencyStrategy() != null ) {
65
66                 Iterator JavaDoc iter = cfg.getClassMappings();
67                 while ( iter.hasNext() ) {
68                     PersistentClass clazz = (PersistentClass) iter.next();
69                     Iterator JavaDoc props = clazz.getPropertyClosureIterator();
70                     boolean hasLob = false;
71                     while ( props.hasNext() ) {
72                         Property prop = (Property) props.next();
73                         if ( prop.getValue().isSimpleValue() ) {
74                             String JavaDoc type = ( (SimpleValue) prop.getValue() ).getTypeName();
75                             if ( "blob".equals( type ) || "clob".equals( type ) ) hasLob = true;
76                             if ( Blob JavaDoc.class.getName().equals( type ) || Clob JavaDoc.class.getName().equals( type ) ) hasLob = true;
77                         }
78                     }
79                     if ( !hasLob && !clazz.isInherited() ) {
80                         cfg.setCacheConcurrencyStrategy(
81                                 clazz.getEntityName(),
82                                 getCacheConcurrencyStrategy()
83                         );
84                     }
85                 }
86
87                 iter = cfg.getCollectionMappings();
88                 while ( iter.hasNext() ) {
89                     Collection coll = (Collection) iter.next();
90                     cfg.setCollectionCacheConcurrencyStrategy(
91                             coll.getRole(),
92                             getCacheConcurrencyStrategy()
93                     );
94                 }
95
96             }
97
98             setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) );
99
100         }
101         catch (Exception JavaDoc e) {
102             e.printStackTrace();
103             throw e;
104         }
105
106     }
107
108     public String JavaDoc getCacheConcurrencyStrategy() {
109         return "nonstrict-read-write";
110     }
111
112     protected void setUp() throws Exception JavaDoc {
113         if ( getSessions() == null || lastTestClass != getClass() ) {
114             buildSessionFactory( getMappings() );
115             lastTestClass = getClass();
116         }
117     }
118
119     protected void runTest() throws Throwable JavaDoc {
120         final boolean stats = ( (SessionFactoryImplementor) sessions ).getStatistics().isStatisticsEnabled();
121         try {
122             if ( stats ) sessions.getStatistics().clear();
123
124             super.runTest();
125
126             if ( stats ) sessions.getStatistics().logSummary();
127
128             if ( session != null && session.isOpen() ) {
129                 if ( session.isConnected() ) session.connection().rollback();
130                 session.close();
131                 session = null;
132                 fail( "unclosed session" );
133             }
134             else {
135                 session = null;
136             }
137         }
138         catch (Throwable JavaDoc e) {
139             try {
140                 if ( session != null && session.isOpen() ) {
141                     if ( session.isConnected() ) session.connection().rollback();
142                     session.close();
143                 }
144             }
145             catch (Exception JavaDoc ignore) {
146             }
147             try {
148                 if ( dropAfterFailure() && sessions != null ) {
149                     sessions.close();
150                     sessions = null;
151                 }
152             }
153             catch (Exception JavaDoc ignore) {
154             }
155             throw e;
156         }
157     }
158
159     protected boolean dropAfterFailure() {
160         return true;
161     }
162
163     public org.hibernate.classic.Session openSession() throws HibernateException {
164         session = getSessions().openSession();
165         return session;
166     }
167
168     public org.hibernate.classic.Session openSession(Interceptor interceptor)
169             throws HibernateException {
170         session = getSessions().openSession( interceptor );
171         return session;
172     }
173
174     protected abstract String JavaDoc[] getMappings();
175
176     private void setSessions(SessionFactory sessions) {
177         EJB3TestCase.sessions = sessions;
178     }
179
180     protected SessionFactory getSessions() {
181         return sessions;
182     }
183
184     private void setDialect(Dialect dialect) {
185         EJB3TestCase.dialect = dialect;
186     }
187
188     protected Dialect getDialect() {
189         return dialect;
190     }
191
192     protected static void setCfg(Configuration cfg) {
193         EJB3TestCase.cfg = cfg;
194     }
195
196     protected static Configuration getCfg() {
197         return cfg;
198     }
199
200     /**
201      * @deprecated
202      */

203     public Properties JavaDoc getExtraProperties() {
204         return new Properties JavaDoc();
205     }
206
207     protected String JavaDoc getBaseForMappings() {
208         return "org/hibernate/ejb/test/";
209     }
210
211     protected void configure(Configuration cfg) {
212         cfg.setListener( "flush", new EJB3FlushEventListener() );
213         cfg.setListener( "auto-flush", new EJB3AutoFlushEventListener() );
214     }
215
216 }
217
Popular Tags