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:
@@ -9,7 +9,6 @@ dependencies {
|
||||
implementation project(':spring-statemachine-samples-common')
|
||||
implementation project(':spring-statemachine-recipes-common')
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'org.springframework.shell:spring-shell-core'
|
||||
implementation ('org.hsqldb:hsqldb')
|
||||
implementation ('org.springframework:spring-jdbc')
|
||||
testImplementation(testFixtures(project(':spring-statemachine-core')))
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
import org.springframework.statemachine.recipes.persist.PersistStateMachineHandler;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(scanBasePackages = "demo")
|
||||
public class Application {
|
||||
|
||||
//tag::snippetA[]
|
||||
|
||||
@@ -15,34 +15,62 @@
|
||||
*/
|
||||
package demo.persist;
|
||||
|
||||
import demo.BasicCommand;
|
||||
import demo.Command;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.shell.command.annotation.Command;
|
||||
import org.springframework.shell.command.annotation.Option;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Command
|
||||
@Configuration
|
||||
public class PersistCommands {
|
||||
|
||||
@Autowired
|
||||
private Persist persist;
|
||||
|
||||
@Command(command = "persist db", description = "List entries from db")
|
||||
public String listDbEntries() {
|
||||
return persist.listDbEntries();
|
||||
@Bean
|
||||
public Command list() {
|
||||
return new BasicCommand("list", "List entries from db") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
return persist.listDbEntries();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "persist process", description = "Process order")
|
||||
public void process(@Option(longNames = {"", "id"}, description = "Order id") int order) {
|
||||
persist.change(order, "PROCESS");
|
||||
@Bean
|
||||
public Command process() {
|
||||
return new BasicCommand("process [orderId]", "Process order with [orderId]") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
int order = Integer.parseInt(args[0]);
|
||||
persist.change(order, "PROCESS");
|
||||
return "Order " + order + " processed";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "persist send", description = "Send order")
|
||||
public void send(@Option(longNames = {"", "id"}, description = "Order id") int order) {
|
||||
persist.change(order, "SEND");
|
||||
@Bean
|
||||
public Command send() {
|
||||
return new BasicCommand("send [orderId]", "Send order with [orderId]") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
int order = Integer.parseInt(args[0]);
|
||||
persist.change(order, "SEND");
|
||||
return "Order " + order + " sent";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "persist deliver", description = "Deliver order")
|
||||
public void deliver(@Option(longNames = {"", "id"}, description = "Order id") int order) {
|
||||
persist.change(order, "DELIVER");
|
||||
@Bean
|
||||
public Command deliver() {
|
||||
return new BasicCommand("deliver [orderId]", "Deliver order with [orderId]") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
int order = Integer.parseInt(args[0]);
|
||||
persist.change(order, "DELIVER");
|
||||
return "Order " + order + " delivered";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,22 +15,30 @@
|
||||
*/
|
||||
package demo.persist;
|
||||
|
||||
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 reactor.core.publisher.Mono;
|
||||
|
||||
@Command
|
||||
@Configuration
|
||||
public class StateMachineCommands extends AbstractStateMachineCommands<String, String> {
|
||||
|
||||
@Command(command = "sm event", description = "Sends an event to a state machine")
|
||||
public String event(@Option(longNames = { "", "event" }, required = true, description = "The event") final String 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) {
|
||||
String event = args[0];
|
||||
getStateMachine()
|
||||
.sendEvent(Mono.just(MessageBuilder
|
||||
.withPayload(event).build()))
|
||||
.subscribe();
|
||||
return "Event " + event + " sent";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
|
||||
<context:component-scan base-package="demo" />
|
||||
</beans>
|
||||
@@ -0,0 +1 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -1,125 +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.persist;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.state.State;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import demo.CommonConfiguration;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
@SpringBootTest(classes = { CommonConfiguration.class, Application.class, StateMachineCommands.class })
|
||||
public class PersistTests {
|
||||
|
||||
@Autowired
|
||||
private StateMachineCommands commands;
|
||||
|
||||
@Autowired
|
||||
private StateMachine<String, String> machine;
|
||||
|
||||
@Autowired
|
||||
private Persist persist;
|
||||
|
||||
@Test
|
||||
public void testNotStarted() throws Exception {
|
||||
assertThat(commands.state()).isEqualTo("No state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialState() throws Exception {
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
doStartAndAssert(machine);
|
||||
assertThat(listener.stateChangedLatch.await(3, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateEnteredLatch.await(3, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(machine.getState().getIds()).containsExactly("PLACED");
|
||||
assertThat(listener.statesEntered).hasSize(1);
|
||||
assertThat(listener.statesEntered.get(0).getId()).isEqualTo("PLACED");
|
||||
assertThat(listener.statesExited).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialDbList() {
|
||||
// dataOrder [id=1, state=PLACED]Order [id=2, state=PROCESSING]Order [id=3, state=SENT]Order [id=4, state=DELIVERED]
|
||||
assertThat(persist.listDbEntries()).contains("PLACED");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate1() {
|
||||
persist.change(1, "PROCESS");
|
||||
assertThat(persist.listDbEntries()).contains("id=1, state=PROCESSING");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate2() {
|
||||
persist.change(2, "SEND");
|
||||
assertThat(persist.listDbEntries()).contains("id=2, state=SENT");
|
||||
}
|
||||
|
||||
private static class TestListener extends StateMachineListenerAdapter<String, String> {
|
||||
|
||||
volatile CountDownLatch stateChangedLatch = new CountDownLatch(1);
|
||||
volatile CountDownLatch stateEnteredLatch = new CountDownLatch(1);
|
||||
volatile CountDownLatch stateExitedLatch = new CountDownLatch(0);
|
||||
volatile CountDownLatch transitionLatch = new CountDownLatch(0);
|
||||
volatile List<Transition<String, String>> transitions = new ArrayList<Transition<String, String>>();
|
||||
List<State<String, String>> statesEntered = new ArrayList<State<String, String>>();
|
||||
List<State<String, String>> statesExited = new ArrayList<State<String, String>>();
|
||||
|
||||
@Override
|
||||
public void stateChanged(State<String, String> from, State<String, String> to) {
|
||||
stateChangedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateEntered(State<String, String> state) {
|
||||
statesEntered.add(state);
|
||||
stateEnteredLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateExited(State<String, String> state) {
|
||||
statesExited.add(state);
|
||||
stateExitedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transition(Transition<String, String> transition) {
|
||||
transitions.add(transition);
|
||||
transitionLatch.countDown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user