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.status.testUtil.StatusChecker;
22  //import ch.qos.logback.core.util.StatusPrinter;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  
31  public class CompilerTest {
32  
33      Map<String, String> converterMap = new HashMap<String, String>();
34      Context context = new ContextBase();
35  
36      @BeforeEach
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 converterStart() throws Exception {
84          {
85              Parser<Object> p = new Parser<Object>("abc %hello");
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      @Test
95      public void testFormat() throws Exception {
96          {
97              Parser<Object> p = new Parser<Object>("abc %7hello");
98              p.setContext(context);
99              Node t = p.parse();
100             Converter<Object> head = p.compile(t, converterMap);
101             String result = write(head, new Object());
102             assertEquals("abc   Hello", result);
103         }
104 
105         {
106             Parser<Object> p = new Parser<Object>("abc %-7hello");
107             p.setContext(context);
108             Node t = p.parse();
109             Converter<Object> head = p.compile(t, converterMap);
110             String result = write(head, new Object());
111             assertEquals("abc Hello  ", result);
112         }
113 
114         {
115             Parser<Object> p = new Parser<Object>("abc %.3hello");
116             p.setContext(context);
117             Node t = p.parse();
118             Converter<Object> head = p.compile(t, converterMap);
119             String result = write(head, new Object());
120             assertEquals("abc llo", result);
121         }
122 
123         {
124             Parser<Object> p = new Parser<Object>("abc %.-3hello");
125             p.setContext(context);
126             Node t = p.parse();
127             Converter<Object> head = p.compile(t, converterMap);
128             String result = write(head, new Object());
129             assertEquals("abc Hel", result);
130         }
131 
132         {
133             Parser<Object> p = new Parser<Object>("abc %4.5OTT");
134             p.setContext(context);
135             Node t = p.parse();
136             Converter<Object> head = p.compile(t, converterMap);
137             String result = write(head, new Object());
138             assertEquals("abc  123", result);
139         }
140         {
141             Parser<Object> p = new Parser<Object>("abc %-4.5OTT");
142             p.setContext(context);
143             Node t = p.parse();
144             Converter<Object> head = p.compile(t, converterMap);
145             String result = write(head, new Object());
146             assertEquals("abc 123 ", result);
147         }
148         {
149             Parser<Object> p = new Parser<Object>("abc %3.4hello");
150             p.setContext(context);
151             Node t = p.parse();
152             Converter<Object> head = p.compile(t, converterMap);
153             String result = write(head, new Object());
154             assertEquals("abc ello", result);
155         }
156         {
157             Parser<Object> p = new Parser<Object>("abc %-3.-4hello");
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 Hell", result);
163         }
164     }
165 
166     @Test
167     public void testComposite() throws Exception {
168         // {
169         // Parser<Object> p = new Parser<Object>("%(ABC)");
170         // p.setContext(context);
171         // Node t = p.parse();
172         // Converter<Object> head = p.compile(t, converterMap);
173         // String result = write(head, new Object());
174         // assertEquals("ABC", result);
175         // }
176         {
177             Context c = new ContextBase();
178             Parser<Object> p = new Parser<Object>("%(ABC %hello)");
179             p.setContext(c);
180             Node t = p.parse();
181             Converter<Object> head = p.compile(t, converterMap);
182             String result = write(head, new Object());
183             // StatusPrinter.print(c);
184             assertEquals("ABC Hello", result);
185         }
186         {
187             Parser<Object> p = new Parser<Object>("%(ABC %hello)");
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("ABC Hello", result);
193         }
194     }
195 
196     @Test
197     public void testCompositeFormatting() throws Exception {
198         {
199             Parser<Object> p = new Parser<Object>("xyz %4.10(ABC)");
200             p.setContext(context);
201             Node t = p.parse();
202             Converter<Object> head = p.compile(t, converterMap);
203             String result = write(head, new Object());
204             assertEquals("xyz  ABC", result);
205         }
206 
207         {
208             Parser<Object> p = new Parser<Object>("xyz %-4.10(ABC)");
209             p.setContext(context);
210             Node t = p.parse();
211             Converter<Object> head = p.compile(t, converterMap);
212             String result = write(head, new Object());
213             assertEquals("xyz ABC ", result);
214         }
215 
216         {
217             Parser<Object> p = new Parser<Object>("xyz %.2(ABC %hello)");
218             p.setContext(context);
219             Node t = p.parse();
220             Converter<Object> head = p.compile(t, converterMap);
221             String result = write(head, new Object());
222             assertEquals("xyz lo", result);
223         }
224 
225         {
226             Parser<Object> p = new Parser<Object>("xyz %.-2(ABC)");
227             p.setContext(context);
228             Node t = p.parse();
229             Converter<Object> head = p.compile(t, converterMap);
230             String result = write(head, new Object());
231             assertEquals("xyz AB", result);
232         }
233 
234         {
235             Parser<Object> p = new Parser<Object>("xyz %30.30(ABC %20hello)");
236             p.setContext(context);
237             Node t = p.parse();
238             Converter<Object> head = p.compile(t, converterMap);
239             String result = write(head, new Object());
240             assertEquals("xyz       ABC                Hello", result);
241         }
242     }
243 
244     @Test
245     public void testUnknownWord() throws Exception {
246         Parser<Object> p = new Parser<Object>("%unknown");
247         p.setContext(context);
248         Node t = p.parse();
249         p.compile(t, converterMap);
250         StatusChecker checker = new StatusChecker(context.getStatusManager());
251         checker.assertContainsMatch("\\[unknown] is not a valid conversion word");
252     }
253 
254     @Test
255     public void testWithNopEscape() throws Exception {
256         {
257             Parser<Object> p = new Parser<Object>("xyz %hello\\_world");
258             p.setContext(context);
259             Node t = p.parse();
260             Converter<Object> head = p.compile(t, converterMap);
261             String result = write(head, new Object());
262             assertEquals("xyz Helloworld", result);
263         }
264     }
265 
266 }