KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > context > exe > HibernateCustomizationTest


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       // create the custom object
32
CustomLongClass customLongObject = new CustomLongClass("customname");
33       contextInstance.setVariable("custom hibernate object", customLongObject);
34       
35       saveAndReload();
36
37       // get the custom hibernatable object from the variables
38
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       // create the custom object
53
CustomStringClass customStringObject = new CustomStringClass("customname");
54       contextInstance.setVariable("custom hibernate object", customStringObject);
55       
56       saveAndReload();
57       
58       // get the custom hibernatable object from the variables
59
customStringObject = (CustomStringClass) contextInstance.getVariable("custom hibernate object");
60       assertNotNull(customStringObject);
61       assertEquals("customname", customStringObject.getName());
62
63     } finally {
64       dropDatabase();
65     }
66   }
67
68   /*
69   public void testDateVariableInstance() {
70     List jbpmTypes = JbpmType.getJbpmTypes();
71
72     jbpmTypes.add(0, new JbpmType("java.util.Date org.jbpm.context.exe.variableinstance.DateInstance"));
73     try {
74       createDatabase(DateInstance.class);
75       
76       //long before = System.currentTimeMillis();
77       Date beforeDate = new Date();
78
79       // create the custom object
80       Date date = new Date();
81       contextInstance.setVariable("date", date);
82       
83       //long after = System.currentTimeMillis();
84       Date afterDate = new Date();
85       
86       saveAndReload();
87       
88       // get the custom hibernatable object from the variables
89       date = (Date) contextInstance.getVariable("date");
90       // test for not null on all three dates
91       assertNotNull(date);
92       assertNotNull(beforeDate);
93       assertNotNull(afterDate);
94       
95       // set up dates in calendar
96       Calendar datecal = new GregorianCalendar();
97       datecal.setTime(date);
98       Calendar beforecal = new GregorianCalendar();
99       beforecal.setTime(beforeDate);
100       Calendar aftercal = new GregorianCalendar();
101       aftercal.setTime(afterDate);
102
103       // compare all subcomponents of dates to better find problem with some
104       // database systems
105       assertTrue(aftercal.get(Calendar.YEAR)<=datecal.get(Calendar.YEAR));
106       assertTrue(datecal.get(Calendar.YEAR)<=aftercal.get(Calendar.YEAR));
107     
108       assertTrue(aftercal.get(Calendar.MONTH)<=datecal.get(Calendar.MONTH));
109       assertTrue(datecal.get(Calendar.MONTH)<=aftercal.get(Calendar.MONTH));
110     
111       assertTrue(aftercal.get(Calendar.DAY_OF_MONTH)<=datecal.get(Calendar.DAY_OF_MONTH));
112       assertTrue(datecal.get(Calendar.DAY_OF_MONTH)<=aftercal.get(Calendar.DAY_OF_MONTH));
113     
114       assertTrue(aftercal.get(Calendar.HOUR_OF_DAY)<=datecal.get(Calendar.HOUR_OF_DAY));
115       assertTrue(datecal.get(Calendar.HOUR_OF_DAY)<=aftercal.get(Calendar.HOUR_OF_DAY));
116     
117       assertTrue(aftercal.get(Calendar.MINUTE)<=datecal.get(Calendar.MINUTE));
118       assertTrue(datecal.get(Calendar.MINUTE)<=aftercal.get(Calendar.MINUTE));
119     
120       assertTrue(aftercal.get(Calendar.SECOND)<=datecal.get(Calendar.SECOND));
121       assertTrue(datecal.get(Calendar.SECOND)<=aftercal.get(Calendar.SECOND));
122     
123       assertTrue(aftercal.get(Calendar.MILLISECOND)<=datecal.get(Calendar.MILLISECOND));
124       assertTrue(datecal.get(Calendar.MILLISECOND)<=aftercal.get(Calendar.MILLISECOND));
125
126       // compare full dates
127       assertTrue(beforeDate.before(date)|beforeDate.equals(date));
128       assertTrue(afterDate.after(date)|afterDate.equals(date));
129
130       // compare using date and long
131       //assertTrue(before<=date.getTime());
132       //assertTrue(date.getTime()<=after);
133     } finally {
134       jbpmTypes.remove(0);
135       dropDatabase();
136     }
137   }
138
139   public void testDoubleVariableInstance() {
140     List jbpmTypes = JbpmType.getJbpmTypes();
141
142     jbpmTypes.add(0, new JbpmType("java.lang.Double org.jbpm.context.exe.variableinstance.DoubleInstance"));
143     try {
144       createDatabase(DoubleInstance.class);
145
146       // set a double in the variables
147       contextInstance.setVariable("double", new Double(3.3));
148       
149       saveAndReload();
150       
151       // get the custom hibernatable object from the variables
152       Double d = (Double) contextInstance.getVariable("double");
153       assertNotNull(d);
154       assertEquals(new Double(3.3), d);
155       
156     } finally {
157       jbpmTypes.remove(0);
158       dropDatabase();
159     }
160   }
161   */

162
163   private void saveAndReload() {
164     // save and close
165
graphSession.saveProcessInstance(processInstance);
166     // commit the transaction
167
jbpmSession.commitTransaction();
168     // commit the session
169
jbpmSession.close();
170     // reopen and load
171
jbpmSession = jbpmSessionFactory.openJbpmSession();
172     jbpmSession.beginTransaction();
173     // get the reloaded context instance
174
graphSession = jbpmSession.getGraphSession();
175     processInstance = graphSession.loadProcessInstance(processInstance.getId());
176     contextInstance = processInstance.getContextInstance();
177   }
178
179   public void createDatabase(Class JavaDoc extraHibernateConfigClass) {
180     configuration = AbstractDbTestCase.getTestConfiguration();
181
182     // if previous tests still left a default database schema
183
jbpmDbSchema = new JbpmSchema(configuration);
184     if (jbpmDbSchema.getJbpmTables().size()>0) {
185       // drop it
186
jbpmDbSchema.dropSchema();
187     }
188     
189     // add a custom mapping to the hibernate configuration
190
configuration.addClass(extraHibernateConfigClass);
191
192     // create the custom database schema
193
jbpmDbSchema = new JbpmSchema(configuration);
194     jbpmDbSchema.createSchema();
195
196     // create a jbpm session factory
197
jbpmSessionFactory = new JbpmSessionFactory(configuration);
198     jbpmSession = jbpmSessionFactory.openJbpmSession();
199     graphSession = jbpmSession.getGraphSession();
200     // begin a transaction
201
jbpmSession.beginTransaction();
202
203     // create and save the process definition
204
processDefinition = new ProcessDefinition();
205     processDefinition.addDefinition(new ContextDefinition());
206     graphSession.saveProcessDefinition(processDefinition);
207
208     // create the process instance
209
processInstance = new ProcessInstance(processDefinition);
210     // set the jbpmSession in the context because its used to determine the jbpm-type for the custom object.
211
contextInstance = processInstance.getContextInstance();
212   }
213
214   public void dropDatabase() {
215     // commit the transaction
216
if (jbpmSession!=null) jbpmSession.commitTransaction();
217     // commit the session
218
if (jbpmSession!=null) jbpmSession.close();
219     // drop the database schema
220
if (jbpmDbSchema!=null) jbpmDbSchema.dropSchema();
221   }
222 }
223
Popular Tags