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