001: package com.ecyrd.jspwiki.workflow;
002:
003: import java.math.BigDecimal;
004: import java.security.Principal;
005: import java.util.Date;
006:
007: import junit.framework.TestCase;
008:
009: import com.ecyrd.jspwiki.WikiException;
010: import com.ecyrd.jspwiki.auth.GroupPrincipal;
011: import com.ecyrd.jspwiki.auth.WikiPrincipal;
012:
013: public class WorkflowTest extends TestCase {
014: Workflow w;
015:
016: Task initTask;
017:
018: Decision decision;
019:
020: Task finishTask;
021:
022: String ATTR = "TestAttribute";
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026:
027: // Create workflow; owner is test user
028: w = new Workflow("workflow.myworkflow", new WikiPrincipal(
029: "Owner1"));
030:
031: // Create custom initialization task
032: initTask = new TaskTest.NormalTask(w);
033:
034: // Create finish task
035: finishTask = new TaskTest.NormalTask(w);
036:
037: // Create an intermetidate decision step
038: Principal actor = new GroupPrincipal("Admin");
039: decision = new SimpleDecision(w, "decision.AdminDecision",
040: actor);
041:
042: // Hook the steps together
043: initTask.addSuccessor(Outcome.STEP_COMPLETE, decision);
044: decision.addSuccessor(Outcome.DECISION_APPROVE, finishTask);
045:
046: // Stash page name as message attribute
047: w.addMessageArgument("MyPage");
048:
049: // Set workflow's first step
050: w.setFirstStep(initTask);
051: }
052:
053: public void testWorkflow() {
054: // Make sure everything is set to their proper default values
055: assertNull(w.getCurrentStep());
056: assertNull(w.getCurrentActor());
057: assertEquals(0, w.getHistory().size());
058: assertEquals(Workflow.ID_NOT_SET, w.getId());
059: assertNull(w.getWorkflowManager());
060: assertEquals(new WikiPrincipal("Owner1"), w.getOwner());
061: assertEquals(Workflow.CREATED, w.getCurrentState());
062: assertEquals(Workflow.TIME_NOT_SET, w.getStartTime());
063: assertEquals(Workflow.TIME_NOT_SET, w.getEndTime());
064: }
065:
066: public void testSetWorkflowManager() {
067: assertNull(w.getWorkflowManager());
068: WorkflowManager m = new WorkflowManager();
069: w.setWorkflowManager(m);
070: assertEquals(m, w.getWorkflowManager());
071: }
072:
073: public void testGetSetAttribute() {
074: assertNull(w.getAttribute(ATTR));
075: w.setAttribute(ATTR, "Test String");
076: assertNotNull(w.getAttribute(ATTR));
077: assertEquals("Test String", w.getAttribute(ATTR));
078: }
079:
080: public void testGetMessageArgs() throws WikiException {
081: Object[] args;
082:
083: // Before start, arg1=Owner1, arg2=- (no current actor), arg3=MyPage
084: args = w.getMessageArguments();
085: assertEquals("Owner1", args[0]);
086: assertEquals("-", args[1]);
087: assertEquals("MyPage", args[2]);
088:
089: // After start (at Decision), arg1=Owner1, arg2=Admin, arg3=MyPage
090: w.start();
091: args = w.getMessageArguments();
092: assertEquals("Owner1", args[0]);
093: assertEquals("Admin", args[1]);
094: assertEquals("MyPage", args[2]);
095:
096: // After end, arg1=Owner1, arg2=-, arg3=MyPage
097: decision.decide(Outcome.DECISION_APPROVE);
098: args = w.getMessageArguments();
099: assertEquals("Owner1", args[0]);
100: assertEquals("-", args[1]);
101: assertEquals("MyPage", args[2]);
102: }
103:
104: public void testGetMessageArgObjects() {
105: // Try passing some valid object types: Date, Number
106: w.addMessageArgument(new Date());
107: w.addMessageArgument(new Integer(1));
108: w.addMessageArgument(new Double(2));
109: w.addMessageArgument(new BigDecimal(3.14));
110:
111: // Try passing an invalid one: e.g., a Workflow (it should fail)
112: try {
113: w.addMessageArgument(w);
114: } catch (IllegalArgumentException e) {
115: // Swallow
116: return;
117: }
118: // We should never get here
119: fail("Illegal argument passed...");
120: }
121:
122: public void testGetMessageKey() {
123: assertEquals("workflow.myworkflow", w.getMessageKey());
124: }
125:
126: public void testGetOwner() {
127: assertEquals(new WikiPrincipal("Owner1"), w.getOwner());
128: }
129:
130: public void testStart() throws WikiException {
131: assertFalse(w.isStarted());
132: w.start();
133: assertTrue(w.isStarted());
134: }
135:
136: public void testWaitstate() throws WikiException {
137: w.start();
138:
139: // Default workflow should have hit the Decision step and put itself
140: // into WAITING
141: assertEquals(Workflow.WAITING, w.getCurrentState());
142: }
143:
144: public void testRestart() throws WikiException {
145: w.start();
146:
147: // Default workflow should have hit the Decision step and put itself
148: // into WAITING
149: assertEquals(Workflow.WAITING, w.getCurrentState());
150: w.restart();
151: assertEquals(Workflow.WAITING, w.getCurrentState());
152: }
153:
154: public void testAbortBeforeStart() throws WikiException {
155: // Workflow hasn't been started yet
156: assertFalse(w.isAborted());
157: w.abort();
158: assertTrue(w.isAborted());
159:
160: // Try to start anyway
161: try {
162: w.start();
163: } catch (IllegalStateException e) {
164: // Swallow
165: return;
166: }
167: // We should never get here
168: fail("Workflow allowed itself to be started even though it was aborted!");
169: }
170:
171: public void testAbortDuringWait() throws WikiException {
172: // Start workflow, then abort while in WAITING state
173: assertFalse(w.isAborted());
174: w.start();
175: w.abort();
176: assertTrue(w.isAborted());
177:
178: // Try to restart anyway
179: try {
180: w.restart();
181: } catch (IllegalStateException e) {
182: // Swallow
183: return;
184: }
185: // We should never get here
186: fail("Workflow allowed itself to be re-started even though it was aborted!");
187: }
188:
189: public void testAbortAfterCompletion() throws WikiException {
190: // Start workflow, then abort after completion
191: assertFalse(w.isAborted());
192: w.start();
193: Decision d = (Decision) w.getCurrentStep();
194: d.decide(Outcome.DECISION_APPROVE);
195:
196: // Try to abort anyway
197: try {
198: w.abort();
199: assertTrue(w.isAborted());
200: } catch (IllegalStateException e) {
201: // Swallow
202: return;
203: }
204: // We should never get here
205: fail("Workflow allowed itself to be aborted even though it was completed!");
206: }
207:
208: public void testCurrentState() throws WikiException {
209: assertEquals(Workflow.CREATED, w.getCurrentState());
210: w.start();
211: assertEquals(Workflow.WAITING, w.getCurrentState());
212: Decision d = (Decision) w.getCurrentStep();
213: d.decide(Outcome.DECISION_APPROVE);
214: assertEquals(Workflow.COMPLETED, w.getCurrentState());
215: }
216:
217: public void testCurrentStep() throws WikiException {
218: assertNull(w.getCurrentStep());
219: w.start();
220:
221: // Workflow stops at the decision step
222: assertEquals(decision, w.getCurrentStep());
223: Decision d = (Decision) w.getCurrentStep();
224: d.decide(Outcome.DECISION_APPROVE);
225:
226: // After we decide, it blows through step 3 and leaves us with a null
227: // step (done)
228: assertNull(w.getCurrentStep());
229: }
230:
231: public void testPreviousStep() throws WikiException {
232: // If not started, no previous steps available for anything
233: assertNull(w.getPreviousStep());
234: assertEquals(null, w.previousStep(initTask));
235: assertEquals(null, w.previousStep(decision));
236: assertEquals(null, w.previousStep(finishTask));
237:
238: // Once we start, initTask and decisions' predecessors are known, but
239: // finish task is indeterminate
240: w.start();
241: assertEquals(null, w.previousStep(initTask));
242: assertEquals(initTask, w.previousStep(decision));
243: assertEquals(null, w.previousStep(finishTask));
244:
245: // Once we decide, the finish task returns the correct predecessor
246: Decision d = (Decision) w.getCurrentStep();
247: d.decide(Outcome.DECISION_APPROVE);
248: assertEquals(null, w.previousStep(initTask));
249: assertEquals(initTask, w.previousStep(decision));
250: assertEquals(decision, w.previousStep(finishTask));
251: }
252:
253: public void testCurrentActor() throws WikiException {
254: // Before starting, actor should be null
255: assertNull(w.getCurrentActor());
256:
257: // After starting, actor should be GroupPrincipal Admin
258: w.start();
259: assertEquals(new GroupPrincipal("Admin"), w.getCurrentActor());
260:
261: // After decision, actor should be null again
262: Decision d = (Decision) w.getCurrentStep();
263: d.decide(Outcome.DECISION_APPROVE);
264: assertNull(w.getCurrentActor());
265: }
266:
267: public void testHistory() throws WikiException {
268: assertEquals(0, w.getHistory().size());
269: w.start();
270: assertEquals(2, w.getHistory().size());
271: Decision d = (Decision) w.getCurrentStep();
272: d.decide(Outcome.DECISION_APPROVE);
273: assertEquals(3, w.getHistory().size());
274: }
275:
276: public void testGetStartTime() throws WikiException {
277: // Start time should be not be set until we start the workflow
278: assertEquals(Workflow.TIME_NOT_SET, w.getStartTime());
279: w.start();
280: assertFalse(Workflow.TIME_NOT_SET == w.getStartTime());
281: Decision d = (Decision) w.getCurrentStep();
282: d.decide(Outcome.DECISION_APPROVE);
283: assertFalse(Workflow.TIME_NOT_SET == w.getStartTime());
284: }
285:
286: public void testGetEndTime() throws WikiException {
287: // End time should be not set until we finish all 3 steps
288: assertEquals(Workflow.TIME_NOT_SET, w.getEndTime());
289: w.start();
290: assertEquals(Workflow.TIME_NOT_SET, w.getEndTime());
291: Decision d = (Decision) w.getCurrentStep();
292: d.decide(Outcome.DECISION_APPROVE);
293: assertFalse(Workflow.TIME_NOT_SET == w.getEndTime());
294: }
295:
296: public void testIsCompleted() throws WikiException {
297: // Workflow isn't completed until we finish all 3 steps
298: assertFalse(w.isCompleted());
299: w.start();
300: assertFalse(w.isCompleted());
301: Decision d = (Decision) w.getCurrentStep();
302: d.decide(Outcome.DECISION_APPROVE);
303: assertTrue(w.isCompleted());
304: }
305:
306: public void testIsStarted() throws WikiException {
307: assertFalse(w.isStarted());
308: w.start();
309: assertTrue(w.isStarted());
310: }
311:
312: public void testStartTwice() throws WikiException {
313: w.start();
314: try {
315: w.start();
316: } catch (IllegalStateException e) {
317: // Swallow
318: return;
319: }
320: // We should never get here
321: fail("Workflow allowed itself to be started twice!");
322: }
323:
324: public void testSetId() {
325: assertEquals(Workflow.ID_NOT_SET, w.getId());
326: w.setId(1001);
327: assertEquals(1001, w.getId());
328: }
329:
330: }
|