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 static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertSame; 025import static org.junit.Assert.fail; 026 027import java.lang.reflect.Method; 028import java.util.List; 029 030import org.apache.mina.statemachine.annotation.Transition; 031import org.apache.mina.statemachine.annotation.Transitions; 032import org.apache.mina.statemachine.transition.MethodTransition; 033import org.junit.Before; 034import org.junit.Test; 035 036/** 037 * Tests {@link StateMachineFactory}. 038 * 039 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 040 */ 041public class StateMachineFactoryTest { 042 Method barInA; 043 044 Method error; 045 046 Method fooInA; 047 048 Method fooInB; 049 050 Method barInC; 051 052 Method fooOrBarInCOrFooInD; 053 054 @Before 055 public void setUp() throws Exception { 056 barInA = States.class.getDeclaredMethod("barInA", new Class[0]); 057 error = States.class.getDeclaredMethod("error", new Class[0]); 058 fooInA = States.class.getDeclaredMethod("fooInA", new Class[0]); 059 fooInB = States.class.getDeclaredMethod("fooInB", new Class[0]); 060 barInC = States.class.getDeclaredMethod("barInC", new Class[0]); 061 fooOrBarInCOrFooInD = States.class.getDeclaredMethod("fooOrBarInCOrFooInD", new Class[0]); 062 } 063 064 @Test 065 public void testCreate() throws Exception { 066 States states = new States(); 067 StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(States.A, states); 068 069 State a = sm.getState(States.A); 070 State b = sm.getState(States.B); 071 State c = sm.getState(States.C); 072 State d = sm.getState(States.D); 073 074 assertEquals(States.A, a.getId()); 075 assertNull(a.getParent()); 076 assertEquals(States.B, b.getId()); 077 assertSame(a, b.getParent()); 078 assertEquals(States.C, c.getId()); 079 assertSame(b, c.getParent()); 080 assertEquals(States.D, d.getId()); 081 assertSame(a, d.getParent()); 082 083 List<org.apache.mina.statemachine.transition.Transition> trans = null; 084 085 trans = a.getTransitions(); 086 assertEquals(3, trans.size()); 087 assertEquals(new MethodTransition("bar", barInA, states), trans.get(0)); 088 assertEquals(new MethodTransition("*", error, states), trans.get(1)); 089 assertEquals(new MethodTransition("foo", b, fooInA, states), trans.get(2)); 090 091 trans = b.getTransitions(); 092 assertEquals(1, trans.size()); 093 assertEquals(new MethodTransition("foo", c, fooInB, states), trans.get(0)); 094 095 trans = c.getTransitions(); 096 assertEquals(3, trans.size()); 097 assertEquals(new MethodTransition("bar", a, barInC, states), trans.get(0)); 098 assertEquals(new MethodTransition("foo", d, fooOrBarInCOrFooInD, states), trans.get(1)); 099 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}