<commit_before>package org.headstar.scheelite;

import com.google.common.base.Optional;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/**
 * Created by Per on 2014-01-16.
 */
public class StateMachineTest extends TestBase {

    @Test
    public void testThrowExceptionResolver() {
        // given
        TestEntity e = spy(new TestEntity(StateId.A));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestEventX event = new TestEventX();
        MultipleTransitionsTriggeredResolver<TestEntity, StateId> resolver = spy(new ThrowExceptionResolver<TestEntity, StateId>());

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .withMultipleTransitionsTriggerPolicy(resolver)
                .withTransition(a, b, new TestGuard(true))
                .withTransition(a, b, new TestGuard(true))
                .build();

        // when
        try {
            stateMachine.process(e, event);
            fail("should have thrown");
        } catch(IllegalStateException ex) {
            // expected
            assertThat(ex.getMessage(), containsString("multiple transitions triggered"));
        }

        // then ... exception should be thrown
        verify(a).onEvent(e, event);
    }

    @Test(expectedExceptions = InvalidStateIdException.class)
    public void testNullStateId() {
        // given
        TestEntity e = new TestEntity(null);
        TestState a = new TestState(StateId.A);
        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .build();

        // when
        stateMachine.process(e, event);

        // then ... exception should be thrown
    }


    @Test(expectedExceptions = InvalidStateIdException.class)
    public void testNoStateForStateId() {
        // given
        TestEntity e = new TestEntity(StateId.B);
        TestState a = new TestState(StateId.A);
        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .build();

        // when
        stateMachine.process(e, event);

        // then ... exception should be thrown
    }

    @Test
    public void testInitialTransition() {
        TestEntity e = spy(new TestEntity(StateId.B));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestState c = spy(new TestState(StateId.C));
        TestDefaultAction initialAction1 = spy(new TestDefaultAction());
        TestDefaultAction initialAction2 = spy(new TestDefaultAction());
        TestDefaultAction initialAction3 = spy(new TestDefaultAction());

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a, initialAction1)
                .withCompositeState(a, initialAction2, b)
                .withCompositeState(b, initialAction3, c)
                .build();

        // when
        stateMachine.initialTransition(e);

        // then
        assertEquals(e.getStateId(), StateId.C);

        InOrder inOrder = inOrder(a, b, c, initialAction1, initialAction2, initialAction3, e);
        inOrder.verify(initialAction1).execute(e);
        inOrder.verify(a).onEntry(e);
        inOrder.verify(initialAction2).execute(e);
        inOrder.verify(b).onEntry(e);
        inOrder.verify(initialAction3).execute(e);
        inOrder.verify(c).onEntry(e);

        assertEquals(e.getStateId(), StateId.C);
    }

    @Test
    public void testSimpleTransition() {
        // given
        TestEntity e = spy(new TestEntity(StateId.A));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestGuard guard = spy(new TestGuard(true));
        TestAction action = spy(new TestAction());

        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder<commit_msg>improved default transitions test<commit_after>package org.headstar.scheelite;

import com.google.common.base.Optional;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/**
 * Created by Per on 2014-01-16.
 */
public class StateMachineTest extends TestBase {

    @Test
    public void testThrowExceptionResolver() {
        // given
        TestEntity e = spy(new TestEntity(StateId.A));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestEventX event = new TestEventX();
        MultipleTransitionsTriggeredResolver<TestEntity, StateId> resolver = spy(new ThrowExceptionResolver<TestEntity, StateId>());

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .withMultipleTransitionsTriggerPolicy(resolver)
                .withTransition(a, b, new TestGuard(true))
                .withTransition(a, b, new TestGuard(true))
                .build();

        // when
        try {
            stateMachine.process(e, event);
            fail("should have thrown");
        } catch(IllegalStateException ex) {
            // expected
            assertThat(ex.getMessage(), containsString("multiple transitions triggered"));
        }

        // then ... exception should be thrown
        verify(a).onEvent(e, event);
    }

    @Test(expectedExceptions = InvalidStateIdException.class)
    public void testNullStateId() {
        // given
        TestEntity e = new TestEntity(null);
        TestState a = new TestState(StateId.A);
        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .build();

        // when
        stateMachine.process(e, event);

        // then ... exception should be thrown
    }


    @Test(expectedExceptions = InvalidStateIdException.class)
    public void testNoStateForStateId() {
        // given
        TestEntity e = new TestEntity(StateId.B);
        TestState a = new TestState(StateId.A);
        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a)
                .build();

        // when
        stateMachine.process(e, event);

        // then ... exception should be thrown
    }

    @Test
    public void testInitialTransition() {
        // given
        TestEntity e = spy(new TestEntity(StateId.B));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestState c = spy(new TestState(StateId.C));
        TestDefaultAction defaultAction1 = spy(new TestDefaultAction());
        TestDefaultAction defaultAction2 = spy(new TestDefaultAction());
        TestDefaultAction defaultAction3 = spy(new TestDefaultAction());

        StateMachine<TestEntity> stateMachine = builder
                .withInitialTransition(a, defaultAction1)
                .withCompositeState(a, defaultAction2, b)
                .withCompositeState(b, defaultAction3, c)
                .build();

        // when
        stateMachine.initialTransition(e);

        // then
        assertEquals(e.getStateId(), StateId.C);

        InOrder inOrder = inOrder(a, b, c, defaultAction1, defaultAction2, defaultAction3, e);
        inOrder.verify(defaultAction1).execute(e);
        inOrder.verify(a).onEntry(e);
        inOrder.verify(defaultAction2).execute(e);
        inOrder.verify(b).onEntry(e);
        inOrder.verify(defaultAction3).execute(e);
        inOrder.verify(c).onEntry(e);

        assertEquals(e.getStateId(), StateId.C);

        verifyStateInteraction(a, TestEntity.class, onEntry(1), onExit(0), onEvent(0));
        verifyStateInteraction(b, TestEntity.class, onEntry(1), onExit(0), onEvent(0));
        verifyStateInteraction(c, TestEntity.class, onEntry(1), onExit(0), onEvent(0));
        verify(defaultAction1).execute(e);
        verify(defaultAction2).execute(e);
        verify(defaultAction3).execute(e);
    }

    @Test
    public void testSimpleTransition() {
        // given
        TestEntity e = spy(new TestEntity(StateId.A));
        TestState a = spy(new TestState(StateId.A));
        TestState b = spy(new TestState(StateId.B));
        TestGuard guard = spy(new TestGuard(true));
        TestAction action = spy(new TestAction());

        TestEventX event = new TestEventX();

        StateMachine<TestEntity> stateMachine = builder
{'fraction_non_alphanumeric': 0.10653858749044864, 'fraction_numerical': 0.0056762362187534115, 'mean_word_length': 2.566368236667964, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3017140', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2465, 'n_words': 541}