directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r472911 [2/2] - in /directory/sandbox/niklas/mina-sm: ./ src/main/java/org/apache/mina/sm/ src/main/java/org/apache/mina/sm/annotation/ src/main/java/org/apache/mina/sm/context/ src/main/java/org/apache/mina/sm/event/ src/main/java/org/apac...
Date Thu, 09 Nov 2006 14:25:38 GMT
Modified: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java?view=diff&rev=472911&r1=472910&r2=472911
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java
(original)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java
Thu Nov  9 06:25:36 2006
@@ -22,8 +22,10 @@
 
 import junit.framework.TestCase;
 
-import org.apache.mina.sm.finite.FsmState;
-import org.apache.mina.sm.finite.MethodTransition;
+import org.apache.mina.sm.annotation.Handler;
+import org.apache.mina.sm.annotation.Handlers;
+import org.apache.mina.sm.event.Event;
+import org.apache.mina.sm.transition.MethodTransition;
 
 /**
  * Tests {@link StateMachineProxyFactory}.
@@ -38,15 +40,16 @@
     {
         ReentrantStateMachineHandler handler = new ReentrantStateMachineHandler();
         
-        FsmState s1 = new FsmState( "s1" );
-        FsmState s2 = new FsmState( "s2" );
-        FsmState s3 = new FsmState( "s3" );
+        State s1 = new State( "s1" );
+        State s2 = new State( "s2" );
+        State s3 = new State( "s3" );
         
         s1.addTransition( new MethodTransition( "call1", s2, handler ) );
         s2.addTransition( new MethodTransition( "call2", s3, handler ) );
         s3.addTransition( new MethodTransition( "call3", handler ) );
         
-        Reentrant reentrant = ( Reentrant ) StateMachineProxyFactory.create( Reentrant.class,
s1 );
+        StateMachine sm = new StateMachine( new State[] { s1, s2, s3 }, "s1" );
+        Reentrant reentrant = ( Reentrant ) StateMachineProxyFactory.create( Reentrant.class,
sm );
         reentrant.call1( reentrant );
         assertTrue( handler.finished );
     }
@@ -55,12 +58,12 @@
     {
         TapeDeckStateMachineHandler handler = new TapeDeckStateMachineHandler();
         
-        FsmState parent = new FsmState( "parent" );
-        FsmState s1 = new FsmState( "s1", parent );
-        FsmState s2 = new FsmState( "s2", parent );
-        FsmState s3 = new FsmState( "s3", parent );
-        FsmState s4 = new FsmState( "s4", parent );
-        FsmState s5 = new FsmState( "s5", parent );
+        State parent = new State( "parent" );
+        State s1 = new State( "s1", parent );
+        State s2 = new State( "s2", parent );
+        State s3 = new State( "s3", parent );
+        State s4 = new State( "s4", parent );
+        State s5 = new State( "s5", parent );
         
         parent.addTransition( new MethodTransition( "*", "error", handler ) );
         s1.addTransition( new MethodTransition( "insert", s2, "inserted", handler ) );
@@ -70,7 +73,34 @@
         s4.addTransition( new MethodTransition( "eject",  s1, "ejected",  handler ) );
         s5.addTransition( new MethodTransition( "pause",  s3, "playing",  handler ) );
         
-        TapeDeck player = ( TapeDeck ) StateMachineProxyFactory.create( TapeDeck.class, s1
);
+        StateMachine sm = new StateMachine( new State[] { s1, s2, s3, s4, s5 }, "s1" );
+        TapeDeck player = ( TapeDeck ) StateMachineProxyFactory.create( TapeDeck.class, sm
);
+        player.insert( "Kings of convenience - Riot on an empty street" );
+        player.start();
+        player.pause();
+        player.pause();
+        player.eject();
+        player.stop();
+        player.eject();
+        
+        LinkedList messages = handler.messages;
+        assertEquals( "Tape 'Kings of convenience - Riot on an empty street' inserted", messages.removeFirst()
);
+        assertEquals( "Playing", messages.removeFirst() );
+        assertEquals( "Paused", messages.removeFirst() );
+        assertEquals( "Playing", messages.removeFirst() );
+        assertEquals( "Error: Cannot eject at this time", messages.removeFirst() );
+        assertEquals( "Stopped", messages.removeFirst() );
+        assertEquals( "Tape ejected", messages.removeFirst() );
+        assertTrue( messages.isEmpty() );
+    }
+    
+    public void testTapeDeckStateMachineAnnotations() throws Exception
+    {
+        TapeDeckStateMachineHandler handler = new TapeDeckStateMachineHandler();
+        
+        StateMachine sm = StateMachineFactory.create( TapeDeckStateMachineHandler.S1, handler
);
+        
+        TapeDeck player = ( TapeDeck ) StateMachineProxyFactory.create( TapeDeck.class, sm
);
         player.insert( "Kings of convenience - Riot on an empty street" );
         player.start();
         player.pause();
@@ -123,32 +153,47 @@
     }
     
     public static class TapeDeckStateMachineHandler {
-        private LinkedList messages = new LinkedList();
+        @org.apache.mina.sm.annotation.State public static final String PARENT = "parent";
+        @org.apache.mina.sm.annotation.State(PARENT) public static final String S1 = "s1";
+        @org.apache.mina.sm.annotation.State(PARENT) public static final String S2 = "s2";
+        @org.apache.mina.sm.annotation.State(PARENT) public static final String S3 = "s3";
+        @org.apache.mina.sm.annotation.State(PARENT) public static final String S4 = "s4";
+        @org.apache.mina.sm.annotation.State(PARENT) public static final String S5 = "s5";
+        
+        private LinkedList<String> messages = new LinkedList<String>();
+        
+        @Handler( on = "insert", in = "s1", next = "s2" )
         public void inserted( String name )
         {
             messages.add( "Tape '" + name + "' inserted" );
         }
         
+        @Handler( on = "eject",  in = "s4", next = "s1" )
         public void ejected()
         {
             messages.add( "Tape ejected" );
         }
         
+        @Handlers( { @Handler( on = "start", in = "s2", next = "s3" ),
+                     @Handler( on = "pause", in = "s5", next = "s3" ) } )
         public void playing()
         {
             messages.add( "Playing" );
         }
         
+        @Handler( on = "pause",  in = "s3", next = "s5" )
         public void paused()
         {
             messages.add( "Paused" );
         }
         
+        @Handler( on = "stop",  in = "s3", next = "s4" )
         public void stopped()
         {
             messages.add( "Stopped" );
         }
         
+        @Handler( on = "*",  in = "parent" )
         public void error( Event event )
         {
             messages.add( "Error: Cannot " + event.getId() + " at this time" );

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineTest.java?view=auto&rev=472911
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineTest.java
(added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineTest.java
Thu Nov  9 06:25:36 2006
@@ -0,0 +1,162 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.sm;
+
+import junit.framework.TestCase;
+
+import org.apache.mina.sm.context.DefaultStateContext;
+import org.apache.mina.sm.context.StateContext;
+import org.apache.mina.sm.event.Event;
+import org.apache.mina.sm.transition.AbstractTransition;
+
+/**
+ * Tests {@link StateMachine}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class StateMachineTest extends TestCase
+{
+
+    public void testBreakAndContinue() throws Exception
+    {
+        State s1 = new State( "s1" );
+        s1.addTransition( new BreakAndContinueTransition( "foo" ) );
+        s1.addTransition( new SuccessTransition( "foo" ) );
+        
+        StateContext context = new DefaultStateContext();
+        StateMachine sm = new StateMachine( new State[] { s1 }, "s1" );
+        sm.handle( new Event( "foo", context ) );
+        assertEquals( true, context.getAttribute( "success" ));
+    }
+    
+    public void testBreakAndGotoNow() throws Exception
+    {
+        State s1 = new State( "s1" );
+        State s2 = new State( "s2" );
+        s1.addTransition( new BreakAndGotoNowTransition( "foo", "s2" ) );
+        s2.addTransition( new SuccessTransition( "foo" ) );
+        
+        StateContext context = new DefaultStateContext();
+        StateMachine sm = new StateMachine( new State[] { s1, s2 }, "s1" );
+        sm.handle( new Event( "foo", context ) );
+        assertEquals( true, context.getAttribute( "success" ));
+    }
+    
+    public void testBreakAndGotoNext() throws Exception
+    {
+        State s1 = new State( "s1" );
+        State s2 = new State( "s2" );
+        s1.addTransition( new BreakAndGotoNextTransition( "foo", "s2" ) );
+        s2.addTransition( new SuccessTransition( "foo" ) );
+        
+        StateContext context = new DefaultStateContext();
+        StateMachine sm = new StateMachine( new State[] { s1, s2 }, "s1" );
+        sm.handle( new Event( "foo", context ) );
+        assertSame( s2, context.getCurrentState() );
+        sm.handle( new Event( "foo", context ) );
+        assertEquals( true, context.getAttribute( "success" ));
+    }
+    
+    private static class SuccessTransition extends AbstractTransition
+    {
+        public SuccessTransition( Object eventId )
+        {
+            super( eventId );
+        }
+        
+        public SuccessTransition( Object eventId, State nextState )
+        {
+            super( eventId, nextState );
+        }
+
+        @Override
+        protected boolean doExecute( Event event )
+        {
+            event.getContext().setAttribute( "success", true );
+            return true;
+        }
+    }
+    
+    private static class BreakAndContinueTransition extends AbstractTransition
+    {
+        public BreakAndContinueTransition( Object eventId )
+        {
+            super( eventId );
+        }
+        
+        public BreakAndContinueTransition( Object eventId, State nextState )
+        {
+            super( eventId, nextState );
+        }
+
+        @Override
+        protected boolean doExecute( Event event )
+        {
+            StateControl.breakAndContinue();
+            return true;
+        }
+    }
+    
+    private static class BreakAndGotoNowTransition extends AbstractTransition
+    {
+        private final String stateId;
+        public BreakAndGotoNowTransition( Object eventId, String stateId )
+        {
+            super( eventId );
+            this.stateId = stateId;
+        }
+        
+        public BreakAndGotoNowTransition( Object eventId, State nextState, String stateId
)
+        {
+            super( eventId, nextState );
+            this.stateId = stateId;
+        }
+
+        @Override
+        protected boolean doExecute( Event event )
+        {
+            StateControl.breakAndGotoNow( stateId );
+            return true;
+        }
+    }
+    
+    private static class BreakAndGotoNextTransition extends AbstractTransition
+    {
+        private final String stateId;
+        public BreakAndGotoNextTransition( Object eventId, String stateId )
+        {
+            super( eventId );
+            this.stateId = stateId;
+        }
+        
+        public BreakAndGotoNextTransition( Object eventId, State nextState, String stateId
)
+        {
+            super( eventId, nextState );
+            this.stateId = stateId;
+        }
+
+        @Override
+        protected boolean doExecute( Event event )
+        {
+            StateControl.breakAndGotoNext( stateId );
+            return true;
+        }
+    }    
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateTest.java?view=auto&rev=472911
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateTest.java (added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateTest.java Thu Nov
 9 06:25:36 2006
@@ -0,0 +1,95 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.sm;
+
+import org.apache.mina.sm.transition.Transition;
+
+import com.agical.rmock.extension.junit.RMockTestCase;
+
+/**
+ * Tests {@link FsmState}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class StateTest extends RMockTestCase
+{
+    State state;
+    Transition transition1;
+    Transition transition2;
+    Transition transition3;
+    
+    protected void setUp() throws Exception
+    {
+        state = new State( "test" );
+        transition1 = ( Transition ) mock( Transition.class );
+        transition2 = ( Transition ) mock( Transition.class );
+        transition3 = ( Transition ) mock( Transition.class );
+    }
+
+    public void testAddFirstTransition() throws Exception
+    {
+        assertTrue( state.getTransitions().isEmpty() );
+        state.addTransition( transition1 );
+        assertFalse( state.getTransitions().isEmpty() );
+        assertEquals( 1, state.getTransitions().size() );
+        assertSame( transition1, state.getTransitions().get( 0 ) );
+    }
+    
+    public void testUnweightedTransitions() throws Exception
+    {
+        assertTrue( state.getTransitions().isEmpty() );
+        state.addTransition( transition1 );
+        state.addTransition( transition2 );
+        state.addTransition( transition3 );
+        assertEquals( 3, state.getTransitions().size() );
+        assertSame( transition1, state.getTransitions().get( 0 ) );
+        assertSame( transition2, state.getTransitions().get( 1 ) );
+        assertSame( transition3, state.getTransitions().get( 2 ) );
+    }
+    
+    public void testWeightedTransitions() throws Exception
+    {
+        assertTrue( state.getTransitions().isEmpty() );
+        state.addTransition( transition1, 10 );
+        state.addTransition( transition2, 5 );
+        state.addTransition( transition3, 7 );
+        assertEquals( 3, state.getTransitions().size() );
+        assertSame( transition2, state.getTransitions().get( 0 ) );
+        assertSame( transition3, state.getTransitions().get( 1 ) );
+        assertSame( transition1, state.getTransitions().get( 2 ) );
+    }
+    
+    public void testAddTransitionReturnsSelf() throws Exception
+    {
+        assertSame( state, state.addTransition( transition1 ) );
+    }
+    
+    public void testAddNullTransitionThrowsException() throws Exception
+    {
+        try
+        {
+            state.addTransition( null );
+            fail( "null transition added. NullPointerException expected." );
+        }
+        catch (NullPointerException npe) {
+        }
+    }
+    
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Copied: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/transition/MethodTransitionTest.java
(from r466955, directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/MethodTransitionTest.java)
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/transition/MethodTransitionTest.java?view=diff&rev=472911&p1=directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/MethodTransitionTest.java&r1=466955&p2=directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/transition/MethodTransitionTest.java&r2=472911
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/MethodTransitionTest.java
(original)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/transition/MethodTransitionTest.java
Thu Nov  9 06:25:36 2006
@@ -16,14 +16,14 @@
  *   limitations under the License.
  *
  */
