package org.requirementsascode;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class ExceptionHandlingTest extends AbstractTestCase{
	private static final String EXCEPTION_HANDLING_FLOW = "Exception Handling Flow";
	private static final String SYSTEM_THROWS_EXCEPTION = "System throws Exception";
	private static final String SYSTEM_HANDLES_EXCEPTION = "System handles exception";

	@Before
	public void setup() {
		setupWith(new TestUseCaseRunner());
	}
	
	@Test
	public void doesNotHandleExceptionIfNoExceptionOccurs() {		
		useCaseModel.useCase(USE_CASE)
			.basicFlow()
				.step(SYSTEM_DISPLAYS_TEXT).system(displayConstantText())
			.flow(EXCEPTION_HANDLING_FLOW)
				.after(SYSTEM_DISPLAYS_TEXT)
					.step(SYSTEM_HANDLES_EXCEPTION)
						.handle(ArrayIndexOutOfBoundsException.class).system(e -> {});

		useCaseRunner.run();

		assertEquals(SYSTEM_DISPLAYS_TEXT + ";", runStepNames());
	}
	
	@Test
	public void doesNotHandleExceptionIfSystemReactionDoesNotThrowException() {		
		useCaseModel.useCase(USE_CASE)
			.basicFlow()
				.step(SYSTEM_DISPLAYS_TEXT).system(displayConstantText())		
			.flow(EXCEPTION_HANDLING_FLOW)
				.after(SYSTEM_DISPLAYS_TEXT)
					.step(SYSTEM_HANDLES_EXCEPTION).handle(ArrayIndexOutOfBoundsException.class).system(e -> {});
		
		useCaseRunner.run();
		
		assertEquals(SYSTEM_DISPLAYS_TEXT +";", runStepNames());
	}
	
	@Test
	public void handlesExceptionAfterSpecificStep() {				
		useCaseModel.useCase(USE_CASE)
			.basicFlow()
				.step(SYSTEM_THROWS_EXCEPTION).system(throwArrayIndexOutOfBoundsException())		
			.flow(EXCEPTION_HANDLING_FLOW).after(SYSTEM_THROWS_EXCEPTION)
				.step(SYSTEM_HANDLES_EXCEPTION).handle(ArrayIndexOutOfBoundsException.class).system(e -> {});
		
		useCaseRunner.run();
		
		assertEquals(SYSTEM_HANDLES_EXCEPTION, latestStepName());
	}
	
	@Test
	public void handlesExceptionAtAnyTime() {			
		useCaseModel.useCase(USE_CASE)
			.basicFlow()
				.step(SYSTEM_DISPLAYS_TEXT).system(displayConstantText())		
			.flow(ALTERNATIVE_FLOW).after(SYSTEM_DISPLAYS_TEXT)
				.step(SYSTEM_THROWS_EXCEPTION).system(throwArrayIndexOutOfBoundsException())
			.flow(EXCEPTION_HANDLING_FLOW).when(r -> true)
				.step(SYSTEM_HANDLES_EXCEPTION).handle(ArrayIndexOutOfBoundsException.class).system(e -> {});
		
		useCaseRunner.run();
		
		assertEquals(SYSTEM_DISPLAYS_TEXT +";" + SYSTEM_THROWS_EXCEPTION + ";" + SYSTEM_HANDLES_EXCEPTION +";", runStepNames());
	}
}

@@ -70,16 +70,294 @@
 tEquals;
+%0Aimport static org.requirementsascode.testutil.Names.ALTERNATIVE_FLOW;%0Aimport static org.requirementsascode.testutil.Names.ALTERNATIVE_FLOW_2;%0Aimport static org.requirementsascode.testutil.Names.SYSTEM_DISPLAYS_TEXT;%0Aimport static org.requirementsascode.testutil.Names.USE_CASE;
 %0A%0Aimport
@@ -464,90 +464,8 @@
 se%7B%0A
-%09private static final String EXCEPTION_HANDLING_FLOW = %22Exception Handling Flow%22;%0A
 %09pri
@@ -890,34 +890,27 @@
 %09%09.flow(
-EXCEPTION_HANDLING
+ALTERNATIVE
 _FLOW)%0A%09
@@ -1345,34 +1345,27 @@
 %09%09.flow(
-EXCEPTION_HANDLING
+ALTERNATIVE
 _FLOW)%0A%09
@@ -1791,34 +1791,27 @@
 %09%09.flow(
-EXCEPTION_HANDLING
+ALTERNATIVE
 _FLOW).a
@@ -2347,31 +2347,26 @@
 low(
-EXCEPTION_HANDLING
+ALTERNATIVE
 _FLOW
+_2
 ).wh

Update ExceptionHandlingTest to use standard names