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.AbstractTransition;
29  import org.junit.Test;
30  
31  /**
32   * Tests {@link StateMachine}.
33   *
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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 }