KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > db > AbstractDbTestCase


1 package org.jbpm.bpel.db;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.hibernate.HibernateException;
13 import org.hibernate.cfg.Configuration;
14
15 import org.jbpm.db.*;
16 import org.jbpm.graph.exe.ProcessInstance;
17 import org.jbpm.instantiation.ClassLoaderUtil;
18
19 import org.jbpm.bpel.def.BpelDefinition;
20
21 public class AbstractDbTestCase extends TestCase {
22
23   private static final Log log = LogFactory.getLog(AbstractDbTestCase.class);
24   
25   private static JbpmSessionFactory jbpmSessionFactory = null;
26   private static boolean isJbpmSessionFactoryInitialized = false;
27   private static JbpmSchema jbpmSchema = null;
28   
29   public static synchronized JbpmSessionFactory getJbpmSessionFactory() {
30     if (! isJbpmSessionFactoryInitialized) {
31       isJbpmSessionFactoryInitialized = true;
32
33       Configuration configuration = getTestConfiguration();
34       log.debug("creating the default jbpm database schema...");
35       jbpmSchema = new JbpmSchema(configuration);
36       jbpmSchema.createSchema();
37       try {
38         jbpmSessionFactory = JbpmSessionFactory.buildJbpmSessionFactory(configuration);
39       } catch (RuntimeException JavaDoc e) {
40         e.printStackTrace();
41         throw e;
42       }
43     }
44     return jbpmSessionFactory;
45   }
46
47   public static Configuration getTestConfiguration() {
48     Configuration configuration = JbpmSessionFactory.createConfiguration();
49     InputStream JavaDoc is = ClassLoaderUtil.getStream("jbpm.test.hibernate.properties");
50     if (is!=null) {
51       Properties JavaDoc properties = new Properties JavaDoc();
52       try {
53         properties.load(is);
54       } catch (IOException JavaDoc e) {
55         throw new RuntimeException JavaDoc("couldn't load jbpm.dbtest.hibernate.properties", e);
56       }
57       Iterator JavaDoc iter = properties.keySet().iterator();
58       while (iter.hasNext()) {
59         String JavaDoc key = (String JavaDoc) iter.next();
60         String JavaDoc value = properties.getProperty(key);
61         log.debug("overwriting jbpm.test.hibernate.properties: "+key+"="+value);
62         configuration.setProperty(key, value);
63       }
64     }
65     return configuration;
66   }
67   
68   public static String JavaDoc[] dropForeignKeys = null;
69   public static String JavaDoc[] createForeignKeys = null;
70
71   protected JbpmSession jbpmSession = null;
72   protected GraphSession graphSession = null;
73   protected TaskMgmtSession taskMgmtSession = null;
74   protected ContextSession contextSession = null;
75   protected LoggingSession loggingSession = null;
76   
77   public void setUp() {
78   log.debug("STARTING "+this.getClass().getName());
79     beginSessionTransaction();
80   }
81
82   public void tearDown() {
83     log.debug("ENDING "+this.getClass().getName());
84     commitAndCloseSession();
85     clearDatabase();
86   }
87
88   protected void beginSessionTransaction() {
89     jbpmSession = getJbpmSessionFactory().openJbpmSession();
90     jbpmSession.beginTransaction();
91     graphSession = jbpmSession.getGraphSession();
92     taskMgmtSession = jbpmSession.getTaskMgmtSession();
93     contextSession = jbpmSession.getContextSession();
94     loggingSession = jbpmSession.getLoggingSession();
95   }
96
97   protected void commitAndCloseSession() {
98     jbpmSession.commitTransaction();
99     jbpmSession.close();
100     graphSession = null;
101     jbpmSession = null;
102   }
103   
104   protected void newTransaction() {
105     commitAndCloseSession();
106     beginSessionTransaction();
107   }
108   
109   protected BpelDefinition saveAndReload(BpelDefinition processDefinition) {
110     graphSession.saveProcessDefinition(processDefinition);
111     newTransaction();
112     long id = processDefinition.getId();
113     try {
114       return (BpelDefinition) jbpmSession.getSession().load( BpelDefinition.class, new Long JavaDoc(id) );
115     } catch (HibernateException e) {
116       log.error(e);
117       throw new RuntimeException JavaDoc("couldn't load process definition '" + id + "'", e);
118     }
119   }
120
121   protected ProcessInstance saveAndReload(ProcessInstance processInstance) {
122     graphSession.saveProcessInstance(processInstance);
123     newTransaction();
124     ProcessInstance reloadedProcessInstance = graphSession.loadProcessInstance(processInstance.getId());
125     return reloadedProcessInstance;
126   }
127   
128   protected void clearDatabase() {
129     try {
130       jbpmSchema.cleanSchema();
131       getJbpmSessionFactory().evictCachedProcessDefinitions();
132     } catch (Throwable JavaDoc t) {
133       t.printStackTrace();
134     }
135   }
136 }
137
Popular Tags