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: */
020: package org.apache.mina.statemachine;
021:
022: import java.lang.reflect.Method;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.mina.statemachine.State;
028: import org.apache.mina.statemachine.StateMachine;
029: import org.apache.mina.statemachine.StateMachineCreationException;
030: import org.apache.mina.statemachine.StateMachineFactory;
031: import org.apache.mina.statemachine.annotation.Transition;
032: import org.apache.mina.statemachine.annotation.Transitions;
033: import org.apache.mina.statemachine.transition.MethodTransition;
034:
035: /**
036: * Tests {@link StateMachineFactory}.
037: *
038: * @author The Apache MINA Project (dev@mina.apache.org)
039: * @version $Rev: 592475 $, $Date: 2007-11-06 09:03:42 -0700 (Tue, 06 Nov 2007) $
040: */
041: public class StateMachineFactoryTest extends TestCase {
042: Method barInA;
043: Method error;
044: Method fooInA;
045: Method fooInB;
046: Method barInC;
047: Method fooOrBarInCOrFooInD;
048:
049: protected void setUp() throws Exception {
050: barInA = States.class.getDeclaredMethod("barInA", new Class[0]);
051: error = States.class.getDeclaredMethod("error", new Class[0]);
052: fooInA = States.class.getDeclaredMethod("fooInA", new Class[0]);
053: fooInB = States.class.getDeclaredMethod("fooInB", new Class[0]);
054: barInC = States.class.getDeclaredMethod("barInC", new Class[0]);
055: fooOrBarInCOrFooInD = States.class.getDeclaredMethod(
056: "fooOrBarInCOrFooInD", new Class[0]);
057: }
058:
059: public void testCreate() throws Exception {
060: States states = new States();
061: StateMachine sm = StateMachineFactory.getInstance(
062: Transition.class).create(States.A, states);
063:
064: State a = sm.getState(States.A);
065: State b = sm.getState(States.B);
066: State c = sm.getState(States.C);
067: State d = sm.getState(States.D);
068:
069: assertEquals(States.A, a.getId());
070: assertNull(a.getParent());
071: assertEquals(States.B, b.getId());
072: assertSame(a, b.getParent());
073: assertEquals(States.C, c.getId());
074: assertSame(b, c.getParent());
075: assertEquals(States.D, d.getId());
076: assertSame(a, d.getParent());
077:
078: List<org.apache.mina.statemachine.transition.Transition> trans = null;
079:
080: trans = a.getTransitions();
081: assertEquals(3, trans.size());
082: assertEquals(new MethodTransition("bar", barInA, states), trans
083: .get(0));
084: assertEquals(new MethodTransition("*", error, states), trans
085: .get(1));
086: assertEquals(new MethodTransition("foo", b, fooInA, states),
087: trans.get(2));
088:
089: trans = b.getTransitions();
090: assertEquals(1, trans.size());
091: assertEquals(new MethodTransition("foo", c, fooInB, states),
092: trans.get(0));
093:
094: trans = c.getTransitions();
095: assertEquals(3, trans.size());
096: assertEquals(new MethodTransition("bar", a, barInC, states),
097: trans.get(0));
098: assertEquals(new MethodTransition("foo", d,
099: fooOrBarInCOrFooInD, states), trans.get(1));
100: assertEquals(new MethodTransition("bar", d,
101: fooOrBarInCOrFooInD, states), trans.get(2));
102:
103: trans = d.getTransitions();
104: assertEquals(1, trans.size());
105: assertEquals(new MethodTransition("foo", fooOrBarInCOrFooInD,
106: states), trans.get(0));
107: }
108:
109: public void testCreateStates() throws Exception {
110: State[] states = StateMachineFactory
111: .createStates(StateMachineFactory
112: .getFields(States.class));
113: assertEquals(States.A, states[0].getId());
114: assertNull(states[0].getParent());
115: assertEquals(States.B, states[1].getId());
116: assertEquals(states[0], states[1].getParent());
117: assertEquals(States.C, states[2].getId());
118: assertEquals(states[1], states[2].getParent());
119: assertEquals(States.D, states[3].getId());
120: assertEquals(states[0], states[3].getParent());
121: }
122:
123: public void testCreateStatesMissingParents() throws Exception {
124: try {
125: StateMachineFactory.createStates(StateMachineFactory
126: .getFields(StatesWithMissingParents.class));
127: fail("Missing parents. FsmCreationException expected.");
128: } catch (StateMachineCreationException fce) {
129: }
130: }
131:
132: public static class States {
133: @org.apache.mina.statemachine.annotation.State
134: protected static final String A = "a";
135: @org.apache.mina.statemachine.annotation.State(A)
136: protected static final String B = "b";
137: @org.apache.mina.statemachine.annotation.State(B)
138: protected static final String C = "c";
139: @org.apache.mina.statemachine.annotation.State(A)
140: protected static final String D = "d";
141:
142: @Transition(on="bar",in=A)
143: protected void barInA() {
144: }
145:
146: @Transition(on="bar",in=C,next=A)
147: protected void barInC() {
148: }
149:
150: @Transition(in=A)
151: protected void error() {
152: }
153:
154: @Transition(on="foo",in=A,next=B)
155: protected void fooInA() {
156: }
157:
158: @Transition(on="foo",in=B,next=C)
159: protected void fooInB() {
160: }
161:
162: @Transitions({@Transition(on={"foo","bar"},in=C,next=D),@Transition(on="foo",in=D)})
163: protected void fooOrBarInCOrFooInD() {
164: }
165:
166: }
167:
168: public static class StatesWithMissingParents {
169: @org.apache.mina.statemachine.annotation.State("b")
170: public static final String A = "a";
171: @org.apache.mina.statemachine.annotation.State("c")
172: public static final String B = "b";
173: @org.apache.mina.statemachine.annotation.State("d")
174: public static final String C = "c";
175: @org.apache.mina.statemachine.annotation.State("e")
176: public static final String D = "d";
177: }
178: }
|