001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.statemachine;
021
022import org.apache.mina.statemachine.State;
023import org.apache.mina.statemachine.transition.Transition;
024import org.junit.BeforeClass;
025import org.junit.Test;
026
027import com.agical.rmock.extension.junit.RMockTestCase;
028
029/**
030 * Tests {@link State}.
031 *
032 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
033 */
034public class StateTest extends RMockTestCase {
035    State state;
036
037    Transition transition1;
038
039    Transition transition2;
040
041    Transition transition3;
042
043    @BeforeClass
044    protected void setUp() throws Exception {
045        state = new State("test");
046        transition1 = (Transition) mock(Transition.class);
047        transition2 = transition1; //(Transition) mock(Transition.class);
048        transition3 = transition1; //(Transition) mock(Transition.class);
049    }
050
051    @Test
052    public void testAddFirstTransition() throws Exception {
053        assertTrue(state.getTransitions().isEmpty());
054        state.addTransition(transition1);
055        assertFalse(state.getTransitions().isEmpty());
056        assertEquals(1, state.getTransitions().size());
057        assertSame(transition1, state.getTransitions().get(0));
058    }
059
060    @Test
061    public void testUnweightedTransitions() throws Exception {
062        assertTrue(state.getTransitions().isEmpty());
063        state.addTransition(transition1);
064        state.addTransition(transition2);
065        state.addTransition(transition3);
066        assertEquals(3, state.getTransitions().size());
067        assertSame(transition1, state.getTransitions().get(0));
068        assertSame(transition2, state.getTransitions().get(1));
069        assertSame(transition3, state.getTransitions().get(2));
070    }
071
072    @Test
073    public void testWeightedTransitions() throws Exception {
074        assertTrue(state.getTransitions().isEmpty());
075        state.addTransition(transition1, 10);
076        state.addTransition(transition2, 5);
077        state.addTransition(transition3, 7);
078        assertEquals(3, state.getTransitions().size());
079        assertSame(transition2, state.getTransitions().get(0));
080        assertSame(transition3, state.getTransitions().get(1));
081        assertSame(transition1, state.getTransitions().get(2));
082    }
083
084    @Test
085    public void testAddTransitionReturnsSelf() throws Exception {
086        assertSame(state, state.addTransition(transition1));
087    }
088
089    @Test
090    public void testAddNullTransitionThrowsException() throws Exception {
091        try {
092            state.addTransition(null);
093            fail("null transition added. IllegalArgumentException expected.");
094        } catch (IllegalArgumentException npe) {
095        }
096    }
097
098}