1 package org.jbpm.bpel.exe.state; 2 3 import java.util.Date ; 4 5 import junit.framework.TestCase; 6 7 import org.jbpm.context.exe.ContextInstance; 8 import org.jbpm.graph.exe.ExecutionContext; 9 import org.jbpm.graph.exe.ProcessInstance; 10 import org.jbpm.graph.exe.Token; 11 12 import org.jbpm.bpel.def.Activity; 13 import org.jbpm.bpel.def.BpelDefinition; 14 import org.jbpm.bpel.def.Empty; 15 import org.jbpm.bpel.def.Scope; 16 import org.jbpm.bpel.exe.Fault; 17 import org.jbpm.bpel.exe.ScopeInstance; 18 19 23 public abstract class AbstractStateTestCase extends TestCase { 24 ScopeInstance scope; 25 Token root; 26 TestInstance activeChild; 27 TestInstance handlingChild; 28 TestInstance completedChild; 29 BpelDefinition pd; 30 LogActivity scopeCompletionLog; 31 protected LogActivity handlerLog; 32 33 public void setUp() { 34 pd = new BpelDefinition("process"); 35 ProcessInstance pi = new ProcessInstance(pd); 36 37 Empty activity = new Empty("child"); 38 pd.getScope().addNode(activity); 39 40 root = pi.getRootToken(); 41 Scope scopeActivity = new Scope(); 42 43 scopeCompletionLog = new LogActivity(); 44 scopeActivity.connect(scopeCompletionLog); 45 scopeActivity.setName("scope"); 46 root.setNode(scopeActivity); 47 48 Token token = new Token(root, "scope"); 49 token.setNode(activity); 50 scope = ScopeInstance.create(token); 51 scope.setState( getState() ); 52 53 Token aChild = new Token(token, "active"); 54 activeChild = createTestScope(aChild); 55 activeChild.setState(ActiveState.NORMAL_PROCESSING); 56 57 aChild = new Token(token, "handling"); 58 handlingChild = createTestScope(aChild); 59 handlingChild.setState(TerminatingState.TERMINATING_EXPLICITLY); 60 61 aChild = new Token(token, "completed"); 62 completedChild = createTestScope(aChild); 63 completedChild.setState(EndedState.COMPLETED); 64 65 handlerLog = new LogActivity(); 66 } 67 68 public abstract ScopeState getState(); 69 70 public void testExit() { 71 scope.exit(); 72 73 assertEquals( EndedState.EXITED, scope.getState() ); 74 assertNotNull( scope.getToken().getEnd() ); 75 } 76 77 public void testFaulted() { 78 try { scope.faulted(null); } 79 catch(IllegalStateException e) { return; } 80 fail("faulted can't be invoked at this state"); 81 } 82 83 public void testTerminate() { 84 try { scope.terminate(); } 85 catch(IllegalStateException e) { return; } 86 fail("terminate can't be invoked at this state"); 87 } 88 89 public void testCompleted() { 90 try { scope.completed(); } 91 catch(IllegalStateException e) { return; } 92 fail("completed can't be invoked at this state"); 93 } 94 95 public void testCompensate() { 96 try { scope.compensate(); } 97 catch(IllegalStateException e) { return; } 98 fail("compensate can't be invoked at this state"); 99 } 100 101 public void testChildrenTerminated() { 102 try { scope.getState().childrenTerminated(scope); } 103 catch(IllegalStateException e) { return; } 104 fail("children terminated can't be invoked at this state"); 105 } 106 107 void assertChildrenCompensated() { 108 assertNull(activeChild.compensated); 109 assertNull(handlingChild.compensated); 110 assertNotNull(completedChild.compensated); 111 } 112 113 void assertChildrenTerminated() { 114 assertTrue(activeChild.canceled); 115 assertTrue(handlingChild.canceled); 116 assertFalse(completedChild.canceled); 117 } 118 119 public TestInstance createTestScope(Token token) { 120 ContextInstance contextInstance = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class); 121 TestInstance instance = new TestInstance( token ); 122 contextInstance.createVariable(ScopeInstance.SCOPE_NAME, instance, token); 123 return instance; 124 } 125 126 static class TestInstance extends ScopeInstance { 127 boolean childCompensated = false; 128 boolean childTerminated = false; 129 boolean childFaulted = false; 130 131 boolean canceled = false; 132 Date compensated; 133 134 public TestInstance( Token token ) { 135 super ( token ); 136 } 137 138 public void childCompensated(ScopeInstance child) { 139 childCompensated = true; 140 } 141 142 public void childTerminated(ScopeInstance child) { 143 childTerminated = true; 144 } 145 146 public void faulted( Fault fault ) { 147 childFaulted = true; 148 } 149 150 public void terminate() { 151 canceled = true; 152 } 153 154 public void compensate() { 155 compensated = new Date (); 156 } 157 } 158 159 static class LogActivity extends Activity { 160 boolean executed = false; 161 162 public void execute(ExecutionContext context) { 163 executed = true; 164 } 165 } 166 167 } 168 | Popular Tags |