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