diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/processor/StateMachineHandlerCallHelper.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/processor/StateMachineHandlerCallHelper.java index f0c2d84c..1320e923 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/processor/StateMachineHandlerCallHelper.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/processor/StateMachineHandlerCallHelper.java @@ -86,13 +86,14 @@ public class StateMachineHandlerCallHelper implements InitializingBean, Be WithStateMachine withStateMachine = AnnotationUtils.findAnnotation(handler.getBeanClass(), WithStateMachine.class); - if (StringUtils.hasText(withStateMachine.name())) { - updateCache(metaAnnotation.annotationType().getName() + withStateMachine.name(), - new CacheEntry(handler, annotation, metaAnnotation)); - } + // don't check name if id is set as name defaults to + // 'stateMachine' and would cause additional cache entry if (StringUtils.hasText(withStateMachine.id())) { updateCache(metaAnnotation.annotationType().getName() + withStateMachine.id(), new CacheEntry(handler, annotation, metaAnnotation)); + } else if (StringUtils.hasText(withStateMachine.name())) { + updateCache(metaAnnotation.annotationType().getName() + withStateMachine.name(), + new CacheEntry(handler, annotation, metaAnnotation)); } } } diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java index d7814752..3c5cefca 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/annotation/MethodAnnotationTests.java @@ -40,6 +40,7 @@ import org.springframework.statemachine.StateMachineSystemConstants; import org.springframework.statemachine.action.Action; import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; +import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import org.springframework.statemachine.config.configurers.StateConfigurer.History; @@ -332,6 +333,26 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { assertThat(bean10.OnTransition1Latch.await(2, TimeUnit.SECONDS), is(true)); } + @Test + @SuppressWarnings("unchecked") + public void testMethodAnnotations10() throws Exception { + context.register(BeanConfig11.class, Config4.class); + context.refresh(); + + ObjectStateMachine machine = + context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class); + Bean11 bean11 = context.getBean(Bean11.class); + machine.start(); + + machine.sendEvent(TestEvents.E1); + + assertThat(machine.getState().getIds(), contains(TestStates.S2)); + assertThat(bean11.OnTransition1Latch.await(2, TimeUnit.SECONDS), is(true)); + assertThat(bean11.OnTransition2Latch.await(2, TimeUnit.SECONDS), is(true)); + assertThat(bean11.count1, is(1)); + assertThat(bean11.count2, is(1)); + } + @WithStateMachine static class Bean1 { CountDownLatch onTransitionFromS1ToS2Latch = new CountDownLatch(1); @@ -647,6 +668,26 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { } } + @WithStateMachine(id="myMachine") + static class Bean11 { + CountDownLatch OnTransition1Latch = new CountDownLatch(1); + CountDownLatch OnTransition2Latch = new CountDownLatch(1); + volatile int count1 = 0; + volatile int count2 = 0; + + @OnTransition(target = "S1") + public void OnTransition1() { + count1++; + OnTransition1Latch.countDown(); + } + + @OnStateEntry(target = "S2") + public void OnTransition2() { + count2++; + OnTransition2Latch.countDown(); + } + } + @Configuration static class BeanConfig1 { @@ -747,6 +788,16 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { } + @Configuration + static class BeanConfig11 { + + @Bean + public Bean11 bean11() { + return new Bean11(); + } + + } + @Configuration @EnableStateMachine(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, "fooMachine"}) static class Config1 extends EnumStateMachineConfigurerAdapter { @@ -906,4 +957,35 @@ public class MethodAnnotationTests extends AbstractStateMachineTests { } } + + @Configuration + @EnableStateMachine + static class Config4 extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config.withConfiguration() + .machineId("myMachine"); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states.withStates() + .initial(TestStates.S1) + .states(EnumSet.allOf(TestStates.class)); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(TestStates.S1).target(TestStates.S2) + .event(TestEvents.E1) + .and() + .withExternal() + .source(TestStates.S2).target(TestStates.S1) + .event(TestEvents.E2); + } + } + }