KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > spi > BaseFunctionalWorkflowTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.spi;
6
7 import com.opensymphony.user.EntityNotFoundException;
8 import com.opensymphony.user.Group;
9 import com.opensymphony.user.User;
10 import com.opensymphony.user.UserManager;
11
12 import com.opensymphony.workflow.AbstractWorkflow;
13 import com.opensymphony.workflow.QueryNotSupportedException;
14 import com.opensymphony.workflow.WorkflowException;
15 import com.opensymphony.workflow.basic.BasicWorkflow;
16 import com.opensymphony.workflow.loader.WorkflowDescriptor;
17 import com.opensymphony.workflow.query.Expression;
18 import com.opensymphony.workflow.query.FieldExpression;
19 import com.opensymphony.workflow.query.NestedExpression;
20 import com.opensymphony.workflow.query.WorkflowExpressionQuery;
21 import com.opensymphony.workflow.query.WorkflowQuery;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import java.util.Collections JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34
35 /**
36  * This test case is functional in that it attempts to validate the entire
37  * lifecycle of a workflow. This is also a good resource for beginners
38  * to OSWorkflow. This class is extended to for various SPI's.
39  *
40  * @author Eric Pugh (epugh@upstate.com)
41  */

42 public abstract class BaseFunctionalWorkflowTest extends TestCase {
43     //~ Static fields/initializers /////////////////////////////////////////////
44

45     private static final String JavaDoc USER_TEST = "test";
46
47     //~ Instance fields ////////////////////////////////////////////////////////
48

49     protected AbstractWorkflow workflow;
50     protected Log log;
51     protected WorkflowDescriptor workflowDescriptor;
52
53     //~ Constructors ///////////////////////////////////////////////////////////
54

55     public BaseFunctionalWorkflowTest(String JavaDoc s) {
56         super(s);
57         log = LogFactory.getLog(getClass());
58     }
59
60     //~ Methods ////////////////////////////////////////////////////////////////
61

62     public void testExampleWorkflow() throws Exception JavaDoc {
63         WorkflowQuery query;
64
65         String JavaDoc workflowName = getClass().getResource("/samples/example.xml").toString();
66         assertTrue("canInitialize for workflow " + workflowName + " is false", workflow.canInitialize(workflowName, 1));
67
68         long workflowId = workflow.initialize(workflowName, 1, new HashMap JavaDoc());
69         String JavaDoc workorderName = workflow.getWorkflowName(workflowId);
70         workflowDescriptor = workflow.getWorkflowDescriptor(workorderName);
71
72         if (log.isDebugEnabled()) {
73             log.debug("Name of workorder:" + workorderName);
74         }
75
76         assertTrue("Expected external-permission permA in step 1 not found", workflow.getSecurityPermissions(workflowId).contains("permA"));
77
78         List JavaDoc currentSteps = workflow.getCurrentSteps(workflowId);
79         assertEquals("Unexpected number of current steps", 1, currentSteps.size());
80         assertEquals("Unexpected current step", 1, ((Step) currentSteps.get(0)).getStepId());
81
82         List JavaDoc historySteps = workflow.getHistorySteps(workflowId);
83         assertEquals("Unexpected number of history steps", 0, historySteps.size());
84
85         if (log.isDebugEnabled()) {
86             log.debug("Perform Finish First Draft");
87         }
88
89         workflow.doAction(workflowId, 1, Collections.EMPTY_MAP);
90
91         int[] actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
92         assertEquals(3, actions.length);
93         historySteps = workflow.getHistorySteps(workflowId);
94         assertEquals("Unexpected number of history steps", 1, historySteps.size());
95
96         Step historyStep = (Step) historySteps.get(0);
97         assertEquals(USER_TEST, historyStep.getCaller());
98         assertNull(historyStep.getDueDate());
99
100         // check system date, add in a 1 second fudgefactor.
101
assertTrue("history step finish date " + historyStep.getFinishDate() + " is in the future!", (historyStep.getFinishDate().getTime() - 1000) < System.currentTimeMillis());
102         logActions(actions);
103
104         if (log.isDebugEnabled()) {
105             log.debug("Perform Finish Foo");
106         }
107
108         workflow.doAction(workflowId, 12, Collections.EMPTY_MAP);
109
110         //Step lastHistoryStep = historyStep;
111
historySteps = workflow.getHistorySteps(workflowId);
112         assertEquals("Unexpected number of history steps", 2, historySteps.size());
113
114         if (log.isDebugEnabled()) {
115             log.debug("Perform Stay in Bar");
116         }
117
118         workflow.doAction(workflowId, 113, Collections.EMPTY_MAP);
119         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
120         assertEquals(2, actions.length);
121         assertTrue((actions[0] == 13) && (actions[1] == 113));
122         logActions(actions);
123
124         //historyStep = (Step) historySteps.get(0);
125
//assertEquals(lastHistoryStep.getId(), historyStep.getId());
126
if (log.isDebugEnabled()) {
127             log.debug("Perform Finish Bar");
128         }
129
130         workflow.doAction(workflowId, 13, Collections.EMPTY_MAP);
131         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
132         assertEquals(1, actions.length);
133         logActions(actions);
134
135         if (log.isDebugEnabled()) {
136             log.debug("Perform Finish Baz");
137         }
138
139         workflow.doAction(workflowId, 14, Collections.EMPTY_MAP);
140         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
141         logActions(actions);
142         historySteps = workflow.getHistorySteps(workflowId);
143         assertEquals("Unexpected number of history steps", 5, historySteps.size());
144
145         if (log.isDebugEnabled()) {
146             log.debug("Perform Finish Editing");
147         }
148
149         workflow.doAction(workflowId, 3, Collections.EMPTY_MAP);
150         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
151         assertEquals(3, actions.length);
152         logActions(actions);
153
154         if (log.isDebugEnabled()) {
155             log.debug("Perform Publish Doc");
156         }
157
158         workflow.doAction(workflowId, 7, Collections.EMPTY_MAP);
159         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
160         assertEquals(1, actions.length);
161         logActions(actions);
162
163         if (log.isDebugEnabled()) {
164             log.debug("Perform Publish Document");
165         }
166
167         workflow.doAction(workflowId, 11, Collections.EMPTY_MAP);
168
169         actions = workflow.getAvailableActions(workflowId, Collections.EMPTY_MAP);
170         assertEquals(0, actions.length);
171         historySteps = workflow.getHistorySteps(workflowId);
172         assertEquals("Unexpected number of history steps", 8, historySteps.size());
173
174         query = new WorkflowQuery(WorkflowQuery.OWNER, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, USER_TEST);
175
176         try {
177             List JavaDoc workflows = workflow.query(query);
178             assertEquals("Unexpected number of workflow query results", 1, workflows.size());
179
180             WorkflowQuery queryLeft = new WorkflowQuery(WorkflowQuery.OWNER, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, USER_TEST);
181             WorkflowQuery queryRight = new WorkflowQuery(WorkflowQuery.STATUS, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, "Finished");
182             query = new WorkflowQuery(queryLeft, WorkflowQuery.AND, queryRight);
183             workflows = workflow.query(query);
184             assertEquals("Unexpected number of workflow query results", 1, workflows.size());
185         } catch (QueryNotSupportedException ex) {
186             System.out.println("query not supported");
187         }
188     }
189
190     public void testExceptionOnIllegalStayInCurrentStep() throws Exception JavaDoc {
191         String JavaDoc workflowName = getClass().getResource("/samples/example.xml").toString();
192         assertTrue("canInitialize for workflow " + workflowName + " is false", workflow.canInitialize(workflowName, 1));
193
194         try {
195             long workflowId = workflow.initialize(workflowName, 2, new HashMap JavaDoc());
196             fail("initial action result specified target step of current step. Succeeded but should not have.");
197         } catch (WorkflowException e) {
198             // expected, no such thing as current step for initial action
199
}
200     }
201
202     public void testMetadataAccess() throws Exception JavaDoc {
203         String JavaDoc workflowName = getClass().getResource("/samples/example.xml").toString();
204
205         long workflowId = workflow.initialize(workflowName, 1, new HashMap JavaDoc());
206         WorkflowDescriptor wfDesc = workflow.getWorkflowDescriptor(workflowName);
207
208         Map JavaDoc meta = wfDesc.getMetaAttributes();
209         assertTrue("missing metadata", ((String JavaDoc) meta.get("workflow-meta1")).equals("workflow-meta1-value"));
210         assertTrue("missing metadata", ((String JavaDoc) meta.get("workflow-meta2")).equals("workflow-meta2-value"));
211
212         meta = wfDesc.getStep(1).getMetaAttributes();
213         assertTrue("missing metadata", ((String JavaDoc) meta.get("step-meta1")).equals("step-meta1-value"));
214         assertTrue("missing metadata", ((String JavaDoc) meta.get("step-meta2")).equals("step-meta2-value"));
215
216         meta = wfDesc.getAction(1).getMetaAttributes();
217         assertTrue("missing metadata", ((String JavaDoc) meta.get("action-meta1")).equals("action-meta1-value"));
218         assertTrue("missing metadata", ((String JavaDoc) meta.get("action-meta2")).equals("action-meta2-value"));
219     }
220
221     public void testWorkflowExpressionQuery() throws Exception JavaDoc {
222         List JavaDoc workflows;
223         WorkflowExpressionQuery query;
224
225         String JavaDoc workflowName = getClass().getResource("/samples/example.xml").toString();
226         assertTrue("canInitialize for workflow " + workflowName + " is false", workflow.canInitialize(workflowName, 1));
227
228         //------------------- FieldExpression.OWNER + FieldExpression.CURRENT_STEPS ----------------------
229
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.OWNER, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, USER_TEST));
230
231         try {
232             workflows = workflow.query(query);
233             assertEquals("empty OWNER+CURRENT_STEPS", 0, workflows.size());
234         } catch (QueryNotSupportedException e) {
235             log.error("Store does not support query");
236
237             return;
238         }
239
240         long workflowId = workflow.initialize(workflowName, 1, new HashMap JavaDoc());
241         workflows = workflow.query(query);
242         assertEquals("OWNER+CURRENT_STEPS", 1, workflows.size());
243
244         //------------------- FieldExpression.NAME + FieldExpression.ENTRY ----------------------------------
245
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.NAME, FieldExpression.ENTRY, FieldExpression.EQUALS, "notexistingname"));
246         workflows = workflow.query(query);
247         assertEquals("empty NAME+ENTRY", 0, workflows.size());
248
249         query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.NAME, FieldExpression.ENTRY, FieldExpression.EQUALS, workflowName));
250         workflows = workflow.query(query);
251         assertEquals("NAME+ENTRY", 1, workflows.size());
252
253         //------------------- FieldExpression.STATE + FieldExpression.ENTRY ----------------------------------
254
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.STATE, FieldExpression.ENTRY, FieldExpression.EQUALS, new Integer JavaDoc(WorkflowEntry.COMPLETED)));
255         workflows = workflow.query(query);
256         assertEquals("empty STATE+ENTRY", 0, workflows.size());
257
258         query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.STATE, FieldExpression.ENTRY, FieldExpression.EQUALS, new Integer JavaDoc(WorkflowEntry.ACTIVATED)));
259         workflows = workflow.query(query);
260         assertEquals("STATE+ENTRY", 1, workflows.size());
261
262         // --------------------------- empty nested query : AND ---------------------------------
263
Expression queryLeft = new FieldExpression(FieldExpression.OWNER, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, USER_TEST);
264         Expression queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Finished");
265         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
266                         queryLeft, queryRight
267                     }, NestedExpression.AND));
268         workflows = workflow.query(query);
269         assertEquals("empty nested query AND", 0, workflows.size());
270
271         // -------------------------- negated nested query: AND ----------------------------------
272
queryLeft = new FieldExpression(FieldExpression.OWNER, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, USER_TEST);
273         queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Finished", true);
274         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
275                         queryLeft, queryRight
276                     }, NestedExpression.AND));
277         workflows = workflow.query(query);
278         assertEquals("negated nested query AND", 1, workflows.size());
279
280         // -------------------------- nested query: AND + same context ------------------------------------------
281
queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Underway");
282         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
283                         queryLeft, queryRight
284                     }, NestedExpression.AND));
285         workflows = workflow.query(query);
286         assertEquals("nested query AND", 1, workflows.size());
287
288         // ------------------------- empty nested query: OR + mixed context -------------------------------------
289
queryLeft = new FieldExpression(FieldExpression.FINISH_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc());
290         queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Finished");
291         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
292                         queryLeft, queryRight
293                     }, NestedExpression.OR));
294         workflows = workflow.query(query);
295         assertEquals("empty nested query OR + mixed context", 0, workflows.size());
296
297         // ------------------------- negated nested query: OR -------------------------------------
298
queryLeft = new FieldExpression(FieldExpression.FINISH_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc());
299         queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Finished", true);
300         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
301                         queryLeft, queryRight
302                     }, NestedExpression.OR));
303         workflows = workflow.query(query);
304         assertEquals("negated nested query OR", 1, workflows.size());
305
306         // ------------------------- nested query: OR + mixed context -------------------------------------
307
queryLeft = new FieldExpression(FieldExpression.FINISH_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc());
308         queryRight = new FieldExpression(FieldExpression.NAME, FieldExpression.ENTRY, FieldExpression.EQUALS, workflowName);
309         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
310                         queryLeft, queryRight
311                     }, NestedExpression.OR));
312         workflows = workflow.query(query);
313         assertEquals("nested query OR + mixed context", 1, workflows.size());
314
315         // --------------------- START_DATE+CURRENT_STEPS -------------------------------------------------
316
//there should be one step that has been started
317
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.START_DATE, FieldExpression.CURRENT_STEPS, FieldExpression.LT, new Date JavaDoc(System.currentTimeMillis() + 1000)));
318         workflows = workflow.query(query);
319         assertEquals("Expected to find one workflow step that was started", 1, workflows.size());
320
321         // --------------------- empty FINISH_DATE+HISTORY_STEPS -------------------------------------------
322
//there should be no steps that have been completed
323
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.FINISH_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc()));
324         workflows = workflow.query(query);
325         assertEquals("Expected to find no history steps that were completed", 0, workflows.size());
326
327         // =================================================================================================
328
workflow.doAction(workflowId, 1, Collections.EMPTY_MAP);
329
330         // --------------------- START_DATE+HISTORY_STEPS -------------------------------------------------
331
//there should be two step that have been started
332
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.START_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc()));
333         workflows = workflow.query(query);
334         assertEquals("Expected to find 1 workflow step in the history", 1, workflows.size());
335
336         // --------------------- FINISH_DATE+HISTORY_STEPS -------------------------------------------
337
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.FINISH_DATE, FieldExpression.HISTORY_STEPS, FieldExpression.LT, new Date JavaDoc(System.currentTimeMillis() + 1000)));
338         workflows = workflow.query(query);
339         assertEquals("Expected to find 1 history steps that was completed", 1, workflows.size());
340
341         // --------------------- ACTION + HISTORY_STEPS ----------------------------------------------
342
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.ACTION, FieldExpression.HISTORY_STEPS, FieldExpression.EQUALS, new Integer JavaDoc(1)));
343         workflows = workflow.query(query);
344         assertEquals("ACTION + HISTORY_STEPS", 1, workflows.size());
345
346         // --------------------- STEP + HISTORY_STEPS ----------------------------------------------
347
query = (new WorkflowExpressionQuery(new FieldExpression(FieldExpression.STEP, FieldExpression.HISTORY_STEPS, FieldExpression.EQUALS, new Integer JavaDoc(1))));
348         workflows = workflow.query(query);
349         assertEquals("STEP + HISTORY_STEPS", 1, workflows.size());
350
351         // --------------------- CALLER + HISTORY_STEPS --------------------------------------------
352
query = new WorkflowExpressionQuery(new FieldExpression(FieldExpression.CALLER, FieldExpression.HISTORY_STEPS, FieldExpression.EQUALS, USER_TEST));
353         workflows = workflow.query(query);
354         assertEquals("CALLER + HISTORY_STEPS", 1, workflows.size());
355
356         //----------------------------------------------------------------------------
357
// ----- some more tests using nested expressions
358
long workflowId2 = workflow.initialize(workflowName, 1, Collections.EMPTY_MAP);
359         workflow.changeEntryState(workflowId, WorkflowEntry.SUSPENDED);
360         queryRight = new FieldExpression(FieldExpression.STATE, FieldExpression.ENTRY, FieldExpression.EQUALS, new Integer JavaDoc(WorkflowEntry.ACTIVATED));
361         queryLeft = new FieldExpression(FieldExpression.STATE, FieldExpression.ENTRY, FieldExpression.EQUALS, new Integer JavaDoc(WorkflowEntry.SUSPENDED));
362         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
363                         queryLeft, queryRight
364                     }, NestedExpression.OR));
365         workflows = workflow.query(query);
366         assertEquals(2, workflows.size());
367
368         queryLeft = new FieldExpression(FieldExpression.OWNER, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, USER_TEST);
369         queryRight = new FieldExpression(FieldExpression.STATUS, FieldExpression.CURRENT_STEPS, FieldExpression.EQUALS, "Finished", true);
370         query = new WorkflowExpressionQuery(new NestedExpression(new Expression[] {
371                         queryLeft, queryRight
372                     }, NestedExpression.AND));
373         workflows = workflow.query(query);
374         assertEquals("Expected to find 2 workflows in current steps", 2, workflows.size());
375     }
376
377     public void testWorkflowQuery() throws Exception JavaDoc {
378         WorkflowQuery query = null;
379         List JavaDoc workflows;
380
381         String JavaDoc workflowName = getClass().getResource("/samples/example.xml").toString();
382         assertTrue("canInitialize for workflow " + workflowName + " is false", workflow.canInitialize(workflowName, 1));
383
384         try {
385             query = new WorkflowQuery(WorkflowQuery.OWNER, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, USER_TEST);
386             workflows = workflow.query(query);
387             assertEquals(0, workflows.size());
388         } catch (QueryNotSupportedException e) {
389             log.error("Store does not support query");
390         }
391
392         try {
393             long workflowId = workflow.initialize(workflowName, 1, new HashMap JavaDoc());
394             workflows = workflow.query(query);
395             assertEquals(1, workflows.size());
396         } catch (QueryNotSupportedException e) {
397             log.error("Store does not support query");
398         }
399
400         try {
401             WorkflowQuery queryLeft = new WorkflowQuery(WorkflowQuery.OWNER, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, USER_TEST);
402             WorkflowQuery queryRight = new WorkflowQuery(WorkflowQuery.STATUS, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, "Finished");
403             query = new WorkflowQuery(queryLeft, WorkflowQuery.AND, queryRight);
404             workflows = workflow.query(query);
405             assertEquals(0, workflows.size());
406
407             queryRight = new WorkflowQuery(WorkflowQuery.STATUS, WorkflowQuery.CURRENT, WorkflowQuery.EQUALS, "Underway");
408             query = new WorkflowQuery(queryLeft, WorkflowQuery.AND, queryRight);
409             workflows = workflow.query(query);
410             assertEquals(1, workflows.size());
411         } catch (QueryNotSupportedException e) {
412             log.error("Store does not support query");
413         }
414     }
415
416     protected void setUp() throws Exception JavaDoc {
417         workflow = new BasicWorkflow(USER_TEST);
418
419         UserManager um = UserManager.getInstance();
420         assertNotNull("Could not get UserManager", um);
421
422         try {
423             um.getUser(USER_TEST);
424         } catch (EntityNotFoundException enfe) {
425             User test = um.createUser(USER_TEST);
426             test.setPassword("test");
427
428             Group foos = um.createGroup("foos");
429             Group bars = um.createGroup("bars");
430             Group bazs = um.createGroup("bazs");
431             test.addToGroup(foos);
432             test.addToGroup(bars);
433             test.addToGroup(bazs);
434         }
435     }
436
437     protected void logActions(int[] actions) {
438         for (int i = 0; i < actions.length; i++) {
439             String JavaDoc name = workflowDescriptor.getAction(actions[i]).getName();
440             int actionId = workflowDescriptor.getAction(actions[i]).getId();
441
442             if (log.isDebugEnabled()) {
443                 log.debug("Actions Available: " + name + " id:" + actionId);
444             }
445         }
446     }
447 }
448
Popular Tags