001: package com.ecyrd.jspwiki.workflow;
002:
003: import java.util.Collection;
004: import java.util.List;
005:
006: import junit.framework.TestCase;
007:
008: import com.ecyrd.jspwiki.WikiException;
009: import com.ecyrd.jspwiki.auth.WikiPrincipal;
010:
011: public class TaskTest extends TestCase {
012:
013: Workflow m_workflow;
014: Task m_task;
015:
016: /** Sample Task that completes normally. */
017: public static class NormalTask extends Task {
018: public NormalTask(Workflow workflow) {
019: super (workflow, "task.normal");
020: }
021:
022: public Outcome execute() throws WikiException {
023: return Outcome.STEP_COMPLETE;
024: }
025:
026: }
027:
028: /** Sample Task that encounters an error during processing. */
029: public static class ErrorTask extends Task {
030: public ErrorTask(Workflow workflow) {
031: super (workflow, "task.error");
032: }
033:
034: public Outcome execute() throws WikiException {
035: addError("Found an error.");
036: addError("Found a second one!");
037: return Outcome.STEP_ABORT;
038: }
039:
040: }
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: m_workflow = new Workflow("workflow.key", new WikiPrincipal(
045: "Owner1"));
046: m_task = new NormalTask(m_workflow);
047: }
048:
049: public void testGetActor() {
050: assertNotSame(new WikiPrincipal("Actor1"), m_task.getActor());
051: assertEquals(SystemPrincipal.SYSTEM_USER, m_task.getActor());
052: }
053:
054: public void testSuccessors() {
055: // If task finishes normally, branch to a decision (d1)
056: Step d1 = new SimpleDecision(m_workflow, "decision1.key",
057: new WikiPrincipal("Actor1"));
058: m_task.addSuccessor(Outcome.STEP_COMPLETE, d1);
059:
060: // If the task aborts, branch to an alternate decision (d2)
061: Step d2 = new SimpleDecision(m_workflow, "decision2.key",
062: new WikiPrincipal("Actor2"));
063: m_task.addSuccessor(Outcome.STEP_ABORT, d2);
064:
065: assertEquals(d1, m_task.getSuccessor(Outcome.STEP_COMPLETE));
066: assertEquals(d2, m_task.getSuccessor(Outcome.STEP_ABORT));
067:
068: // The other Outcomes should return null when looked up
069: assertNull(m_task.getSuccessor(Outcome.DECISION_APPROVE));
070: assertNull(m_task.getSuccessor(Outcome.DECISION_DENY));
071: assertNull(m_task.getSuccessor(Outcome.DECISION_HOLD));
072: assertNull(m_task.getSuccessor(Outcome.DECISION_REASSIGN));
073: assertNull(m_task.getSuccessor(Outcome.STEP_CONTINUE));
074: }
075:
076: public void testErrors() {
077: m_task.addError("Error deciding something.");
078: m_task.addError("Error deciding something else.");
079:
080: List errors = m_task.getErrors();
081: assertEquals(2, errors.size());
082: assertEquals("Error deciding something.", errors.get(0));
083: assertEquals("Error deciding something else.", errors.get(1));
084: }
085:
086: public void testAvailableOutcomes() {
087: Collection outcomes = m_task.getAvailableOutcomes();
088: assertFalse(outcomes.contains(Outcome.DECISION_APPROVE));
089: assertFalse(outcomes.contains(Outcome.DECISION_DENY));
090: assertFalse(outcomes.contains(Outcome.DECISION_HOLD));
091: assertFalse(outcomes.contains(Outcome.DECISION_REASSIGN));
092: assertTrue(outcomes.contains(Outcome.STEP_ABORT));
093: assertTrue(outcomes.contains(Outcome.STEP_COMPLETE));
094: }
095:
096: public void testGetEndTime() throws WikiException {
097: assertEquals(Workflow.TIME_NOT_SET, m_task.getEndTime());
098: m_task.start();
099: m_task.setOutcome(m_task.execute());
100: assertTrue((Workflow.TIME_NOT_SET != m_task.getEndTime()));
101: }
102:
103: public void testGetMessageKey() {
104: assertEquals("task.normal", m_task.getMessageKey());
105: }
106:
107: public void testGetOutcome() throws WikiException {
108: assertEquals(Outcome.STEP_CONTINUE, m_task.getOutcome());
109: m_task.start();
110: m_task.setOutcome(m_task.execute());
111: assertEquals(Outcome.STEP_COMPLETE, m_task.getOutcome());
112:
113: // Test the "error task"
114: m_task = new ErrorTask(m_workflow);
115: assertEquals(Outcome.STEP_CONTINUE, m_task.getOutcome());
116: m_task.start();
117: m_task.setOutcome(m_task.execute());
118: assertEquals(Outcome.STEP_ABORT, m_task.getOutcome());
119: }
120:
121: public void testGetStartTime() throws WikiException {
122: assertEquals(Workflow.TIME_NOT_SET, m_task.getStartTime());
123: m_task.start();
124: m_task.execute();
125: assertTrue((Workflow.TIME_NOT_SET != m_task.getStartTime()));
126: }
127:
128: public void testGetWorkflow() {
129: assertEquals(m_workflow, m_task.getWorkflow());
130: }
131:
132: public void testIsCompleted() throws WikiException {
133: assertFalse(m_task.isCompleted());
134: m_task.start();
135: m_task.setOutcome(m_task.execute());
136: assertTrue(m_task.isCompleted());
137: }
138:
139: public void testIsStarted() throws WikiException {
140: assertFalse(m_task.isStarted());
141: m_task.start();
142: assertTrue(m_task.isStarted());
143: }
144:
145: public void testStartTwice() throws WikiException {
146: m_task.start();
147: try {
148: m_task.start();
149: } catch (IllegalStateException e) {
150: // Swallow
151: return;
152: }
153: // We should never get here
154: fail("Decision allowed itself to be started twice!");
155: }
156:
157: }
|