package unquietcode.tools.esm;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;

/**
 * @author Ben Fagin
 * @version 2013-07-15
 */
public class Reflective_T {

	@Test
	public void testSimpleReflective() {
		final AtomicInteger enteringBlue = new AtomicInteger();
		final AtomicInteger exitingBlue = new AtomicInteger();
		final AtomicInteger enteringGreen = new AtomicInteger();
		final AtomicInteger enteringAny = new AtomicInteger();
		final AtomicInteger exitingAny = new AtomicInteger();
		final AtomicInteger transitionAny = new AtomicInteger();

		ReflectiveStateMachine sm = new ReflectiveStateMachine() {

			@Override
			protected void declareTransitions() {
				addTransition(null, "blue");
				addTransition("blue", "green");
				addTransition("green", null);
			}

			public void onEnteringBlue(String state) {
				assertEquals("blue", state);
				enteringBlue.incrementAndGet();
			}

			public void onBlue() {
				enteringBlue.incrementAndGet();
			}

			public void onExitingBlue() {
				exitingBlue.incrementAndGet();
			}

			public void onGreen() {
				enteringGreen.incrementAndGet();
			}

			public void onEntering() {
				enteringAny.incrementAndGet();
			}

			public void onExiting() {
				exitingAny.incrementAndGet();
			}

			public void onTransition() {
				transitionAny.incrementAndGet();
			}
		};

		sm.transition("blue");
		sm.transition("green");
		sm.transition(null);

		assertEquals(2, enteringBlue.get());
		assertEquals(1, exitingBlue.get());
		assertEquals(1, enteringGreen.get());
		assertEquals(3, enteringAny.get());
		assertEquals(3, exitingAny.get());
		assertEquals(3, transitionAny.get());
	}

	@Test(expected=UnsupportedOperationException.class)
	public void testException() {
		ReflectiveStateMachine sm = new ReflectiveStateMachine() {
			public void onBlue() {
				throw new UnsupportedOperationException("error");
			}
		};

		sm.addTransition(null, "blue");
		sm.transition("blue");
	}
}

@@ -1724,36 +1724,22 @@
 xpected=
-UnsupportedOperation
+Custom
 Exceptio
@@ -1883,28 +1883,14 @@
 new 
-UnsupportedOperation
+Custom
 Exce
@@ -1978,10 +1978,136 @@
 e%22);%0A%09%7D%0A
-%7D
 %0A
+%09private static class CustomException extends RuntimeException %7B%0A%09%09public CustomException(String msg) %7B%0A%09%09%09super(msg);%0A%09%09%7D%0A%09%7D%0A%7D

use a custom exception in test to avoid errors