1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.subst;
15
16 import ch.qos.logback.core.ContextBase;
17 import ch.qos.logback.core.spi.ScanException;
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21
22
23
24
25 public class NodeToStringTransformerTest {
26
27 ContextBase propertyContainer0 = new ContextBase();
28
29 @BeforeEach
30 public void setUp() {
31 propertyContainer0.putProperty("k0", "v0");
32 propertyContainer0.putProperty("zero", "0");
33 propertyContainer0.putProperty("v0.jdbc.url", "http://..");
34 propertyContainer0.putProperty("host", "local");
35
36 }
37
38 private Node makeNode(String input) throws ScanException {
39 Tokenizer tokenizer = new Tokenizer(input);
40 Parser parser = new Parser(tokenizer.tokenize());
41 return parser.parse();
42 }
43
44 @Test
45 public void literal() throws ScanException {
46 String input = "abv";
47 Node node = makeNode(input);
48 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
49 Assertions.assertEquals(input, nodeToStringTransformer.transform());
50 }
51
52 void checkInputEqualsOutput(String input) throws ScanException {
53 Node node = makeNode(input);
54 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
55 Assertions.assertEquals(input, nodeToStringTransformer.transform());
56 }
57
58 @Test
59 public void literalWithNestedAccolades() throws ScanException {
60 checkInputEqualsOutput("%logger{35}");
61 checkInputEqualsOutput("%a{35} %b{35} c");
62 checkInputEqualsOutput("%replace(%msg){'\\d{14,16}', 'XXXX'}");
63 checkInputEqualsOutput("TEST %d{HHmmssSSS} [%thread] %-5level %logger{36} - %msg%n");
64 }
65
66 @Test
67 public void variable() throws ScanException {
68 String input = "${k0}";
69 Node node = makeNode(input);
70 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
71 Assertions.assertEquals("v0", nodeToStringTransformer.transform());
72 }
73
74 @Test
75 public void recursion0() throws ScanException {
76 assumeCycle("${nested:-${nested}}");
77 assumeCycle("${:-${}}");
78 assumeCycle("${$a:-${a:-${a:-b}}");
79 }
80
81 @Test
82 public void recursion1() throws ScanException {
83 propertyContainer0.putProperty("k", "${a}");
84 propertyContainer0.putProperty("a", "${k}");
85 assumeCycle("${k}");
86 }
87
88
89 @Test
90 public void cascadedTransformation() throws ScanException {
91 propertyContainer0.putProperty("x", "${a}");
92 propertyContainer0.putProperty("a", "b");
93 propertyContainer0.putProperty("b", "c");
94 String result = transform("${${x}}");
95 Assertions.assertEquals("c", result);
96 }
97
98 public void assumeCycle(String input) throws ScanException {
99
100 try {
101 transform(input);
102 } catch (IllegalArgumentException e) {
103 return;
104 }
105 Assertions.fail("circular reference should have been caught input=" + input);
106 }
107
108 private String transform(String input) throws ScanException {
109 Node node = makeNode(input);
110 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
111 return nodeToStringTransformer.transform();
112 }
113
114 @Test
115 public void literalVariableLiteral() throws ScanException {
116 String input = "a${k0}c";
117 Node node = makeNode(input);
118 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
119 Assertions.assertEquals("av0c", nodeToStringTransformer.transform());
120 }
121
122 @Test
123 public void nestedVariable() throws ScanException {
124 String input = "a${k${zero}}b";
125 Node node = makeNode(input);
126 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
127 Assertions.assertEquals("av0b", nodeToStringTransformer.transform());
128 }
129
130 @Test
131 public void LOGBACK729() throws ScanException {
132 String input = "${${k0}.jdbc.url}";
133 Node node = makeNode(input);
134 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
135 Assertions.assertEquals("http://..", nodeToStringTransformer.transform());
136 }
137
138 @Test
139 public void LOGBACK744_withColon() throws ScanException {
140 String input = "%d{HH:mm:ss.SSS} host:${host} %logger{36} - %msg%n";
141 Node node = makeNode(input);
142 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
143 System.out.println(nodeToStringTransformer.transform());
144 Assertions.assertEquals("%d{HH:mm:ss.SSS} host:local %logger{36} - %msg%n", nodeToStringTransformer.transform());
145 }
146
147 @Test
148 public void loneColonShouldReadLikeAnyOtherCharacter() throws ScanException {
149 String input = "java:comp/env/jdbc/datasource";
150 Node node = makeNode(input);
151 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
152 Assertions.assertEquals(input, nodeToStringTransformer.transform());
153 }
154
155 @Test
156 public void withDefaultValue() throws ScanException {
157 String input = "${k67:-b}c";
158 Node node = makeNode(input);
159 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
160 Assertions.assertEquals("bc", nodeToStringTransformer.transform());
161 }
162
163 @Test
164 public void defaultValueNestedAsVar() throws ScanException {
165 String input = "a${k67:-x${k0}}c";
166 Node node = makeNode(input);
167 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
168 Assertions.assertEquals("axv0c", nodeToStringTransformer.transform());
169 }
170
171 @Test
172 public void LOGBACK_1101() throws ScanException {
173 String input = "a: {y}";
174 Node node = makeNode(input);
175 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
176 Assertions.assertEquals("a: {y}", nodeToStringTransformer.transform());
177 }
178
179 @Test
180 public void definedAsEmpty() throws ScanException {
181 propertyContainer0.putProperty("empty", "");
182 String input = "a=${empty}";
183 Node node = makeNode(input);
184 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
185 Assertions.assertEquals("a=", nodeToStringTransformer.transform());
186 }
187
188 @Test
189 public void emptyDefault() throws ScanException {
190 propertyContainer0.putProperty("empty", "");
191 String input = "a=${undef:-${empty}}";
192 Node node = makeNode(input);
193 NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
194 Assertions.assertEquals("a=", nodeToStringTransformer.transform());
195 }
196 }