Allow user to use machine id

- change original id to uuid.
- This new id is now for user disposal.
- Currently only meant for a top-level machine.
- Can be set via annotation config, model classes or via
  new StateMachineFactory.getStateMachine(String).
- Is persisted with DefaultStateMachinePersister and
  StateMachineContext.
- Related new tests in ConfigurationTests and StateMachinePersistTests3.
- Fixes #186
This commit is contained in:
Janne Valkealahti
2016-03-24 07:54:36 +00:00
parent a82ff5b936
commit 1bf65760d5
20 changed files with 375 additions and 31 deletions

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import org.springframework.messaging.Message;
@@ -173,6 +174,11 @@ public class StateMachineAccessTests {
public void setInitialEnabled(boolean enabled) {
}
@Override
public UUID getUuid() {
return null;
}
@Override
public String getId() {
return null;

View File

@@ -242,6 +242,31 @@ public class ConfigurationTests extends AbstractStateMachineTests {
assertThat(bfFromMachine, sameInstance(Config16.beanFactory));
}
@Test
public void testMachineIdFlat() {
context.register(Config17.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> stateMachine = context.getBean(StateMachine.class);
assertThat(stateMachine, notNullValue());
assertThat(stateMachine.getId(), is("testid1"));
}
@Test
public void testMachineIdViaFactory() {
context.register(Config18.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachineFactory<String, String> stateMachineFactory = context.getBean(StateMachineFactory.class);
StateMachine<String,String> stateMachine = stateMachineFactory.getStateMachine();
assertThat(stateMachine, notNullValue());
assertThat(stateMachine.getId(), is("testid1"));
stateMachine = stateMachineFactory.getStateMachine("testid2");
assertThat(stateMachine, notNullValue());
assertThat(stateMachine.getId(), is("testid2"));
}
@Configuration
@EnableStateMachine
public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -731,4 +756,62 @@ public class ConfigurationTests extends AbstractStateMachineTests {
}
@Configuration
public static class Config17 {
public static BeanFactory beanFactory = new DefaultListableBeanFactory();
@Bean
StateMachine<String, String> stateMachine() throws Exception {
Builder<String, String> builder = StateMachineBuilder.builder();
builder.configureConfiguration()
.withConfiguration()
.machineId("testid1")
.autoStartup(false)
.beanFactory(beanFactory);
builder.configureStates()
.withStates()
.initial("S1").state("S2");
builder.configureTransitions()
.withExternal()
.source("S1").target("S2").event("E1")
.and()
.withExternal()
.source("S2").target("S1").event("E2");
StateMachine<String, String> stateMachine = builder.build();
return stateMachine;
}
}
@Configuration
@EnableStateMachineFactory
public static class Config18 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.machineId("testid1")
.autoStartup(true);
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("S1")
.target("S2")
.event("E1");
}
}
}

View File

@@ -59,7 +59,7 @@ public class StateMachineModelTests {
ConfigurationData<String, String> configurationData = new ConfigurationData<>(beanFactory, taskExecutor, taskScheduler, autoStart,
ensemble, listeners, securityEnabled, transitionSecurityAccessDecisionManager, eventSecurityAccessDecisionManager,
eventSecurityRule, transitionSecurityRule, verifierEnabled, verifier);
eventSecurityRule, transitionSecurityRule, verifierEnabled, verifier, null);
Collection<StateData<String, String>> stateData = new ArrayList<>();
StateData<String, String> stateData1 = new StateData<String, String>(null, null, "S1", null, null, null);

View File

@@ -0,0 +1,112 @@
/*
* 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.persist;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
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.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
public class StateMachinePersistTests3 extends AbstractStateMachineTests {
@Override
protected AnnotationConfigApplicationContext buildContext() {
return new AnnotationConfigApplicationContext();
}
@Test
public void testPersistMachineId() throws Exception {
context.register(Config1.class);
context.refresh();
InMemoryStateMachinePersist1 stateMachinePersist = new InMemoryStateMachinePersist1();
StateMachinePersister<String, String, String> persister = new DefaultStateMachinePersister<>(stateMachinePersist);
@SuppressWarnings("unchecked")
StateMachineFactory<String, String> stateMachineFactory = context.getBean(StateMachineFactory.class);
StateMachine<String,String> stateMachine = stateMachineFactory.getStateMachine("testid2");
assertThat(stateMachine, notNullValue());
assertThat(stateMachine.getId(), is("testid2"));
persister.persist(stateMachine, "xxx");
stateMachine = stateMachineFactory.getStateMachine();
assertThat(stateMachine, notNullValue());
assertThat(stateMachine.getId(), nullValue());
stateMachine = persister.restore(stateMachine, "xxx");
assertThat(stateMachine.getId(), is("testid2"));
}
@Configuration
@EnableStateMachineFactory
public static class Config1 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.autoStartup(true);
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("S1")
.target("S2")
.event("E1");
}
}
static class InMemoryStateMachinePersist1 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);
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.Test;
import org.springframework.expression.ExpressionParser;
@@ -209,6 +210,11 @@ public class StateContextExpressionMethodsTests {
return null;
}
@Override
public UUID getUuid() {
return null;
}
@Override
public String getId() {
return null;