1
2
3
4
5
6
7
8
9
10
11
12
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
48
49
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
65
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
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
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 }