/*
 * Copyright © 2016 - 2017 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an “AS IS” BASIS, without warranties or conditions of any kind,
 * EITHER EXPRESS OR IMPLIED. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.vrg.rapid;

import com.google.common.base.Stopwatch;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import java.util.concurrent.TimeUnit;

/**
 * This pretty prints test results when using surefire.
 */
public class CustomRunListener extends RunListener {
    private static final String RESET = "\u001B[0m";
    private static final String GREEN = "\u001B[32m";
    private static final String RED = "\u001B[31m";
    private static final Description FAILED = Description.createTestDescription("FAILED", "FAILED");
    private final Stopwatch stopwatch = Stopwatch.createUnstarted();

    @Override
    public void testFinished(final Description description) throws Exception {
        if (!description.getChildren().contains(FAILED)) {
            final long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.println(GREEN + "     [PASS] [Time: " + elapsedTime + " ms]" + RESET + " " + description);
        }
    }

    @Override
    public void testFailure(final Failure failure) throws Exception {
        final long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
        System.out.println(RED + "     [FAILED] [Time: " + elapsedTime + " ms]" + RESET + " " + failure);
        System.out.println(RED + "     " + RESET + failure.getException());
        failure.getDescription().addChild(FAILED);
    }

    @Override
    public void testStarted(final Description description) throws Exception {
        stopwatch.reset();
        stopwatch.start();
    }
}

@@ -1292,16 +1292,174 @@
 ted();%0A%0A
+    @Override%0A    public void testRunStarted(final Description description) %7B%0A        System.out.println(%22Runtime diagnostics: %22 + getRuntimeStats());%0A    %7D%0A%0A
     @Ove
@@ -1535,32 +1535,32 @@
 ows Exception %7B%0A
-
         if (!des
@@ -2044,32 +2044,68 @@
 dTime + %22 ms%5D%22 +
+ getRuntimeStats()%0A                +
  RESET + %22 %22 + f
@@ -2362,24 +2362,24 @@
 ch.reset();%0A
-
         stop
@@ -2398,11 +2398,275 @@
 );%0A    %7D
+%0A%0A    private String getRuntimeStats() %7B%0A        final Runtime runtime = Runtime.getRuntime();%0A        return String.format(%22%5BProc:%25d Mem:%25s Free:%25s%5D%22, runtime.availableProcessors(),%0A                              runtime.totalMemory(), runtime.freeMemory());%0A    %7D
 %0A%7D%0A

Print diagnostics when test fails