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;
15  
16  import org.junit.jupiter.api.Assertions;
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.PatternLayoutBase;
22  import ch.qos.logback.core.pattern.parser.test.AbstractPatternLayoutBaseTest;
23  import ch.qos.logback.core.status.Status;
24  import ch.qos.logback.core.status.testUtil.StatusChecker;
25  
26  public class SamplePatternLayoutTest extends AbstractPatternLayoutBaseTest<Object> {
27  
28      Context context = new ContextBase();
29      StatusChecker checker = new StatusChecker(context);
30  
31      public PatternLayoutBase<Object> getPatternLayoutBase() {
32          return new SamplePatternLayout<Object>();
33      }
34  
35      public Object getEventObject() {
36          return new Object();
37      }
38  
39      @Test
40      public void testOK() {
41          PatternLayoutBase<Object> plb = getPatternLayoutBase();
42          Context context = new ContextBase();
43          plb.setContext(context);
44          plb.setPattern("x%OTT");
45          plb.start();
46          String s = plb.doLayout(new Object());
47          // System.out.println(s);
48  
49          // StatusManager sm = context.getStatusManager();
50          // StatusPrinter.print(sm);
51          Assertions.assertEquals("x123", s);
52      }
53  
54      @Test
55      public void testEscapeClosingParentheses() {
56          PatternLayoutBase<Object> plb = getPatternLayoutBase();
57          Context context = new ContextBase();
58          plb.setContext(context);
59          plb.setPattern("x(%OTT\\)y");
60          plb.start();
61          String s = plb.doLayout(new Object());
62          Assertions.assertEquals("x(123)y", s);
63      }
64  
65      @Test
66      public void testEscapeBothParentheses() {
67          PatternLayoutBase<Object> plb = getPatternLayoutBase();
68          Context context = new ContextBase();
69          plb.setContext(context);
70          plb.setPattern("x\\(%OTT\\)y");
71          plb.start();
72          String s = plb.doLayout(new Object());
73          Assertions.assertEquals("x(123)y", s);
74      }
75  
76      @Test
77      public void testPercentAsLiteral() {
78          PatternLayoutBase<Object> plb = getPatternLayoutBase();
79          Context context = new ContextBase();
80          plb.setContext(context);
81          plb.setPattern("hello \\% world");
82          plb.start();
83          String s = plb.doLayout(new Object());
84          Assertions.assertEquals("hello % world", s);
85      }
86  
87      @Test
88      public void noClosingCurlyBrace() {
89          PatternLayoutBase<Object> plb = getPatternLayoutBase();
90          plb.setContext(context);
91          plb.setPattern("%x %hello{asd");
92          plb.start();
93  
94          checker.assertContainsMatch(Status.ERROR, "Failed to parse pattern");
95      }
96  
97      @Override
98      public Context getContext() {
99          return context;
100     }
101 }