LoggingTestExtension.java

  1. package fr.sii.ogham.testing.extension.junit;

  2. import java.util.Optional;

  3. import org.junit.jupiter.api.extension.AfterEachCallback;
  4. import org.junit.jupiter.api.extension.BeforeAllCallback;
  5. import org.junit.jupiter.api.extension.BeforeEachCallback;
  6. import org.junit.jupiter.api.extension.ExtensionContext;
  7. import org.junit.jupiter.api.extension.RegisterExtension;
  8. import org.junit.platform.commons.support.AnnotationSupport;

  9. import fr.sii.ogham.testing.extension.common.LogTestInformation;
  10. import fr.sii.ogham.testing.extension.common.Printer;
  11. import fr.sii.ogham.testing.extension.common.TestInformationLogger;

  12. /**
  13.  * Write information about test. This is useful when there are many tests:
  14.  * <ul>
  15.  * <li>To quickly find the logs for the test</li>
  16.  * <li>To quickly know if the test has failed or succeeded</li>
  17.  * <li>To quickly identify the test failure</li>
  18.  * <li>To quickly find failed tests</li>
  19.  * </ul>
  20.  *
  21.  * @author AurĂ©lien Baudet
  22.  *
  23.  */
  24. public class LoggingTestExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback {
  25.     private TestInformationLogger logger;

  26.     /**
  27.      * No logger initialized (it will be initialized using annotation). If you
  28.      * use {@link RegisterExtension} annotation, please use another constructor.
  29.      *
  30.      */
  31.     public LoggingTestExtension() {
  32.         super();
  33.     }

  34.     /**
  35.      * Initializes with the provided max line length.
  36.      *
  37.      * Uses Slf4j logger and default marker ("test-info").
  38.      *
  39.      * @param maxLength
  40.      *            the length of each line
  41.      */
  42.     public LoggingTestExtension(int maxLength) {
  43.         super();
  44.         this.logger = new TestInformationLogger(maxLength);
  45.     }

  46.     /**
  47.      * Initializes with the provided max line length and marker.
  48.      *
  49.      * Uses Slf4j logger.
  50.      *
  51.      * @param maxLength
  52.      *            the length of each line
  53.      * @param marker
  54.      *            the marker for logs
  55.      */
  56.     public LoggingTestExtension(int maxLength, String marker) {
  57.         super();
  58.         this.logger = new TestInformationLogger(maxLength, marker);
  59.     }

  60.     /**
  61.      *
  62.      * @param maxLength
  63.      *            the length of each line
  64.      * @param marker
  65.      *            the marker for logs
  66.      * @param logger
  67.      *            the logger
  68.      */
  69.     public LoggingTestExtension(int maxLength, String marker, Printer logger) {
  70.         super();
  71.         this.logger = new TestInformationLogger(maxLength, marker, logger);
  72.     }

  73.     @Override
  74.     public void beforeAll(ExtensionContext context) throws InstantiationException, IllegalAccessException {
  75.         if (logger != null) {
  76.             return;
  77.         }
  78.         LogTestInformation annotation = AnnotationSupport.findAnnotation(context.getElement(), LogTestInformation.class).orElse(null);
  79.         if (annotation == null) {
  80.             logger = new TestInformationLogger();
  81.         } else {
  82.             logger = new TestInformationLogger(annotation.maxLength(), annotation.marker(), annotation.printer().newInstance());
  83.         }
  84.     }

  85.     @Override
  86.     public void beforeEach(ExtensionContext context) throws Exception {
  87.         logger.writeStart(context.getDisplayName());
  88.     }

  89.     @Override
  90.     public void afterEach(ExtensionContext context) throws Exception {
  91.         Optional<Throwable> executionException = context.getExecutionException();
  92.         if (executionException.isPresent()) {
  93.             logger.writeFailure(context.getDisplayName(), executionException.get());
  94.         } else {
  95.             logger.writeSuccess(context.getDisplayName());
  96.         }
  97.     }

  98. }