diff --git a/build.gradle b/build.gradle index fb1ab9c5..6bc43211 100644 --- a/build.gradle +++ b/build.gradle @@ -249,6 +249,19 @@ project('spring-statemachine-uml') { } } +project('spring-statemachine-build-tests') { + description = "Spring State Machine Build Tests" + + dependencies { + testCompile project(":spring-statemachine-uml") + testCompile project(":spring-statemachine-test") + testCompile "org.springframework:spring-test:$springVersion" + testCompile "org.hamcrest:hamcrest-core:$hamcrestVersion" + testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion" + testCompile "junit:junit:$junitVersion" + } +} + configure(recipeProjects()) { dependencies { compile project(":spring-statemachine-recipes-common") diff --git a/settings.gradle b/settings.gradle index f37c6fb2..295c042f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,7 +7,7 @@ include 'spring-statemachine-zookeeper' include 'spring-statemachine-redis' include 'spring-statemachine-cluster' include 'spring-statemachine-uml' - +include 'spring-statemachine-build-tests' include 'spring-statemachine-recipes' include 'spring-statemachine-samples' diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java new file mode 100644 index 00000000..60d388f6 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016 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.buildtests; + +import org.junit.After; +import org.junit.Before; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public abstract class AbstractBuildTests { + + protected AnnotationConfigApplicationContext context; + + @Before + public void setup() { + context = buildContext(); + } + + @After + public void clean() { + if (context != null) { + context.close(); + } + context = null; + } + + /** + * Builds the context. + * + * @return the annotation config application context + */ + protected AnnotationConfigApplicationContext buildContext() { + return null; + } +} diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java new file mode 100644 index 00000000..8d301d81 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2016 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.buildtests; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.statemachine.StateMachine; +import org.springframework.statemachine.config.EnableStateMachine; +import org.springframework.statemachine.config.StateMachineConfigurerAdapter; +import org.springframework.statemachine.config.builders.StateMachineModelConfigurer; +import org.springframework.statemachine.config.model.StateMachineModelFactory; +import org.springframework.statemachine.guard.Guard; +import org.springframework.statemachine.test.StateMachineTestPlan; +import org.springframework.statemachine.test.StateMachineTestPlanBuilder; +import org.springframework.statemachine.uml.UmlStateMachineModelFactory; + +public class ChoiceExitTests extends AbstractBuildTests { + + @Test + @SuppressWarnings("unchecked") + public void testChoiceExitViaError() throws Exception { + context.register(Config1.class); + context.refresh(); + StateMachine stateMachine = context.getBean(StateMachine.class); + + StateMachineTestPlan plan = + StateMachineTestPlanBuilder.builder() + .stateMachine(stateMachine) + .step().expectState("READY").and() + .step().sendEvent("E1").expectStateChanged(2).expectStates("DOSTUFF", "START").and() + .step().sendEvent("E2").expectStateChanged(2).expectState("READY").and() + .build(); + plan.test(); + } + + @Test + @SuppressWarnings("unchecked") + public void testChoiceExitNotViaError() throws Exception { + context.register(Config2.class); + context.refresh(); + StateMachine stateMachine = context.getBean(StateMachine.class); + + StateMachineTestPlan plan = + StateMachineTestPlanBuilder.builder() + .stateMachine(stateMachine) + .step().expectState("READY").and() + .step().sendEvent("E1").expectStateChanged(2).expectStates("DOSTUFF", "START").and() + .step().sendEvent("E2").expectStateChanged(1).expectState("READY").and() + .build(); + plan.test(); + } + + @Configuration + @EnableStateMachine + public static class Config1 extends StateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineModelConfigurer model) throws Exception { + model + .withModel() + .factory(modelFactory()); + } + + @Bean + public StateMachineModelFactory modelFactory() { + return new UmlStateMachineModelFactory("classpath:org/springframework/statemachine/buildtests/choice-exit.uml"); + } + + @Bean + public Guard exitGuard() { + return (context) -> { + return true; + }; + } + } + + @Configuration + @EnableStateMachine + public static class Config2 extends StateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineModelConfigurer model) throws Exception { + model + .withModel() + .factory(modelFactory()); + } + + @Bean + public StateMachineModelFactory modelFactory() { + return new UmlStateMachineModelFactory("classpath:org/springframework/statemachine/buildtests/choice-exit.uml"); + } + + @Bean + public Guard exitGuard() { + return (context) -> { + return false; + }; + } + } + + @Override + protected AnnotationConfigApplicationContext buildContext() { + return new AnnotationConfigApplicationContext(); + } +} diff --git a/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.di b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.di new file mode 100644 index 00000000..bf9abab3 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.di @@ -0,0 +1,2 @@ + + diff --git a/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.notation b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.notation new file mode 100644 index 00000000..ad15bd67 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.notation @@ -0,0 +1,236 @@ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.uml b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.uml new file mode 100644 index 00000000..ca831a09 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/resources/org/springframework/statemachine/buildtests/choice-exit.uml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + bean + exitGuard + + + + + + + + + + + + + + + +