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; 017import static org.junit.Assert.assertNotNull; 018import static org.junit.Assert.assertNull; 019import static org.junit.Assert.assertTrue; 020import static org.junit.Assert.fail; 021 022import java.util.List; 023 024import org.junit.Test; 025import org.xml.sax.Attributes; 026 027import ch.qos.logback.core.ContextBase; 028import ch.qos.logback.core.joran.action.Action; 029 030/** 031 * Test SimpleRuleStore for various explicit rule combinations. 032 * 033 * We also test that explicit patterns are case sensitive. 034 * 035 * @author Ceki Gülcü 036 */ 037public class SimpleRuleStoreTest { 038 039 SimpleRuleStore srs = new SimpleRuleStore(new ContextBase()); 040 CaseCombinator cc = new CaseCombinator(); 041 042 @Test 043 public void smoke() throws Exception { 044 srs.addRule(new ElementSelector("a/b"), new XAction()); 045 046 // test for all possible case combinations of "a/b" 047 for (String s : cc.combinations("a/b")) { 048 System.out.println("s=" + s); 049 List<Action> r = srs.matchActions(new ElementPath(s)); 050 assertNotNull(r); 051 assertEquals(1, r.size()); 052 053 if (!(r.get(0) instanceof XAction)) { 054 fail("Wrong type"); 055 } 056 } 057 } 058 059 @Test 060 public void smokeII() throws Exception { 061 srs.addRule(new ElementSelector("a/b"), new XAction()); 062 srs.addRule(new ElementSelector("a/b"), new YAction()); 063 064 for (String s : cc.combinations("a/b")) { 065 List<Action> r = srs.matchActions(new ElementPath(s)); 066 assertNotNull(r); 067 assertEquals(2, r.size()); 068 069 if (!(r.get(0) instanceof XAction)) { 070 fail("Wrong type"); 071 } 072 073 if (!(r.get(1) instanceof YAction)) { 074 fail("Wrong type"); 075 } 076 } 077 } 078 079 @Test 080 public void testSlashSuffix() throws Exception { 081 ElementSelector pa = new ElementSelector("a/"); 082 srs.addRule(pa, new XAction()); 083 084 for (String s : cc.combinations("a")) { 085 List<Action> r = srs.matchActions(new ElementPath(s)); 086 assertNotNull(r); 087 assertEquals(1, r.size()); 088 089 if (!(r.get(0) instanceof XAction)) { 090 fail("Wrong type"); 091 } 092 } 093 094 } 095 096 @Test 097 public void testTail1() throws Exception { 098 srs.addRule(new ElementSelector("*/b"), new XAction()); 099 100 for (String s : cc.combinations("a/b")) { 101 List<Action> r = srs.matchActions(new ElementPath(s)); 102 assertNotNull(r); 103 104 assertEquals(1, r.size()); 105 106 if (!(r.get(0) instanceof XAction)) { 107 fail("Wrong type"); 108 } 109 } 110 } 111 112 @Test 113 public void testTail2() throws Exception { 114 SimpleRuleStore srs = new SimpleRuleStore(new ContextBase()); 115 srs.addRule(new ElementSelector("*/c"), new XAction()); 116 117 for (String s : cc.combinations("a/b/c")) { 118 List<Action> r = srs.matchActions(new ElementPath(s)); 119 assertNotNull(r); 120 121 assertEquals(1, r.size()); 122 123 if (!(r.get(0) instanceof XAction)) { 124 fail("Wrong type"); 125 } 126 } 127 } 128 129 @Test 130 public void testTail3() throws Exception { 131 srs.addRule(new ElementSelector("*/b"), new XAction()); 132 srs.addRule(new ElementSelector("*/a/b"), new YAction()); 133 134 for (String s : cc.combinations("a/b")) { 135 List<Action> r = srs.matchActions(new ElementPath(s)); 136 assertNotNull(r); 137 assertEquals(1, r.size()); 138 139 if (!(r.get(0) instanceof YAction)) { 140 fail("Wrong type"); 141 } 142 } 143 } 144 145 @Test 146 public void testTail4() throws Exception { 147 srs.addRule(new ElementSelector("*/b"), new XAction()); 148 srs.addRule(new ElementSelector("*/a/b"), new YAction()); 149 srs.addRule(new ElementSelector("a/b"), new ZAction()); 150 151 for (String s : cc.combinations("a/b")) { 152 List<Action> r = srs.matchActions(new ElementPath(s)); 153 assertNotNull(r); 154 assertEquals(1, r.size()); 155 156 if (!(r.get(0) instanceof ZAction)) { 157 fail("Wrong type"); 158 } 159 } 160 } 161 162 @Test 163 public void testSuffix() throws Exception { 164 srs.addRule(new ElementSelector("a"), new XAction()); 165 srs.addRule(new ElementSelector("a/*"), new YAction()); 166 167 for (String s : cc.combinations("a/b")) { 168 List<Action> r = srs.matchActions(new ElementPath(s)); 169 assertNotNull(r); 170 assertEquals(1, r.size()); 171 assertTrue(r.get(0) instanceof YAction); 172 } 173 } 174 175 @Test 176 public void testDeepSuffix() throws Exception { 177 srs.addRule(new ElementSelector("a"), new XAction(1)); 178 srs.addRule(new ElementSelector("a/b/*"), new XAction(2)); 179 180 for (String s : cc.combinations("a/other")) { 181 List<Action> r = srs.matchActions(new ElementPath(s)); 182 assertNull(r); 183 } 184 } 185 186 @Test 187 public void testPrefixSuffixInteraction1() throws Exception { 188 srs.addRule(new ElementSelector("a"), new ZAction()); 189 srs.addRule(new ElementSelector("a/*"), new YAction()); 190 srs.addRule(new ElementSelector("*/a/b"), new XAction(3)); 191 192 for (String s : cc.combinations("a/b")) { 193 List<Action> r = srs.matchActions(new ElementPath(s)); 194 assertNotNull(r); 195 196 assertEquals(1, r.size()); 197 198 assertTrue(r.get(0) instanceof XAction); 199 XAction xaction = (XAction) r.get(0); 200 assertEquals(3, xaction.id); 201 } 202 } 203 204 @Test 205 public void testPrefixSuffixInteraction2() throws Exception { 206 srs.addRule(new ElementSelector("tG"), new XAction()); 207 srs.addRule(new ElementSelector("tG/tS"), new YAction()); 208 srs.addRule(new ElementSelector("tG/tS/test"), new ZAction()); 209 srs.addRule(new ElementSelector("tG/tS/test/*"), new XAction(9)); 210 211 for (String s : cc.combinations("tG/tS/toto")) { 212 List<Action> r = srs.matchActions(new ElementPath(s)); 213 assertNull(r); 214 } 215 } 216 217 class XAction extends Action { 218 int id = 0; 219 220 XAction() { 221 } 222 223 XAction(int id) { 224 this.id = id; 225 } 226 227 public void begin(InterpretationContext ec, String name, Attributes attributes) { 228 } 229 230 public void end(InterpretationContext ec, String name) { 231 } 232 233 public void finish(InterpretationContext ec) { 234 } 235 236 public String toString() { 237 return "XAction(" + id + ")"; 238 } 239 } 240 241 class YAction extends Action { 242 public void begin(InterpretationContext ec, String name, Attributes attributes) { 243 } 244 245 public void end(InterpretationContext ec, String name) { 246 } 247 248 public void finish(InterpretationContext ec) { 249 } 250 } 251 252 class ZAction extends Action { 253 public void begin(InterpretationContext ec, String name, Attributes attributes) { 254 } 255 256 public void end(InterpretationContext ec, String name) { 257 } 258 259 public void finish(InterpretationContext ec) { 260 } 261 } 262 263}