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