1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.pattern.parser;
11  
12  import static org.junit.Assert.*;
13  
14  import org.junit.Test;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.pattern.PatternLayoutBase;
19  
20  
21  public class SamplePatternLayoutTest extends AbstractPatternLayoutBaseTest {
22  
23    Context context = new ContextBase();
24  
25    public PatternLayoutBase<Object> getPatternLayoutBase() {
26      return new SamplePatternLayout<Object>();
27    }
28  
29    public Object getEventObject() {
30      return new Object();
31    }
32    
33    @Test
34    public void testOK() {
35      PatternLayoutBase<Object> plb = getPatternLayoutBase();
36      Context context = new ContextBase();
37      plb.setContext(context);
38      plb.setPattern("x%OTT");
39      plb.start();
40      String s = plb.doLayout(new Object());
41      //System.out.println(s);
42  
43      //StatusManager sm = context.getStatusManager();
44      //StatusPrinter.print(sm);
45      assertEquals("x123", s);
46    }
47  
48  
49    
50    @Test
51    public void testEscapeClosingParentheses() {
52      PatternLayoutBase<Object> plb = getPatternLayoutBase();
53      Context context = new ContextBase();
54      plb.setContext(context);
55      plb.setPattern("x(%OTT\\)y");
56      plb.start();
57      String s = plb.doLayout(new Object());
58      assertEquals("x(123)y", s);
59    }
60    
61    @Test
62    public void testEscapeBothParentheses() {
63      PatternLayoutBase<Object> plb = getPatternLayoutBase();
64      Context context = new ContextBase();
65      plb.setContext(context);
66      plb.setPattern("x\\(%OTT\\)y");
67      plb.start();
68      String s = plb.doLayout(new Object());
69      assertEquals("x(123)y", s);
70    }
71  
72    @Test
73    public void testPercentAsLiteral() {
74      PatternLayoutBase<Object> plb = getPatternLayoutBase();
75      Context context = new ContextBase();
76      plb.setContext(context);
77      plb.setPattern("hello \\% world");
78      plb.start();
79      String s = plb.doLayout(new Object());
80      assertEquals("hello % world", s);
81    }
82  
83    
84    @Override
85    public Context getContext() {
86      return  context;
87    }
88  }