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.joran.spi;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertNull;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import java.util.List;
23  
24  import org.junit.Test;
25  import org.xml.sax.Attributes;
26  
27  import ch.qos.logback.core.ContextBase;
28  import ch.qos.logback.core.joran.action.Action;
29  
30  /**
31   * Test SimpleRuleStore for various explicit rule combinations.
32   * 
33   * We also test that explicit patterns are case sensitive.
34   * 
35   * @author Ceki Gülcü
36   */
37  public class SimpleRuleStoreTest {
38  
39      SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
40      CaseCombinator cc = new CaseCombinator();
41  
42      @Test
43      public void smoke() throws Exception {
44          srs.addRule(new ElementSelector("a/b"), new XAction());
45  
46          // test for all possible case combinations of "a/b"
47          for (String s : cc.combinations("a/b")) {
48              System.out.println("s=" + s);
49              List<Action> r = srs.matchActions(new ElementPath(s));
50              assertNotNull(r);
51              assertEquals(1, r.size());
52  
53              if (!(r.get(0) instanceof XAction)) {
54                  fail("Wrong type");
55              }
56          }
57      }
58  
59      @Test
60      public void smokeII() throws Exception {
61          srs.addRule(new ElementSelector("a/b"), new XAction());
62          srs.addRule(new ElementSelector("a/b"), new YAction());
63  
64          for (String s : cc.combinations("a/b")) {
65              List<Action> r = srs.matchActions(new ElementPath(s));
66              assertNotNull(r);
67              assertEquals(2, r.size());
68  
69              if (!(r.get(0) instanceof XAction)) {
70                  fail("Wrong type");
71              }
72  
73              if (!(r.get(1) instanceof YAction)) {
74                  fail("Wrong type");
75              }
76          }
77      }
78  
79      @Test
80      public void testSlashSuffix() throws Exception {
81          ElementSelector pa = new ElementSelector("a/");
82          srs.addRule(pa, new XAction());
83  
84          for (String s : cc.combinations("a")) {
85              List<Action> r = srs.matchActions(new ElementPath(s));
86              assertNotNull(r);
87              assertEquals(1, r.size());
88  
89              if (!(r.get(0) instanceof XAction)) {
90                  fail("Wrong type");
91              }
92          }
93  
94      }
95  
96      @Test
97      public void testTail1() throws Exception {
98          srs.addRule(new ElementSelector("*/b"), new XAction());
99  
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 }