1 package org.hibernate.ejb.test; 3 4 import java.sql.Blob ; 5 import java.sql.Clob ; 6 import java.util.Iterator ; 7 import java.util.Properties ; 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 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 lastTestClass; 31 private org.hibernate.classic.Session session; 32 33 protected boolean recreateSchema() { 34 return true; 35 } 36 37 public EJB3TestCase(String x) { 38 super( x ); 39 } 40 41 private void buildSessionFactory(String [] files) throws Exception { 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 iter = cfg.getClassMappings(); 67 while ( iter.hasNext() ) { 68 PersistentClass clazz = (PersistentClass) iter.next(); 69 Iterator props = clazz.getPropertyClosureIterator(); 70 boolean hasLob = false; 71 while ( props.hasNext() ) { 72 Property prop = (Property) props.next(); 73 if ( prop.getValue().isSimpleValue() ) { 74 String type = ( (SimpleValue) prop.getValue() ).getTypeName(); 75 if ( "blob".equals( type ) || "clob".equals( type ) ) hasLob = true; 76 if ( Blob .class.getName().equals( type ) || Clob .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( ) ); 99 100 } 101 catch (Exception e) { 102 e.printStackTrace(); 103 throw e; 104 } 105 106 } 107 108 public String getCacheConcurrencyStrategy() { 109 return "nonstrict-read-write"; 110 } 111 112 protected void setUp() throws Exception { 113 if ( getSessions() == null || lastTestClass != getClass() ) { 114 buildSessionFactory( getMappings() ); 115 lastTestClass = getClass(); 116 } 117 } 118 119 protected void runTest() throws Throwable { 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 e) { 139 try { 140 if ( session != null && session.isOpen() ) { 141 if ( session.isConnected() ) session.connection().rollback(); 142 session.close(); 143 } 144 } 145 catch (Exception ignore) { 146 } 147 try { 148 if ( dropAfterFailure() && sessions != null ) { 149 sessions.close(); 150 sessions = null; 151 } 152 } 153 catch (Exception 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 [] 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 203 public Properties getExtraProperties() { 204 return new Properties (); 205 } 206 207 protected String 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 |