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 return true;
92 }
93 }
94
95 private static class BreakAndContinueTransition extends AbstractTransition {
96 public BreakAndContinueTransition(Object eventId) {
97 super(eventId);
98 }
99
100 public BreakAndContinueTransition(Object eventId, State nextState) {
101 super(eventId, nextState);
102 }
103
104 @Override
105 protected boolean doExecute(Event event) {
106 StateControl.breakAndContinue();
107 return true;
108 }
109 }
110
111 private static class BreakAndGotoNowTransition extends AbstractTransition {
112 private final String stateId;
113
114 public BreakAndGotoNowTransition(Object eventId, String stateId) {
115 super(eventId);
116 this.stateId = stateId;
117 }
118
119 public BreakAndGotoNowTransition(Object eventId, State nextState, String stateId) {
120 super(eventId, nextState);
121 this.stateId = stateId;
122 }
123
124 @Override
125 protected boolean doExecute(Event event) {
126 StateControl.breakAndGotoNow(stateId);
127 return true;
128 }
129 }
130
131 private static class BreakAndGotoNextTransition extends AbstractTransition {
132 private final String stateId;
133
134 public BreakAndGotoNextTransition(Object eventId, String stateId) {
135 super(eventId);
136 this.stateId = stateId;
137 }
138
139 public BreakAndGotoNextTransition(Object eventId, State nextState, String stateId) {
140 super(eventId, nextState);
141 this.stateId = stateId;
142 }
143
144 @Override
145 protected boolean doExecute(Event event) {
146 StateControl.breakAndGotoNext(stateId);
147 return true;
148 }
149 }
150
151 private static class SampleSelfTransition extends AbstractSelfTransition {
152 public SampleSelfTransition() {
153 super();
154 }
155
156 @Override
157 protected boolean doExecute(StateContext stateContext, State state) {
158 stateContext.setAttribute("SelfSuccess" + state.getId(), true);
159 return true;
160 }
161
162 }
163
164 @Test
165 public void testOnEntry() throws Exception {
166 State s1 = new State("s1");
167 State s2 = new State("s2");
168
169 s1.addTransition(new SuccessTransition("foo", s2));
170 s1.addOnExitSelfTransaction(new SampleSelfTransition());
171 s2.addOnEntrySelfTransaction(new SampleSelfTransition());
172
173 StateContext context = new DefaultStateContext();
174 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
175 sm.handle(new Event("foo", context));
176 assertEquals(true, context.getAttribute("success"));
177 assertEquals(true, context.getAttribute("SelfSuccess" + s1.getId()));
178 assertEquals(true, context.getAttribute("SelfSuccess" + s2.getId()));
179
180 }
181
182 }