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 ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.pattern.Converter;
19  import ch.qos.logback.core.pattern.Converter123;
20  import ch.qos.logback.core.pattern.ConverterHello;
21  import ch.qos.logback.core.testUtil.StatusChecker;
22  import ch.qos.logback.core.util.StatusPrinter;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  public class CompilerTest {
32  
33      Map<String, String> converterMap = new HashMap<String, String>();
34      Context context = new ContextBase();
35  
36      @Before
37      public void setUp() {
38          converterMap.put("OTT", Converter123.class.getName());
39          converterMap.put("hello", ConverterHello.class.getName());
40          converterMap.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
41      }
42  
43      String write(final Converter<Object> head, Object event) {
44          StringBuilder buf = new StringBuilder();
45          Converter<Object> c = head;
46          while (c != null) {
47              c.write(buf, event);
48              c = c.getNext();
49          }
50          return buf.toString();
51      }
52  
53      @Test
54      public void testLiteral() throws Exception {
55          Parser<Object> p = new Parser<Object>("hello");
56          Node t = p.parse();
57          Converter<Object> head = p.compile(t, converterMap);
58          String result = write(head, new Object());
59          assertEquals("hello", result);
60      }
61  
62      @Test
63      public void testBasic() throws Exception {
64          {
65              Parser<Object> p = new Parser<Object>("abc %hello");
66              p.setContext(context);
67              Node t = p.parse();
68              Converter<Object> head = p.compile(t, converterMap);
69              String result = write(head, new Object());
70              assertEquals("abc Hello", result);
71          }
72          {
73              Parser<Object> p = new Parser<Object>("abc %hello %OTT");
74              p.setContext(context);
75              Node t = p.parse();
76              Converter<Object> head = p.compile(t, converterMap);
77              String result = write(head, new Object());
78              assertEquals("abc Hello 123", result);
79          }
80      }
81  
82      @Test
83      public void testFormat() throws Exception {
84          {
85              Parser<Object> p = new Parser<Object>("abc %7hello");
86              p.setContext(context);
87              Node t = p.parse();
88              Converter<Object> head = p.compile(t, converterMap);
89              String result = write(head, new Object());
90              assertEquals("abc   Hello", result);
91          }
92  
93          {
94              Parser<Object> p = new Parser<Object>("abc %-7hello");
95              p.setContext(context);
96              Node t = p.parse();
97              Converter<Object> head = p.compile(t, converterMap);
98              String result = write(head, new Object());
99              assertEquals("abc Hello  ", result);
100         }
101 
102         {
103             Parser<Object> p = new Parser<Object>("abc %.3hello");
104             p.setContext(context);
105             Node t = p.parse();
106             Converter<Object> head = p.compile(t, converterMap);
107             String result = write(head, new Object());
108             assertEquals("abc llo", result);
109         }
110 
111         {
112             Parser<Object> p = new Parser<Object>("abc %.-3hello");
113             p.setContext(context);
114             Node t = p.parse();
115             Converter<Object> head = p.compile(t, converterMap);
116             String result = write(head, new Object());
117             assertEquals("abc Hel", result);
118         }
119 
120         {
121             Parser<Object> p = new Parser<Object>("abc %4.5OTT");
122             p.setContext(context);
123             Node t = p.parse();
124             Converter<Object> head = p.compile(t, converterMap);
125             String result = write(head, new Object());
126             assertEquals("abc  123", result);
127         }
128         {
129             Parser<Object> p = new Parser<Object>("abc %-4.5OTT");
130             p.setContext(context);
131             Node t = p.parse();
132             Converter<Object> head = p.compile(t, converterMap);
133             String result = write(head, new Object());
134             assertEquals("abc 123 ", result);
135         }
136         {
137             Parser<Object> p = new Parser<Object>("abc %3.4hello");
138             p.setContext(context);
139             Node t = p.parse();
140             Converter<Object> head = p.compile(t, converterMap);
141             String result = write(head, new Object());
142             assertEquals("abc ello", result);
143         }
144         {
145             Parser<Object> p = new Parser<Object>("abc %-3.-4hello");
146             p.setContext(context);
147             Node t = p.parse();
148             Converter<Object> head = p.compile(t, converterMap);
149             String result = write(head, new Object());
150             assertEquals("abc Hell", result);
151         }
152     }
153 
154     @Test
155     public void testComposite() throws Exception {
156         // {
157         // Parser<Object> p = new Parser<Object>("%(ABC)");
158         // p.setContext(context);
159         // Node t = p.parse();
160         // Converter<Object> head = p.compile(t, converterMap);
161         // String result = write(head, new Object());
162         // assertEquals("ABC", result);
163         // }
164         {
165             Context c = new ContextBase();
166             Parser<Object> p = new Parser<Object>("%(ABC %hello)");
167             p.setContext(c);
168             Node t = p.parse();
169             Converter<Object> head = p.compile(t, converterMap);
170             String result = write(head, new Object());
171             StatusPrinter.print(c);
172             assertEquals("ABC Hello", result);
173         }
174         {
175             Parser<Object> p = new Parser<Object>("%(ABC %hello)");
176             p.setContext(context);
177             Node t = p.parse();
178             Converter<Object> head = p.compile(t, converterMap);
179             String result = write(head, new Object());
180             assertEquals("ABC Hello", result);
181         }
182     }
183 
184     @Test
185     public void testCompositeFormatting() throws Exception {
186         {
187             Parser<Object> p = new Parser<Object>("xyz %4.10(ABC)");
188             p.setContext(context);
189             Node t = p.parse();
190             Converter<Object> head = p.compile(t, converterMap);
191             String result = write(head, new Object());
192             assertEquals("xyz  ABC", result);
193         }
194 
195         {
196             Parser<Object> p = new Parser<Object>("xyz %-4.10(ABC)");
197             p.setContext(context);
198             Node t = p.parse();
199             Converter<Object> head = p.compile(t, converterMap);
200             String result = write(head, new Object());
201             assertEquals("xyz ABC ", result);
202         }
203 
204         {
205             Parser<Object> p = new Parser<Object>("xyz %.2(ABC %hello)");
206             p.setContext(context);
207             Node t = p.parse();
208             Converter<Object> head = p.compile(t, converterMap);
209             String result = write(head, new Object());
210             assertEquals("xyz lo", result);
211         }
212 
213         {
214             Parser<Object> p = new Parser<Object>("xyz %.-2(ABC)");
215             p.setContext(context);
216             Node t = p.parse();
217             Converter<Object> head = p.compile(t, converterMap);
218             String result = write(head, new Object());
219             assertEquals("xyz AB", result);
220         }
221 
222         {
223             Parser<Object> p = new Parser<Object>("xyz %30.30(ABC %20hello)");
224             p.setContext(context);
225             Node t = p.parse();
226             Converter<Object> head = p.compile(t, converterMap);
227             String result = write(head, new Object());
228             assertEquals("xyz       ABC                Hello", result);
229         }
230     }
231 
232     @Test
233     public void testUnknownWord() throws Exception {
234         Parser<Object> p = new Parser<Object>("%unknown");
235         p.setContext(context);
236         Node t = p.parse();
237         p.compile(t, converterMap);
238         StatusChecker checker = new StatusChecker(context.getStatusManager());
239         checker.assertContainsMatch("\\[unknown] is not a valid conversion word");
240     }
241 
242     @Test
243     public void testWithNopEscape() throws Exception {
244         {
245             Parser<Object> p = new Parser<Object>("xyz %hello\\_world");
246             p.setContext(context);
247             Node t = p.parse();
248             Converter<Object> head = p.compile(t, converterMap);
249             String result = write(head, new Object());
250             assertEquals("xyz Helloworld", result);
251         }
252     }
253 
254 }