Enhance things around StateMachinePersister

- Polish StateMachinePersister
- Add redis specific RedisStateMachinePersister
- Add more docs.
- Change eventservice sample to use StateMachinePersister.
- Relates #184
- Relates #185
This commit is contained in:
Janne Valkealahti
2016-03-11 18:43:45 +00:00
parent 5cd7d348e9
commit 66fe71e9cc
9 changed files with 333 additions and 36 deletions

View File

@@ -0,0 +1,118 @@
/*
* 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.docs;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import java.util.HashMap;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.persist.StateMachinePersister;
public class DocsConfigurationSampleTests5 extends AbstractStateMachineTests {
@Override
protected AnnotationConfigApplicationContext buildContext() {
return new AnnotationConfigApplicationContext();
}
@SuppressWarnings("unchecked")
@Test
public void testPersist() throws Exception {
context.register(Config1.class, Config2.class);
context.refresh();
// tag::snippetC[]
InMemoryStateMachinePersist stateMachinePersist = new InMemoryStateMachinePersist();
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
StateMachine<String, String> stateMachine1 = context.getBean("machine1", StateMachine.class);
StateMachine<String, String> stateMachine2 = context.getBean("machine2", StateMachine.class);
stateMachine1.start();
stateMachine1.sendEvent("E1");
assertThat(stateMachine1.getState().getIds(), contains("S2"));
persister.persist(stateMachine1, "myid");
persister.restore(stateMachine2, "myid");
assertThat(stateMachine2.getState().getIds(), contains("S2"));
// end::snippetC[]
}
// tag::snippetA[]
@Configuration
@EnableStateMachine(name = "machine1")
static class Config1 extends Config {
}
@Configuration
@EnableStateMachine(name = "machine2")
static class Config2 extends Config {
}
static class Config extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("S1")
.state("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("S1")
.target("S2")
.event("E1");
}
}
// end::snippetA[]
// tag::snippetB[]
static class InMemoryStateMachinePersist implements StateMachinePersist<String, String, String> {
private final HashMap<String, StateMachineContext<String, String>> contexts = new HashMap<>();
@Override
public void write(StateMachineContext<String, String> context, String contextOjb) throws Exception {
contexts.put(contextOjb, context);
}
@Override
public StateMachineContext<String, String> read(String contextOjb) throws Exception {
return contexts.get(contextOjb);
}
}
// end::snippetB[]
}

View File

@@ -56,7 +56,7 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
persister.persist(stateMachine, "xxx");
persister.reset(stateMachine, "xxx");
persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), contains("S2"));
stateMachine.sendEvent("E2");
@@ -77,7 +77,7 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
StateMachinePersister<TestStates, TestEvents, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
persister.persist(stateMachine, "xxx");
persister.reset(stateMachine, "xxx");
persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), contains(TestStates.S2));
stateMachine.sendEvent(TestEvents.E2);
@@ -99,7 +99,7 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
persister.persist(stateMachine, "xxx");
stateMachine.getExtendedState().getVariables().remove("foo");
stateMachine = persister.reset(stateMachine, "xxx");
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getExtendedState().get("foo", String.class), is("bar"));
}
@@ -120,13 +120,13 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
persister.persist(stateMachine, "xxx");
stateMachine = persister.reset(stateMachine, "xxx");
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2", "S22"));
stateMachine.sendEvent("E2");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S3", "S31"));
stateMachine = persister.reset(stateMachine, "xxx");
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2", "S22"));
}
@@ -151,7 +151,7 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
persister.persist(stateMachine, "xxx");
stateMachine = persister.reset(stateMachine, "xxx");
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S12", "S22", "S32"));
stateMachine.sendEvent("E4");
@@ -159,10 +159,43 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
stateMachine.sendEvent("E6");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S13", "S23", "S33"));
stateMachine = persister.reset(stateMachine, "xxx");
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S12", "S22", "S32"));
}
@SuppressWarnings("unchecked")
@Test
public void testSubsInRegions() throws Exception {
context.register(Config51.class, Config52.class);
context.refresh();
InMemoryStateMachinePersist1 stateMachinePersist = new InMemoryStateMachinePersist1();
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
StateMachine<String, String> stateMachine1 = context.getBean("machine1", StateMachine.class);
StateMachine<String, String> stateMachine2 = context.getBean("machine2", StateMachine.class);
stateMachine1.start();
assertThat(stateMachine1.getState().getIds(), containsInAnyOrder("S11", "S111", "S21"));
persister.persist(stateMachine1, "xxx");
stateMachine2 = persister.restore(stateMachine2, "xxx");
assertThat(stateMachine2.getState().getIds(), containsInAnyOrder("S11", "S111", "S21"));
stateMachine1.sendEvent("E1");
assertThat(stateMachine1.getState().getIds(), containsInAnyOrder("S12", "S21"));
persister.persist(stateMachine1, "xxx");
assertThat(stateMachine2.getState().getIds(), containsInAnyOrder("S11", "S111", "S21"));
stateMachine2 = persister.restore(stateMachine2, "xxx");
assertThat(stateMachine2.getState().getIds(), containsInAnyOrder("S12", "S21"));
stateMachine1.sendEvent("E2");
assertThat(stateMachine1.getState().getIds(), containsInAnyOrder("S12", "S22", "S221"));
persister.persist(stateMachine1, "xxx");
assertThat(stateMachine2.getState().getIds(), containsInAnyOrder("S12", "S21"));
stateMachine2 = persister.restore(stateMachine2, "xxx");
assertThat(stateMachine2.getState().getIds(), containsInAnyOrder("S12", "S22", "S221"));
}
@Configuration
@EnableStateMachine
static class Config1 extends StateMachineConfigurerAdapter<String, String> {
@@ -328,6 +361,62 @@ public class StateMachinePersistTests extends AbstractStateMachineTests {
}
}
static class Config5 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("S11")
.state("S11")
.state("S12")
.state("S13")
.and()
.withStates()
.parent("S11")
.initial("S111")
.state("S111")
.state("S112")
.and()
.withStates()
.initial("S21")
.state("S21")
.state("S22")
.state("S23")
.and()
.withStates()
.parent("S22")
.initial("S221")
.state("S221")
.state("S222");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("S11")
.target("S12")
.event("E1")
.and()
.withExternal()
.source("S21")
.target("S22")
.event("E2");
}
}
@Configuration
@EnableStateMachine(name = "machine1")
static class Config51 extends Config5 {
}
@Configuration
@EnableStateMachine(name = "machine2")
static class Config52 extends Config5 {
}
static class InMemoryStateMachinePersist1 implements StateMachinePersist<String, String, String> {
private final HashMap<String, StateMachineContext<String, String>> contexts = new HashMap<>();