KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > exe > ActivityExeTest


1 package org.jbpm.bpel.exe;
2
3 import junit.framework.TestCase;
4
5 import org.jbpm.context.def.ContextDefinition;
6 import org.jbpm.graph.def.ProcessDefinition;
7 import org.jbpm.graph.def.Transition;
8 import org.jbpm.graph.exe.ExecutionContext;
9 import org.jbpm.graph.exe.ProcessInstance;
10 import org.jbpm.graph.exe.Token;
11
12 import org.jbpm.bpel.data.def.Snippet;
13 import org.jbpm.bpel.def.Activity;
14 import org.jbpm.bpel.def.BpelDefinition;
15 import org.jbpm.bpel.def.Empty;
16 import org.jbpm.bpel.def.Link;
17 import org.jbpm.bpel.xml.BpelException;
18
19 /**
20  * @author Juan Cantú
21  * @version $Revision: 1.4 $ $Date: 2005/05/07 00:03:44 $
22  */

23 public class ActivityExeTest extends TestCase {
24
25   public static Snippet TRUE = createBooleanExpression(true);
26     
27   public static Snippet FALSE = createBooleanExpression(false);
28
29   public static Snippet createBooleanExpression(boolean value) {
30     try {
31       Snippet expr = new Snippet();
32       expr.setText(value ? "true()" : "false()");
33       expr.setUse(Snippet.Use.EXPRESSION);
34       expr.parseScript();
35       return expr;
36     }
37     catch (BpelException e) {
38       System.out.println(e);
39       return null;
40     }
41   }
42   
43   protected Activity node;
44   protected Activity successor;
45   protected TestTransition testTransition;
46   protected Token token;
47   protected ExecutionContext context;
48   protected Link targetLink;
49   protected LinkInstance target;
50   
51   public void setUp() {
52     ProcessDefinition pd = new BpelDefinition("testDef");
53     pd.addDefinition(new ContextDefinition());
54     ProcessInstance pi = new ProcessInstance(pd);
55     token = pi.getRootToken();
56     context = new ExecutionContext(token);
57     
58     node = createBpelActivity();
59     pd.addNode(node);
60     
61     testTransition = new TestTransition();
62     node.addLeavingTransition(testTransition);
63     successor = new Empty("successor");
64     
65     targetLink = new Link("target");
66     node.addTarget(targetLink);
67     target = LinkInstance.create(token, "target");
68       
69     Link positiveLink = new Link("positive");
70     node.addSource(positiveLink);
71     successor.addTarget(positiveLink);
72     positiveLink.setTransitionCondition(TRUE);
73     LinkInstance.create(token, "positive");
74     
75     Link negativeLink = new Link("negative");
76     node.addSource(negativeLink);
77     successor.addTarget(negativeLink);
78     negativeLink.setTransitionCondition(FALSE);
79     LinkInstance.create(token, "negative");
80     
81     Link defaultLink = new Link("default");
82     node.addSource(defaultLink);
83     successor.addTarget(defaultLink);
84     LinkInstance.create(token, "default");
85   }
86
87   protected Activity createBpelActivity() {
88     Activity basic = new Empty("basic");
89     return basic;
90   }
91   
92   protected Activity getExeNode() {
93     return node;
94   }
95   
96   private void enter() {
97     getExeNode().enter(context);
98   }
99   
100   private void setTokenAtActivity(Token aToken) {
101     aToken.setNode(getExeNode());
102   }
103   
104   
105   public void testHandleTokenNoTargets() {
106     node.getTargets().remove(node.getTarget("target"));
107     enter();
108     assertCompleted();
109   }
110   
111   public void testHandleTokenDefaultUnset() {
112     enter();
113     assertNotCompleted();
114   }
115   
116   public void testHandleTokenDefaultPositive() {
117     target.setReached(Boolean.TRUE);
118     enter();
119     assertCompleted();
120   }
121   
122   public void testHandleTokenDefaultNegativeFail() {
123     target.setReached(Boolean.FALSE);
124     node.setSuppressJoinFailure(Boolean.FALSE);
125     try {
126       enter();
127     } catch(RuntimeException JavaDoc e) {
128       assertNotCompleted();
129       return;
130     }
131     fail("join failure exception must be thrown when join condition is false");
132   }
133   
134   public void testHandleTokenDefaultNegativeSuppress() {
135     target.setReached(Boolean.FALSE);
136     node.setSuppressJoinFailure(Boolean.TRUE);
137     enter();
138     assertSkipped();
139   }
140   
141   public void testHandleTokenTrueJoinCondition() {
142     target.setReached(Boolean.FALSE);
143     node.setJoinCondition(TRUE);
144     enter();
145     assertCompleted();
146   }
147   
148   public void testHandleTokenFalseJoinConditionFail() {
149     target.setReached(Boolean.TRUE);
150     node.setSuppressJoinFailure(Boolean.FALSE);
151     node.setJoinCondition(FALSE);
152     try {
153       enter();
154     } catch(RuntimeException JavaDoc e) {
155       assertNotCompleted();
156       return;
157     }
158     fail("join failure exception must be thrown when join condition is false");
159   }
160   
161   public void testHandleTokenFalseJoinConditionSuppress() {
162     target.setReached(Boolean.TRUE);
163     node.setJoinCondition(FALSE);
164     node.setSuppressJoinFailure(Boolean.TRUE);
165     enter();
166     assertSkipped();
167   }
168   
169   public void testLinkResolvedPositive() throws Exception JavaDoc {
170     target.setReached(Boolean.TRUE);
171     
172     Token firstChild = new Token(token, "firstChild");
173     setTokenAtActivity(firstChild);
174     Token secondChild = new Token(token, "secondChild");
175     
176     targetLink.execute(new ExecutionContext(secondChild));
177     assertCompleted();
178   }
179   
180   public void testLinkResolvedNegativeSuppress() throws Exception JavaDoc {
181     Token firstChild = new Token(token, "firstChild");
182     Token currentToken = new Token(firstChild, "current");
183     setTokenAtActivity(currentToken);
184     Token secondChild = new Token(token, "secondChild");
185     Token secondGrandChild = new Token(secondChild, "secondGrandChild");
186     // TODO remove this instantiation?
187
new Token(secondChild, "firstGrandChild");
188    
189     node.setSuppressJoinFailure(Boolean.TRUE);
190     targetLink.setNegativeStatus(secondGrandChild);
191     assertSkipped();
192   }
193   
194   public void testLinkResolvedNegativeFail() throws Exception JavaDoc {
195     target.setReached(Boolean.FALSE);
196     
197     Token firstChild = new Token(token, "firstChild");
198     setTokenAtActivity(firstChild);
199     Token secondChild = new Token(token, "secondChild");
200     
201     node.setSuppressJoinFailure(Boolean.FALSE);
202     
203     try {
204       targetLink.setNegativeStatus(secondChild);
205     } catch(RuntimeException JavaDoc e) {
206       assertNotCompleted();
207       return;
208     }
209     fail("join failure exception must be thrown when join condition is false");
210   }
211   
212   public void assertCompleted() {
213     //TODO assert node handlerLog as completed
214
assertEquals(Boolean.TRUE, LinkInstance.get(token, "positive").getReached());
215     assertEquals(Boolean.FALSE, LinkInstance.get(token, "negative").getReached());
216     assertEquals(Boolean.TRUE, LinkInstance.get(token, "default").getReached());
217     assertEquals(1, testTransition.getCallCount());
218   }
219   
220   public void assertNotCompleted() {
221     //TODO assert node handlerLog as not completed
222
assertEquals(null, LinkInstance.get(token, "positive").getReached());
223     assertEquals(null, LinkInstance.get(token, "negative").getReached());
224     assertEquals(null, LinkInstance.get(token, "default").getReached());
225     assertEquals(0, testTransition.getCallCount());
226   }
227   
228   public void assertSkipped() {
229     //TODO assert node handlerLog as skipped
230
assertEquals(Boolean.FALSE, LinkInstance.get(token, "positive").getReached());
231     assertEquals(Boolean.FALSE, LinkInstance.get(token, "negative").getReached());
232     assertEquals(Boolean.FALSE, LinkInstance.get(token, "default").getReached());
233     assertEquals(1, testTransition.getCallCount());
234   }
235   
236   public static class TestTransition extends Transition {
237     int callCount = 0;
238     public void take(ExecutionContext context) {
239       callCount++;
240     }
241     
242     public int getCallCount() {
243       return callCount;
244     }
245   }
246 }
247
Popular Tags