-package org.apache.mina.sm.finite;
+package org.apache.mina.sm.transition;
 
 import java.lang.reflect.Method;
 
-import org.apache.mina.sm.Event;
 import org.apache.mina.sm.State;
 import org.apache.mina.sm.context.StateContext;
-import org.apache.mina.sm.finite.MethodTransition;
+import org.apache.mina.sm.event.Event;
+import org.apache.mina.sm.transition.MethodTransition;
 
 import com.agical.rmock.extension.junit.RMockTestCase;
 
@@ -47,8 +47,8 @@
     protected void setUp() throws Exception {
         super.setUp();
         
-        currentState = (State) mock(State.class);
-        nextState = (State) mock(State.class);
+        currentState = new State( "current" );
+        nextState = new State( "next" );
         target = (Target) mock(Target.class);
         subsetAllArgsMethod1 = Target.class.getMethod("subsetAllArgs", new Class[] {
                 TestStateContext.class, B.class, A.class, Integer.TYPE
@@ -71,7 +71,6 @@
     
     public void testExecuteNoArgsMethodOnNoArgsEvent() throws Exception {
         target.noArgs();
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, "noArgs", target);
         assertTrue(t.execute(noArgsEvent));
@@ -79,7 +78,6 @@
     
     public void testExecuteNoArgsMethodOnArgsEvent() throws Exception {
         target.noArgs();
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, "noArgs", target);
         assertTrue(t.execute(argsEvent));
@@ -94,7 +92,6 @@
     public void testExecuteExactArgsMethodOnArgsEvent() throws Exception {
         target.exactArgs((A) args[0], (B) args[1], (C) args[2], 
                          ((Integer) args[3]).intValue(), ((Boolean) args[4]).booleanValue());
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, "exactArgs", target);
         assertTrue(t.execute(argsEvent));
@@ -108,7 +105,6 @@
     
     public void testExecuteSubsetExactArgsMethodOnArgsEvent() throws Exception {
         target.subsetExactArgs((A) args[0], (A) args[1], ((Integer) args[3]).intValue());
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, "subsetExactArgs",
target);
         assertTrue(t.execute(argsEvent));
@@ -117,7 +113,6 @@
     public void testExecuteAllArgsMethodOnArgsEvent() throws Exception {
         target.allArgs(argsEvent, context, (A) args[0], (B) args[1], (C) args[2], 
                 ((Integer) args[3]).intValue(), ((Boolean) args[4]).booleanValue());
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, "allArgs", target);
         assertTrue(t.execute(argsEvent));
@@ -125,7 +120,6 @@
     
     public void testExecuteSubsetAllArgsMethod1OnArgsEvent() throws Exception {
         target.subsetAllArgs(context, (B) args[1], (A) args[2], ((Integer) args[3]).intValue());
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, subsetAllArgsMethod1,
target);
         assertTrue(t.execute(argsEvent));
@@ -133,7 +127,6 @@
     
     public void testExecuteSubsetAllArgsMethod2OnArgsEvent() throws Exception {
         target.subsetAllArgs(argsEvent, (B) args[1], (B) args[2], ((Boolean) args[4]).booleanValue());
-        context.setCurrentState( nextState );
         startVerification();
         MethodTransition t = new MethodTransition("event", nextState, subsetAllArgsMethod2,
target);
         assertTrue(t.execute(argsEvent));

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/transition/MethodTransitionTest.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message