001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.*;
030:
031: import junit.framework.TestCase;
032: import junit.framework.*;
033:
034: public class TestStateMachine extends TestCase {
035: class TestSM extends StateMachine {
036: private String seq = null; // we'll accumulate the states here (yuck)
037:
038: String getSeq() {
039: return seq;
040: }
041:
042: protected void _set(State s) {
043: if (seq == null) {
044: seq = s.getKey();
045: } else {
046: seq = seq + " " + s.getKey();
047: }
048: super ._set(s);
049: }
050: }
051:
052: public TestSM createSM() {
053: TestSM sm = new TestSM();
054: sm.add(new StateMachine.State("A") {
055: public void invoke() {
056: transit("B");
057: }
058: });
059: sm.add(new StateMachine.State("B") {
060: public void invoke() {
061: transit("C");
062: }
063: });
064: sm.add(new StateMachine.State("C") {
065: public void invoke() {
066: transit("D");
067: }
068: });
069: sm.add(new StateMachine.State("D") {
070: public void invoke() {
071: transit("DONE");
072: }
073: });
074:
075: sm.reset("A");
076: return sm;
077: }
078:
079: public void test_step() {
080: TestSM sm = createSM();
081: while (sm.getState() != StateMachine.DONE) {
082: sm.step();
083: }
084: assertEquals(sm.getSeq(), "A B C D DONE");
085: }
086:
087: public void test_stepUntilDone() {
088: TestSM sm = createSM();
089: sm.stepUntilDone();
090: assertEquals(sm.getSeq(), "A B C D DONE");
091: }
092:
093: public void test_go() {
094: TestSM sm = createSM();
095: // replace B with a state which requires several invokes
096: sm.add(new StateMachine.State("B") {
097: private int count = 0;
098:
099: public void invoke() {
100: count++;
101: if (count >= 3)
102: transit("C");
103: else
104: transit("B");
105: }
106: });
107:
108: while (sm.getState() != StateMachine.DONE) {
109: sm.go();
110: }
111: assertEquals(sm.getSeq(), "A B B B C D DONE");
112: }
113:
114: public void test_reset() {
115: TestSM sm = createSM();
116: sm.stepUntilDone();
117: sm.reset("A");
118: sm.stepUntilDone();
119: assertEquals(sm.getSeq(), "A B C D DONE A B C D DONE");
120: }
121: }
|