Remove dependency to Spring Shell
This commit updates the samples to use Java's standard CLI utilities instead of Spring Shell. Resolves #1184
This commit is contained in:
@@ -8,7 +8,7 @@ dependencies {
|
||||
management platform(project(":spring-statemachine-platform"))
|
||||
implementation project(':spring-statemachine-samples-common')
|
||||
implementation project(':spring-statemachine-core')
|
||||
implementation 'org.springframework.shell:spring-shell-starter'
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
testImplementation(testFixtures(project(':spring-statemachine-core')))
|
||||
testImplementation (project(':spring-statemachine-test'))
|
||||
testImplementation 'org.hamcrest:hamcrest-core'
|
||||
|
||||
@@ -15,66 +15,12 @@
|
||||
*/
|
||||
package demo.turnstile;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.shell.command.annotation.CommandScan;
|
||||
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;
|
||||
|
||||
@CommandScan
|
||||
@SpringBootApplication(scanBasePackages = "demo")
|
||||
public class Application {
|
||||
|
||||
//tag::snippetA[]
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
static class StateMachineConfig
|
||||
extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<States, Events> states)
|
||||
throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(States.LOCKED)
|
||||
.states(EnumSet.allOf(States.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
|
||||
throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(States.LOCKED)
|
||||
.target(States.UNLOCKED)
|
||||
.event(Events.COIN)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.UNLOCKED)
|
||||
.target(States.LOCKED)
|
||||
.event(Events.PUSH);
|
||||
}
|
||||
|
||||
}
|
||||
//end::snippetA[]
|
||||
|
||||
//tag::snippetB[]
|
||||
public enum States {
|
||||
LOCKED, UNLOCKED
|
||||
}
|
||||
//end::snippetB[]
|
||||
|
||||
//tag::snippetC[]
|
||||
public enum Events {
|
||||
COIN, PUSH
|
||||
}
|
||||
//end::snippetC[]
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package demo.turnstile;
|
||||
|
||||
//tag::snippetC[]
|
||||
public enum Events {
|
||||
COIN, PUSH
|
||||
}
|
||||
//end::snippetC[]
|
||||
@@ -15,24 +15,30 @@
|
||||
*/
|
||||
package demo.turnstile;
|
||||
|
||||
import demo.BasicCommand;
|
||||
import demo.Command;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.shell.command.annotation.Command;
|
||||
import org.springframework.shell.command.annotation.Option;
|
||||
|
||||
import demo.AbstractStateMachineCommands;
|
||||
import demo.turnstile.Application.Events;
|
||||
import demo.turnstile.Application.States;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Command
|
||||
@Configuration
|
||||
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
|
||||
|
||||
@Command(command = "sm event", description = "Sends an event to a state machine")
|
||||
public String event(@Option(longNames = { "", "event" }, required = true, description = "The event") final Events event) {
|
||||
getStateMachine()
|
||||
.sendEvent(Mono.just(MessageBuilder
|
||||
.withPayload(event).build()))
|
||||
.subscribe();
|
||||
return "Event " + event + " send";
|
||||
@Bean
|
||||
public Command event() {
|
||||
return new BasicCommand("event", "Sends an event to a state machine") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
Events event = Events.valueOf(args[0]);
|
||||
getStateMachine()
|
||||
.sendEvent(Mono.just(MessageBuilder
|
||||
.withPayload(event).build()))
|
||||
.subscribe();
|
||||
return "Event " + event + " sent";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package demo.turnstile;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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 java.util.EnumSet;
|
||||
|
||||
//tag::snippetA[]
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public class StateMachineConfiguration
|
||||
extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<States, Events> states)
|
||||
throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(States.LOCKED)
|
||||
.states(EnumSet.allOf(States.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
|
||||
throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(States.LOCKED)
|
||||
.target(States.UNLOCKED)
|
||||
.event(Events.COIN)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.UNLOCKED)
|
||||
.target(States.LOCKED)
|
||||
.event(Events.PUSH);
|
||||
}
|
||||
|
||||
}
|
||||
//end::snippetA[]
|
||||
@@ -0,0 +1,7 @@
|
||||
package demo.turnstile;
|
||||
|
||||
//tag::snippetB[]
|
||||
public enum States {
|
||||
LOCKED, UNLOCKED
|
||||
}
|
||||
//end::snippetB[]
|
||||
@@ -1 +1 @@
|
||||
spring.shell.interactive.enabled=true
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015-2020 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
|
||||
*
|
||||
* https://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 demo.turnstile;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
|
||||
import static org.springframework.statemachine.TestUtils.doStopAndAssert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.statemachine.ObjectStateMachine;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.StateMachineSystemConstants;
|
||||
import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.state.State;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
import demo.CommonConfiguration;
|
||||
import demo.turnstile.Application.Events;
|
||||
import demo.turnstile.Application.States;
|
||||
|
||||
public class TurnstileTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
private StateMachine<States, Events> machine;
|
||||
|
||||
private TestListener listener;
|
||||
|
||||
private StateMachineCommands commands;
|
||||
|
||||
@Test
|
||||
public void testNotStarted() throws Exception {
|
||||
assertThat(commands.state()).isEqualTo("No state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialState() throws Exception {
|
||||
doStartAndAssert(machine);
|
||||
listener.stateChangedLatch.await(1, TimeUnit.SECONDS);
|
||||
listener.stateEnteredLatch.await(1, TimeUnit.SECONDS);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.LOCKED);
|
||||
assertThat(listener.statesEntered).hasSize(1);
|
||||
assertThat(listener.statesEntered.get(0).getId()).isEqualTo(States.LOCKED);
|
||||
assertThat(listener.statesExited).isEmpty();
|
||||
}
|
||||
|
||||
static class Config {
|
||||
|
||||
@Autowired
|
||||
private StateMachine<States,Events> machine;
|
||||
|
||||
@Bean
|
||||
public StateMachineListener<States, Events> stateMachineListener() {
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
return listener;
|
||||
}
|
||||
}
|
||||
|
||||
static class TestListener extends StateMachineListenerAdapter<States, Events> {
|
||||
|
||||
volatile CountDownLatch stateChangedLatch = new CountDownLatch(1);
|
||||
volatile CountDownLatch stateEnteredLatch = new CountDownLatch(2);
|
||||
volatile CountDownLatch stateExitedLatch = new CountDownLatch(0);
|
||||
volatile CountDownLatch transitionLatch = new CountDownLatch(0);
|
||||
volatile List<Transition<States, Events>> transitions = new ArrayList<Transition<States,Events>>();
|
||||
List<State<States, Events>> statesEntered = new ArrayList<State<States,Events>>();
|
||||
List<State<States, Events>> statesExited = new ArrayList<State<States,Events>>();
|
||||
volatile int transitionCount = 0;
|
||||
|
||||
@Override
|
||||
public void stateChanged(State<States, Events> from, State<States, Events> to) {
|
||||
stateChangedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateEntered(State<States, Events> state) {
|
||||
statesEntered.add(state);
|
||||
stateEnteredLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateExited(State<States, Events> state) {
|
||||
statesExited.add(state);
|
||||
stateExitedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transition(Transition<States, Events> transition) {
|
||||
transitions.add(transition);
|
||||
transitionLatch.countDown();
|
||||
transitionCount++;
|
||||
}
|
||||
|
||||
public void reset(int c1, int c2, int c3) {
|
||||
reset(c1, c2, c3, 0);
|
||||
}
|
||||
|
||||
public void reset(int c1, int c2, int c3, int c4) {
|
||||
stateChangedLatch = new CountDownLatch(c1);
|
||||
stateEnteredLatch = new CountDownLatch(c2);
|
||||
stateExitedLatch = new CountDownLatch(c3);
|
||||
transitionLatch = new CountDownLatch(c4);
|
||||
statesEntered.clear();
|
||||
statesExited.clear();
|
||||
transitionCount = 0;
|
||||
transitions.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(CommonConfiguration.class, Application.class, Config.class);
|
||||
context.refresh();
|
||||
machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
listener = context.getBean(TestListener.class);
|
||||
commands = context.getBean(StateMachineCommands.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void clean() {
|
||||
doStopAndAssert(machine);
|
||||
context.close();
|
||||
context = null;
|
||||
machine = null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user