1 package org.hibernate.ejb.test; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 import java.util.Properties ; 10 import javax.persistence.EntityManagerFactory; 11 import javax.persistence.Persistence; 12 13 import org.hibernate.ejb.HibernatePersistence; 14 15 16 19 public abstract class TestCase extends junit.framework.TestCase { 20 protected EntityManagerFactory factory; 21 22 public TestCase() { 23 super(); 24 } 25 26 public TestCase(String name) { 27 super( name ); 28 } 29 30 public void setUp() { 31 factory = new HibernatePersistence().createEntityManagerFactory( getConfig() ); 32 } 33 34 public void tearDown() { 35 factory.close(); 36 } 37 38 public abstract Class [] getAnnotatedClasses(); 39 40 public Map <Class , String > getCachedClasses() { 41 return new HashMap <Class , String >(); 42 } 43 44 public Map <String , String > getCachedCollections() { 45 return new HashMap <String , String >(); 46 } 47 48 public static Properties loadProperties() { 49 Properties props = new Properties (); 50 InputStream stream = Persistence.class.getResourceAsStream( "/persistence.properties" ); 51 if ( stream != null ) { 52 try { 53 props.load( stream ); 54 } 55 catch (Exception e) { 56 throw new RuntimeException ( "could not load persistence.properties" ); 57 } 58 finally { 59 try { 60 stream.close(); 61 } 62 catch (IOException ioe) { 63 } 64 } 65 } 66 return props; 67 } 68 69 public Map getConfig() { 70 Map config = loadProperties(); 71 ArrayList <Class > classes = new ArrayList <Class >(); 72 73 for ( Class clazz : getAnnotatedClasses() ) { 74 classes.add( clazz ); 75 } 76 config.put( HibernatePersistence.LOADED_CLASSES, classes ); 77 for ( Map.Entry <Class , String > entry : getCachedClasses().entrySet() ) { 78 config.put( 79 HibernatePersistence.CLASS_CACHE_PREFIX + "." + entry.getKey().getName(), 80 entry.getValue() 81 ); 82 } 83 for ( Map.Entry <String , String > entry : getCachedCollections().entrySet() ) { 84 config.put( 85 HibernatePersistence.COLLECTION_CACHE_PREFIX + "." + entry.getKey(), 86 entry.getValue() 87 ); 88 } 89 return config; 90 } 91 } 92 | Popular Tags |