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.*;
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  
25  public class SamplePatternLayoutTest extends AbstractPatternLayoutBaseTest<Object> {
26  
27      Context context = new ContextBase();
28  
29      public PatternLayoutBase<Object> getPatternLayoutBase() {
30          return new SamplePatternLayout<Object>();
31      }
32  
33      public Object getEventObject() {
34          return new Object();
35      }
36  
37      @Test
38      public void testOK() {
39          PatternLayoutBase<Object> plb = getPatternLayoutBase();
40          Context context = new ContextBase();
41          plb.setContext(context);
42          plb.setPattern("x%OTT");
43          plb.start();
44          String s = plb.doLayout(new Object());
45          // System.out.println(s);
46  
47          // StatusManager sm = context.getStatusManager();
48          // StatusPrinter.print(sm);
49          assertEquals("x123", s);
50      }
51  
52      @Test
53      public void testEscapeClosingParentheses() {
54          PatternLayoutBase<Object> plb = getPatternLayoutBase();
55          Context context = new ContextBase();
56          plb.setContext(context);
57          plb.setPattern("x(%OTT\\)y");
58          plb.start();
59          String s = plb.doLayout(new Object());
60          assertEquals("x(123)y", s);
61      }
62  
63      @Test
64      public void testEscapeBothParentheses() {
65          PatternLayoutBase<Object> plb = getPatternLayoutBase();
66          Context context = new ContextBase();
67          plb.setContext(context);
68          plb.setPattern("x\\(%OTT\\)y");
69          plb.start();
70          String s = plb.doLayout(new Object());
71          assertEquals("x(123)y", s);
72      }
73  
74      @Test
75      public void testPercentAsLiteral() {
76          PatternLayoutBase<Object> plb = getPatternLayoutBase();
77          Context context = new ContextBase();
78          plb.setContext(context);
79          plb.setPattern("hello \\% world");
80          plb.start();
81          String s = plb.doLayout(new Object());
82          assertEquals("hello % world", s);
83      }
84  
85      @Override
86      public Context getContext() {
87          return context;
88      }
89  }