01: package com.ecyrd.jspwiki.workflow;
02:
03: import java.util.Properties;
04:
05: import junit.framework.TestCase;
06:
07: import com.ecyrd.jspwiki.TestEngine;
08: import com.ecyrd.jspwiki.WikiEngine;
09: import com.ecyrd.jspwiki.WikiException;
10: import com.ecyrd.jspwiki.auth.GroupPrincipal;
11: import com.ecyrd.jspwiki.auth.WikiPrincipal;
12:
13: public class WorkflowManagerTest extends TestCase {
14: protected Workflow w;
15: protected WorkflowManager wm;
16: protected WikiEngine m_engine;
17:
18: protected void setUp() throws Exception {
19: super .setUp();
20: Properties props = new Properties();
21: props.load(TestEngine.findTestProperties());
22: m_engine = new TestEngine(props);
23: wm = m_engine.getWorkflowManager();
24: // Create a workflow with 3 steps, with a Decision in the middle
25: w = new Workflow("workflow.key", new WikiPrincipal("Owner1"));
26: w.setWorkflowManager(m_engine.getWorkflowManager());
27: Step startTask = new TaskTest.NormalTask(w);
28: Step endTask = new TaskTest.NormalTask(w);
29: Decision decision = new SimpleDecision(w,
30: "decision.editWikiApproval",
31: new WikiPrincipal("Actor1"));
32: startTask.addSuccessor(Outcome.STEP_COMPLETE, decision);
33: decision.addSuccessor(Outcome.DECISION_APPROVE, endTask);
34: w.setFirstStep(startTask);
35:
36: // Add a message argument to the workflow with the page name
37: w.addMessageArgument("MyPage");
38: }
39:
40: public void testStart() throws WikiException {
41: // Once we start the workflow, it should show that it's started
42: // and the WM should have assigned it an ID
43: assertEquals(Workflow.ID_NOT_SET, w.getId());
44: assertFalse(w.isStarted());
45: wm.start(w);
46: assertFalse(Workflow.ID_NOT_SET == w.getId());
47: assertTrue(w.isStarted());
48: }
49:
50: public void testWorkflows() throws WikiException {
51: // There should be no workflows in the cache, and none in completed list
52: assertEquals(0, wm.getWorkflows().size());
53: assertEquals(0, wm.getCompletedWorkflows().size());
54:
55: // After starting, there should be 1 in the cache, with ID=1
56: wm.start(w);
57: assertEquals(1, wm.getWorkflows().size());
58: assertEquals(0, wm.getCompletedWorkflows().size());
59: Workflow workflow = (Workflow) wm.getWorkflows().iterator()
60: .next();
61: assertEquals(w, workflow);
62: assertEquals(1, workflow.getId());
63:
64: // After forcing a decision on step 2, the workflow should complete and vanish from the cache
65: Decision d = (Decision) w.getCurrentStep();
66: d.decide(Outcome.DECISION_APPROVE);
67: assertEquals(0, wm.getWorkflows().size());
68: assertEquals(1, wm.getCompletedWorkflows().size());
69: }
70:
71: public void testRequiresApproval() {
72: // Test properties says we need approvals for workflow.saveWikiPage & workflow.foo
73: assertFalse(wm.requiresApproval("workflow.saveWikiPage"));
74: assertTrue(wm.requiresApproval("workflow.foo"));
75: assertTrue(wm.requiresApproval("workflow.bar"));
76: }
77:
78: public void testGetApprover() throws WikiException {
79: // Test properties says workflow.saveWikiPage approver is GP Admin; workflow.foo is 'janne'
80: assertEquals(new WikiPrincipal("janne",
81: WikiPrincipal.LOGIN_NAME), wm
82: .getApprover("workflow.foo"));
83: assertEquals(new GroupPrincipal("Admin"), wm
84: .getApprover("workflow.bar"));
85:
86: // 'saveWikiPage' workflow doesn't require approval, so we will need to catch an Exception
87: try {
88: assertEquals(new GroupPrincipal("Admin"), wm
89: .getApprover("workflow.saveWikiPage"));
90: } catch (WikiException e) {
91: // Swallow
92: return;
93: }
94: // We should never get here
95: fail("Workflow.bar doesn't need approval!");
96: }
97:
98: }
|