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 The Apache MINA Project (dev@mina.apache.org)
33   * @version $Rev: 671930 $, $Date: 2008-06-26 17:58:30 +0200 (Thu, 26 Jun 2008) $
34   */
35  public class StateTest extends RMockTestCase {
36      State state;
37      Transition transition1;
38      Transition transition2;
39      Transition transition3;
40  
41      @BeforeClass
42      protected void setUp() throws Exception {
43          state = new State("test");
44          transition1 = (Transition) mock(Transition.class);
45          transition2 = transition1; //(Transition) mock(Transition.class);
46          transition3 = transition1; //(Transition) mock(Transition.class);
47      }
48  
49      @Test
50      public void testAddFirstTransition() throws Exception {
51          assertTrue(state.getTransitions().isEmpty());
52          state.addTransition(transition1);
53          assertFalse(state.getTransitions().isEmpty());
54          assertEquals(1, state.getTransitions().size());
55          assertSame(transition1, state.getTransitions().get(0));
56      }
57  
58      @Test
59      public void testUnweightedTransitions() throws Exception {
60          assertTrue(state.getTransitions().isEmpty());
61          state.addTransition(transition1);
62          state.addTransition(transition2);
63          state.addTransition(transition3);
64          assertEquals(3, state.getTransitions().size());
65          assertSame(transition1, state.getTransitions().get(0));
66          assertSame(transition2, state.getTransitions().get(1));
67          assertSame(transition3, state.getTransitions().get(2));
68      }
69      
70      @Test
71      public void testWeightedTransitions() throws Exception {
72          assertTrue(state.getTransitions().isEmpty());
73          state.addTransition(transition1, 10);
74          state.addTransition(transition2, 5);
75          state.addTransition(transition3, 7);
76          assertEquals(3, state.getTransitions().size());
77          assertSame(transition2, state.getTransitions().get(0));
78          assertSame(transition3, state.getTransitions().get(1));
79          assertSame(transition1, state.getTransitions().get(2));
80      }
81  
82      @Test
83      public void testAddTransitionReturnsSelf() throws Exception {
84          assertSame(state, state.addTransition(transition1));
85      }
86  
87      @Test
88      public void testAddNullTransitionThrowsException() throws Exception {
89          try {
90              state.addTransition(null);
91              fail("null transition added. NullPointerException expected.");
92          } catch (NullPointerException npe) {
93          }
94      }
95      
96  }