TestLogger.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util.test;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.Arrays;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.testng.ITestContext;
  34. import org.testng.ITestResult;
  35. import org.testng.TestListenerAdapter;

  36. /***********************************************************************************************************************
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **********************************************************************************************************************/
  41. public class TestLogger extends TestListenerAdapter
  42.   {
  43.     private static final String S = "***********************************************";
  44.     private static final String SEPARATOR = S + S + S + S;

  45.     @Override
  46.     public void onStart (@Nonnull final ITestContext testContext)
  47.       {
  48.         super.onStart(testContext);
  49.         final String testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
  50.         final Logger log = LoggerFactory.getLogger(testClass);
  51.         log.info("STARTING TESTS OF {}", testClass);
  52.       }

  53.     @Override
  54.     public void onFinish (@Nonnull final ITestContext testContext)
  55.       {
  56.         super.onFinish(testContext);
  57.         final String testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
  58.         final Logger log = LoggerFactory.getLogger(testClass);
  59.         log.info("FINISHED TESTS OF {}", testClass);
  60.       }

  61. //    @Override
  62. //    public void onConfigurationSuccess (final @Nonnull ITestResult result)
  63. //      {
  64. //        super.onConfigurationSuccess(result);
  65. //        final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  66. //        log.info("====== ON CONFIG SUCCESS");
  67. //      }

  68.     @Override
  69.     public void onConfigurationSkip (@Nonnull final ITestResult result)
  70.       {
  71.         super.onConfigurationSkip(result);
  72.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  73.         final Throwable throwable = result.getThrowable();
  74.         log.warn("CONFIGURATION SKIPPED {}", getMessage(throwable));

  75.         if (throwable != null)
  76.           {
  77.             log.warn("CONFIGURATION SKIPPED", result.getThrowable());
  78.           }
  79.       }

  80.     @Override
  81.     public void onConfigurationFailure (@Nonnull final ITestResult result)
  82.       {
  83.         super.onConfigurationFailure(result);
  84.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  85.         final Throwable throwable = result.getThrowable();
  86.         log.error("CONFIGURATION FAILED {}", getMessage(throwable));

  87.         if (throwable != null)
  88.           {
  89.             log.error("CONFIGURATION FAILED", result.getThrowable());
  90.           }
  91.       }

  92.     @Override
  93.     public void onTestStart (@Nonnull final ITestResult result)
  94.       {
  95.         super.onTestStart(result);
  96.         final String args = getArgs(result);
  97.         final int max = Math.max(args.length() + 5, result.getName().length() + 7);
  98.         final String separator = SEPARATOR.substring(0, Math.min(max, SEPARATOR.length()));
  99.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());

  100.         log.info(separator);
  101.         log.info("TEST \"{}\"", result.getName().replace('_', ' '));

  102.         if (!"".equals(args))
  103.           {
  104.             log.info("ARGS {}", args);
  105.           }

  106.         log.info(separator);
  107.       }

  108.     @Override
  109.     public void onTestFailure (@Nonnull final ITestResult result)
  110.       {
  111.         super.onTestFailure(result);
  112.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  113.         final Throwable throwable = result.getThrowable();
  114.         final String args = getArgs(result);

  115.         log.error("TEST FAILED in {} msec - {}{} - {}",
  116.                  result.getEndMillis() - result.getStartMillis(),
  117.                  result.getName().replace('_', ' '),
  118.                  "".equals(args) ? "" : " " + args,
  119.                  getMessage(throwable));

  120.         if (throwable != null)
  121.           {
  122.             log.error("TEST FAILED", result.getThrowable());
  123.           }

  124.         log.error("");
  125.       }

  126.     @Override
  127.     public void onTestFailedButWithinSuccessPercentage (@Nonnull final ITestResult result)
  128.       {
  129.         super.onTestFailedButWithinSuccessPercentage(result);
  130.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  131.         log.info("TEST FAILED WITHIN SUCCESS PERCENTAGE in {} msec", result.getEndMillis() - result.getStartMillis());
  132.         log.info("");
  133.       }

  134.     @Override
  135.     public void onTestSkipped (@Nonnull final ITestResult result)
  136.       {
  137.         super.onTestSkipped(result);
  138.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  139.         final Throwable throwable = result.getThrowable();
  140.         log.info("TEST SKIPPED {}", getMessage(throwable));

  141.         if (throwable != null)
  142.           {
  143.             log.info("TEST SKIPPED", result.getThrowable());
  144.           }

  145.         log.info("");
  146.       }

  147.     @Override
  148.     public void onTestSuccess (@Nonnull final ITestResult result)
  149.       {
  150.         super.onTestSuccess(result);
  151.         final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
  152.         log.info("TEST PASSED in {} msec", result.getEndMillis() - result.getStartMillis());
  153.         log.info("");
  154.       }

  155.     @Nonnull
  156.     private String getArgs (@Nonnull final ITestResult result)
  157.       {
  158.         String args = "";

  159.         final Object[] parameters = result.getParameters();

  160.         if ((parameters != null) && parameters.length > 0)
  161.           {
  162.             args = Arrays.toString(parameters);
  163.           }

  164.         return args;
  165.       }

  166.     @Nonnull
  167.     private static String getMessage (@Nullable final Throwable throwable)
  168.       {
  169.         return (throwable == null) ? "" : throwable.toString();
  170. //        return (throwable == null) ? "" : throwable.toString().replaceAll("\n*", "");
  171.       }
  172.   }