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.ContextBase;
17  import ch.qos.logback.core.spi.ScanException;
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  /**
24   * @author Ceki Gülcü
25   */
26  public class NodeToStringTransformerTest {
27  
28      ContextBase propertyContainer0 = new ContextBase();
29  
30      @Before
31      public void setUp() {
32          propertyContainer0.putProperty("k0", "v0");
33          propertyContainer0.putProperty("zero", "0");
34          propertyContainer0.putProperty("v0.jdbc.url", "http://..");
35          propertyContainer0.putProperty("host", "local");
36  
37      }
38  
39      private Node makeNode(String input) throws ScanException {
40          Tokenizer tokenizer = new Tokenizer(input);
41          Parser parser = new Parser(tokenizer.tokenize());
42          return parser.parse();
43      }
44  
45      @Test
46      public void literal() throws ScanException {
47          String input = "abv";
48          Node node = makeNode(input);
49          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
50          assertEquals(input, nodeToStringTransformer.transform());
51      }
52  
53      void checkInputEqualsOutput(String input) throws ScanException {
54          Node node = makeNode(input);
55          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
56          assertEquals(input, nodeToStringTransformer.transform());
57      }
58  
59      @Test
60      public void literalWithNestedAccolades() throws ScanException {
61          checkInputEqualsOutput("%logger{35}");
62          checkInputEqualsOutput("%a{35} %b{35} c");
63          checkInputEqualsOutput("%replace(%msg){'\\d{14,16}', 'XXXX'}");
64          checkInputEqualsOutput("TEST %d{HHmmssSSS} [%thread] %-5level %logger{36} - %msg%n");
65      }
66  
67      @Test
68      public void variable() throws ScanException {
69          String input = "${k0}";
70          Node node = makeNode(input);
71          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
72          assertEquals("v0", nodeToStringTransformer.transform());
73      }
74  
75      @Test
76      public void literalVariableLiteral() throws ScanException {
77          String input = "a${k0}c";
78          Node node = makeNode(input);
79          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
80          assertEquals("av0c", nodeToStringTransformer.transform());
81      }
82  
83      @Test
84      public void nestedVariable() throws ScanException {
85          String input = "a${k${zero}}b";
86          Node node = makeNode(input);
87          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
88          assertEquals("av0b", nodeToStringTransformer.transform());
89      }
90  
91      @Test
92      public void LOGBACK729() throws ScanException {
93          String input = "${${k0}.jdbc.url}";
94          Node node = makeNode(input);
95          NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
96          assertEquals("http://..", nodeToStringTransformer.transform());
97      }
98  
99      @Test
100     public void LOGBACK744_withColon() throws ScanException {
101         String input = "%d{HH:mm:ss.SSS} host:${host} %logger{36} - %msg%n";
102         Node node = makeNode(input);
103         NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
104         System.out.println(nodeToStringTransformer.transform());
105         assertEquals("%d{HH:mm:ss.SSS} host:local %logger{36} - %msg%n", nodeToStringTransformer.transform());
106     }
107 
108     @Test
109     public void loneColonShouldReadLikeAnyOtherCharacter() throws ScanException {
110         String input = "java:comp/env/jdbc/datasource";
111         Node node = makeNode(input);
112         NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
113         assertEquals(input, nodeToStringTransformer.transform());
114     }
115 
116     @Test
117     public void withDefaultValue() throws ScanException {
118         String input = "${k67:-b}c";
119         Node node = makeNode(input);
120         NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
121         assertEquals("bc", nodeToStringTransformer.transform());
122     }
123 
124     @Test
125     public void defaultValueNestedAsVar() throws ScanException {
126         String input = "a${k67:-x${k0}}c";
127         Node node = makeNode(input);
128         NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
129         assertEquals("axv0c", nodeToStringTransformer.transform());
130     }
131 
132     @Test
133     public void LOGBACK_1101() throws ScanException {
134         String input = "a: {y}";
135         Node node = makeNode(input);
136         NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
137         assertEquals("a: {y}", nodeToStringTransformer.transform());
138     }
139 }