package at.willhaben.willtest.maven.utils;

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

/**
 * Copied over from https://bitbucket.willhaben.at/users/whmalema/repos/fail-on-contexterrors-maven-plugin/browse
 * @see <a href="https://bitbucket.willhaben.at/users/whmalema/repos/fail-on-contexterrors-maven-plugin/browse/src/main/java/at/willhaben/maven/FailFastOnSpringContextLoadingProblemsRunListener.java">FailFastOnSpringContextLoadingProblemsRunListener.java</a>
 */
@RunListener.ThreadSafe
public class FailFastOnSpringContextLoadingProblemsRunListener extends RunListener {


    private static int numberOfContextProblems = 0;
    private static int numberOfFailures = 0;

    @Override
    public void testRunStarted(Description description) throws Exception {
        if (numberOfContextProblems == 0)
            super.testRunStarted(description);
        else {
            abortTest();
        }
    }

    @Override
    public void testStarted(Description description) throws Exception {
        if (numberOfContextProblems == 0)
            super.testStarted(description);
        else {
            abortTest();
        }
    }

    private void abortTest() {
        String errorMessage = "Found failing ITs because the Application Context couldn't be loaded. \n" +
                "Check the error above, fix it and try again!";
        System.err.println("[ERROR] " + errorMessage);
        throw new RuntimeException(errorMessage);
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
        super.testRunFinished(result);
        System.err.println("Found " + numberOfFailures + " failures when running ITs");
        System.err.println("Found " + numberOfContextProblems + " failures when loading spring context.");
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        super.testFailure(failure);
        numberOfFailures++;
        Throwable cause = failure.getException();
        String message = cause.toString();
        if (message.contains("java.lang.IllegalStateException: Failed to load ApplicationContext")) {
            numberOfContextProblems++;
            System.err.println("Found failures when loading spring context. Cause: " + cause);
        }



    }

}

@@ -41,45 +41,8 @@
 s;%0A%0A
-import org.junit.runner.Description;%0A
 impo
@@ -170,1149 +170,372 @@
 r;%0A%0A
-/**%0A * Copied over from https://bitbucket.willhaben.at/users/whmalema/repos/fail-on-contexterrors-maven-plugin/browse%0A * @see %3Ca href=%22https://bitbucket.willhaben.at/users/whmalema/repos/fail-on-contexterrors-maven-plugin/browse/src/main/java/at/willhaben/maven/FailFastOnSpringContextLoadingProblemsRunListener.java%22%3EFailFastOnSpringContextLoadingProblemsRunListener.java%3C/a%3E%0A */%0A@RunListener.ThreadSafe%0Apublic class FailFastOnSpringContextLoadingProblemsRunListener extends RunListener %7B%0A%0A%0A    private static int numberOfContextProblems = 0;%0A    private static int numberOfFailures = 0;%0A%0A    @Override%0A    public void testRunStarted(Description description) throws Exception %7B%0A        if (numberOfContextProblems == 0)%0A            super.testRunStarted(description);%0A        else %7B%0A            abortTest();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void testStarted(Description description) throws Exception %7B%0A        if (numberOfContextProblems == 0)%0A            super.testStarted(description);%0A        else %7B%0A            abortTest();%0A        %7D%0A    %7D%0A%0A    private void abortTest() %7B%0A        String errorMessage = %22Found failing ITs bec
+@RunListener.ThreadSafe%0Apublic class FailFastOnSpringContextLoadingProblemsRunListener extends RunListener %7B%0A%0A%0A    private static int numberOfContextProblems = 0;%0A    private static int numberOfFailures = 0;%0A%0A%0A    private void abortTest(Failure failure) %7B%0A        throw new RuntimeException(%22Found failing IT (%22+ failure.getDescription().getClassName() +%22). C
 ause
+:
  the
@@ -602,157 +602,97 @@
    %22
-Check the error above, fix it and try again!%22;%0A        System.err.println(%22%5BERROR%5D %22 + errorMessage);%0A        throw new RuntimeException(errorMessage
+Try to execute tests of %22 + failure.getDescription().getDisplayName() + %22 in your IDE.%5Cn%22
 );%0A 
@@ -1410,87 +1410,24 @@
     
-System.err.println(%22Found failures when loading spring context. Cause: %22 + caus
+abortTest(failur
 e);%0A

improve output and error message