@WithStateMachine may register too many processors

- Change processor registration so that if @WithStateMachine is
  used with 'id', 'name' is skipped because it defaults to 'stateMachine'
  and would cause extra registration.
- Fixes #370
This commit is contained in:
Janne Valkealahti
2017-06-08 09:52:49 +01:00
parent 7fccb6db94
commit 1dd3790e4a
2 changed files with 87 additions and 4 deletions

View File

@@ -86,13 +86,14 @@ public class StateMachineHandlerCallHelper<S, E> 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));
}
}
}

View File

@@ -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<TestStates,TestEvents> 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<TestStates, TestEvents> {
@@ -906,4 +957,35 @@ public class MethodAnnotationTests extends AbstractStateMachineTests {
}
}
@Configuration
@EnableStateMachine
static class Config4 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineConfigurationConfigurer<TestStates, TestEvents> config) throws Exception {
config.withConfiguration()
.machineId("myMachine");
}
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states.withStates()
.initial(TestStates.S1)
.states(EnumSet.allOf(TestStates.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> 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);
}
}
}