KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.context.exe;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.jbpm.context.def.*;
7 import org.jbpm.db.*;
8 import org.jbpm.graph.def.*;
9 import org.jbpm.graph.exe.*;
10
11 public class VariableInstanceDbTest extends AbstractDbTestCase {
12
13   ProcessInstance processInstance;
14   ContextInstance contextInstance;
15   
16   public void setUp() {
17     super.setUp();
18     
19     ProcessDefinition processDefinition = new ProcessDefinition();
20     processDefinition.addDefinition(new ContextDefinition());
21     graphSession.saveProcessDefinition(processDefinition);
22
23     processInstance = new ProcessInstance(processDefinition);
24     contextInstance = processInstance.getContextInstance();
25   }
26   
27   public void testVariableInstanceString() {
28     contextInstance.setVariable("comment", "it's not the size that matters, it's how you use it.");
29     
30     processInstance = saveAndReload(processInstance);
31     contextInstance = processInstance.getContextInstance();
32     
33     assertEquals("it's not the size that matters, it's how you use it.", contextInstance.getVariable("comment"));
34   }
35
36   public void testVariableInstanceLong() {
37     contextInstance.setVariable("new salary", new Long JavaDoc(500000));
38     
39     processInstance = saveAndReload(processInstance);
40     contextInstance = processInstance.getContextInstance();
41     
42     assertEquals(new Long JavaDoc(500000), contextInstance.getVariable("new salary"));
43   }
44
45   public void testVariableInstanceByteArray() {
46     String JavaDoc text = "oh, what a wonderfull world";
47     for ( int i=0; i<10; i++) text+=text;
48     byte[] bytes = text.getBytes();
49     assertEquals(text, new String JavaDoc(bytes));
50     contextInstance.setVariable("a lot of bytes", bytes);
51     
52     processInstance = saveAndReload(processInstance);
53     
54     contextInstance = processInstance.getContextInstance();
55     bytes = (byte[]) contextInstance.getVariable("a lot of bytes");
56     assertEquals(text, new String JavaDoc(bytes));
57   }
58   
59   public void testString() {
60     contextInstance.setVariable("a", new String JavaDoc("3"));
61     processInstance = saveAndReload(processInstance);
62     contextInstance = processInstance.getContextInstance();
63     assertEquals("3", contextInstance.getVariable("a"));
64   }
65
66   public void testBoolean() {
67     contextInstance.setVariable("a", Boolean.TRUE);
68     processInstance = saveAndReload(processInstance);
69     contextInstance = processInstance.getContextInstance();
70     assertEquals(Boolean.TRUE, contextInstance.getVariable("a"));
71   }
72
73   public void testCharacter() {
74     contextInstance.setVariable("a", new Character JavaDoc('c'));
75     processInstance = saveAndReload(processInstance);
76     contextInstance = processInstance.getContextInstance();
77     assertEquals(new Character JavaDoc('c'), contextInstance.getVariable("a"));
78   }
79
80   public void testFloat() {
81     contextInstance.setVariable("a", new Float JavaDoc(3.3));
82     processInstance = saveAndReload(processInstance);
83     contextInstance = processInstance.getContextInstance();
84     assertEquals(new Float JavaDoc(3.3), contextInstance.getVariable("a"));
85   }
86   
87   public void testDouble() {
88     contextInstance.setVariable("a", new Double JavaDoc(3.3));
89     processInstance = saveAndReload(processInstance);
90     contextInstance = processInstance.getContextInstance();
91     assertEquals(new Double JavaDoc(3.3), contextInstance.getVariable("a"));
92   }
93   
94   public static class MySerializableClass implements Serializable {
95     private static final long serialVersionUID = 1L;
96     int member;
97     MySerializableClass(int member){this.member = member;}
98     public boolean equals(Object JavaDoc o) {
99       if (! (o instanceof MySerializableClass)) return false;
100       return ( member == ((MySerializableClass)o).member );
101     }
102   }
103   public void testCustomTypeSerializable() {
104     contextInstance.setVariable("a", new MySerializableClass(4));
105     processInstance = saveAndReload(processInstance);
106     contextInstance = processInstance.getContextInstance();
107     assertEquals(new MySerializableClass(4), contextInstance.getVariable("a"));
108   }
109
110   public void testLong() {
111     contextInstance.setVariable("a", new Long JavaDoc(3));
112     processInstance = saveAndReload(processInstance);
113     contextInstance = processInstance.getContextInstance();
114     assertEquals(new Long JavaDoc(3), contextInstance.getVariable("a"));
115   }
116   
117   public void testByte() {
118     contextInstance.setVariable("a", new Byte JavaDoc("3"));
119     processInstance = saveAndReload(processInstance);
120     contextInstance = processInstance.getContextInstance();
121     assertEquals(new Byte JavaDoc("3"), contextInstance.getVariable("a"));
122   }
123   
124   public void testShort() {
125     contextInstance.setVariable("a", new Short JavaDoc("3"));
126     processInstance = saveAndReload(processInstance);
127     contextInstance = processInstance.getContextInstance();
128     assertEquals(new Short JavaDoc("3"), contextInstance.getVariable("a"));
129   }
130   
131   public void testInteger() {
132     contextInstance.setVariable("a", new Integer JavaDoc(3));
133     processInstance = saveAndReload(processInstance);
134     contextInstance = processInstance.getContextInstance();
135     assertEquals(new Integer JavaDoc(3), contextInstance.getVariable("a"));
136   }
137
138   public void testDate() {
139     Date now = new Date();
140     contextInstance.setVariable("a", now);
141     processInstance = saveAndReload(processInstance);
142     contextInstance = processInstance.getContextInstance();
143     assertEquals(now, contextInstance.getVariable("a"));
144   }
145
146   public void testNullUpdate() {
147     contextInstance.setVariable("a", "blablabla");
148     processInstance = saveAndReload(processInstance);
149     contextInstance = processInstance.getContextInstance();
150     contextInstance.setVariable("a", null);
151     processInstance = saveAndReload(processInstance);
152     contextInstance = processInstance.getContextInstance();
153     assertNull(contextInstance.getVariable("a"));
154   }
155
156   public void testChangeTypeException() {
157     // this one does not use a converter
158
contextInstance.setVariable("a", new String JavaDoc("3"));
159     processInstance = saveAndReload(processInstance);
160     contextInstance = processInstance.getContextInstance();
161     try {
162       contextInstance.setVariable("a", new Integer JavaDoc(3));
163       fail("expected exception");
164     } catch (RuntimeException JavaDoc e) {
165       // OK
166
}
167   }
168
169   public void testChangeTypeException2() {
170     // this one uses a converter
171
contextInstance.setVariable("a", new Integer JavaDoc(3));
172     processInstance = saveAndReload(processInstance);
173     contextInstance = processInstance.getContextInstance();
174     try {
175       contextInstance.setVariable("a", new String JavaDoc("3"));
176       fail("expected exception");
177     } catch (RuntimeException JavaDoc e) {
178       // OK
179
}
180   }
181
182   public void testChangeTypeWithDeleteIsAllowed() {
183     contextInstance.setVariable("a", new String JavaDoc("3"));
184     processInstance = saveAndReload(processInstance);
185     contextInstance = processInstance.getContextInstance();
186     contextInstance.deleteVariable("a");
187     processInstance = saveAndReload(processInstance);
188     contextInstance = processInstance.getContextInstance();
189     contextInstance.setVariable("a", new Integer JavaDoc(3));
190   }
191 }
192
Popular Tags