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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertSame;
24
25 import org.apache.mina.statemachine.context.DefaultStateContext;
26 import org.apache.mina.statemachine.context.StateContext;
27 import org.apache.mina.statemachine.event.Event;
28 import org.apache.mina.statemachine.transition.AbstractSelfTransition;
29 import org.apache.mina.statemachine.transition.AbstractTransition;
30 import org.junit.Test;
31
32
33
34
35
36
37 public class StateMachineTest {
38
39 @Test
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 @Test
52 public void testBreakAndGotoNow() throws Exception {
53 State s1 = new State("s1");
54 State s2 = new State("s2");
55 s1.addTransition(new BreakAndGotoNowTransition("foo", "s2"));
56 s2.addTransition(new SuccessTransition("foo"));
57
58 StateContext context = new DefaultStateContext();
59 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
60 sm.handle(new Event("foo", context));
61 assertEquals(true, context.getAttribute("success"));
62 }
63
64 @Test
65 public void testBreakAndGotoNext() throws Exception {
66 State s1 = new State("s1");
67 State s2 = new State("s2");
68 s1.addTransition(new BreakAndGotoNextTransition("foo", "s2"));
69 s2.addTransition(new SuccessTransition("foo"));
70
71 StateContext context = new DefaultStateContext();
72 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
73 sm.handle(new Event("foo", context));
74 assertSame(s2, context.getCurrentState());
75 sm.handle(new Event("foo", context));
76 assertEquals(true, context.getAttribute("success"));
77 }
78
79 private static class SuccessTransition extends AbstractTransition {
80 public SuccessTransition(Object eventId) {
81 super(eventId);
82 }
83
84 public SuccessTransition(Object eventId, State nextState) {
85 super(eventId, nextState);
86 }
87
88 @Override
89 protected boolean doExecute(Event event) {
90 event.getContext().setAttribute("success", true);
91
92 return true;
93 }
94 }
95
96 private static class BreakAndContinueTransition extends AbstractTransition {
97 public BreakAndContinueTransition(Object eventId) {
98 super(eventId);
99 }
100
101 public BreakAndContinueTransition(Object eventId, State nextState) {
102 super(eventId, nextState);
103 }
104
105 @Override
106 protected boolean doExecute(Event event) {
107 StateControl.breakAndContinue();
108
109 return true;
110 }
111 }
112
113 private static class BreakAndGotoNowTransition extends AbstractTransition {
114 private final String stateId;
115
116 public BreakAndGotoNowTransition(Object eventId, String stateId) {
117 super(eventId);
118 this.stateId = stateId;
119 }
120
121 public BreakAndGotoNowTransition(Object eventId, State nextState, String stateId) {
122 super(eventId, nextState);
123 this.stateId = stateId;
124 }
125
126
127
128
129 @Override
130 protected boolean doExecute(Event event) {
131 StateControl.breakAndGotoNow(stateId);
132
133 return true;
134 }
135 }
136
137 private static class BreakAndGotoNextTransition extends AbstractTransition {
138 private final String stateId;
139
140 public BreakAndGotoNextTransition(Object eventId, String stateId) {
141 super(eventId);
142 this.stateId = stateId;
143 }
144
145 public BreakAndGotoNextTransition(Object eventId, State nextState, String stateId) {
146 super(eventId, nextState);
147 this.stateId = stateId;
148 }
149
150
151
152
153 @Override
154 protected boolean doExecute(Event event) {
155 StateControl.breakAndGotoNext(stateId);
156
157 return true;
158 }
159 }
160
161 private static class SampleSelfTransition extends AbstractSelfTransition {
162 public SampleSelfTransition() {
163 super();
164 }
165
166 @Override
167 protected boolean doExecute(StateContext stateContext, State state) {
168 stateContext.setAttribute("SelfSuccess" + state.getId(), true);
169 return true;
170 }
171
172 }
173
174 @Test
175 public void testOnEntry() throws Exception {
176 State s1 = new State("s1");
177 State s2 = new State("s2");
178
179 s1.addTransition(new SuccessTransition("foo", s2));
180 s1.addOnExitSelfTransaction(new SampleSelfTransition());
181 s2.addOnEntrySelfTransaction(new SampleSelfTransition());
182
183 StateContext context = new DefaultStateContext();
184 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
185 sm.handle(new Event("foo", context));
186 assertEquals(true, context.getAttribute("success"));
187 assertEquals(true, context.getAttribute("SelfSuccess" + s1.getId()));
188 assertEquals(true, context.getAttribute("SelfSuccess" + s2.getId()));
189
190 }
191
192 }