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.joran.spi; 015 016import static org.junit.Assert.assertEquals; 017 018import org.junit.Test; 019 020/** 021 * Test pattern manipulation code. 022 * 023 * @author Ceki Gulcu 024 */ 025public class ElementSelectorTest { 026 027 @Test 028 public void test1() { 029 ElementSelector p = new ElementSelector("a"); 030 assertEquals(1, p.size()); 031 assertEquals("a", p.peekLast()); 032 assertEquals("a", p.get(0)); 033 } 034 035 @Test 036 public void testSuffix() { 037 ElementSelector p = new ElementSelector("a/"); 038 assertEquals(1, p.size()); 039 assertEquals("a", p.peekLast()); 040 assertEquals("a", p.get(0)); 041 } 042 043 @Test 044 public void test2() { 045 ElementSelector p = new ElementSelector("a/b"); 046 assertEquals(2, p.size()); 047 assertEquals("b", p.peekLast()); 048 assertEquals("a", p.get(0)); 049 assertEquals("b", p.get(1)); 050 } 051 052 @Test 053 public void test3() { 054 ElementSelector p = new ElementSelector("a123/b1234/cvvsdf"); 055 assertEquals(3, p.size()); 056 assertEquals("a123", p.get(0)); 057 assertEquals("b1234", p.get(1)); 058 assertEquals("cvvsdf", p.get(2)); 059 } 060 061 @Test 062 public void test4() { 063 ElementSelector p = new ElementSelector("/a123/b1234/cvvsdf"); 064 assertEquals(3, p.size()); 065 assertEquals("a123", p.get(0)); 066 assertEquals("b1234", p.get(1)); 067 assertEquals("cvvsdf", p.get(2)); 068 } 069 070 @Test 071 public void test5() { 072 ElementSelector p = new ElementSelector("//a"); 073 assertEquals(1, p.size()); 074 assertEquals("a", p.get(0)); 075 } 076 077 @Test 078 public void test6() { 079 ElementSelector p = new ElementSelector("//a//b"); 080 assertEquals(2, p.size()); 081 assertEquals("a", p.get(0)); 082 assertEquals("b", p.get(1)); 083 } 084 085 // test tail matching 086 @Test 087 public void testTailMatch() { 088 { 089 ElementPath p = new ElementPath("/a/b"); 090 ElementSelector ruleElementSelector = new ElementSelector("*"); 091 assertEquals(0, ruleElementSelector.getTailMatchLength(p)); 092 } 093 094 { 095 ElementPath p = new ElementPath("/a"); 096 ElementSelector ruleElementSelector = new ElementSelector("*/a"); 097 assertEquals(1, ruleElementSelector.getTailMatchLength(p)); 098 } 099 100 { 101 ElementPath p = new ElementPath("/A"); 102 ElementSelector ruleElementSelector = new ElementSelector("*/a"); 103 assertEquals(1, ruleElementSelector.getTailMatchLength(p)); 104 } 105 106 { 107 ElementPath p = new ElementPath("/a"); 108 ElementSelector ruleElementSelector = new ElementSelector("*/A"); 109 assertEquals(1, ruleElementSelector.getTailMatchLength(p)); 110 } 111 112 { 113 ElementPath p = new ElementPath("/a/b"); 114 ElementSelector ruleElementSelector = new ElementSelector("*/b"); 115 assertEquals(1, ruleElementSelector.getTailMatchLength(p)); 116 } 117 118 { 119 ElementPath p = new ElementPath("/a/B"); 120 ElementSelector ruleElementSelector = new ElementSelector("*/b"); 121 assertEquals(1, ruleElementSelector.getTailMatchLength(p)); 122 } 123 124 { 125 ElementPath p = new ElementPath("/a/b/c"); 126 ElementSelector ruleElementSelector = new ElementSelector("*/b/c"); 127 assertEquals(2, ruleElementSelector.getTailMatchLength(p)); 128 } 129 } 130 131 // test prefix matching 132 @Test 133 public void testPrefixMatch() { 134 { 135 ElementPath p = new ElementPath("/a/b"); 136 ElementSelector ruleElementSelector = new ElementSelector("/x/*"); 137 assertEquals(0, ruleElementSelector.getPrefixMatchLength(p)); 138 } 139 140 { 141 ElementPath p = new ElementPath("/a"); 142 ElementSelector ruleElementSelector = new ElementSelector("/x/*"); 143 assertEquals(0, ruleElementSelector.getPrefixMatchLength(p)); 144 } 145 146 { 147 ElementPath p = new ElementPath("/a/b"); 148 ElementSelector ruleElementSelector = new ElementSelector("/a/*"); 149 assertEquals(1, ruleElementSelector.getPrefixMatchLength(p)); 150 } 151 152 { 153 ElementPath p = new ElementPath("/a/b"); 154 ElementSelector ruleElementSelector = new ElementSelector("/A/*"); 155 assertEquals(1, ruleElementSelector.getPrefixMatchLength(p)); 156 } 157 158 { 159 ElementPath p = new ElementPath("/A/b"); 160 ElementSelector ruleElementSelector = new ElementSelector("/a/*"); 161 assertEquals(1, ruleElementSelector.getPrefixMatchLength(p)); 162 } 163 164 { 165 ElementPath p = new ElementPath("/a/b"); 166 ElementSelector ruleElementSelector = new ElementSelector("/a/b/*"); 167 assertEquals(2, ruleElementSelector.getPrefixMatchLength(p)); 168 } 169 170 { 171 ElementPath p = new ElementPath("/a/b"); 172 ElementSelector ruleElementSelector = new ElementSelector("/*"); 173 assertEquals(0, ruleElementSelector.getPrefixMatchLength(p)); 174 } 175 } 176 177}