package hu.advancedweb.scott.runtime;

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * An alternative way to produce the detailed failure reports is to register this JUnit listener.
 * 
 * @see ScottReportingRule
 * @author David Csakvari
 */
public class ScottRunListener extends RunListener {
	
	private Description description;
	
	@Override
	public void testStarted(Description description) throws Exception {
		this.description = description;
		super.testStarted(description);
	}
	
	@Override
	public void testFailure(Failure failure) throws Exception {
		// TODO: the simple System.out.println really not fits nicely into the test reports.
		// try changing the original exception's message with reflection. See issue #8.
		
		System.out.println(failure.getTestHeader() + " FAILED!");
		System.out.println(FailureRenderer.render(description, failure.getException()));
		super.testFailure(failure);
	}
}

@@ -32,16 +32,49 @@
 ntime;%0A%0A
+import java.lang.reflect.Field;%0A%0A
 import o
@@ -102,16 +102,16 @@
 iption;%0A
-
 import o
@@ -683,133 +683,211 @@
 %7B%0A%09%09
-// TODO: the simple System.out.println really not fits nicely into the test r
+String scottReport = FailureRenderer.render(description, failure.getException());%0A%09%09setExceptionMessage(failure.getException(), scottR
 eport
-s.%0A%09%09// try changing the original e
+);%0A%09%09super.testFailure(failure);%0A%09%7D%0A%09%0A%09private void setE
 xception
 's m
@@ -886,225 +886,535 @@
 tion
-'s m
+M
 essage
- with reflection. See issue #8.%0A%09%09%0A%09%09System.out.println(failure.getTestHeader() + %22 FAILED!%22);%0A%09%09System.out.println(FailureRenderer.render(description, failure.getException()));%0A%09%09super.testFailure(failure);
+(Object object, Object fieldValue) %7B%0A%09    final String fieldName = %22detailMessage%22;%0A%09%09Class%3C?%3E clazz = object.getClass();%0A%09    while (clazz != null) %7B%0A%09        try %7B%0A%09            Field field = clazz.getDeclaredField(fieldName);%0A%09            field.setAccessible(true);%0A%09            field.set(object, fieldValue);%0A%09            return;%0A%09        %7D catch (NoSuchFieldException e) %7B%0A%09            clazz = clazz.getSuperclass();%0A%09        %7D catch (Exception e) %7B%0A%09            throw new IllegalStateException(e);%0A%09        %7D%0A%09    %7D
 %0A%09%7D%0A

Update exception message in run listener rather than just sysoutting the report.