1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.pattern.parser;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.fail;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.junit.Test;
23
24 import ch.qos.logback.core.Context;
25 import ch.qos.logback.core.ContextBase;
26 import ch.qos.logback.core.pattern.FormatInfo;
27 import ch.qos.logback.core.spi.ScanException;
28 import ch.qos.logback.core.testUtil.StatusChecker;
29
30 public class ParserTest {
31
32 String BARE = Token.BARE_COMPOSITE_KEYWORD_TOKEN.getValue().toString();
33 Context context = new ContextBase();
34
35 @Test
36 public void testBasic() throws Exception {
37 Parser<Object> p = new Parser<>("hello");
38 Node t = p.parse();
39 assertEquals(Node.LITERAL, t.getType());
40 assertEquals("hello", t.getValue());
41 }
42
43 @Test
44 public void testKeyword() throws Exception {
45
46 {
47 Parser<Object> p = new Parser<>("hello%xyz");
48 Node t = p.parse();
49 Node witness = new Node(Node.LITERAL, "hello");
50 witness.next = new SimpleKeywordNode("xyz");
51 assertEquals(witness, t);
52 }
53
54 {
55 Parser<Object> p = new Parser<>("hello%xyz{x}");
56 Node t = p.parse();
57 Node witness = new Node(Node.LITERAL, "hello");
58 SimpleKeywordNode n = new SimpleKeywordNode("xyz");
59 List<String> optionList = new ArrayList<String>();
60 optionList.add("x");
61 n.setOptions(optionList);
62 witness.next = n;
63 assertEquals(witness, t);
64 }
65 }
66
67 @Test
68 public void testComposite() throws Exception {
69 {
70 Parser<Object> p = new Parser<>("hello%(%child)");
71 Node t = p.parse();
72
73 Node witness = new Node(Node.LITERAL, "hello");
74 CompositeNode composite = new CompositeNode(BARE);
75 Node child = new SimpleKeywordNode("child");
76 composite.setChildNode(child);
77 witness.next = composite;
78
79
80
81
82 assertEquals(witness, t);
83 }
84
85
86 {
87 Parser<Object> p = new Parser<>("hello%(%child )");
88 Node t = p.parse();
89
90 Node witness = new Node(Node.LITERAL, "hello");
91 CompositeNode composite = new CompositeNode(BARE);
92 Node child = new SimpleKeywordNode("child");
93 composite.setChildNode(child);
94 witness.next = composite;
95 child.next = new Node(Node.LITERAL, " ");
96 assertEquals(witness, t);
97 }
98
99 {
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
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 }