Support @WithStateMachine as meta annotation
- Remove explicit check for @Component in favour of @WithStateMachine which makes it more logical to check meta annotations. - Fixes #179
This commit is contained in:
@@ -18,6 +18,10 @@ package org.springframework.statemachine.annotation;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -41,7 +45,7 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testClassAnnotations() throws Exception {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig.class, FooConfig.class, BarConfig.class);
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig1.class, FooConfig.class, BarConfig.class);
|
||||
|
||||
ObjectStateMachine<TestStates,TestEvents> fooMachine =
|
||||
context.getBean("fooMachine", ObjectStateMachine.class);
|
||||
@@ -76,6 +80,28 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testClassAnnotationsWithMeta() throws Exception {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig2.class, JeeConfig.class, FooConfig.class);
|
||||
|
||||
ObjectStateMachine<TestStates,TestEvents> jeeMachine =
|
||||
context.getBean("jeeMachine", ObjectStateMachine.class);
|
||||
|
||||
assertThat(context.containsBean("fooMachine"), is(true));
|
||||
assertThat(context.containsBean("jeeMachine"), is(true));
|
||||
|
||||
JeeBean jeeBean = context.getBean(JeeBean.class);
|
||||
FooBean fooBean = context.getBean(FooBean.class);
|
||||
fooBean.resetMethodLatch();
|
||||
jeeMachine.start();
|
||||
jeeMachine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
|
||||
assertThat(jeeBean.onJeeMethodLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(fooBean.onFooMethodLatch.await(2, TimeUnit.SECONDS), is(false));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@WithStateMachine(name = "fooMachine")
|
||||
static class FooBean {
|
||||
|
||||
@@ -104,8 +130,26 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine(name = "jeeMachine")
|
||||
public @interface WithJeeMachine {
|
||||
}
|
||||
|
||||
@WithJeeMachine
|
||||
static class JeeBean {
|
||||
|
||||
CountDownLatch onJeeMethodLatch = new CountDownLatch(1);
|
||||
|
||||
@OnTransition(source = "S1", target = "S2")
|
||||
public void jeeMethod() {
|
||||
onJeeMethodLatch.countDown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig {
|
||||
static class BeanConfig1 {
|
||||
|
||||
@Bean
|
||||
public FooBean fooBean() {
|
||||
@@ -119,6 +163,21 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig2 {
|
||||
|
||||
@Bean
|
||||
public FooBean fooBean() {
|
||||
return new FooBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JeeBean jeeBean() {
|
||||
return new JeeBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine(name = "fooMachine")
|
||||
static class FooConfig extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
@@ -189,4 +248,39 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine(name = "jeeMachine")
|
||||
static class JeeConfig extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(TestStates.S1)
|
||||
.state(TestStates.S2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(TestStates.S1)
|
||||
.target(TestStates.S2)
|
||||
.event(TestEvents.E1)
|
||||
.guard(testGuard())
|
||||
.action(testAction());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestGuard testGuard() {
|
||||
return new TestGuard();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestAction testAction() {
|
||||
return new TestAction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,6 +60,14 @@ public class DocsConfigurationSampleTests4 extends AbstractStateMachineTests {
|
||||
}
|
||||
// end::snippetAA[]
|
||||
|
||||
// tag::snippetAAA[]
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine(name = "myMachineBeanName")
|
||||
public @interface WithMyBean {
|
||||
}
|
||||
// end::snippetAAA[]
|
||||
|
||||
// tag::snippetB[]
|
||||
@WithStateMachine
|
||||
public class Bean3 {
|
||||
|
||||
@@ -143,6 +143,18 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
assertThat(bean1.onMethod9Latch.await(2, TimeUnit.SECONDS), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMetaAnnotation1() throws Exception {
|
||||
context.register(Config1.class, BeanConfig2.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
Bean2 bean2 = context.getBean(Bean2.class);
|
||||
machine.start();
|
||||
assertThat(bean2.onMethod0Latch.await(2, TimeUnit.SECONDS), is(true));
|
||||
}
|
||||
|
||||
@WithStateMachine
|
||||
static class Bean1 {
|
||||
|
||||
@@ -220,6 +232,23 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine
|
||||
public @interface WithStateMachineMeta1 {
|
||||
}
|
||||
|
||||
@WithStateMachineMeta1
|
||||
static class Bean2 {
|
||||
|
||||
CountDownLatch onMethod0Latch = new CountDownLatch(1);
|
||||
|
||||
@OnTransition(target = "S1")
|
||||
public void method0() {
|
||||
onMethod0Latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig1 {
|
||||
|
||||
@@ -230,6 +259,16 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig2 {
|
||||
|
||||
@Bean
|
||||
public Bean2 bean2() {
|
||||
return new Bean2();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@OnTransition
|
||||
|
||||
Reference in New Issue
Block a user