Package org.springframework.yarn.support.statemachine

Source Code of org.springframework.yarn.support.statemachine.ConfigurationTests$Config1

package org.springframework.yarn.support.statemachine;

import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.EnumSet;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.messaging.MessageHeaders;
import org.springframework.yarn.support.statemachine.action.Action;
import org.springframework.yarn.support.statemachine.config.EnableStateMachine;
import org.springframework.yarn.support.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.yarn.support.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.yarn.support.statemachine.config.builders.StateMachineTransitionConfigurer;

public class ConfigurationTests extends AbstractStateMachineTests {

  @SuppressWarnings({ "unchecked" })
  @Test
  public void testStates() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config1.class);
    assertTrue(ctx.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    EnumStateMachine<TestStates,TestEvents> machine =
        ctx.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
    assertThat(machine, notNullValue());
    TestAction testAction = ctx.getBean("testAction", TestAction.class);
    assertThat(testAction, notNullValue());
    ctx.close();
  }

  private static class TestAction implements Action {

    @Override
    public void execute(MessageHeaders headers) {
    }

  }

  @Configuration
  @EnableStateMachine
  public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {

    @Override
    public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
      states
        .withStates()
          .initial(TestStates.S1)
          .state(TestStates.S1)
          .state(TestStates.S2)
          .state(TestStates.S3)
          .state(TestStates.S4);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
      transitions
        .withExternal()
          .source(TestStates.S1)
          .target(TestStates.S2)
          .event(TestEvents.E1)
          .action(testAction());
    }

    @Bean
    public TestAction testAction() {
      return new TestAction();
    }

    @Bean
    public TaskExecutor taskExecutor() {
      return new SyncTaskExecutor();
    }

  }

  @Configuration
  @EnableStateMachine
  public static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {

    @Override
    public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
      states
        .withStates()
          .initial(TestStates.S1)
          .states(EnumSet.allOf(TestStates.class));
    }

  }

}
TOP

Related Classes of org.springframework.yarn.support.statemachine.ConfigurationTests$Config1

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.