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