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 static org.junit.Assert.assertEquals;
017import static org.junit.Assert.fail;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.junit.Test;
023
024import ch.qos.logback.core.Context;
025import ch.qos.logback.core.ContextBase;
026import ch.qos.logback.core.pattern.FormatInfo;
027import ch.qos.logback.core.spi.ScanException;
028import ch.qos.logback.core.testUtil.StatusChecker;
029
030public class ParserTest {
031
032    String BARE = Token.BARE_COMPOSITE_KEYWORD_TOKEN.getValue().toString();
033    Context context = new ContextBase();
034
035    @Test
036    public void testBasic() throws Exception {
037        Parser<Object> p = new Parser<>("hello");
038        Node t = p.parse();
039        assertEquals(Node.LITERAL, t.getType());
040        assertEquals("hello", t.getValue());
041    }
042
043    @Test
044    public void testKeyword() throws Exception {
045
046        {
047            Parser<Object> p = new Parser<>("hello%xyz");
048            Node t = p.parse();
049            Node witness = new Node(Node.LITERAL, "hello");
050            witness.next = new SimpleKeywordNode("xyz");
051            assertEquals(witness, t);
052        }
053
054        {
055            Parser<Object> p = new Parser<>("hello%xyz{x}");
056            Node t = p.parse();
057            Node witness = new Node(Node.LITERAL, "hello");
058            SimpleKeywordNode n = new SimpleKeywordNode("xyz");
059            List<String> optionList = new ArrayList<String>();
060            optionList.add("x");
061            n.setOptions(optionList);
062            witness.next = n;
063            assertEquals(witness, t);
064        }
065    }
066
067    @Test
068    public void testComposite() throws Exception {
069        {
070            Parser<Object> p = new Parser<>("hello%(%child)");
071            Node t = p.parse();
072
073            Node witness = new Node(Node.LITERAL, "hello");
074            CompositeNode composite = new CompositeNode(BARE);
075            Node child = new SimpleKeywordNode("child");
076            composite.setChildNode(child);
077            witness.next = composite;
078
079            // System.out.println("w:" + witness);
080            // System.out.println(t);
081
082            assertEquals(witness, t);
083        }
084
085        // System.out.println("testRecursive part 2");
086        {
087            Parser<Object> p = new Parser<>("hello%(%child )");
088            Node t = p.parse();
089
090            Node witness = new Node(Node.LITERAL, "hello");
091            CompositeNode composite = new CompositeNode(BARE);
092            Node child = new SimpleKeywordNode("child");
093            composite.setChildNode(child);
094            witness.next = composite;
095            child.next = new Node(Node.LITERAL, " ");
096            assertEquals(witness, t);
097        }
098
099        {
100            Parser<Object> p = new Parser<>("hello%(%child %h)");
101            Node t = p.parse();
102            Node witness = new Node(Node.LITERAL, "hello");
103            CompositeNode composite = new CompositeNode(BARE);
104            Node child = new SimpleKeywordNode("child");
105            composite.setChildNode(child);
106            child.next = new Node(Node.LITERAL, " ");
107            child.next.next = new SimpleKeywordNode("h");
108            witness.next = composite;
109            assertEquals(witness, t);
110        }
111
112        {
113            Parser<Object> p = new Parser<>("hello%(%child %h) %m");
114            Node t = p.parse();
115            Node witness = new Node(Node.LITERAL, "hello");
116            CompositeNode composite = new CompositeNode(BARE);
117            Node child = new SimpleKeywordNode("child");
118            composite.setChildNode(child);
119            child.next = new Node(Node.LITERAL, " ");
120            child.next.next = new SimpleKeywordNode("h");
121            witness.next = composite;
122            composite.next = new Node(Node.LITERAL, " ");
123            composite.next.next = new SimpleKeywordNode("m");
124            assertEquals(witness, t);
125        }
126
127        {
128            Parser<Object> p = new Parser<>("hello%( %child \\(%h\\) ) %m");
129            Node t = p.parse();
130            Node witness = new Node(Node.LITERAL, "hello");
131            CompositeNode composite = new CompositeNode(BARE);
132            Node child = new Node(Node.LITERAL, " ");
133            composite.setChildNode(child);
134            Node c = child;
135            c = c.next = new SimpleKeywordNode("child");
136            c = c.next = new Node(Node.LITERAL, " (");
137            c = c.next = new SimpleKeywordNode("h");
138            c = c.next = new Node(Node.LITERAL, ") ");
139            witness.next = composite;
140            composite.next = new Node(Node.LITERAL, " ");
141            composite.next.next = new SimpleKeywordNode("m");
142            assertEquals(witness, t);
143        }
144
145    }
146
147    @Test
148    public void testNested() throws Exception {
149        {
150            Parser<Object> p = new Parser<>("%top %(%child%(%h))");
151            Node t = p.parse();
152            Node witness = new SimpleKeywordNode("top");
153            Node w = witness.next = new Node(Node.LITERAL, " ");
154            CompositeNode composite = new CompositeNode(BARE);
155            w = w.next = composite;
156            Node child = new SimpleKeywordNode("child");
157            composite.setChildNode(child);
158            composite = new CompositeNode(BARE);
159            child.next = composite;
160            composite.setChildNode(new SimpleKeywordNode("h"));
161
162            assertEquals(witness, t);
163        }
164    }
165
166    @Test
167    public void testFormattingInfo() throws Exception {
168        {
169            Parser<Object> p = new Parser<>("%45x");
170            Node t = p.parse();
171            FormattingNode witness = new SimpleKeywordNode("x");
172            witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
173            assertEquals(witness, t);
174        }
175        {
176            Parser<Object> p = new Parser<>("%4.5x");
177            Node t = p.parse();
178            FormattingNode witness = new SimpleKeywordNode("x");
179            witness.setFormatInfo(new FormatInfo(4, 5));
180            assertEquals(witness, t);
181        }
182
183        {
184            Parser<Object> p = new Parser<>("%-4.5x");
185            Node t = p.parse();
186            FormattingNode witness = new SimpleKeywordNode("x");
187            witness.setFormatInfo(new FormatInfo(4, 5, false, true));
188            assertEquals(witness, t);
189        }
190        {
191            Parser<Object> p = new Parser<>("%-4.-5x");
192            Node t = p.parse();
193            FormattingNode witness = new SimpleKeywordNode("x");
194            witness.setFormatInfo(new FormatInfo(4, 5, false, false));
195            assertEquals(witness, t);
196        }
197
198        {
199            Parser<Object> p = new Parser<>("%-4.5x %12y");
200            Node t = p.parse();
201            FormattingNode witness = new SimpleKeywordNode("x");
202            witness.setFormatInfo(new FormatInfo(4, 5, false, true));
203            Node n = witness.next = new Node(Node.LITERAL, " ");
204            n = n.next = new SimpleKeywordNode("y");
205            ((FormattingNode) n).setFormatInfo(new FormatInfo(12, Integer.MAX_VALUE));
206            assertEquals(witness, t);
207        }
208    }
209
210    @Test
211    public void testOptions0() throws Exception {
212        Parser<Object> p = new Parser<>("%45x{'test '}");
213        Node t = p.parse();
214        SimpleKeywordNode witness = new SimpleKeywordNode("x");
215        witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
216        List<String> ol = new ArrayList<String>();
217        ol.add("test ");
218        witness.setOptions(ol);
219        assertEquals(witness, t);
220    }
221
222    @Test
223    public void testOptions1() throws Exception {
224        Parser<Object> p = new Parser<>("%45x{a, b}");
225        Node t = p.parse();
226        SimpleKeywordNode witness = new SimpleKeywordNode("x");
227        witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
228        List<String> ol = new ArrayList<String>();
229        ol.add("a");
230        ol.add("b");
231        witness.setOptions(ol);
232        assertEquals(witness, t);
233    }
234
235    // see http://jira.qos.ch/browse/LBCORE-180
236    @Test
237    public void keywordGluedToLitteral() throws Exception {
238        Parser<Object> p = new Parser<>("%x{}a");
239        Node t = p.parse();
240        SimpleKeywordNode witness = new SimpleKeywordNode("x");
241        witness.setOptions(new ArrayList<String>());
242        witness.next = new Node(Node.LITERAL, "a");
243        assertEquals(witness, t);
244    }
245
246    @Test
247    public void testCompositeFormatting() throws Exception {
248        Parser<Object> p = new Parser<>("hello%5(XYZ)");
249        Node t = p.parse();
250
251        Node witness = new Node(Node.LITERAL, "hello");
252        CompositeNode composite = new CompositeNode(BARE);
253        composite.setFormatInfo(new FormatInfo(5, Integer.MAX_VALUE));
254        Node child = new Node(Node.LITERAL, "XYZ");
255        composite.setChildNode(child);
256        witness.next = composite;
257
258        assertEquals(witness, t);
259
260    }
261
262    @Test
263    public void empty() {
264        try {
265            Parser<Object> p = new Parser<>("");
266            p.parse();
267            fail("");
268        } catch (ScanException e) {
269
270        }
271    }
272
273    @Test
274    public void lbcore193() throws Exception {
275        try {
276            Parser<Object> p = new Parser<>("hello%(abc");
277            p.setContext(context);
278            p.parse();
279            fail("where the is exception?");
280        } catch (ScanException ise) {
281            assertEquals("Expecting RIGHT_PARENTHESIS token but got null", ise.getMessage());
282        }
283        StatusChecker sc = new StatusChecker(context);
284        sc.assertContainsMatch("Expecting RIGHT_PARENTHESIS");
285        sc.assertContainsMatch("See also " + Parser.MISSING_RIGHT_PARENTHESIS);
286    }
287
288}