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
38 public class StateMachineTest extends TestCase {
39
40 public void testBreakAndContinue() throws Exception {
41 State s1 = new State("s1");
42 s1.addTransition(new BreakAndContinueTransition("foo"));
43 s1.addTransition(new SuccessTransition("foo"));
44
45 StateContext context = new DefaultStateContext();
46 StateMachine sm = new StateMachine(new State[] { s1 }, "s1");
47 sm.handle(new Event("foo", context));
48 assertEquals(true, context.getAttribute("success"));
49 }
50
51 public void testBreakAndGotoNow() throws Exception {
52 State s1 = new State("s1");
53 State s2 = new State("s2");
54 s1.addTransition(new BreakAndGotoNowTransition("foo", "s2"));
55 s2.addTransition(new SuccessTransition("foo"));
56
57 StateContext context = new DefaultStateContext();
58 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
59 sm.handle(new Event("foo", context));
60 assertEquals(true, context.getAttribute("success"));
61 }
62
63 public void testBreakAndGotoNext() throws Exception {
64 State s1 = new State("s1");
65 State s2 = new State("s2");
66 s1.addTransition(new BreakAndGotoNextTransition("foo", "s2"));
67 s2.addTransition(new SuccessTransition("foo"));
68
69 StateContext context = new DefaultStateContext();
70 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
71 sm.handle(new Event("foo", context));
72 assertSame(s2, context.getCurrentState());
73 sm.handle(new Event("foo", context));
74 assertEquals(true, context.getAttribute("success"));
75 }
76
77 private static class SuccessTransition extends AbstractTransition {
78 public SuccessTransition(Object eventId) {
79 super(eventId);
80 }
81
82 public SuccessTransition(Object eventId, State nextState) {
83 super(eventId, nextState);
84 }
85
86 @Override
87 protected boolean doExecute(Event event) {
88 event.getContext().setAttribute("success", true);
89 return true;
90 }
91 }
92
93 private static class BreakAndContinueTransition extends AbstractTransition {
94 public BreakAndContinueTransition(Object eventId) {
95 super(eventId);
96 }
97
98 public BreakAndContinueTransition(Object eventId, State nextState) {
99 super(eventId, nextState);
100 }
101
102 @Override
103 protected boolean doExecute(Event event) {
104 StateControl.breakAndContinue();
105 return true;
106 }
107 }
108
109 private static class BreakAndGotoNowTransition extends AbstractTransition {
110 private final String stateId;
111
112 public BreakAndGotoNowTransition(Object eventId, String stateId) {
113 super(eventId);
114 this.stateId = stateId;
115 }
116
117 public BreakAndGotoNowTransition(Object eventId, State nextState, String stateId) {
118 super(eventId, nextState);
119 this.stateId = stateId;
120 }
121
122 @Override
123 protected boolean doExecute(Event event) {
124 StateControl.breakAndGotoNow(stateId);
125 return true;
126 }
127 }
128
129 private static class BreakAndGotoNextTransition extends AbstractTransition {
130 private final String stateId;
131
132 public BreakAndGotoNextTransition(Object eventId, String stateId) {
133 super(eventId);
134 this.stateId = stateId;
135 }
136
137 public BreakAndGotoNextTransition(Object eventId, State nextState, String stateId) {
138 super(eventId, nextState);
139 this.stateId = stateId;
140 }
141
142 @Override
143 protected boolean doExecute(Event event) {
144 StateControl.breakAndGotoNext(stateId);
145 return true;
146 }
147 }
148 }