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 junit.framework.TestCase;
023:
024: import org.apache.mina.statemachine.State;
025: import org.apache.mina.statemachine.StateControl;
026: import org.apache.mina.statemachine.StateMachine;
027: import org.apache.mina.statemachine.context.DefaultStateContext;
028: import org.apache.mina.statemachine.context.StateContext;
029: import org.apache.mina.statemachine.event.Event;
030: import org.apache.mina.statemachine.transition.AbstractTransition;
031:
032: /**
033: * Tests {@link StateMachine}.
034: *
035: * @author The Apache MINA Project (dev@mina.apache.org)
036: * @version $Rev: 586090 $, $Date: 2007-10-18 13:12:08 -0600 (Thu, 18 Oct 2007) $
037: */
038: public class StateMachineTest extends TestCase {
039:
040: public void testBreakAndContinue() throws Exception {
041: State s1 = new State("s1");
042: s1.addTransition(new BreakAndContinueTransition("foo"));
043: s1.addTransition(new SuccessTransition("foo"));
044:
045: StateContext context = new DefaultStateContext();
046: StateMachine sm = new StateMachine(new State[] { s1 }, "s1");
047: sm.handle(new Event("foo", context));
048: assertEquals(true, context.getAttribute("success"));
049: }
050:
051: public void testBreakAndGotoNow() throws Exception {
052: State s1 = new State("s1");
053: State s2 = new State("s2");
054: s1.addTransition(new BreakAndGotoNowTransition("foo", "s2"));
055: s2.addTransition(new SuccessTransition("foo"));
056:
057: StateContext context = new DefaultStateContext();
058: StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
059: sm.handle(new Event("foo", context));
060: assertEquals(true, context.getAttribute("success"));
061: }
062:
063: public void testBreakAndGotoNext() throws Exception {
064: State s1 = new State("s1");
065: State s2 = new State("s2");
066: s1.addTransition(new BreakAndGotoNextTransition("foo", "s2"));
067: s2.addTransition(new SuccessTransition("foo"));
068:
069: StateContext context = new DefaultStateContext();
070: StateMachine sm = new StateMachine(new State[] { s1, s2 }, "s1");
071: sm.handle(new Event("foo", context));
072: assertSame(s2, context.getCurrentState());
073: sm.handle(new Event("foo", context));
074: assertEquals(true, context.getAttribute("success"));
075: }
076:
077: private static class SuccessTransition extends AbstractTransition {
078: public SuccessTransition(Object eventId) {
079: super (eventId);
080: }
081:
082: public SuccessTransition(Object eventId, State nextState) {
083: super (eventId, nextState);
084: }
085:
086: @Override
087: protected boolean doExecute(Event event) {
088: event.getContext().setAttribute("success", true);
089: return true;
090: }
091: }
092:
093: private static class BreakAndContinueTransition extends
094: AbstractTransition {
095: public BreakAndContinueTransition(Object eventId) {
096: super (eventId);
097: }
098:
099: public BreakAndContinueTransition(Object eventId,
100: State nextState) {
101: super (eventId, nextState);
102: }
103:
104: @Override
105: protected boolean doExecute(Event event) {
106: StateControl.breakAndContinue();
107: return true;
108: }
109: }
110:
111: private static class BreakAndGotoNowTransition extends
112: AbstractTransition {
113: private final String stateId;
114:
115: public BreakAndGotoNowTransition(Object eventId, String stateId) {
116: super (eventId);
117: this .stateId = stateId;
118: }
119:
120: public BreakAndGotoNowTransition(Object eventId,
121: State nextState, String stateId) {
122: super (eventId, nextState);
123: this .stateId = stateId;
124: }
125:
126: @Override
127: protected boolean doExecute(Event event) {
128: StateControl.breakAndGotoNow(stateId);
129: return true;
130: }
131: }
132:
133: private static class BreakAndGotoNextTransition extends
134: AbstractTransition {
135: private final String stateId;
136:
137: public BreakAndGotoNextTransition(Object eventId, String stateId) {
138: super (eventId);
139: this .stateId = stateId;
140: }
141:
142: public BreakAndGotoNextTransition(Object eventId,
143: State nextState, String stateId) {
144: super (eventId, nextState);
145: this .stateId = stateId;
146: }
147:
148: @Override
149: protected boolean doExecute(Event event) {
150: StateControl.breakAndGotoNext(stateId);
151: return true;
152: }
153: }
154: }
|