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              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         @SuppressWarnings("unused")
    153         public SampleSelfTransition() {
    154             super();
    155         }
    156
    157         @Override
    158         protected boolean doExecute(StateContext stateContext, State state) {
    159             stateContext.setAttribute("SelfSuccess" + state.getId(), true);
    160             return true;
    161         }
    162
    163     }
    164
    165     @Test
    166     public void testOnEntry() throws Exception {
    167         State s1 = new State("s1");
    168         State s2 = new State("s2");
    169
    170         s1.addTransition(new SuccessTransition("foo", s2));
    171         s1.addOnExitSelfTransaction(new SampleSelfTransition());
    172         s2.addOnEntrySelfTransaction(new SampleSelfTransition());
    173
    174         StateContext context = new DefaultStateContext();
    175         StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
    176         sm.handle(new Event("foo", context));
    177         assertEquals(true, context.getAttribute("success"));
    178         assertEquals(true, context.getAttribute("SelfSuccess" + s1.getId()));
    179         assertEquals(true, context.getAttribute("SelfSuccess" + s2.getId()));
    180
    181     }
    182
    183 }