Package org.springframework.yarn.support.statemachine

Source Code of org.springframework.yarn.support.statemachine.StateMachineTests$Config

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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.messaging.support.MessageBuilder;
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 StateMachineTests extends AbstractStateMachineTests {

  @Test
  public void test1() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    assertTrue(ctx.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    @SuppressWarnings("unchecked")
    EnumStateMachine<TestStates,TestEvents> machine =
        ctx.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
    assertThat(machine, notNullValue());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "jee1").build());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).setHeader("foo", "jee2").build());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E4).setHeader("foo", "jee2").build());
    ctx.close();
  }

  private static class LoggingAction implements Action {

    private static final Log log = LogFactory.getLog(StateMachineTests.LoggingAction.class);

    private String message;

    public LoggingAction(String message) {
      this.message = message;
    }

    @Override
    public void execute(MessageHeaders headers) {
      log.info("Hello from LoggingAction " + message + " foo=" + headers.get("foo"));
    }

  }

  @Configuration
  @EnableStateMachine
  static class Config 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, TestEvents.E4)
          .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(loggingAction())
          .action(loggingAction())
          .and()
        .withExternal()
          .source(TestStates.S2)
          .target(TestStates.S3)
          .event(TestEvents.E2)
          .action(loggingAction())
          .and()
        .withExternal()
          .source(TestStates.S3)
          .target(TestStates.S4)
          .event(TestEvents.E3)
          .action(loggingAction())
          .and()
        .withExternal()
          .source(TestStates.S4)
          .target(TestStates.S3)
          .event(TestEvents.E4)
          .action(loggingAction());
    }

    @Bean
    public LoggingAction loggingAction() {
      return new LoggingAction("as bean");
    }

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

  }

}
TOP

Related Classes of org.springframework.yarn.support.statemachine.StateMachineTests$Config

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.