001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.pattern.parser;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.ContextBase;
018import ch.qos.logback.core.pattern.Converter;
019import ch.qos.logback.core.pattern.Converter123;
020import ch.qos.logback.core.pattern.ConverterHello;
021import ch.qos.logback.core.testUtil.StatusChecker;
022import ch.qos.logback.core.util.StatusPrinter;
023import org.junit.Before;
024import org.junit.Test;
025
026import java.util.HashMap;
027import java.util.Map;
028
029import static org.junit.Assert.assertEquals;
030
031public class CompilerTest {
032
033    Map<String, String> converterMap = new HashMap<String, String>();
034    Context context = new ContextBase();
035
036    @Before
037    public void setUp() {
038        converterMap.put("OTT", Converter123.class.getName());
039        converterMap.put("hello", ConverterHello.class.getName());
040        converterMap.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
041    }
042
043    String write(final Converter<Object> head, Object event) {
044        StringBuilder buf = new StringBuilder();
045        Converter<Object> c = head;
046        while (c != null) {
047            c.write(buf, event);
048            c = c.getNext();
049        }
050        return buf.toString();
051    }
052
053    @Test
054    public void testLiteral() throws Exception {
055        Parser<Object> p = new Parser<Object>("hello");
056        Node t = p.parse();
057        Converter<Object> head = p.compile(t, converterMap);
058        String result = write(head, new Object());
059        assertEquals("hello", result);
060    }
061
062    @Test
063    public void testBasic() throws Exception {
064        {
065            Parser<Object> p = new Parser<Object>("abc %hello");
066            p.setContext(context);
067            Node t = p.parse();
068            Converter<Object> head = p.compile(t, converterMap);
069            String result = write(head, new Object());
070            assertEquals("abc Hello", result);
071        }
072        {
073            Parser<Object> p = new Parser<Object>("abc %hello %OTT");
074            p.setContext(context);
075            Node t = p.parse();
076            Converter<Object> head = p.compile(t, converterMap);
077            String result = write(head, new Object());
078            assertEquals("abc Hello 123", result);
079        }
080    }
081
082    @Test
083    public void testFormat() throws Exception {
084        {
085            Parser<Object> p = new Parser<Object>("abc %7hello");
086            p.setContext(context);
087            Node t = p.parse();
088            Converter<Object> head = p.compile(t, converterMap);
089            String result = write(head, new Object());
090            assertEquals("abc   Hello", result);
091        }
092
093        {
094            Parser<Object> p = new Parser<Object>("abc %-7hello");
095            p.setContext(context);
096            Node t = p.parse();
097            Converter<Object> head = p.compile(t, converterMap);
098            String result = write(head, new Object());
099            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}