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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.fail;
26  
27  import java.lang.reflect.Method;
28  import java.util.List;
29  
30  import org.apache.mina.statemachine.annotation.Transition;
31  import org.apache.mina.statemachine.annotation.Transitions;
32  import org.apache.mina.statemachine.transition.MethodTransition;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   * Tests {@link StateMachineFactory}.
38   *
39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40   */
41  public class StateMachineFactoryTest {
42      Method barInA;
43  
44      Method error;
45  
46      Method fooInA;
47  
48      Method fooInB;
49  
50      Method barInC;
51  
52      Method fooOrBarInCOrFooInD;
53  
54      @Before
55      public void setUp() throws Exception {
56          barInA = States.class.getDeclaredMethod("barInA", new Class[0]);
57          error = States.class.getDeclaredMethod("error", new Class[0]);
58          fooInA = States.class.getDeclaredMethod("fooInA", new Class[0]);
59          fooInB = States.class.getDeclaredMethod("fooInB", new Class[0]);
60          barInC = States.class.getDeclaredMethod("barInC", new Class[0]);
61          fooOrBarInCOrFooInD = States.class.getDeclaredMethod("fooOrBarInCOrFooInD", new Class[0]);
62      }
63  
64      @Test
65      public void testCreate() throws Exception {
66          States states = new States();
67          StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(States.A, states);
68  
69          State a = sm.getState(States.A);
70          State b = sm.getState(States.B);
71          State c = sm.getState(States.C);
72          State d = sm.getState(States.D);
73  
74          assertEquals(States.A, a.getId());
75          assertNull(a.getParent());
76          assertEquals(States.B, b.getId());
77          assertSame(a, b.getParent());
78          assertEquals(States.C, c.getId());
79          assertSame(b, c.getParent());
80          assertEquals(States.D, d.getId());
81          assertSame(a, d.getParent());
82  
83          List<org.apache.mina.statemachine.transition.Transition> trans = null;
84  
85          trans = a.getTransitions();
86          assertEquals(3, trans.size());
87          assertEquals(new MethodTransition("bar", barInA, states), trans.get(0));
88          assertEquals(new MethodTransition("*", error, states), trans.get(1));
89          assertEquals(new MethodTransition("foo", b, fooInA, states), trans.get(2));
90  
91          trans = b.getTransitions();
92          assertEquals(1, trans.size());
93          assertEquals(new MethodTransition("foo", c, fooInB, states), trans.get(0));
94  
95          trans = c.getTransitions();
96          assertEquals(3, trans.size());
97          assertEquals(new MethodTransition("bar", a, barInC, states), trans.get(0));
98          assertEquals(new MethodTransition("foo", d, fooOrBarInCOrFooInD, states), trans.get(1));
99          assertEquals(new MethodTransition("bar", d, fooOrBarInCOrFooInD, states), trans.get(2));
100 
101         trans = d.getTransitions();
102         assertEquals(1, trans.size());
103         assertEquals(new MethodTransition("foo", fooOrBarInCOrFooInD, states), trans.get(0));
104     }
105 
106     @Test
107     public void testCreateStates() throws Exception {
108         State[] states = StateMachineFactory.createStates(StateMachineFactory.getFields(States.class));
109         assertEquals(States.A, states[0].getId());
110         assertNull(states[0].getParent());
111         assertEquals(States.B, states[1].getId());
112         assertEquals(states[0], states[1].getParent());
113         assertEquals(States.C, states[2].getId());
114         assertEquals(states[1], states[2].getParent());
115         assertEquals(States.D, states[3].getId());
116         assertEquals(states[0], states[3].getParent());
117     }
118 
119     @Test
120     public void testCreateStatesMissingParents() throws Exception {
121         try {
122             StateMachineFactory.createStates(StateMachineFactory.getFields(StatesWithMissingParents.class));
123             fail("Missing parents. FsmCreationException expected.");
124         } catch (StateMachineCreationException fce) {
125         }
126     }
127 
128     public static class States {
129         @org.apache.mina.statemachine.annotation.State
130         protected static final String A = "a";
131 
132         @org.apache.mina.statemachine.annotation.State(A)
133         protected static final String B = "b";
134 
135         @org.apache.mina.statemachine.annotation.State(B)
136         protected static final String C = "c";
137 
138         @org.apache.mina.statemachine.annotation.State(A)
139         protected static final String D = "d";
140 
141         @Transition(on = "bar", in = A)
142         protected void barInA() {
143         }
144 
145         @Transition(on = "bar", in = C, next = A)
146         protected void barInC() {
147         }
148 
149         @Transition(in = A)
150         protected void error() {
151         }
152 
153         @Transition(on = "foo", in = A, next = B)
154         protected void fooInA() {
155         }
156 
157         @Transition(on = "foo", in = B, next = C)
158         protected void fooInB() {
159         }
160 
161         @Transitions({ @Transition(on = { "foo", "bar" }, in = C, next = D), @Transition(on = "foo", in = D) })
162         protected void fooOrBarInCOrFooInD() {
163         }
164 
165     }
166 
167     public static class StatesWithMissingParents {
168         @org.apache.mina.statemachine.annotation.State("b")
169         public static final String A = "a";
170 
171         @org.apache.mina.statemachine.annotation.State("c")
172         public static final String B = "b";
173 
174         @org.apache.mina.statemachine.annotation.State("d")
175         public static final String C = "c";
176 
177         @org.apache.mina.statemachine.annotation.State("e")
178         public static final String D = "d";
179     }
180 }