1+ /**
2+ * Alipay.com Inc. Copyright (c) 2004-2020 All Rights Reserved.
3+ */
4+ package org .squirrelframework .foundation .issues ;
5+
6+ import junit .framework .Assert ;
7+ import org .junit .Test ;
8+ import org .squirrelframework .foundation .fsm .StateMachineBuilderFactory ;
9+ import org .squirrelframework .foundation .fsm .StateMachineConfiguration ;
10+ import org .squirrelframework .foundation .fsm .UntypedStateMachine ;
11+ import org .squirrelframework .foundation .fsm .UntypedStateMachineBuilder ;
12+ import org .squirrelframework .foundation .fsm .annotation .StateMachineParameters ;
13+ import org .squirrelframework .foundation .fsm .impl .AbstractUntypedStateMachine ;
14+
15+ public class Issue116 {
16+
17+ enum FSMEvent {
18+ SELF_TRANSITION
19+ }
20+
21+ @ Test
22+ public void testSelfTransit () {
23+ UntypedStateMachineBuilder builder =StateMachineBuilderFactory .create (MyStateMachine .class );
24+ builder .defineState ("S" );
25+ builder .transition ().from ("S" ).to ("S" ).on (FSMEvent .SELF_TRANSITION ).callMethod ("transit" );
26+ UntypedStateMachine fsm =
27+ builder .newStateMachine ("S" ,StateMachineConfiguration .create ().enableDebugMode (true ));
28+ fsm .start ();
29+ String startLog = ((MyStateMachine )fsm ).consumeLog ();
30+ Assert .assertTrue (startLog .equals ("entryS" ));
31+ fsm .fire (FSMEvent .SELF_TRANSITION );
32+ String execLog = ((MyStateMachine )fsm ).consumeLog ();
33+ Assert .assertTrue (execLog .equals ("exitS-transit-entryS" ));
34+ }
35+
36+ @ StateMachineParameters (
37+ stateType =String .class ,
38+ eventType =FSMEvent .class ,
39+ contextType =String .class )
40+ static class MyStateMachine extends AbstractUntypedStateMachine {
41+
42+ StringBuilder logger =new StringBuilder ();
43+
44+ protected void entryS (String from ,String to ,FSMEvent event ,String ctx ) {
45+ logger .append ("entryS" );
46+ }
47+
48+ protected void exitS (String from ,String to ,FSMEvent event ,String ctx ) {
49+ logger .append ("exitS" );
50+ }
51+
52+ protected void transit (String from ,String to ,FSMEvent event ,String ctx ) {
53+ logger .append ("-transit-" );
54+ }
55+
56+ String consumeLog () {
57+ final String result =logger .toString ();
58+ logger =new StringBuilder ();
59+ return result ;
60+ }
61+ }
62+ }