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 java.util.ArrayList;
17  import java.util.List;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  
22  import ch.qos.logback.core.Context;
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.pattern.FormatInfo;
25  import ch.qos.logback.core.spi.ScanException;
26  import ch.qos.logback.core.status.testUtil.StatusChecker;
27  
28  public class ParserTest {
29  
30      String BARE = Token.BARE_COMPOSITE_KEYWORD_TOKEN.getValue().toString();
31      Context context = new ContextBase();
32  
33      @Test
34      public void testBasic() throws Exception {
35          Parser<Object> p = new Parser<>("hello");
36          Node t = p.parse();
37          Assertions.assertEquals(Node.LITERAL, t.getType());
38          Assertions.assertEquals("hello", t.getValue());
39      }
40  
41      @Test
42      public void testKeyword() throws Exception {
43  
44          {
45              Parser<Object> p = new Parser<>("hello%xyz");
46              Node t = p.parse();
47              Node witness = new Node(Node.LITERAL, "hello");
48              witness.next = new SimpleKeywordNode("xyz");
49              Assertions.assertEquals(witness, t);
50          }
51  
52          {
53              Parser<Object> p = new Parser<>("hello%xyz{x}");
54              Node t = p.parse();
55              Node witness = new Node(Node.LITERAL, "hello");
56              SimpleKeywordNode n = new SimpleKeywordNode("xyz");
57              List<String> optionList = new ArrayList<String>();
58              optionList.add("x");
59              n.setOptions(optionList);
60              witness.next = n;
61              Assertions.assertEquals(witness, t);
62          }
63      }
64  
65      @Test
66      public void testComposite() throws Exception {
67          {
68              Parser<Object> p = new Parser<>("hello%(%child)");
69              Node t = p.parse();
70  
71              Node witness = new Node(Node.LITERAL, "hello");
72              CompositeNode composite = new CompositeNode(BARE);
73              Node child = new SimpleKeywordNode("child");
74              composite.setChildNode(child);
75              witness.next = composite;
76  
77              // System.out.println("w:" + witness);
78              // System.out.println(t);
79  
80              Assertions.assertEquals(witness, t);
81          }
82  
83          // System.out.println("testRecursive part 2");
84          {
85              Parser<Object> p = new Parser<>("hello%(%child )");
86              Node t = p.parse();
87  
88              Node witness = new Node(Node.LITERAL, "hello");
89              CompositeNode composite = new CompositeNode(BARE);
90              Node child = new SimpleKeywordNode("child");
91              composite.setChildNode(child);
92              witness.next = composite;
93              child.next = new Node(Node.LITERAL, " ");
94              Assertions.assertEquals(witness, t);
95          }
96  
97          {
98              Parser<Object> p = new Parser<>("hello%(%child %h)");
99              Node t = p.parse();
100             Node witness = new Node(Node.LITERAL, "hello");
101             CompositeNode composite = new CompositeNode(BARE);
102             Node child = new SimpleKeywordNode("child");
103             composite.setChildNode(child);
104             child.next = new Node(Node.LITERAL, " ");
105             child.next.next = new SimpleKeywordNode("h");
106             witness.next = composite;
107             Assertions.assertEquals(witness, t);
108         }
109 
110         {
111             Parser<Object> p = new Parser<>("hello%(%child %h) %m");
112             Node t = p.parse();
113             Node witness = new Node(Node.LITERAL, "hello");
114             CompositeNode composite = new CompositeNode(BARE);
115             Node child = new SimpleKeywordNode("child");
116             composite.setChildNode(child);
117             child.next = new Node(Node.LITERAL, " ");
118             child.next.next = new SimpleKeywordNode("h");
119             witness.next = composite;
120             composite.next = new Node(Node.LITERAL, " ");
121             composite.next.next = new SimpleKeywordNode("m");
122             Assertions.assertEquals(witness, t);
123         }
124 
125         {
126             Parser<Object> p = new Parser<>("hello%( %child \\(%h\\) ) %m");
127             Node t = p.parse();
128             Node witness = new Node(Node.LITERAL, "hello");
129             CompositeNode composite = new CompositeNode(BARE);
130             Node child = new Node(Node.LITERAL, " ");
131             composite.setChildNode(child);
132             Node c = child;
133             c = c.next = new SimpleKeywordNode("child");
134             c = c.next = new Node(Node.LITERAL, " (");
135             c = c.next = new SimpleKeywordNode("h");
136             c = c.next = new Node(Node.LITERAL, ") ");
137             witness.next = composite;
138             composite.next = new Node(Node.LITERAL, " ");
139             composite.next.next = new SimpleKeywordNode("m");
140             Assertions.assertEquals(witness, t);
141         }
142 
143     }
144 
145     @Test
146     public void testNested() throws Exception {
147         {
148             Parser<Object> p = new Parser<>("%top %(%child%(%h))");
149             Node t = p.parse();
150             Node witness = new SimpleKeywordNode("top");
151             Node w = witness.next = new Node(Node.LITERAL, " ");
152             CompositeNode composite = new CompositeNode(BARE);
153             w = w.next = composite;
154             Node child = new SimpleKeywordNode("child");
155             composite.setChildNode(child);
156             composite = new CompositeNode(BARE);
157             child.next = composite;
158             composite.setChildNode(new SimpleKeywordNode("h"));
159 
160             Assertions.assertEquals(witness, t);
161         }
162     }
163 
164     @Test
165     public void testFormattingInfo() throws Exception {
166         {
167             Parser<Object> p = new Parser<>("%45x");
168             Node t = p.parse();
169             FormattingNode witness = new SimpleKeywordNode("x");
170             witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
171             Assertions.assertEquals(witness, t);
172         }
173         {
174             Parser<Object> p = new Parser<>("%4.5x");
175             Node t = p.parse();
176             FormattingNode witness = new SimpleKeywordNode("x");
177             witness.setFormatInfo(new FormatInfo(4, 5));
178             Assertions.assertEquals(witness, t);
179         }
180 
181         {
182             Parser<Object> p = new Parser<>("%-4.5x");
183             Node t = p.parse();
184             FormattingNode witness = new SimpleKeywordNode("x");
185             witness.setFormatInfo(new FormatInfo(4, 5, false, true));
186             Assertions.assertEquals(witness, t);
187         }
188         {
189             Parser<Object> p = new Parser<>("%-4.-5x");
190             Node t = p.parse();
191             FormattingNode witness = new SimpleKeywordNode("x");
192             witness.setFormatInfo(new FormatInfo(4, 5, false, false));
193             Assertions.assertEquals(witness, t);
194         }
195 
196         {
197             Parser<Object> p = new Parser<>("%-4.5x %12y");
198             Node t = p.parse();
199             FormattingNode witness = new SimpleKeywordNode("x");
200             witness.setFormatInfo(new FormatInfo(4, 5, false, true));
201             Node n = witness.next = new Node(Node.LITERAL, " ");
202             n = n.next = new SimpleKeywordNode("y");
203             ((FormattingNode) n).setFormatInfo(new FormatInfo(12, Integer.MAX_VALUE));
204             Assertions.assertEquals(witness, t);
205         }
206     }
207 
208     @Test
209     public void testOptions0() throws Exception {
210         Parser<Object> p = new Parser<>("%45x{'test '}");
211         Node t = p.parse();
212         SimpleKeywordNode witness = new SimpleKeywordNode("x");
213         witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
214         List<String> ol = new ArrayList<String>();
215         ol.add("test ");
216         witness.setOptions(ol);
217         Assertions.assertEquals(witness, t);
218     }
219 
220     @Test
221     public void testOptions1() throws Exception {
222         Parser<Object> p = new Parser<>("%45x{a, b}");
223         Node t = p.parse();
224         SimpleKeywordNode witness = new SimpleKeywordNode("x");
225         witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
226         List<String> ol = new ArrayList<String>();
227         ol.add("a");
228         ol.add("b");
229         witness.setOptions(ol);
230         Assertions.assertEquals(witness, t);
231     }
232 
233     // see http://jira.qos.ch/browse/LBCORE-180
234     @Test
235     public void keywordGluedToLitteral() throws Exception {
236         Parser<Object> p = new Parser<>("%x{}a");
237         Node t = p.parse();
238         SimpleKeywordNode witness = new SimpleKeywordNode("x");
239         witness.setOptions(new ArrayList<String>());
240         witness.next = new Node(Node.LITERAL, "a");
241         Assertions.assertEquals(witness, t);
242     }
243 
244     @Test
245     public void testCompositeFormatting() throws Exception {
246         Parser<Object> p = new Parser<>("hello%5(XYZ)");
247         Node t = p.parse();
248 
249         Node witness = new Node(Node.LITERAL, "hello");
250         CompositeNode composite = new CompositeNode(BARE);
251         composite.setFormatInfo(new FormatInfo(5, Integer.MAX_VALUE));
252         Node child = new Node(Node.LITERAL, "XYZ");
253         composite.setChildNode(child);
254         witness.next = composite;
255 
256         Assertions.assertEquals(witness, t);
257 
258     }
259 
260     @Test
261     public void empty() {
262         try {
263             Parser<Object> p = new Parser<>("");
264             p.parse();
265             Assertions.fail("");
266         } catch (ScanException e) {
267 
268         }
269     }
270 
271     @Test
272     public void lbcore193() throws Exception {
273         try {
274             Parser<Object> p = new Parser<>("hello%(abc");
275             p.setContext(context);
276             p.parse();
277             Assertions.fail("where the is exception?");
278         } catch (ScanException ise) {
279             Assertions.assertEquals("Expecting RIGHT_PARENTHESIS token but got null", ise.getMessage());
280         }
281         StatusChecker sc = new StatusChecker(context);
282         sc.assertContainsMatch("Expecting RIGHT_PARENTHESIS");
283         sc.assertContainsMatch("See also " + Parser.MISSING_RIGHT_PARENTHESIS);
284     }
285 
286 }