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 org.apache.mina.statemachine.State;
23  import org.apache.mina.statemachine.transition.Transition;
24  import org.junit.BeforeClass;
25  import org.junit.Test;
26  
27  import com.agical.rmock.extension.junit.RMockTestCase;
28  
29  /**
30   * Tests {@link State}.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class StateTest extends RMockTestCase {
35      State state;
36  
37      Transition transition1;
38  
39      Transition transition2;
40  
41      Transition transition3;
42  
43      @BeforeClass
44      protected void setUp() throws Exception {
45          state = new State("test");
46          transition1 = (Transition) mock(Transition.class);
47          transition2 = transition1; //(Transition) mock(Transition.class);
48          transition3 = transition1; //(Transition) mock(Transition.class);
49      }
50  
51      @Test
52      public void testAddFirstTransition() throws Exception {
53          assertTrue(state.getTransitions().isEmpty());
54          state.addTransition(transition1);
55          assertFalse(state.getTransitions().isEmpty());
56          assertEquals(1, state.getTransitions().size());
57          assertSame(transition1, state.getTransitions().get(0));
58      }
59  
60      @Test
61      public void testUnweightedTransitions() throws Exception {
62          assertTrue(state.getTransitions().isEmpty());
63          state.addTransition(transition1);
64          state.addTransition(transition2);
65          state.addTransition(transition3);
66          assertEquals(3, state.getTransitions().size());
67          assertSame(transition1, state.getTransitions().get(0));
68          assertSame(transition2, state.getTransitions().get(1));
69          assertSame(transition3, state.getTransitions().get(2));
70      }
71  
72      @Test
73      public void testWeightedTransitions() throws Exception {
74          assertTrue(state.getTransitions().isEmpty());
75          state.addTransition(transition1, 10);
76          state.addTransition(transition2, 5);
77          state.addTransition(transition3, 7);
78          assertEquals(3, state.getTransitions().size());
79          assertSame(transition2, state.getTransitions().get(0));
80          assertSame(transition3, state.getTransitions().get(1));
81          assertSame(transition1, state.getTransitions().get(2));
82      }
83  
84      @Test
85      public void testAddTransitionReturnsSelf() throws Exception {
86          assertSame(state, state.addTransition(transition1));
87      }
88  
89      @Test
90      public void testAddNullTransitionThrowsException() throws Exception {
91          try {
92              state.addTransition(null);
93              fail("null transition added. IllegalArgumentException expected.");
94          } catch (IllegalArgumentException npe) {
95          }
96      }
97  
98  }