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-core'
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
testImplementation(testFixtures(project(':spring-statemachine-core')))
|
||||
testImplementation 'io.projectreactor:reactor-test'
|
||||
testImplementation 'org.assertj:assertj-core'
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.lang.annotation.Target;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
@@ -37,7 +38,7 @@ import org.springframework.statemachine.guard.Guard;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Configuration
|
||||
@SpringBootApplication(scanBasePackages = "demo")
|
||||
public class Application {
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -92,7 +92,7 @@ public class CdPlayer {
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
public String getLdcStatus() {
|
||||
public String getLcdStatus() {
|
||||
return cdStatus + " " + trackStatus;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,15 @@ package demo.cdplayer;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import demo.AbstractStateMachineCommands;
|
||||
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
|
||||
public class CdPlayerCommands {
|
||||
@Configuration
|
||||
public class CdPlayerCommands extends AbstractStateMachineCommands<Application.States, Application.Events> {
|
||||
|
||||
@Autowired
|
||||
private CdPlayer cdPlayer;
|
||||
@@ -31,67 +34,119 @@ public class CdPlayerCommands {
|
||||
@Autowired
|
||||
private Library library;
|
||||
|
||||
@Command(command = "cd lcd", description = "Prints CD player lcd info")
|
||||
public String lcd() {
|
||||
return cdPlayer.getLdcStatus();
|
||||
}
|
||||
|
||||
@Command(command = "cd library", description = "List user CD library")
|
||||
public String library() {
|
||||
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
|
||||
StringBuilder buf = new StringBuilder();
|
||||
int i1 = 0;
|
||||
for (Cd cd : library.getCollection()) {
|
||||
buf.append(i1++ + ": " + cd.getName() + "\n");
|
||||
int i2 = 0;
|
||||
for (Track track : cd.getTracks()) {
|
||||
buf.append(" " + i2++ + ": " + track.getName() + " " + format.format(new Date(track.getLength()*1000)) + "\n");
|
||||
@Bean
|
||||
public Command lcd() {
|
||||
return new BasicCommand("lcd", "Prints CD player lcd info") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
return cdPlayer.getLcdStatus();
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd load", description = "Load CD into player")
|
||||
public String load(@Option(longNames = {"", "index"}) int index) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
try {
|
||||
Cd cd = library.getCollection().get(index);
|
||||
cdPlayer.load(cd);
|
||||
buf.append("Loading cd " + cd);
|
||||
} catch (Exception e) {
|
||||
buf.append("Cd with index " + index + " not found, check library");
|
||||
}
|
||||
return buf.toString();
|
||||
@Bean
|
||||
public Command list() {
|
||||
return new BasicCommand("list", "List user CD library") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
|
||||
StringBuilder buf = new StringBuilder();
|
||||
int i1 = 0;
|
||||
for (Cd cd : library.getCollection()) {
|
||||
buf.append(i1++ + ": " + cd.getName() + "\n");
|
||||
int i2 = 0;
|
||||
for (Track track : cd.getTracks()) {
|
||||
buf.append(" " + i2++ + ": " + track.getName() + " " + format.format(new Date(track.getLength()*1000)) + "\n");
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd play", description = "Press player play button")
|
||||
public void play() {
|
||||
cdPlayer.play();
|
||||
@Bean
|
||||
public Command load() {
|
||||
return new BasicCommand("load [index]", "Load CD [index] into player") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
int index = Integer.parseInt(args[0]);
|
||||
try {
|
||||
Cd cd = library.getCollection().get(index);
|
||||
cdPlayer.load(cd);
|
||||
buf.append("Loading cd " + cd);
|
||||
} catch (Exception e) {
|
||||
buf.append("Cd with index " + index + " not found, check library");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd stop", description = "Press player stop button")
|
||||
public void stop() {
|
||||
cdPlayer.stop();
|
||||
@Bean
|
||||
public Command play() {
|
||||
return new BasicCommand("play", "Press player play button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.play();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd pause", description = "Press player pause button")
|
||||
public void pause() {
|
||||
cdPlayer.pause();
|
||||
@Bean
|
||||
public Command stop() {
|
||||
return new BasicCommand("stop", "Press player stop button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.stop();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd eject", description = "Press player eject button")
|
||||
public void eject() {
|
||||
cdPlayer.eject();
|
||||
@Bean
|
||||
public Command pause() {
|
||||
return new BasicCommand("pause", "Press player pause button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.pause();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd forward", description = "Press player forward button")
|
||||
public void forward() {
|
||||
cdPlayer.forward();
|
||||
@Bean
|
||||
public Command eject() {
|
||||
return new BasicCommand("eject", "Press player eject button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.eject();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Command(command = "cd back", description = "Press player back button")
|
||||
public void back() {
|
||||
cdPlayer.back();
|
||||
@Bean
|
||||
public Command forward() {
|
||||
return new BasicCommand("forward", "Press player forward button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.forward();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Command back() {
|
||||
return new BasicCommand("back", "Press player back button") {
|
||||
@Override
|
||||
public String execute(String[] args) {
|
||||
cdPlayer.back();
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,24 +15,32 @@
|
||||
*/
|
||||
package demo.cdplayer;
|
||||
|
||||
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.cdplayer.Application.Events;
|
||||
import demo.cdplayer.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("sm 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";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,389 +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.cdplayer;
|
||||
|
||||
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.StateContext;
|
||||
import org.springframework.statemachine.StateContext.Stage;
|
||||
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.TransitionKind;
|
||||
|
||||
import demo.CommonConfiguration;
|
||||
import demo.cdplayer.Application.Events;
|
||||
import demo.cdplayer.Application.States;
|
||||
|
||||
public class CdPlayerTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
private StateMachine<States,Events> machine;
|
||||
|
||||
private CdPlayer player;
|
||||
|
||||
private Library library;
|
||||
|
||||
private TestListener listener;
|
||||
|
||||
@Test
|
||||
public void testInitialState() throws InterruptedException {
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.IDLE, States.CLOSED);
|
||||
assertLcdStatusStartsWith("No CD");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEjectTwice() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.IDLE, States.OPEN);
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.IDLE, States.CLOSED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayWithCdLoaded() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(1, 0, 0);
|
||||
player.load(library.getCollection().get(0));
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(2, 0, 0);
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.BUSY, States.PLAYING);
|
||||
assertLcdStatusContains("cd1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayWithCdLoadedDeckOpen() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(3, 0, 0);
|
||||
player.load(library.getCollection().get(0));
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(3);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.BUSY, States.PLAYING);
|
||||
assertLcdStatusContains("cd1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayWithNoCdLoaded() throws Exception {
|
||||
listener.reset(0, 0, 0);
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isZero();
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.IDLE, States.CLOSED);
|
||||
assertLcdStatusStartsWith("No CD");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayLcdTimeChanges() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(1, 0, 0);
|
||||
player.load(library.getCollection().get(0));
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(2, 0, 0);
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.BUSY, States.PLAYING);
|
||||
assertLcdStatusContains("cd1");
|
||||
|
||||
listener.reset(0, 0, 0, 0, 1);
|
||||
assertThat(listener.transitionTimerLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(1);
|
||||
assertLcdStatusContains("00:01");
|
||||
|
||||
listener.reset(0, 0, 0, 0, 1);
|
||||
assertThat(listener.transitionTimerLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertLcdStatusContains("00:02");
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(1);
|
||||
|
||||
listener.reset(0, 0, 0, 0, 2);
|
||||
assertThat(listener.transitionTimerLatch.await(4, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(2);
|
||||
// ok we have some timing problems with
|
||||
// this test, so for now just check it's
|
||||
// not previous
|
||||
assertLcdStatusNotContains("00:02");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayPause() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(1, 0, 0);
|
||||
player.load(library.getCollection().get(0));
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(2, 0, 0, 0, 1);
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(listener.transitionTimerLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(1);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.BUSY, States.PLAYING);
|
||||
assertLcdStatusContains("cd1");
|
||||
assertLcdStatusContains("00:01");
|
||||
|
||||
listener.reset(0, 0, 0, 1, 1);
|
||||
assertThat(listener.transitionTimerLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertLcdStatusContains("00:02");
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(1);
|
||||
|
||||
listener.reset(1, 0, 0, 0);
|
||||
player.pause();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
assertLcdStatusContains("00:02");
|
||||
|
||||
listener.reset(1, 0, 0, 1);
|
||||
player.pause();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
assertThat(listener.transitionLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
|
||||
listener.reset(0, 0, 0, 2, 2);
|
||||
assertThat(listener.transitionTimerLatch.await(2100, TimeUnit.MILLISECONDS)).isTrue();
|
||||
assertThat(listener.transitionTimerCount).isEqualTo(2);
|
||||
assertLcdStatusNotContains("00:02");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayStop() throws Exception {
|
||||
listener.reset(1, 0, 0);
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(1, 0, 0);
|
||||
player.load(library.getCollection().get(0));
|
||||
player.eject();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(1);
|
||||
|
||||
listener.reset(2, 0, 0);
|
||||
player.play();
|
||||
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.BUSY, States.PLAYING);
|
||||
|
||||
listener.reset(2, 0, 0);
|
||||
player.stop();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertLcdStatusIs("cd1 ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayDeckOpenNoCd() throws Exception {
|
||||
listener.reset(2, 0, 0);
|
||||
player.eject();
|
||||
player.play();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(listener.stateChangedCount).isEqualTo(2);
|
||||
assertThat(machine.getState().getIds()).containsExactly(States.IDLE, States.CLOSED);
|
||||
}
|
||||
|
||||
private void assertLcdStatusIs(String text) {
|
||||
assertThat(player.getLdcStatus()).isEqualTo(text);
|
||||
}
|
||||
|
||||
private void assertLcdStatusStartsWith(String text) {
|
||||
assertThat(player.getLdcStatus()).startsWith(text);
|
||||
}
|
||||
|
||||
private void assertLcdStatusContains(String text) {
|
||||
assertThat(player.getLdcStatus()).contains(text);
|
||||
}
|
||||
|
||||
private void assertLcdStatusNotContains(String text) {
|
||||
assertThat(player.getLdcStatus()).doesNotContain(text);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@BeforeEach
|
||||
public void setup() throws Exception {
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(CommonConfiguration.class, Application.class, TestConfig.class);
|
||||
context.refresh();
|
||||
machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
player = context.getBean(CdPlayer.class);
|
||||
library = context.getBean(Library.class);
|
||||
listener = context.getBean(TestListener.class);
|
||||
doStartAndAssert(machine);
|
||||
assertThat(listener.stateMachineStartedLatch.await(2, TimeUnit.SECONDS)).isTrue();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void clean() {
|
||||
doStopAndAssert(machine);
|
||||
context.close();
|
||||
context = null;
|
||||
machine = null;
|
||||
player = null;
|
||||
library = null;
|
||||
listener = null;
|
||||
}
|
||||
|
||||
static class TestConfig {
|
||||
|
||||
@Autowired
|
||||
private StateMachine<States,Events> machine;
|
||||
|
||||
@Bean
|
||||
public StateMachineListener<States, Events> stateMachineListener() {
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
return listener;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Library library() {
|
||||
// override library to make it easier to test
|
||||
Track cd1track1 = new Track("cd1track1", 30);
|
||||
Track cd1track2 = new Track("cd1track2", 30);
|
||||
Cd cd1 = new Cd("cd1", new Track[]{cd1track1,cd1track2});
|
||||
Track cd2track1 = new Track("cd2track1", 30);
|
||||
Track cd2track2 = new Track("cd2track2", 30);
|
||||
Cd cd2 = new Cd("cd2", new Track[]{cd2track1,cd2track2});
|
||||
return new Library(new Cd[]{cd1,cd2});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestListener extends StateMachineListenerAdapter<States, Events> {
|
||||
|
||||
volatile CountDownLatch stateMachineStartedLatch = new CountDownLatch(1);
|
||||
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 CountDownLatch transitionTimerLatch = new CountDownLatch(0);
|
||||
volatile int stateChangedCount = 0;
|
||||
volatile int transitionCount = 0;
|
||||
volatile int transitionTimerCount = 0;
|
||||
List<State<States, Events>> statesEntered = new ArrayList<State<States,Events>>();
|
||||
List<State<States, Events>> statesExited = new ArrayList<State<States,Events>>();
|
||||
|
||||
@Override
|
||||
public void stateMachineStarted(StateMachine<States, Events> stateMachine) {
|
||||
stateMachineStartedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(State<States, Events> from, State<States, Events> to) {
|
||||
stateChangedCount++;
|
||||
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 stateContext(StateContext<States, Events> stateContext) {
|
||||
if (stateContext.getStage() == Stage.TRANSITION_END) {
|
||||
if (stateContext.getTransition().getKind() == TransitionKind.INTERNAL
|
||||
&& stateContext.getEvent() == null) {
|
||||
transitionTimerCount++;
|
||||
transitionTimerLatch.countDown();
|
||||
} else {
|
||||
transitionCount++;
|
||||
transitionLatch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
reset(c1, c2, c3, c4, 0);
|
||||
}
|
||||
|
||||
public void reset(int c1, int c2, int c3, int c4, int c5) {
|
||||
stateChangedLatch = new CountDownLatch(c1);
|
||||
stateEnteredLatch = new CountDownLatch(c2);
|
||||
stateExitedLatch = new CountDownLatch(c3);
|
||||
transitionLatch = new CountDownLatch(c4);
|
||||
transitionTimerLatch = new CountDownLatch(c5);
|
||||
stateChangedCount = 0;
|
||||
transitionCount = 0;
|
||||
transitionTimerCount = 0;
|
||||
statesEntered.clear();
|
||||
statesExited.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<Configuration>
|
||||
<Appenders>
|
||||
<Console name="STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{ABSOLUTE} %5p %t %c{2} [%t] - %m%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.springframework.statemachine" level="debug"/>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="STDOUT"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user