1 package org.jbpm.context.exe; 2 3 4 import junit.framework.TestCase; 5 6 import org.hibernate.cfg.Configuration; 7 import org.jbpm.context.def.ContextDefinition; 8 import org.jbpm.db.AbstractDbTestCase; 9 import org.jbpm.db.GraphSession; 10 import org.jbpm.db.JbpmSchema; 11 import org.jbpm.db.JbpmSession; 12 import org.jbpm.db.JbpmSessionFactory; 13 import org.jbpm.graph.def.ProcessDefinition; 14 import org.jbpm.graph.exe.ProcessInstance; 15 16 public class HibernateCustomizationTest extends TestCase { 17 18 Configuration configuration; 19 JbpmSchema jbpmDbSchema; 20 JbpmSessionFactory jbpmSessionFactory; 21 JbpmSession jbpmSession; 22 GraphSession graphSession; 23 ProcessDefinition processDefinition; 24 ProcessInstance processInstance; 25 ContextInstance contextInstance; 26 27 public void testCustomVariableClassWithLongId() { 28 try { 29 createDatabase(CustomLongClass.class); 30 31 CustomLongClass customLongObject = new CustomLongClass("customname"); 33 contextInstance.setVariable("custom hibernate object", customLongObject); 34 35 saveAndReload(); 36 37 customLongObject = (CustomLongClass) contextInstance.getVariable("custom hibernate object"); 39 assertNotNull(customLongObject); 40 assertEquals("customname", customLongObject.getName()); 41 42 } finally { 43 dropDatabase(); 44 } 45 } 46 47 48 public void testCustomVariableClassWithStringId() { 49 try { 50 createDatabase(CustomStringClass.class); 51 52 CustomStringClass customStringObject = new CustomStringClass("customname"); 54 contextInstance.setVariable("custom hibernate object", customStringObject); 55 56 saveAndReload(); 57 58 customStringObject = (CustomStringClass) contextInstance.getVariable("custom hibernate object"); 60 assertNotNull(customStringObject); 61 assertEquals("customname", customStringObject.getName()); 62 63 } finally { 64 dropDatabase(); 65 } 66 } 67 68 162 163 private void saveAndReload() { 164 graphSession.saveProcessInstance(processInstance); 166 jbpmSession.commitTransaction(); 168 jbpmSession.close(); 170 jbpmSession = jbpmSessionFactory.openJbpmSession(); 172 jbpmSession.beginTransaction(); 173 graphSession = jbpmSession.getGraphSession(); 175 processInstance = graphSession.loadProcessInstance(processInstance.getId()); 176 contextInstance = processInstance.getContextInstance(); 177 } 178 179 public void createDatabase(Class extraHibernateConfigClass) { 180 configuration = AbstractDbTestCase.getTestConfiguration(); 181 182 jbpmDbSchema = new JbpmSchema(configuration); 184 if (jbpmDbSchema.getJbpmTables().size()>0) { 185 jbpmDbSchema.dropSchema(); 187 } 188 189 configuration.addClass(extraHibernateConfigClass); 191 192 jbpmDbSchema = new JbpmSchema(configuration); 194 jbpmDbSchema.createSchema(); 195 196 jbpmSessionFactory = new JbpmSessionFactory(configuration); 198 jbpmSession = jbpmSessionFactory.openJbpmSession(); 199 graphSession = jbpmSession.getGraphSession(); 200 jbpmSession.beginTransaction(); 202 203 processDefinition = new ProcessDefinition(); 205 processDefinition.addDefinition(new ContextDefinition()); 206 graphSession.saveProcessDefinition(processDefinition); 207 208 processInstance = new ProcessInstance(processDefinition); 210 contextInstance = processInstance.getContextInstance(); 212 } 213 214 public void dropDatabase() { 215 if (jbpmSession!=null) jbpmSession.commitTransaction(); 217 if (jbpmSession!=null) jbpmSession.close(); 219 if (jbpmDbSchema!=null) jbpmDbSchema.dropSchema(); 221 } 222 } 223 | Popular Tags |