View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * Tests {@link StateMachine}.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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          * {@inheritDoc}
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          * {@inheritDoc}
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 }