package io.enmasse.systemtest;

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

import java.util.Iterator;
import java.util.Map;

public class SystemTestRunListener extends RunListener {


    @Override
    public void testStarted(Description description) throws Exception {
        Logging.log.info(description + "STARTED");
    }

    @Override
    public void testFinished(Description description) throws Exception {
        Logging.log.info(description + "FINISHED");
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
        Logging.log.info("default address space '{}' will be removed", TestBaseWithDefault.defaultAddressSpace);
        TestBase.deleteAddressSpace(TestBaseWithDefault.defaultAddressSpace);
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        Throwable ex = failure.getException();
        if (ex instanceof OutOfMemoryError) {
            Logging.log.error("Got OOM, dumping thread info");
            printThreadDump();
        } else {
            Logging.log.error("Caught exception {}", ex);
        }
    }

    public static void printThreadDump() {
        Map<Thread, StackTraceElement[]> allThreads = Thread.getAllStackTraces();
        Iterator<Thread> iterator = allThreads.keySet().iterator();
        while (iterator.hasNext()) {
            StringBuilder sb = new StringBuilder();
            Thread key = iterator.next();
            StackTraceElement[] trace = allThreads.get(key);
            sb.append(key + "\r\n");
            for (int i = 0; i < trace.length; i++) {
                sb.append(" " + trace[i] + "\r\n");
            }
            Logging.log.error(sb.toString());
        }
    }
}

@@ -741,24 +741,32 @@
 moved%22, 
+Brokered
 TestBase
 WithDefa
@@ -753,35 +753,24 @@
 eredTestBase
-WithDefault
 .defaultAddr
@@ -820,27 +820,209 @@
 ace(
-TestBaseWithDefault
+BrokeredTestBase.defaultAddressSpace);%0A        Logging.log.info(%22default address space '%7B%7D' will be removed%22, StandardTestBase.defaultAddressSpace);%0A        TestBase.deleteAddressSpace(StandardTestBase
 .def

FIX SystemTestRunListener - remove default addr spaces fixed