1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.statemachine;
21
22 import junit.framework.TestCase;
23
24 import org.apache.mina.statemachine.State;
25 import org.apache.mina.statemachine.StateControl;
26 import org.apache.mina.statemachine.StateMachine;
27 import org.apache.mina.statemachine.context.DefaultStateContext;
28 import org.apache.mina.statemachine.context.StateContext;
29 import org.apache.mina.statemachine.event.Event;
30 import org.apache.mina.statemachine.transition.AbstractTransition;
31
32
33
34
35
36
37 public class StateMachineTest extends TestCase {
38
39 public void testBreakAndContinue() throws Exception {
40 State s1 = new State("s1");
41 s1.addTransition(new BreakAndContinueTransition("foo"));
42 s1.addTransition(new SuccessTransition("foo"));
43
44 StateContext context = new DefaultStateContext();
45 StateMachine sm = new StateMachine(new State[] { s1 }, "s1");
46 sm.handle(new Event("foo", context));
47 assertEquals(true, context.getAttribute("success"));
48 }
49
50 public void testBreakAndGotoNow() throws Exception {
51 State s1 = new State("s1");
52 State s2 = new State("s2");
53 s1.addTransition(new BreakAndGotoNowTransition("foo", "s2"));
54 s2.addTransition(new SuccessTransition("foo"));
55
56 StateContext context = new DefaultStateContext();
57 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
58 sm.handle(new Event("foo", context));
59 assertEquals(true, context.getAttribute("success"));
60 }
61
62 public void testBreakAndGotoNext() throws Exception {
63 State s1 = new State("s1");
64 State s2 = new State("s2");
65 s1.addTransition(new BreakAndGotoNextTransition("foo", "s2"));
66 s2.addTransition(new SuccessTransition("foo"));
67
68 StateContext context = new DefaultStateContext();
69 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
70 sm.handle(new Event("foo", context));
71 assertSame(s2, context.getCurrentState());
72 sm.handle(new Event("foo", context));
73 assertEquals(true, context.getAttribute("success"));
74 }
75
76 private static class SuccessTransition extends AbstractTransition {
77 public SuccessTransition(Object eventId) {
78 super(eventId);
79 }
80
81 public SuccessTransition(Object eventId, State nextState) {
82 super(eventId, nextState);
83 }
84
85 @Override
86 protected boolean doExecute(Event event) {
87 event.getContext().setAttribute("success", true);
88 return true;
89 }
90 }
91
92 private static class BreakAndContinueTransition extends AbstractTransition {
93 public BreakAndContinueTransition(Object eventId) {
94 super(eventId);
95 }
96
97 public BreakAndContinueTransition(Object eventId, State nextState) {
98 super(eventId, nextState);
99 }
100
101 @Override
102 protected boolean doExecute(Event event) {
103 StateControl.breakAndContinue();
104 return true;
105 }
106 }
107
108 private static class BreakAndGotoNowTransition extends AbstractTransition {
109 private final String stateId;
110
111 public BreakAndGotoNowTransition(Object eventId, String stateId) {
112 super(eventId);
113 this.stateId = stateId;
114 }
115
116 public BreakAndGotoNowTransition(Object eventId, State nextState, String stateId) {
117 super(eventId, nextState);
118 this.stateId = stateId;
119 }
120
121 @Override
122 protected boolean doExecute(Event event) {
123 StateControl.breakAndGotoNow(stateId);
124 return true;
125 }
126 }
127
128 private static class BreakAndGotoNextTransition extends AbstractTransition {
129 private final String stateId;
130
131 public BreakAndGotoNextTransition(Object eventId, String stateId) {
132 super(eventId);
133 this.stateId = stateId;
134 }
135
136 public BreakAndGotoNextTransition(Object eventId, State nextState, String stateId) {
137 super(eventId, nextState);
138 this.stateId = stateId;
139 }
140
141 @Override
142 protected boolean doExecute(Event event) {
143 StateControl.breakAndGotoNext(stateId);
144 return true;
145 }
146 }
147 }