From 2c50b4f1b7a2f18bf7aa68d77426c652a3637a53 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 1 Apr 2017 11:00:44 +0200 Subject: [PATCH] SS refactor (#1555) --- .../SimpleStateMachineConfiguration.java | 130 +++++++++++------- .../ForkJoinStateMachineTest.java | 3 +- .../HierarchicalStateMachineTest.java | 3 +- .../JunctionStateMachineTest.java | 3 +- .../statemachine/StateEnumMachineTest.java | 3 +- .../StateMachineIntegrationTest.java | 3 +- 6 files changed, 91 insertions(+), 54 deletions(-) diff --git a/spring-state-machine/src/main/java/com/baeldung/spring/statemachine/config/SimpleStateMachineConfiguration.java b/spring-state-machine/src/main/java/com/baeldung/spring/statemachine/config/SimpleStateMachineConfiguration.java index f6c7991cf6..d1b1ce001c 100644 --- a/spring-state-machine/src/main/java/com/baeldung/spring/statemachine/config/SimpleStateMachineConfiguration.java +++ b/spring-state-machine/src/main/java/com/baeldung/spring/statemachine/config/SimpleStateMachineConfiguration.java @@ -1,9 +1,5 @@ package com.baeldung.spring.statemachine.config; -import java.util.Arrays; -import java.util.HashSet; -import java.util.logging.Logger; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.action.Action; @@ -14,6 +10,10 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import org.springframework.statemachine.guard.Guard; +import java.util.Arrays; +import java.util.HashSet; +import java.util.logging.Logger; + @Configuration @EnableStateMachine public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter { @@ -21,94 +21,126 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt public static final Logger LOGGER = Logger.getLogger(SimpleStateMachineConfiguration.class.getName()); @Override - public void configure(StateMachineConfigurationConfigurer config) - throws Exception { + public void configure(StateMachineConfigurationConfigurer config) throws Exception { config - .withConfiguration() - .autoStartup(true) - .listener(new StateMachineListener()); + .withConfiguration() + .autoStartup(true) + .listener(new StateMachineListener()); } @Override public void configure(StateMachineStateConfigurer states) throws Exception { states - .withStates() - .initial("SI") - .end("SF") - .states(new HashSet<>(Arrays.asList("S1", "S2"))) - .stateEntry("S3", entryAction()) - .stateExit("S3", exitAction()) - .state("S4", executeAction(), errorAction()) - .stateDo("S5", executeAction()); + .withStates() + .initial("SI") + .end("SF") + .states(new HashSet<>(Arrays.asList("S1", "S2"))) + .stateEntry("S3", entryAction()) + .stateExit("S3", exitAction()) + .state("S4", executeAction(), errorAction()) + .stateDo("S5", executeAction()); } @Override public void configure(StateMachineTransitionConfigurer transitions) throws Exception { - transitions.withExternal() - .source("SI").target("S1").event("E1").action(initAction()) - .and().withExternal() - .source("S1").target("S2").event("E2") - .and().withExternal() - .source("SI").target("S3").event("E3") - .and().withExternal() - .source("S3").target("S4").event("E4") - .and().withExternal() - .source("S4").target("S5").event("E5") - .and().withExternal() - .source("S5").target("SF").event("end").guard(simpleGuard()); + transitions + .withExternal() + .source("SI") + .target("S1") + .event("E1") + .action(initAction()) + .and() + .withExternal() + .source("S1") + .target("S2") + .event("E2") + .and() + .withExternal() + .source("SI") + .target("S3") + .event("E3") + .and() + .withExternal() + .source("S3") + .target("S4") + .event("E4") + .and() + .withExternal() + .source("S4") + .target("S5") + .event("E5") + .and() + .withExternal() + .source("S5") + .target("SF") + .event("end") + .guard(simpleGuard()); } @Bean public Guard simpleGuard() { return (ctx) -> { - int approvalCount = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0); + int approvalCount = (int) ctx + .getExtendedState() + .getVariables() + .getOrDefault("approvalCount", 0); return approvalCount > 0; }; } @Bean public Action entryAction() { - return (ctx) -> { - LOGGER.info("Entry " + ctx.getTarget().getId()); - }; + return ctx -> LOGGER.info("Entry " + ctx + .getTarget() + .getId()); } @Bean public Action doAction() { - return (ctx) -> { - LOGGER.info("Do " + ctx.getTarget().getId()); - }; + return ctx -> LOGGER.info("Do " + ctx + .getTarget() + .getId()); } @Bean public Action executeAction() { - return (ctx) -> { - LOGGER.info("Execute " + ctx.getTarget().getId()); - int approvals = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0); + return ctx -> { + LOGGER.info("Execute " + ctx + .getTarget() + .getId()); + int approvals = (int) ctx + .getExtendedState() + .getVariables() + .getOrDefault("approvalCount", 0); approvals++; - ctx.getExtendedState().getVariables().put("approvalCount", approvals); + ctx + .getExtendedState() + .getVariables() + .put("approvalCount", approvals); }; } @Bean public Action exitAction() { - return (ctx) -> { - LOGGER.info("Exit " + ctx.getSource().getId() + " -> " + ctx.getTarget().getId()); - }; + return ctx -> LOGGER.info("Exit " + ctx + .getSource() + .getId() + " -> " + ctx + .getTarget() + .getId()); } @Bean public Action errorAction() { - return (ctx) -> { - LOGGER.info("Error " + ctx.getSource().getId() + ctx.getException()); - }; + return ctx -> LOGGER.info("Error " + ctx + .getSource() + .getId() + ctx.getException()); } @Bean public Action initAction() { - return (ctx) -> { - LOGGER.info(ctx.getTarget().getId()); - }; + return ctx -> LOGGER.info(ctx + .getTarget() + .getId()); } } \ No newline at end of file diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java index 0de61da3bd..a9aca82a6e 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java @@ -6,6 +6,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.statemachine.StateMachine; import org.springframework.test.context.ContextConfiguration; @@ -21,7 +22,7 @@ import static org.junit.Assert.assertTrue; @ContextConfiguration(classes = ForkJoinStateMachineConfiguration.class) public class ForkJoinStateMachineTest { - @Resource + @Autowired private StateMachine stateMachine; @Before diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java index 76105368d8..a2fa4f9160 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java @@ -6,6 +6,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.statemachine.StateMachine; import org.springframework.test.context.ContextConfiguration; @@ -20,7 +21,7 @@ import static org.junit.Assert.assertEquals; @ContextConfiguration(classes = HierarchicalStateMachineConfiguration.class) public class HierarchicalStateMachineTest { - @Resource + @Autowired private StateMachine stateMachine; @Before diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java index b91e6af5c5..2b5130ab45 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java @@ -7,6 +7,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.statemachine.StateMachine; import org.springframework.test.context.ContextConfiguration; @@ -18,7 +19,7 @@ import javax.annotation.Resource; @ContextConfiguration(classes = JunctionStateMachineConfiguration.class) public class JunctionStateMachineTest { - @Resource + @Autowired private StateMachine stateMachine; @Before diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java index cd9e58eb8e..19f24ede2d 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java @@ -8,6 +8,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.statemachine.StateMachine; import org.springframework.test.context.ContextConfiguration; @@ -22,7 +23,7 @@ import static org.junit.Assert.assertTrue; @ContextConfiguration(classes = SimpleEnumStateMachineConfiguration.class) public class StateEnumMachineTest { - @Resource + @Autowired private StateMachine stateMachine; @Before diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java index 1c1ab58917..25df7c8cd3 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java @@ -7,6 +7,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.statemachine.StateMachine; @@ -21,7 +22,7 @@ import javax.annotation.Resource; @ContextConfiguration(classes = SimpleStateMachineConfiguration.class) public class StateMachineIntegrationTest { - @Resource + @Autowired private StateMachine stateMachine; @Before