Support context events with StateMachineBuilder

- Fix so that we can annotate @Configuration class with
  @EnableStateMachine assuming config class is not extended
  from StateMachineConfigurer(aka config adapter).
- Allows to create machine via a builder as @Bean still properly
  registring rest of a facilities via @EnableStateMachine.
- Polish other tests by removing registering StateMachineEventPublisherConfiguration
  as it's added via @EnableStateMachine.
- Change AbstractImportingAnnotationConfiguration so that it will not try
  to register bean if implementation returned null definition. Meaning
  StateMachineConfiguration detected that @EnableStateMachine was placed
  on a non-adapter class.
- Fixes #120
This commit is contained in:
Janne Valkealahti
2015-11-14 16:11:24 +00:00
parent 19af615698
commit 2b1d06e097
8 changed files with 129 additions and 15 deletions

View File

@@ -80,6 +80,11 @@ public abstract class AbstractImportingAnnotationConfiguration<B extends Annotat
throw new RuntimeException("Error with onConfigurers", e);
}
// implementation didn't return definition so don't continue registration
if (beanDefinition == null) {
return;
}
if (ObjectUtils.isEmpty(names)) {
// ok, name(s) not given, generate one
names = new String[] { beanNameGenerator.generateBeanName(beanDefinition, registry) };

View File

@@ -34,6 +34,7 @@ import org.springframework.statemachine.config.StateMachineConfig;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfig;
import org.springframework.statemachine.config.builders.StateMachineConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStates;
import org.springframework.statemachine.config.builders.StateMachineTransitions;
import org.springframework.statemachine.config.common.annotation.AbstractImportingAnnotationConfiguration;
@@ -59,6 +60,16 @@ public class StateMachineConfiguration<S, E> extends
@Override
protected BeanDefinition buildBeanDefinition(AnnotationMetadata importingClassMetadata,
Class<? extends Annotation> namedAnnotation) throws Exception {
String enableStateMachineEnclosingClassName = importingClassMetadata.getClassName();
Class<?> enableStateMachineEnclosingClass = ClassUtils.forName(enableStateMachineEnclosingClassName,
getClass().getClassLoader());
// return null if it looks like @EnableStateMachine was annotated with class
// not extending StateMachineConfigurer.
if (!ClassUtils.isAssignable(StateMachineConfigurer.class, enableStateMachineEnclosingClass)) {
return null;
}
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.rootBeanDefinition(StateMachineDelegatingFactoryBean.class);
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(

View File

@@ -38,7 +38,6 @@ import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.event.StateMachineEventPublisherConfiguration;
import org.springframework.statemachine.region.Region;
import org.springframework.statemachine.state.DefaultPseudoState;
import org.springframework.statemachine.state.EnumState;
@@ -227,7 +226,7 @@ public class RegionMachineTests extends AbstractStateMachineTests {
@Test
public void testMultiRegion() throws Exception {
context.register(StateMachineEventPublisherConfiguration.class, Config1.class);
context.register(Config1.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
@@ -259,7 +258,7 @@ public class RegionMachineTests extends AbstractStateMachineTests {
@SuppressWarnings("unchecked")
@Test
public void testRegionsInNestedState() throws Exception {
context.register(StateMachineEventPublisherConfiguration.class, Config2.class);
context.register(Config2.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
@@ -279,7 +278,7 @@ public class RegionMachineTests extends AbstractStateMachineTests {
@Test
public void testParallelRegionExecution() throws Exception {
context.register(StateMachineEventPublisherConfiguration.class, Config3.class, BaseConfig2.class);
context.register(Config3.class, BaseConfig2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
@@ -319,7 +318,7 @@ public class RegionMachineTests extends AbstractStateMachineTests {
@Test
public void testParallelRegionExecutionInInitialState() throws Exception {
context.register(StateMachineEventPublisherConfiguration.class, Config4.class, BaseConfig2.class);
context.register(Config4.class, BaseConfig2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")

View File

@@ -36,7 +36,6 @@ import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.event.StateMachineEventPublisherConfiguration;
import org.springframework.statemachine.state.DefaultPseudoState;
import org.springframework.statemachine.state.EnumState;
import org.springframework.statemachine.state.PseudoState;
@@ -382,7 +381,7 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
@Test
public void testMixedStates() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config2.class);
context.register(BaseConfig.class, Config2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
@@ -396,7 +395,7 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
@Test
public void testStateChangeWithinMachine() {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config3.class);
context.register(BaseConfig.class, Config3.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")

View File

@@ -150,7 +150,7 @@ public class ConfigurationTests extends AbstractStateMachineTests {
assertThat(((List<?>)o3).size(), is(2));
}
@Test(expected = BeanCreationException.class)
@Test
public void testEnableStateMachineNoAdapter() {
context.register(Config12.class);
context.refresh();

View File

@@ -29,9 +29,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.ObjectStateMachine;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
@@ -51,7 +54,7 @@ public class ContextEventTests extends AbstractStateMachineTests {
@SuppressWarnings("unchecked")
@Test
public void contextEventsEnabled() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config.class, Config1.class);
context.register(BaseConfig.class, Config.class, Config1.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
@@ -65,7 +68,7 @@ public class ContextEventTests extends AbstractStateMachineTests {
@SuppressWarnings("unchecked")
@Test
public void contextEventsDisabled() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config.class, Config2.class);
context.register(BaseConfig.class, Config.class, Config2.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
@@ -76,6 +79,34 @@ public class ContextEventTests extends AbstractStateMachineTests {
assertThat(listener.count, is(0));
}
@SuppressWarnings("unchecked")
@Test
public void contextEventsWithManualBuilder() throws Exception {
context.register(BaseConfig.class, Config.class, Config3.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
machine.start();
machine.sendEvent(TestEvents.E1);
StateMachineApplicationEventListener listener = context.getBean(StateMachineApplicationEventListener.class);
listener.latch.await(1, TimeUnit.SECONDS);
assertThat(listener.count, greaterThan(1));
}
@SuppressWarnings("unchecked")
@Test
public void contextEventsWithManualBuilderExternalConfigClass() throws Exception {
context.register(BaseConfig.class, Config.class, ExternalConfig.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
machine.start();
machine.sendEvent(TestEvents.E1);
StateMachineApplicationEventListener listener = context.getBean(StateMachineApplicationEventListener.class);
listener.latch.await(1, TimeUnit.SECONDS);
assertThat(listener.count, greaterThan(1));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -124,6 +155,28 @@ public class ContextEventTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
public static class Config3 {
@Bean
public StateMachine<TestStates, TestEvents> stateMachine() throws Exception {
Builder<TestStates, TestEvents> builder = StateMachineBuilder.builder();
builder.configureStates()
.withStates()
.initial(TestStates.S1)
.state(TestStates.S2);
builder.configureTransitions()
.withExternal()
.source(TestStates.S1)
.target(TestStates.S2)
.event(TestEvents.E1);
return builder.build();
}
}
@Configuration
static class Config {

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.event;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests.TestEvents;
import org.springframework.statemachine.AbstractStateMachineTests.TestStates;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
@Configuration
@EnableStateMachine
public class ExternalConfig {
@Bean
public StateMachine<TestStates, TestEvents> stateMachine() throws Exception {
Builder<TestStates, TestEvents> builder = StateMachineBuilder.builder();
builder.configureStates()
.withStates()
.initial(TestStates.S1)
.state(TestStates.S2);
builder.configureTransitions()
.withExternal()
.source(TestStates.S1)
.target(TestStates.S2)
.event(TestEvents.E1);
return builder.build();
}
}

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.statemachine.event;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
@@ -57,7 +57,7 @@ public class StateMachineEventTests extends AbstractStateMachineTests {
@Test
public void testContextEvents() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config1.class);
context.register(BaseConfig.class, Config1.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
TestEventListener listener = context.getBean(TestEventListener.class);
@@ -80,7 +80,7 @@ public class StateMachineEventTests extends AbstractStateMachineTests {
@Test
public void testEventNotAccepted() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config1.class);
context.register(BaseConfig.class, Config1.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
TestEventListener eventListener = context.getBean(TestEventListener.class);
@@ -110,7 +110,7 @@ public class StateMachineEventTests extends AbstractStateMachineTests {
@Test
public void testSubmachineHandlesEvent() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config2.class);
context.register(BaseConfig.class, Config2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")