KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: TestCase.java,v 1.19 2005/07/14 14:57:29 steveebersole Exp $
2
package org.hibernate.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.engine.SessionFactoryImplementor;
16 import org.hibernate.mapping.Collection;
17 import org.hibernate.mapping.PersistentClass;
18 import org.hibernate.mapping.Property;
19 import org.hibernate.mapping.SimpleValue;
20
21 import junit.framework.AssertionFailedError;
22
23 public abstract class TestCase extends junit.framework.TestCase {
24
25     private static SessionFactory sessions;
26     private static Configuration cfg;
27     private static Dialect dialect;
28     private static Class JavaDoc lastTestClass;
29     private org.hibernate.classic.Session session;
30
31     protected boolean recreateSchema() {
32         return true;
33     }
34
35     public TestCase(String JavaDoc x) {
36         super(x);
37     }
38     
39     protected void configure(Configuration cfg) {}
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     /**
109      * @return
110      */

111     protected String JavaDoc getBaseForMappings() {
112         return "org/hibernate/test/";
113     }
114
115     public String JavaDoc getCacheConcurrencyStrategy() {
116         return "nonstrict-read-write";
117     }
118
119     protected void setUp() throws Exception JavaDoc {
120         if ( getSessions()==null || lastTestClass!=getClass() ) {
121             buildSessionFactory( getMappings() );
122             lastTestClass = getClass();
123         }
124     }
125
126     protected void runTest() throws Throwable JavaDoc {
127         final boolean stats = ( (SessionFactoryImplementor) sessions ).getStatistics().isStatisticsEnabled();
128         try {
129             if (stats) sessions.getStatistics().clear();
130
131             super.runTest();
132
133             if (stats) sessions.getStatistics().logSummary();
134
135             if ( session!=null && session.isOpen() ) {
136                 if ( session.isConnected() ) session.connection().rollback();
137                 session.close();
138                 session = null;
139                 fail("unclosed session");
140             }
141             else {
142                 session=null;
143             }
144         }
145         catch (Throwable JavaDoc e) {
146             try {
147                 if ( session!=null && session.isOpen() ) {
148                     if ( session.isConnected() ) session.connection().rollback();
149                     session.close();
150                 }
151             }
152             catch (Exception JavaDoc ignore) {}
153             try {
154                 if ( dropAfterFailure() && sessions!=null ) {
155                     sessions.close();
156                     sessions=null;
157                 }
158             }
159             catch (Exception JavaDoc ignore) {}
160             throw e;
161         }
162     }
163     
164     protected boolean dropAfterFailure() {
165         return true;
166     }
167
168     public org.hibernate.classic.Session openSession() throws HibernateException {
169         session = getSessions().openSession();
170         return session;
171     }
172
173     public org.hibernate.classic.Session openSession(Interceptor interceptor)
174     throws HibernateException {
175         session = getSessions().openSession(interceptor);
176         return session;
177     }
178
179     protected abstract String JavaDoc[] getMappings();
180
181     private void setSessions(SessionFactory sessions) {
182         TestCase.sessions = sessions;
183     }
184
185     protected SessionFactory getSessions() {
186         return sessions;
187     }
188
189     private void setDialect(Dialect dialect) {
190         TestCase.dialect = dialect;
191     }
192
193     protected Dialect getDialect() {
194         return dialect;
195     }
196
197     protected static void setCfg(Configuration cfg) {
198         TestCase.cfg = cfg;
199     }
200
201     protected static Configuration getCfg() {
202         return cfg;
203     }
204     
205     /**
206      * @deprecated
207      */

208     public Properties JavaDoc getExtraProperties() {
209         return new Properties JavaDoc();
210     }
211
212     public static void assertElementTypeAssignability(java.util.Collection JavaDoc collection, Class JavaDoc clazz) throws AssertionFailedError {
213         Iterator JavaDoc itr = collection.iterator();
214         while ( itr.hasNext() ) {
215             assertClassAssignability( itr.next().getClass(), clazz );
216         }
217     }
218
219     public static void assertClassAssignability(Class JavaDoc source, Class JavaDoc target) throws AssertionFailedError {
220         if ( !target.isAssignableFrom( source ) ) {
221             throw new AssertionFailedError(
222                     "Classes were not assignment-compatible : source<" + source.getName() +
223                     "> target<" + target.getName() + ">"
224             );
225         }
226     }
227 }
228
Popular Tags