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      Transition transition1;
37      Transition transition2;
38      Transition transition3;
39  
40      @BeforeClass
41      protected void setUp() throws Exception {
42          state = new State("test");
43          transition1 = (Transition) mock(Transition.class);
44          transition2 = transition1; //(Transition) mock(Transition.class);
45          transition3 = transition1; //(Transition) mock(Transition.class);
46      }
47  
48      @Test
49      public void testAddFirstTransition() throws Exception {
50          assertTrue(state.getTransitions().isEmpty());
51          state.addTransition(transition1);
52          assertFalse(state.getTransitions().isEmpty());
53          assertEquals(1, state.getTransitions().size());
54          assertSame(transition1, state.getTransitions().get(0));
55      }
56  
57      @Test
58      public void testUnweightedTransitions() throws Exception {
59          assertTrue(state.getTransitions().isEmpty());
60          state.addTransition(transition1);
61          state.addTransition(transition2);
62          state.addTransition(transition3);
63          assertEquals(3, state.getTransitions().size());
64          assertSame(transition1, state.getTransitions().get(0));
65          assertSame(transition2, state.getTransitions().get(1));
66          assertSame(transition3, state.getTransitions().get(2));
67      }
68      
69      @Test
70      public void testWeightedTransitions() throws Exception {
71          assertTrue(state.getTransitions().isEmpty());
72          state.addTransition(transition1, 10);
73          state.addTransition(transition2, 5);
74          state.addTransition(transition3, 7);
75          assertEquals(3, state.getTransitions().size());
76          assertSame(transition2, state.getTransitions().get(0));
77          assertSame(transition3, state.getTransitions().get(1));
78          assertSame(transition1, state.getTransitions().get(2));
79      }
80  
81      @Test
82      public void testAddTransitionReturnsSelf() throws Exception {
83          assertSame(state, state.addTransition(transition1));
84      }
85  
86      @Test
87      public void testAddNullTransitionThrowsException() throws Exception {
88          try {
89              state.addTransition(null);
90              fail("null transition added. IllegalArgumentException expected.");
91          } catch (IllegalArgumentException npe) {
92          }
93      }
94      
95  }