View Javadoc
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 org.junit.jupiter.api.Test;
17  
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.ContextBase;
20  import ch.qos.logback.core.pattern.ExceptionalConverter;
21  import ch.qos.logback.core.pattern.PatternLayoutBase;
22  import ch.qos.logback.core.status.testUtil.StatusChecker;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertFalse;
26  
27  abstract public class AbstractPatternLayoutBaseTest<E> {
28  
29      abstract public PatternLayoutBase<E> getPatternLayoutBase();
30  
31      abstract public E getEventObject();
32  
33      abstract public Context getContext();
34  
35      @Test
36      public void testUnStarted() {
37          PatternLayoutBase<E> plb = getPatternLayoutBase();
38          Context context = new ContextBase();
39          plb.setContext(context);
40          String s = plb.doLayout(getEventObject());
41          assertEquals("", s);
42      }
43  
44  
45  
46      /**
47       * This test checks that the pattern layout implementation starts its
48       * converters. ExceptionalConverter throws an exception if it's convert method
49       * is called before being started.
50       */
51      @Test
52      public void testConverterStart() {
53          PatternLayoutBase<E> plb = getPatternLayoutBase();
54          plb.setContext(getContext());
55          plb.getInstanceConverterMap().put("EX", getExceptionalConverterClassName());
56          plb.setPattern("%EX");
57          plb.start();
58          String result = plb.doLayout(getEventObject());
59          assertFalse(result.contains("%PARSER_ERROR_EX"));
60      }
61  
62      /**
63       *
64       * @return the class for the ExceptionalConverter (used in tests)
65       * @since 1.4.2
66       */
67      protected String getExceptionalConverterClassName() {
68          return ExceptionalConverter.class.getName();
69      }
70  
71      @Test
72      public void testStarted() {
73          PatternLayoutBase<E> plb = getPatternLayoutBase();
74          Context context = new ContextBase();
75          plb.setContext(context);
76          String s = plb.doLayout(getEventObject());
77          assertEquals("", s);
78      }
79  
80      @Test
81      public void testNullPattern() {
82          // System.out.println("testNullPattern");
83          PatternLayoutBase<E> plb = getPatternLayoutBase();
84          Context context = new ContextBase();
85          plb.setContext(context);
86          plb.setPattern(null);
87          plb.start();
88          String s = plb.doLayout(getEventObject());
89          assertEquals("", s);
90          StatusChecker checker = new StatusChecker(context.getStatusManager());
91          checker.assertContainsMatch("Empty or null pattern.");
92      }
93  
94      @Test
95      public void testEmptyPattern() {
96          // System.out.println("testNullPattern");
97          PatternLayoutBase<E> plb = getPatternLayoutBase();
98          Context context = new ContextBase();
99          plb.setContext(context);
100         plb.setPattern("");
101         plb.start();
102         String s = plb.doLayout(getEventObject());
103         assertEquals("", s);
104         StatusChecker checker = new StatusChecker(context.getStatusManager());
105         checker.assertContainsMatch("Empty or null pattern.");
106     }
107 
108 }