KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: TestCase.java,v 1.4 2005/07/09 20:04:44 epbernard Exp $
2
package org.hibernate.ejb.test;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Properties JavaDoc;
10 import javax.persistence.EntityManagerFactory;
11 import javax.persistence.Persistence;
12
13 import org.hibernate.ejb.HibernatePersistence;
14
15
16 /**
17  * @author Emmanuel Bernard
18  */

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 JavaDoc 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 JavaDoc[] getAnnotatedClasses();
39
40     public Map JavaDoc<Class JavaDoc, String JavaDoc> getCachedClasses() {
41         return new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
42     }
43
44     public Map JavaDoc<String JavaDoc, String JavaDoc> getCachedCollections() {
45         return new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
46     }
47
48     public static Properties JavaDoc loadProperties() {
49         Properties JavaDoc props = new Properties JavaDoc();
50         InputStream JavaDoc stream = Persistence.class.getResourceAsStream( "/persistence.properties" );
51         if ( stream != null ) {
52             try {
53                 props.load( stream );
54             }
55             catch (Exception JavaDoc e) {
56                 throw new RuntimeException JavaDoc( "could not load persistence.properties" );
57             }
58             finally {
59                 try {
60                     stream.close();
61                 }
62                 catch (IOException JavaDoc ioe) {
63                 }
64             }
65         }
66         return props;
67     }
68
69     public Map JavaDoc getConfig() {
70         Map JavaDoc config = loadProperties();
71         ArrayList JavaDoc<Class JavaDoc> classes = new ArrayList JavaDoc<Class JavaDoc>();
72
73         for ( Class JavaDoc clazz : getAnnotatedClasses() ) {
74             classes.add( clazz );
75         }
76         config.put( HibernatePersistence.LOADED_CLASSES, classes );
77         for ( Map.Entry JavaDoc<Class JavaDoc, String JavaDoc> entry : getCachedClasses().entrySet() ) {
78             config.put(
79                     HibernatePersistence.CLASS_CACHE_PREFIX + "." + entry.getKey().getName(),
80                     entry.getValue()
81             );
82         }
83         for ( Map.Entry JavaDoc<String JavaDoc, String JavaDoc> 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