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.subst;
15  
16  import ch.qos.logback.core.spi.ScanException;
17  import org.junit.jupiter.api.Test;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  public class TokenizerTest {
25  
26      List<Token> witnessList = new ArrayList<Token>();
27  
28      @Test
29      public void literalOnly() throws ScanException {
30          String input = "abc";
31          Tokenizer tokenizer = new Tokenizer(input);
32          List<Token> tokenList = tokenizer.tokenize();
33          witnessList.add(new Token(Token.Type.LITERAL, input));
34          assertEquals(witnessList, tokenList);
35      }
36  
37      @Test
38      public void literalWithAccolades() throws ScanException {
39          String input0 = "%logger";
40          String input1 = "24";
41          String input2 = " - %m";
42          String input = input0 + "{" + input1 + "}" + input2;
43          Tokenizer tokenizer = new Tokenizer(input);
44          List<Token> tokenList = tokenizer.tokenize();
45          witnessList.add(new Token(Token.Type.LITERAL, input0));
46          witnessList.add(Token.CURLY_LEFT_TOKEN);
47          witnessList.add(new Token(Token.Type.LITERAL, input1));
48  
49          witnessList.add(Token.CURLY_RIGHT_TOKEN);
50          witnessList.add(new Token(Token.Type.LITERAL, input2));
51          assertEquals(witnessList, tokenList);
52      }
53  
54      @Test
55      public void simleVariable() throws ScanException {
56          String input = "${abc}";
57          Tokenizer tokenizer = new Tokenizer(input);
58          List<Token> tokenList = tokenizer.tokenize();
59          witnessList.add(Token.START_TOKEN);
60          witnessList.add(new Token(Token.Type.LITERAL, "abc"));
61          witnessList.add(Token.CURLY_RIGHT_TOKEN);
62          assertEquals(witnessList, tokenList);
63      }
64  
65      @Test
66      public void mix() throws ScanException {
67          String input = "a${b}c";
68          Tokenizer tokenizer = new Tokenizer(input);
69          List<Token> tokenList = tokenizer.tokenize();
70          witnessList.add(new Token(Token.Type.LITERAL, "a"));
71          witnessList.add(Token.START_TOKEN);
72          witnessList.add(new Token(Token.Type.LITERAL, "b"));
73          witnessList.add(Token.CURLY_RIGHT_TOKEN);
74          witnessList.add(new Token(Token.Type.LITERAL, "c"));
75          assertEquals(witnessList, tokenList);
76      }
77  
78      @Test
79      public void nested() throws ScanException {
80          String input = "a${b${c}}";
81          Tokenizer tokenizer = new Tokenizer(input);
82          List<Token> tokenList = tokenizer.tokenize();
83          witnessList.add(new Token(Token.Type.LITERAL, "a"));
84          witnessList.add(Token.START_TOKEN);
85          witnessList.add(new Token(Token.Type.LITERAL, "b"));
86          witnessList.add(Token.START_TOKEN);
87          witnessList.add(new Token(Token.Type.LITERAL, "c"));
88          witnessList.add(Token.CURLY_RIGHT_TOKEN);
89          witnessList.add(Token.CURLY_RIGHT_TOKEN);
90          assertEquals(witnessList, tokenList);
91      }
92  
93      @Test
94      public void basicDefaultSeparator() throws ScanException {
95          String input = "${a:-b}";
96          Tokenizer tokenizer = new Tokenizer(input);
97          List<Token> tokenList = tokenizer.tokenize();
98          witnessList.add(Token.START_TOKEN);
99          witnessList.add(new Token(Token.Type.LITERAL, "a"));
100         witnessList.add(Token.DEFAULT_SEP_TOKEN);
101         witnessList.add(new Token(Token.Type.LITERAL, "b"));
102         witnessList.add(Token.CURLY_RIGHT_TOKEN);
103         assertEquals(witnessList, tokenList);
104     }
105 
106     @Test
107     public void colon() throws ScanException {
108         String input = "a:b";
109         Tokenizer tokenizer = new Tokenizer(input);
110         List<Token> tokenList = tokenizer.tokenize();
111         witnessList.add(new Token(Token.Type.LITERAL, "a"));
112         witnessList.add(new Token(Token.Type.LITERAL, ":b"));
113         assertEquals(witnessList, tokenList);
114     }
115 
116     // /LOGBACK-744
117     @Test
118     public void colonFollowedByDollar() throws ScanException {
119         String input = "a:${b}";
120         Tokenizer tokenizer = new Tokenizer(input);
121         List<Token> tokenList = tokenizer.tokenize();
122         witnessList.add(new Token(Token.Type.LITERAL, "a"));
123         witnessList.add(new Token(Token.Type.LITERAL, ":"));
124         witnessList.add(Token.START_TOKEN);
125         witnessList.add(new Token(Token.Type.LITERAL, "b"));
126         witnessList.add(Token.CURLY_RIGHT_TOKEN);
127         assertEquals(witnessList, tokenList);
128 
129     }
130 
131     @Test
132     public void defaultSeparatorOutsideVariable() throws ScanException {
133 
134         String input = "{a:-b}";
135         Tokenizer tokenizer = new Tokenizer(input);
136         List<Token> tokenList = tokenizer.tokenize();
137         witnessList.add(Token.CURLY_LEFT_TOKEN);
138         witnessList.add(new Token(Token.Type.LITERAL, "a"));
139         witnessList.add(Token.DEFAULT_SEP_TOKEN);
140         witnessList.add(new Token(Token.Type.LITERAL, "b"));
141         witnessList.add(Token.CURLY_RIGHT_TOKEN);
142         assertEquals(witnessList, tokenList);
143     }
144 
145     @Test
146     public void literalContainingColon() throws ScanException {
147         String input = "a:b";
148         Tokenizer tokenizer = new Tokenizer(input);
149         List<Token> tokenList = tokenizer.tokenize();
150         witnessList.add(new Token(Token.Type.LITERAL, "a"));
151         witnessList.add(new Token(Token.Type.LITERAL, ":b"));
152         assertEquals(witnessList, tokenList);
153     }
154 
155     @Test
156     public void literalEndingWithColon_LOGBACK_1140() throws ScanException {
157         String input = "a:";
158         Tokenizer tokenizer = new Tokenizer(input);
159         List<Token> tokenList = tokenizer.tokenize();
160         witnessList.add(new Token(Token.Type.LITERAL, "a"));
161         witnessList.add(new Token(Token.Type.LITERAL, ":"));
162         assertEquals(witnessList, tokenList);
163     }
164 
165     @Test
166     public void literalEndingWithDollar_LOGBACK_1149() throws ScanException {
167         String input = "a$";
168         Tokenizer tokenizer = new Tokenizer(input);
169         List<Token> tokenList = tokenizer.tokenize();
170         witnessList.add(new Token(Token.Type.LITERAL, "a"));
171         witnessList.add(new Token(Token.Type.LITERAL, "$"));
172         assertEquals(witnessList, tokenList);
173     }
174 
175     @Test
176     public void LOGBACK_1101() throws ScanException {
177         String input = "a:{y}";
178         Tokenizer tokenizer = new Tokenizer(input);
179         List<Token> tokenList = tokenizer.tokenize();
180         witnessList.add(new Token(Token.Type.LITERAL, "a"));
181 
182         witnessList.add(new Token(Token.Type.LITERAL, ":"));
183         witnessList.add(Token.CURLY_LEFT_TOKEN);
184         witnessList.add(new Token(Token.Type.LITERAL, "y"));
185 
186         witnessList.add(Token.CURLY_RIGHT_TOKEN);
187         assertEquals(witnessList, tokenList);
188     }
189 
190 }