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.AbstractTransition;
29 import org.junit.Test;
30
31
32
33
34
35
36 public class StateMachineTest {
37
38 @Test
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 @Test
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 @Test
64 public void testBreakAndGotoNext() throws Exception {
65 State s1 = new State("s1");
66 State s2 = new State("s2");
67 s1.addTransition(new BreakAndGotoNextTransition("foo", "s2"));
68 s2.addTransition(new SuccessTransition("foo"));
69
70 StateContext context = new DefaultStateContext();
71 StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
72 sm.handle(new Event("foo", context));
73 assertSame(s2, context.getCurrentState());
74 sm.handle(new Event("foo", context));
75 assertEquals(true, context.getAttribute("success"));
76 }
77
78 private static class SuccessTransition extends AbstractTransition {
79 public SuccessTransition(Object eventId) {
80 super(eventId);
81 }
82
83 public SuccessTransition(Object eventId, State nextState) {
84 super(eventId, nextState);
85 }
86
87 @Override
88 protected boolean doExecute(Event event) {
89 event.getContext().setAttribute("success", true);
90 return true;
91 }
92 }
93
94 private static class BreakAndContinueTransition extends AbstractTransition {
95 public BreakAndContinueTransition(Object eventId) {
96 super(eventId);
97 }
98
99 public BreakAndContinueTransition(Object eventId, State nextState) {
100 super(eventId, nextState);
101 }
102
103 @Override
104 protected boolean doExecute(Event event) {
105 StateControl.breakAndContinue();
106 return true;
107 }
108 }
109
110 private static class BreakAndGotoNowTransition extends AbstractTransition {
111 private final String stateId;
112
113 public BreakAndGotoNowTransition(Object eventId, String stateId) {
114 super(eventId);
115 this.stateId = stateId;
116 }
117
118 public BreakAndGotoNowTransition(Object eventId, State nextState, String stateId) {
119 super(eventId, nextState);
120 this.stateId = stateId;
121 }
122
123 @Override
124 protected boolean doExecute(Event event) {
125 StateControl.breakAndGotoNow(stateId);
126 return true;
127 }
128 }
129
130 private static class BreakAndGotoNextTransition extends AbstractTransition {
131 private final String stateId;
132
133 public BreakAndGotoNextTransition(Object eventId, String stateId) {
134 super(eventId);
135 this.stateId = stateId;
136 }
137
138 public BreakAndGotoNextTransition(Object eventId, State nextState, String stateId) {
139 super(eventId, nextState);
140 this.stateId = stateId;
141 }
142
143 @Override
144 protected boolean doExecute(Event event) {
145 StateControl.breakAndGotoNext(stateId);
146 return true;
147 }
148 }
149 }