1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.pattern.parser.test;
15  
16  import ch.qos.logback.core.pattern.color.ConverterSupplierByClassName;
17  import org.junit.jupiter.api.Test;
18  
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.ContextBase;
21  import ch.qos.logback.core.pattern.ExceptionalConverter;
22  import ch.qos.logback.core.pattern.PatternLayoutBase;
23  import ch.qos.logback.core.status.testUtil.StatusChecker;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  import static org.junit.jupiter.api.Assertions.assertFalse;
27  
28  abstract public class AbstractPatternLayoutBaseTest<E> {
29  
30      abstract public PatternLayoutBase<E> getPatternLayoutBase();
31  
32      abstract public E getEventObject();
33  
34      abstract public Context getContext();
35  
36      @Test
37      public void testUnStarted() {
38          PatternLayoutBase<E> plb = getPatternLayoutBase();
39          Context context = new ContextBase();
40          plb.setContext(context);
41          String s = plb.doLayout(getEventObject());
42          assertEquals("", s);
43      }
44  
45  
46  
47      /**
48       * This test checks that the pattern layout implementation starts its
49       * converters. ExceptionalConverter throws an exception if it's convert method
50       * is called before being started.
51       */
52      @Test
53      public void testConverterStart() {
54          PatternLayoutBase<E> plb = getPatternLayoutBase();
55          plb.setContext(getContext());
56          String exceptionalConverterClassName = getExceptionalConverterClassName();
57          ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName("EX", exceptionalConverterClassName);
58          converterSupplierByClassName.setContext(getContext());
59          plb.getInstanceConverterMap().put("EX", converterSupplierByClassName);
60          plb.setPattern("%EX");
61          plb.start();
62          String result = plb.doLayout(getEventObject());
63          assertFalse(result.contains("%PARSER_ERROR_EX"));
64      }
65  
66      /**
67       *
68       * @return the class for the ExceptionalConverter (used in tests)
69       * @since 1.4.2
70       */
71      protected String getExceptionalConverterClassName() {
72          return ExceptionalConverter.class.getName();
73      }
74  
75      @Test
76      public void testStarted() {
77          PatternLayoutBase<E> plb = getPatternLayoutBase();
78          Context context = new ContextBase();
79          plb.setContext(context);
80          String s = plb.doLayout(getEventObject());
81          assertEquals("", s);
82      }
83  
84      @Test
85      public void testNullPattern() {
86          // System.out.println("testNullPattern");
87          PatternLayoutBase<E> plb = getPatternLayoutBase();
88          Context context = new ContextBase();
89          plb.setContext(context);
90          plb.setPattern(null);
91          plb.start();
92          String s = plb.doLayout(getEventObject());
93          assertEquals("", s);
94          StatusChecker checker = new StatusChecker(context.getStatusManager());
95          checker.assertContainsMatch("Empty or null pattern.");
96      }
97  
98      @Test
99      public void testEmptyPattern() {
100         // System.out.println("testNullPattern");
101         PatternLayoutBase<E> plb = getPatternLayoutBase();
102         Context context = new ContextBase();
103         plb.setContext(context);
104         plb.setPattern("");
105         plb.start();
106         String s = plb.doLayout(getEventObject());
107         assertEquals("", s);
108         StatusChecker checker = new StatusChecker(context.getStatusManager());
109         checker.assertContainsMatch("Empty or null pattern.");
110     }
111 
112 }