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 SimpleDecisionTest extends TestCase {
012:
013: Workflow m_workflow;
014: Decision m_decision;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018: m_workflow = new Workflow("workflow.key", new WikiPrincipal(
019: "Owner1"));
020: m_decision = new SimpleDecision(m_workflow, "decision.key",
021: new WikiPrincipal("Actor1"));
022: }
023:
024: public void testAddFacts() {
025: Fact f1 = new Fact("fact1", new Integer(1));
026: Fact f2 = new Fact("fact2", "A factual String");
027: Fact f3 = new Fact("fact3", Outcome.DECISION_ACKNOWLEDGE);
028: m_decision.addFact(f1);
029: m_decision.addFact(f2);
030: m_decision.addFact(f3);
031:
032: // The facts should be available, and returned in order
033: List facts = m_decision.getFacts();
034: assertEquals(f1, facts.get(0));
035: assertEquals(f2, facts.get(1));
036: assertEquals(f3, facts.get(2));
037: }
038:
039: public void testGetActor() {
040: assertEquals(new WikiPrincipal("Actor1"), m_decision.getActor());
041: }
042:
043: public void testGetDefaultOutcome() {
044: assertEquals(Outcome.DECISION_APPROVE, m_decision
045: .getDefaultOutcome());
046: }
047:
048: public void testIsReassignable() {
049: assertTrue(m_decision.isReassignable());
050: }
051:
052: public void testReassign() {
053: m_decision.reassign(new WikiPrincipal("Actor2"));
054: assertEquals(new WikiPrincipal("Actor2"), m_decision.getActor());
055: }
056:
057: public void testSuccessors() {
058: // If the decision is approved, branch to another decision (d2)
059: Step d2 = new SimpleDecision(m_workflow, "decision2.key",
060: new WikiPrincipal("Actor1"));
061: m_decision.addSuccessor(Outcome.DECISION_APPROVE, d2);
062:
063: // If the decision is denied, branch to another decision (d3)
064: Step d3 = new SimpleDecision(m_workflow, "decision3.key",
065: new WikiPrincipal("Actor1"));
066: m_decision.addSuccessor(Outcome.DECISION_DENY, d3);
067:
068: assertEquals(d2, m_decision
069: .getSuccessor(Outcome.DECISION_APPROVE));
070: assertEquals(d3, m_decision.getSuccessor(Outcome.DECISION_DENY));
071:
072: // The other Outcomes should return null when looked up
073: assertNull(m_decision.getSuccessor(Outcome.DECISION_HOLD));
074: assertNull(m_decision.getSuccessor(Outcome.DECISION_REASSIGN));
075: assertNull(m_decision.getSuccessor(Outcome.STEP_ABORT));
076: }
077:
078: public void testErrors() {
079: m_decision.addError("Error deciding something.");
080: m_decision.addError("Error deciding something else.");
081:
082: List errors = m_decision.getErrors();
083: assertEquals(2, errors.size());
084: assertEquals("Error deciding something.", errors.get(0));
085: assertEquals("Error deciding something else.", errors.get(1));
086: }
087:
088: public void testAvailableOutcomes() {
089: Collection outcomes = m_decision.getAvailableOutcomes();
090: assertTrue(outcomes.contains(Outcome.DECISION_APPROVE));
091: assertTrue(outcomes.contains(Outcome.DECISION_DENY));
092: assertFalse(outcomes.contains(Outcome.DECISION_HOLD));
093: assertFalse(outcomes.contains(Outcome.DECISION_REASSIGN));
094: assertFalse(outcomes.contains(Outcome.STEP_ABORT));
095: assertFalse(outcomes.contains(Outcome.STEP_COMPLETE));
096: }
097:
098: public void testGetEndTime() throws WikiException {
099: assertEquals(Workflow.TIME_NOT_SET, m_decision.getEndTime());
100: m_decision.start();
101: m_decision.decide(Outcome.DECISION_APPROVE);
102: assertTrue((Workflow.TIME_NOT_SET != m_decision.getEndTime()));
103: }
104:
105: public void testGetMessageKey() {
106: assertEquals("decision.key", m_decision.getMessageKey());
107: }
108:
109: public void testGetOutcome() throws WikiException {
110: assertEquals(Outcome.STEP_CONTINUE, m_decision.getOutcome());
111: m_decision.start();
112: m_decision.decide(Outcome.DECISION_APPROVE);
113: assertEquals(Outcome.DECISION_APPROVE, m_decision.getOutcome());
114: }
115:
116: public void testGetStartTime() throws WikiException {
117: assertEquals(Workflow.TIME_NOT_SET, m_decision.getStartTime());
118: m_decision.start();
119: m_decision.decide(Outcome.DECISION_APPROVE);
120: assertTrue((Workflow.TIME_NOT_SET != m_decision.getStartTime()));
121: }
122:
123: public void testGetWorkflow() {
124: assertEquals(m_workflow, m_decision.getWorkflow());
125: }
126:
127: public void testIsCompleted() throws WikiException {
128: assertFalse(m_decision.isCompleted());
129: m_decision.start();
130: m_decision.decide(Outcome.DECISION_APPROVE);
131: assertTrue(m_decision.isCompleted());
132: }
133:
134: public void testIsStarted() throws WikiException {
135: assertFalse(m_decision.isStarted());
136: m_decision.start();
137: assertTrue(m_decision.isStarted());
138: }
139:
140: public void testStartTwice() throws WikiException {
141: m_decision.start();
142: try {
143: m_decision.start();
144: } catch (IllegalStateException e) {
145: // Swallow
146: return;
147: }
148: // We should never get here
149: fail("Decision allowed itself to be started twice!");
150: }
151:
152: }
|