KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > assignment > ExpressionAssignmentHandlerTest


1 package org.jbpm.identity.assignment;
2
3 import java.util.Set JavaDoc;
4
5 import org.jbpm.graph.def.ProcessDefinition;
6 import org.jbpm.graph.exe.ExecutionContext;
7 import org.jbpm.graph.exe.ProcessInstance;
8 import org.jbpm.identity.Group;
9 import org.jbpm.identity.IdentityDbTestCase;
10 import org.jbpm.identity.Membership;
11 import org.jbpm.identity.User;
12 import org.jbpm.security.*;
13 import org.jbpm.taskmgmt.def.Swimlane;
14 import org.jbpm.taskmgmt.exe.PooledActor;
15 import org.jbpm.taskmgmt.exe.SwimlaneInstance;
16 import org.jbpm.taskmgmt.exe.TaskInstance;
17
18 public class ExpressionAssignmentHandlerTest extends IdentityDbTestCase {
19   
20   ExpressionAssignmentHandler expressionAssignmentHandler = null;
21   TaskInstance assignable = new TaskInstance();
22   ProcessInstance processInstance = null;
23   ExecutionContext executionContext = null;
24   
25   public void testFirstTermPrevious() {
26     expressionAssignmentHandler.expression = "previous";
27     expressionAssignmentHandler.assign(assignable, executionContext);
28     assertEquals("bill", assignable.getActorId());
29   }
30
31   public void testFirstTermSwimlane() {
32     expressionAssignmentHandler.expression = "swimlane(boss)";
33     SwimlaneInstance swimlaneInstance = new SwimlaneInstance(new Swimlane("boss"));
34     swimlaneInstance.setActorId("john");
35     processInstance.getTaskMgmtInstance().addSwimlaneInstance(swimlaneInstance);
36     expressionAssignmentHandler.assign(assignable, executionContext);
37     assertEquals("john", assignable.getActorId());
38   }
39
40   public void testFirstTermSwimlaneUnexisting() {
41     expressionAssignmentHandler.expression = "swimlane(sillywoman)";
42     try {
43       expressionAssignmentHandler.assign(assignable, executionContext);
44       fail("expected exception");
45     } catch (ExpressionAssignmentException e) {
46       // OK
47
}
48   }
49
50   public void testFirstTermVariableString() {
51     expressionAssignmentHandler.expression = "variable(actoridstringvariable)";
52     processInstance.getContextInstance().setVariable("actoridstringvariable", "john");
53     expressionAssignmentHandler.assign(assignable, executionContext);
54     assertEquals("john", assignable.getActorId());
55   }
56
57   public void testFirstTermVariableUser() {
58     expressionAssignmentHandler.expression = "variable(uservariable)";
59     User john = identitySession.getUserByName("john");
60     processInstance.getContextInstance().setVariable("uservariable", john);
61     expressionAssignmentHandler.assign(assignable, executionContext);
62     assertEquals("john", assignable.getActorId());
63   }
64
65   public void testFirstTermVariableGroup() {
66     expressionAssignmentHandler.expression = "variable(groupvariable)";
67     Group hellsangels = identitySession.getGroupByName("hellsangels");
68     processInstance.getContextInstance().setVariable("groupvariable", hellsangels);
69     expressionAssignmentHandler.assign(assignable, executionContext);
70     Set JavaDoc pooledActors = assignable.getPooledActors();
71     PooledActor pooledActor = (PooledActor) pooledActors.iterator().next();
72     assertEquals("hellsangels", pooledActor.getActorId());
73   }
74
75   public void testFirstTermVariableUnexisting() {
76     expressionAssignmentHandler.expression = "variable(unexistingvariablename)";
77     try {
78       expressionAssignmentHandler.assign(assignable, executionContext);
79       fail("expected exception");
80     } catch (ExpressionAssignmentException e) {
81       // OK
82
}
83   }
84
85   public void testFirstTermUser() {
86     expressionAssignmentHandler.expression = "user(john)";
87     expressionAssignmentHandler.assign(assignable, executionContext);
88     assertEquals("john", assignable.getActorId());
89   }
90
91   public void testFirstTermUserUnexisting() {
92     expressionAssignmentHandler.expression = "user(idontexist)";
93     try {
94       expressionAssignmentHandler.assign(assignable, executionContext);
95       fail("expected exception");
96     } catch (ExpressionAssignmentException e) {
97       // OK
98
}
99   }
100
101   public void testFirstTermGroup() {
102     expressionAssignmentHandler.expression = "group(hellsangels)";
103     expressionAssignmentHandler.assign(assignable, executionContext);
104     Set JavaDoc pooledActors = assignable.getPooledActors();
105     PooledActor pooledActor = (PooledActor) pooledActors.iterator().next();
106     assertEquals("hellsangels", pooledActor.getActorId());
107   }
108
109   public void testFirstTermGroupUnexisting() {
110     expressionAssignmentHandler.expression = "group(wedontexist)";
111     try {
112       expressionAssignmentHandler.assign(assignable, executionContext);
113       fail("expected exception");
114     } catch (ExpressionAssignmentException e) {
115       // OK
116
}
117   }
118
119   public void testWrongFirstTerm() {
120     expressionAssignmentHandler.expression = "wrong-first-term";
121     try {
122       expressionAssignmentHandler.assign(assignable, executionContext);
123       fail("expected exception");
124     } catch (ExpressionAssignmentException e) {
125       // OK
126
}
127   }
128
129   public void testNextTermGroup() {
130     expressionAssignmentHandler.expression = "user(john) --> group(hierarchy)";
131     expressionAssignmentHandler.assign(assignable, executionContext);
132     Set JavaDoc pooledActors = assignable.getPooledActors();
133     PooledActor pooledActor = (PooledActor) pooledActors.iterator().next();
134     assertEquals("hellsangels", pooledActor.getActorId());
135   }
136
137   public void testNextTermMember() {
138     expressionAssignmentHandler.expression = "group(hellsangels) --> member(leaderofthegang)";
139     expressionAssignmentHandler.assign(assignable, executionContext);
140     assertEquals("john", assignable.getActorId());
141   }
142
143   public void testWrongNextTerm() {
144     expressionAssignmentHandler.expression = "user(john) --> wrong-second-term";
145     try {
146       expressionAssignmentHandler.assign(assignable, executionContext);
147       fail("expected exception");
148     } catch (ExpressionAssignmentException e) {
149       // OK
150
}
151   }
152
153   public void setUp() {
154     super.setUp();
155     expressionAssignmentHandler = new ExpressionAssignmentHandler();
156     setUpProcessInstance();
157     setUpUserData();
158     // now start a fresh, new transaction
159
newTransaction();
160   }
161
162   protected void newTransaction() {
163     super.newTransaction();
164     setUpAssignmentContext();
165   }
166   
167   private void setUpAssignmentContext() {
168     executionContext = new ExecutionContext(processInstance.getRootToken());
169   }
170
171   private void setUpUserData() {
172     User john = new User("john");
173     User bill = new User("bill");
174     Group hellsangels = new Group("hellsangels", "hierarchy");
175     Membership.create(john, "leaderofthegang", hellsangels);
176     Membership.create(bill, hellsangels);
177     
178     identitySession.saveUser(john);
179     identitySession.saveUser(bill);
180     identitySession.saveGroup(hellsangels);
181   }
182
183   private void setUpProcessInstance() {
184     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
185       "<process-definition>" +
186       "</process-definition>"
187     );
188     graphSession.saveProcessDefinition(processDefinition);
189     processInstance = new ProcessInstance(processDefinition);
190     graphSession.saveProcessInstance(processInstance);
191     Authentication.pushAuthenticatedActorId("bill");
192   }
193 }
194
Popular Tags