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.transition;
    21
    22  import java.lang.reflect.Method;
    23
    24  import org.apache.mina.statemachine.State;
    25  import org.apache.mina.statemachine.context.StateContext;
    26  import org.apache.mina.statemachine.event.Event;
    27  import org.apache.mina.statemachine.transition.MethodTransition;
    28
    29  import com.agical.rmock.extension.junit.RMockTestCase;
    30
    31  /**
    32   * Tests {@link MethodTransition}.
    33   *
    34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    35   */
    36  public class MethodTransitionTest extends RMockTestCase {
    37      State currentState;
    38
    39      State nextState;
    40
    41      TestStateContext context;
    42
    43      Target target;
    44
    45      Method subsetAllArgsMethod1;
    46
    47      Method subsetAllArgsMethod2;
    48
    49      Event noArgsEvent;
    50
    51      Event argsEvent;
    52
    53      Object[] args;
    54
    55      protected void setUp() throws Exception {
    56          super.setUp();
    57
    58          currentState = new State("current");
    59          nextState = new State("next");
    60          target = (Target) mock(Target.class);
    61          subsetAllArgsMethod1 = Target.class.getMethod("subsetAllArgs", new Class[] { TestStateContext.class, B.class,
    62                  A.class, Integer.TYPE });
    63          subsetAllArgsMethod2 = Target.class.getMethod("subsetAllArgs", new Class[] { Event.class, B.class, B.class,
    64                  Boolean.TYPE });
    65
    66          args = new Object[] { new A(), new B(), new C(), new Integer(627438), Boolean.TRUE };
    67          context = (TestStateContext) mock(TestStateContext.class);
    68          noArgsEvent = new Event("event", context, new Object[0]);
    69          argsEvent = new Event("event", context, args);
    70      }
    71
    72      public void testExecuteWrongEventId() throws Exception {
    73          startVerification();
    74          MethodTransition t = new MethodTransition("otherEvent", nextState, "noArgs", target);
    75          assertFalse(t.execute(noArgsEvent));
    76      }
    77
    78      public void testExecuteNoArgsMethodOnNoArgsEvent() throws Exception {
    79          target.noArgs();
    80          startVerification();
    81          MethodTransition t = new MethodTransition("event", nextState, "noArgs", target);
    82          assertTrue(t.execute(noArgsEvent));
    83      }
    84
    85      public void testExecuteNoArgsMethodOnArgsEvent() throws Exception {
    86          target.noArgs();
    87          startVerification();
    88          MethodTransition t = new MethodTransition("event", nextState, "noArgs", target);
    89          assertTrue(t.execute(argsEvent));
    90      }
    91
    92      public void testExecuteExactArgsMethodOnNoArgsEvent() throws Exception {
    93          startVerification();
    94          MethodTransition t = new MethodTransition("event", nextState, "exactArgs", target);
    95          assertFalse(t.execute(noArgsEvent));
    96      }
    97
    98      public void testExecuteExactArgsMethodOnArgsEvent() throws Exception {
    99          target.exactArgs((A) args[0], (B) args[1], (C) args[2], ((Integer) args[3]).intValue(),
    100                 ((Boolean) args[4]).booleanValue());
    101         startVerification();
    102         MethodTransition t = new MethodTransition("event", nextState, "exactArgs", target);
    103         assertTrue(t.execute(argsEvent));
    104     }
    105
    106     public void testExecuteSubsetExactArgsMethodOnNoArgsEvent() throws Exception {
    107         startVerification();
    108         MethodTransition t = new MethodTransition("event", nextState, "subsetExactArgs", target);
    109         assertFalse(t.execute(noArgsEvent));
    110     }
    111
    112     public void testExecuteSubsetExactArgsMethodOnArgsEvent() throws Exception {
    113         target.subsetExactArgs((A) args[0], (A) args[1], ((Integer) args[3]).intValue());
    114         startVerification();
    115         MethodTransition t = new MethodTransition("event", nextState, "subsetExactArgs", target);
    116         assertTrue(t.execute(argsEvent));
    117     }
    118
    119     public void testExecuteAllArgsMethodOnArgsEvent() throws Exception {
    120         target.allArgs(argsEvent, context, (A) args[0], (B) args[1], (C) args[2], ((Integer) args[3]).intValue(),
    121                 ((Boolean) args[4]).booleanValue());
    122         startVerification();
    123         MethodTransition t = new MethodTransition("event", nextState, "allArgs", target);
    124         assertTrue(t.execute(argsEvent));
    125     }
    126
    127     public void testExecuteSubsetAllArgsMethod1OnArgsEvent() throws Exception {
    128         target.subsetAllArgs(context, (B) args[1], (A) args[2], ((Integer) args[3]).intValue());
    129         startVerification();
    130         MethodTransition t = new MethodTransition("event", nextState, subsetAllArgsMethod1, target);
    131         assertTrue(t.execute(argsEvent));
    132     }
    133
    134     public void testExecuteSubsetAllArgsMethod2OnArgsEvent() throws Exception {
    135         target.subsetAllArgs(argsEvent, (B) args[1], (B) args[2], ((Boolean) args[4]).booleanValue());
    136         startVerification();
    137         MethodTransition t = new MethodTransition("event", nextState, subsetAllArgsMethod2, target);
    138         assertTrue(t.execute(argsEvent));
    139     }
    140
    141     public interface Target {
    142         void noArgs();
    143
    144         void exactArgs(A a, B b, C c, int integer, boolean bool);
    145
    146         void allArgs(Event event, StateContext ctxt, A a, B b, C c, int integer, boolean bool);
    147
    148         void subsetExactArgs(A a, A b, int integer);
    149
    150         void subsetAllArgs(TestStateContext ctxt, B b, A c, int integer);
    151
    152         void subsetAllArgs(Event event, B b, B c, boolean bool);
    153     }
    154
    155     public interface TestStateContext extends StateContext {
    156     }
    157
    158     public static class A {
    159     }
    160
    161     public static class B extends A {
    162     }
    163
    164     public static class C extends B {
    165     }
    166 }