001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.subst;
015
016import ch.qos.logback.core.ContextBase;
017import ch.qos.logback.core.spi.ScanException;
018import org.junit.Before;
019import org.junit.Test;
020
021import static org.junit.Assert.assertEquals;
022
023/**
024 * @author Ceki Gülcü
025 */
026public class NodeToStringTransformerTest {
027
028    ContextBase propertyContainer0 = new ContextBase();
029
030    @Before
031    public void setUp() {
032        propertyContainer0.putProperty("k0", "v0");
033        propertyContainer0.putProperty("zero", "0");
034        propertyContainer0.putProperty("v0.jdbc.url", "http://..");
035        propertyContainer0.putProperty("host", "local");
036
037    }
038
039    private Node makeNode(String input) throws ScanException {
040        Tokenizer tokenizer = new Tokenizer(input);
041        Parser parser = new Parser(tokenizer.tokenize());
042        return parser.parse();
043    }
044
045    @Test
046    public void literal() throws ScanException {
047        String input = "abv";
048        Node node = makeNode(input);
049        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
050        assertEquals(input, nodeToStringTransformer.transform());
051    }
052
053    void checkInputEqualsOutput(String input) throws ScanException {
054        Node node = makeNode(input);
055        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
056        assertEquals(input, nodeToStringTransformer.transform());
057    }
058
059    @Test
060    public void literalWithNestedAccolades() throws ScanException {
061        checkInputEqualsOutput("%logger{35}");
062        checkInputEqualsOutput("%a{35} %b{35} c");
063        checkInputEqualsOutput("%replace(%msg){'\\d{14,16}', 'XXXX'}");
064        checkInputEqualsOutput("TEST %d{HHmmssSSS} [%thread] %-5level %logger{36} - %msg%n");
065    }
066
067    @Test
068    public void variable() throws ScanException {
069        String input = "${k0}";
070        Node node = makeNode(input);
071        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
072        assertEquals("v0", nodeToStringTransformer.transform());
073    }
074
075    @Test
076    public void literalVariableLiteral() throws ScanException {
077        String input = "a${k0}c";
078        Node node = makeNode(input);
079        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
080        assertEquals("av0c", nodeToStringTransformer.transform());
081    }
082
083    @Test
084    public void nestedVariable() throws ScanException {
085        String input = "a${k${zero}}b";
086        Node node = makeNode(input);
087        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
088        assertEquals("av0b", nodeToStringTransformer.transform());
089    }
090
091    @Test
092    public void LOGBACK729() throws ScanException {
093        String input = "${${k0}.jdbc.url}";
094        Node node = makeNode(input);
095        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
096        assertEquals("http://..", nodeToStringTransformer.transform());
097    }
098
099    @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}