Package org.apache.mina.statemachine

Source Code of org.apache.mina.statemachine.StateMachineFactoryTest$StatesWithMissingParents

/*
*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you under the Apache License, Version 2.0 (the
*  "License"); you may not use this file except in compliance
*  with the License.  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing,
*  software distributed under the License is distributed on an
*  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
*  KIND, either express or implied.  See the License for the
*  specific language governing permissions and limitations
*  under the License.
*
*/
package org.apache.mina.statemachine;

import java.lang.reflect.Method;
import java.util.List;

import junit.framework.TestCase;

import org.apache.mina.statemachine.State;
import org.apache.mina.statemachine.StateMachine;
import org.apache.mina.statemachine.StateMachineCreationException;
import org.apache.mina.statemachine.StateMachineFactory;
import org.apache.mina.statemachine.annotation.Transition;
import org.apache.mina.statemachine.annotation.Transitions;
import org.apache.mina.statemachine.transition.MethodTransition;

/**
* Tests {@link StateMachineFactory}.
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev: 592475 $, $Date: 2007-11-06 17:03:42 +0100 (mar, 06 nov 2007) $
*/
public class StateMachineFactoryTest extends TestCase {
    Method barInA;
    Method error;
    Method fooInA;
    Method fooInB;
    Method barInC;
    Method fooOrBarInCOrFooInD;

    protected void setUp() throws Exception {
        barInA = States.class.getDeclaredMethod("barInA", new Class[0]);
        error = States.class.getDeclaredMethod("error", new Class[0]);
        fooInA = States.class.getDeclaredMethod("fooInA", new Class[0]);
        fooInB = States.class.getDeclaredMethod("fooInB", new Class[0]);
        barInC = States.class.getDeclaredMethod("barInC", new Class[0]);
        fooOrBarInCOrFooInD = States.class.getDeclaredMethod("fooOrBarInCOrFooInD", new Class[0]);
    }

    public void testCreate() throws Exception {
        States states = new States();
        StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(States.A, states);

        State a = sm.getState(States.A);
        State b = sm.getState(States.B);
        State c = sm.getState(States.C);
        State d = sm.getState(States.D);

        assertEquals(States.A, a.getId());
        assertNull(a.getParent());
        assertEquals(States.B, b.getId());
        assertSame(a, b.getParent());
        assertEquals(States.C, c.getId());
        assertSame(b, c.getParent());
        assertEquals(States.D, d.getId());
        assertSame(a, d.getParent());

        List<org.apache.mina.statemachine.transition.Transition> trans = null;

        trans = a.getTransitions();
        assertEquals(3, trans.size());
        assertEquals(new MethodTransition("bar", barInA, states), trans.get(0));
        assertEquals(new MethodTransition("*", error, states), trans.get(1));
        assertEquals(new MethodTransition("foo", b, fooInA, states), trans.get(2));
       
        trans = b.getTransitions();
        assertEquals(1, trans.size());
        assertEquals(new MethodTransition("foo", c, fooInB, states), trans.get(0));

        trans = c.getTransitions();
        assertEquals(3, trans.size());
        assertEquals(new MethodTransition("bar", a, barInC, states), trans.get(0));
        assertEquals(new MethodTransition("foo", d, fooOrBarInCOrFooInD, states), trans.get(1));
        assertEquals(new MethodTransition("bar", d, fooOrBarInCOrFooInD, states), trans.get(2));

        trans = d.getTransitions();
        assertEquals(1, trans.size());
        assertEquals(new MethodTransition("foo", fooOrBarInCOrFooInD, states), trans.get(0));
    }
   
    public void testCreateStates() throws Exception {
        State[] states = StateMachineFactory.createStates(StateMachineFactory.getFields(States.class));
        assertEquals(States.A, states[0].getId());
        assertNull(states[0].getParent());
        assertEquals(States.B, states[1].getId());
        assertEquals(states[0], states[1].getParent());
        assertEquals(States.C, states[2].getId());
        assertEquals(states[1], states[2].getParent());
        assertEquals(States.D, states[3].getId());
        assertEquals(states[0], states[3].getParent());
    }
   
    public void testCreateStatesMissingParents() throws Exception {
        try {
            StateMachineFactory.createStates(StateMachineFactory.getFields(StatesWithMissingParents.class));
            fail("Missing parents. FsmCreationException expected.");
        } catch (StateMachineCreationException fce) {
        }
    }
   
    public static class States {
        @org.apache.mina.statemachine.annotation.State
        protected static final String A = "a";
        @org.apache.mina.statemachine.annotation.State(A)
        protected static final String B = "b";
        @org.apache.mina.statemachine.annotation.State(B)
        protected static final String C = "c";
        @org.apache.mina.statemachine.annotation.State(A)
        protected static final String D = "d";
       
        @Transition(on = "bar", in = A)
        protected void barInA() {
        }

        @Transition(on = "bar", in = C, next = A)
        protected void barInC() {
        }

        @Transition(in = A)
        protected void error() {
        }

        @Transition(on = "foo", in = A, next = B)
        protected void fooInA() {
        }

        @Transition(on = "foo", in = B, next = C)
        protected void fooInB() {
        }

        @Transitions( { @Transition(on = { "foo", "bar" }, in = C, next = D), @Transition(on = "foo", in = D) })
        protected void fooOrBarInCOrFooInD() {
        }
     
    }
   
    public static class StatesWithMissingParents {
        @org.apache.mina.statemachine.annotation.State("b")
        public static final String A = "a";
        @org.apache.mina.statemachine.annotation.State("c")
        public static final String B = "b";
        @org.apache.mina.statemachine.annotation.State("d")
        public static final String C = "c";
        @org.apache.mina.statemachine.annotation.State("e")
        public static final String D = "d";
    }
}
TOP

Related Classes of org.apache.mina.statemachine.StateMachineFactoryTest$StatesWithMissingParents

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.