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 static org.junit.Assert.assertEquals; 017 018import java.util.ArrayList; 019 020import org.junit.Test; 021 022import ch.qos.logback.core.spi.ScanException; 023 024/** 025 * Created with IntelliJ IDEA. 026 * User: ceki 027 * Date: 05.08.12 028 * Time: 00:15 029 * To change this template use File | Settings | File Templates. 030 */ 031public class ParserTest { 032 033 @Test 034 public void literal() throws ScanException { 035 Tokenizer tokenizer = new Tokenizer("abc"); 036 Parser parser = new Parser(tokenizer.tokenize()); 037 Node node = parser.parse(); 038 Node witness = new Node(Node.Type.LITERAL, "abc"); 039 assertEquals(witness, node); 040 } 041 042 @Test 043 public void literalWithAccolade0() throws ScanException { 044 Tokenizer tokenizer = new Tokenizer("{}"); 045 Parser parser = new Parser(tokenizer.tokenize()); 046 Node node = parser.parse(); 047 Node witness = new Node(Node.Type.LITERAL, "{"); 048 witness.next = new Node(Node.Type.LITERAL, "}"); 049 assertEquals(witness, node); 050 } 051 052 @Test 053 public void literalWithAccolade1() throws ScanException { 054 Tokenizer tokenizer = new Tokenizer("%x{a}"); 055 Parser parser = new Parser(tokenizer.tokenize()); 056 Node node = parser.parse(); 057 Node witness = new Node(Node.Type.LITERAL, "%x"); 058 Node t = witness.next = new Node(Node.Type.LITERAL, "{"); 059 t.next = new Node(Node.Type.LITERAL, "a"); 060 t = t.next; 061 t.next = new Node(Node.Type.LITERAL, "}"); 062 assertEquals(witness, node); 063 } 064 065 @Test 066 public void literalWithTwoAccolades() throws ScanException { 067 Tokenizer tokenizer = new Tokenizer("%x{y} %a{b} c"); 068 069 Parser parser = new Parser(tokenizer.tokenize()); 070 Node node = parser.parse(); 071 Node witness = new Node(Node.Type.LITERAL, "%x"); 072 073 Node t = witness.next = new Node(Node.Type.LITERAL, "{"); 074 t.next = new Node(Node.Type.LITERAL, "y"); 075 t = t.next; 076 077 t.next = new Node(Node.Type.LITERAL, "}"); 078 t = t.next; 079 080 t.next = new Node(Node.Type.LITERAL, " %a"); 081 t = t.next; 082 083 t.next = new Node(Node.Type.LITERAL, "{"); 084 t = t.next; 085 086 t.next = new Node(Node.Type.LITERAL, "b"); 087 t = t.next; 088 089 t.next = new Node(Node.Type.LITERAL, "}"); 090 t = t.next; 091 092 t.next = new Node(Node.Type.LITERAL, " c"); 093 094 node.dump(); 095 System.out.println(""); 096 assertEquals(witness, node); 097 } 098 099 @Test 100 public void variable() throws ScanException { 101 Tokenizer tokenizer = new Tokenizer("${abc}"); 102 Parser parser = new Parser(tokenizer.tokenize()); 103 Node node = parser.parse(); 104 Node witness = new Node(Node.Type.VARIABLE, new Node(Node.Type.LITERAL, "abc")); 105 assertEquals(witness, node); 106 } 107 108 @Test 109 public void literalVariableLiteral() throws ScanException { 110 Tokenizer tokenizer = new Tokenizer("a${b}c"); 111 Parser parser = new Parser(tokenizer.tokenize()); 112 Node node = parser.parse(); 113 Node witness = new Node(Node.Type.LITERAL, "a"); 114 witness.next = new Node(Node.Type.VARIABLE, new Node(Node.Type.LITERAL, "b")); 115 witness.next.next = new Node(Node.Type.LITERAL, "c"); 116 assertEquals(witness, node); 117 } 118 119 // /LOGBACK-744 120 @Test 121 public void withColon() throws ScanException { 122 Tokenizer tokenizer = new Tokenizer("a:${b}"); 123 Parser parser = new Parser(tokenizer.tokenize()); 124 Node node = parser.parse(); 125 Node witness = new Node(Node.Type.LITERAL, "a"); 126 Node t = witness.next = new Node(Node.Type.LITERAL, ":"); 127 t.next = new Node(Node.Type.VARIABLE, new Node(Node.Type.LITERAL, "b")); 128 assertEquals(witness, node); 129 } 130 131 @Test 132 public void nested() throws ScanException { 133 Tokenizer tokenizer = new Tokenizer("a${b${c}}d"); 134 Parser parser = new Parser(tokenizer.tokenize()); 135 Node node = parser.parse(); 136 Node witness = new Node(Node.Type.LITERAL, "a"); 137 Node bLiteralNode = new Node(Node.Type.LITERAL, "b"); 138 Node cLiteralNode = new Node(Node.Type.LITERAL, "c"); 139 Node bVariableNode = new Node(Node.Type.VARIABLE, bLiteralNode); 140 Node cVariableNode = new Node(Node.Type.VARIABLE, cLiteralNode); 141 bLiteralNode.next = cVariableNode; 142 143 witness.next = bVariableNode; 144 witness.next.next = new Node(Node.Type.LITERAL, "d"); 145 assertEquals(witness, node); 146 } 147 148 @Test 149 public void withDefault() throws ScanException { 150 Tokenizer tokenizer = new Tokenizer("${b:-c}"); 151 Parser parser = new Parser(tokenizer.tokenize()); 152 Node node = parser.parse(); 153 Node witness = new Node(Node.Type.VARIABLE, new Node(Node.Type.LITERAL, "b")); 154 witness.defaultPart = new Node(Node.Type.LITERAL, "c"); 155 assertEquals(witness, node); 156 } 157 158 @Test 159 public void defaultSeparatorOutsideOfAVariable() throws ScanException { 160 Tokenizer tokenizer = new Tokenizer("{a:-b}"); 161 Parser parser = new Parser(tokenizer.tokenize()); 162 Node node = parser.parse(); 163 164 dump(node); 165 Node witness = new Node(Node.Type.LITERAL, "{"); 166 Node t = witness.next = new Node(Node.Type.LITERAL, "a"); 167 168 t.next = new Node(Node.Type.LITERAL, ":-"); 169 t = t.next; 170 171 t.next = new Node(Node.Type.LITERAL, "b"); 172 t = t.next; 173 174 t.next = new Node(Node.Type.LITERAL, "}"); 175 176 assertEquals(witness, node); 177 } 178 179 @Test 180 public void emptyTokenListDoesNotThrowNullPointerException() throws ScanException { 181 // An empty token list would be returned from Tokenizer.tokenize() 182 // if it were constructed with an empty string. The parser should 183 // be able to handle this. 184 Parser parser = new Parser(new ArrayList<Token>()); 185 parser.parse(); 186 } 187 188 private void dump(Node node) { 189 while (node != null) { 190 System.out.println(node.toString()); 191 node = node.next; 192 } 193 } 194 195}