View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
3    * reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
6    * v1.0 as published by the Eclipse Foundation
7    *
8    * or (per the licensee's choosing)
9    *
10   * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
11   */
12  package ch.qos.logback.core.joran.spi;
13  
14  import java.util.function.Supplier;
15  
16  import org.junit.jupiter.api.Test;
17  import org.xml.sax.Attributes;
18  
19  import ch.qos.logback.core.ContextBase;
20  import ch.qos.logback.core.joran.action.Action;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assertions.fail;
28  
29  /**
30   * Test SimpleRuleStore for various explicit rule combinations.
31   *
32   * We also test that explicit patterns are case-sensitive.
33   *
34   * @author Ceki Gülcü
35   */
36  public class SimpleRuleStoreTest {
37  
38      SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
39      CaseCombinator cc = new CaseCombinator();
40  
41      @Test
42      public void smoke() throws Exception {
43          srs.addRule(new ElementSelector("a/b"), () -> new XAction());
44  
45          // test for all possible case combinations of "a/b"
46          for (String s : cc.combinations("a/b")) {
47              System.out.println("s=" + s);
48              Supplier<Action> r = srs.matchActions(new ElementPath(s));
49              assertNotNull(r);
50  
51              if (!(r.get() instanceof XAction)) {
52                  fail("Wrong type");
53              }
54          }
55      }
56  
57      @Test
58      public void smokeII() throws Exception {
59          srs.addRule(new ElementSelector("a/b"), () -> new XAction());
60  
61          Exception e = assertThrows(IllegalStateException.class, () -> {
62              srs.addRule(new ElementSelector("a/b"), () -> new YAction());
63          });
64          assertEquals("[a][b] already has an associated action supplier", e.getMessage());
65      }
66  
67      @Test
68      public void testSlashSuffix() throws Exception {
69          ElementSelector pa = new ElementSelector("a/");
70          srs.addRule(pa, () -> new XAction());
71  
72          for (String s : cc.combinations("a")) {
73              Supplier<Action> r = srs.matchActions(new ElementPath(s));
74              assertNotNull(r);
75  
76              if (!(r.get() instanceof XAction)) {
77                  fail("Wrong type");
78              }
79          }
80  
81      }
82  
83      @Test
84      public void testTail1() throws Exception {
85          srs.addRule(new ElementSelector("*/b"), () -> new XAction());
86  
87          for (String s : cc.combinations("a/b")) {
88              Supplier<Action> r = srs.matchActions(new ElementPath(s));
89              assertNotNull(r);
90  
91              if (!(r.get() instanceof XAction)) {
92                  fail("Wrong type");
93              }
94          }
95      }
96  
97      @Test
98      public void testTail2() throws Exception {
99          SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
100         srs.addRule(new ElementSelector("*/c"), () -> new XAction());
101 
102         for (String s : cc.combinations("a/b/c")) {
103             Supplier<Action> r = srs.matchActions(new ElementPath(s));
104             assertNotNull(r);
105             if (!(r.get() instanceof XAction)) {
106                 fail("Wrong type");
107             }
108         }
109     }
110 
111     @Test
112     public void testTail3() throws Exception {
113         srs.addRule(new ElementSelector("*/b"), () -> new XAction());
114         srs.addRule(new ElementSelector("*/a/b"), () -> new YAction());
115 
116         for (String s : cc.combinations("a/b")) {
117             Supplier<Action> r = srs.matchActions(new ElementPath(s));
118             assertNotNull(r);
119             Action ya = r.get();
120             if (!(ya instanceof YAction)) {
121                 fail("Wrong type");
122             }
123         }
124     }
125 
126     @Test
127     public void testTail4() throws Exception {
128         srs.addRule(new ElementSelector("*/b"), () -> new XAction());
129         srs.addRule(new ElementSelector("*/a/b"), () -> new YAction());
130         srs.addRule(new ElementSelector("a/b"), () -> new ZAction());
131 
132         for (String s : cc.combinations("a/b")) {
133             Supplier<Action> r = srs.matchActions(new ElementPath(s));
134             assertNotNull(r);
135 
136             if (!(r.get() instanceof ZAction)) {
137                 fail("Wrong type");
138             }
139         }
140     }
141 
142     @Test
143     public void testSuffix() throws Exception {
144         srs.addRule(new ElementSelector("a"), () -> new XAction());
145         srs.addRule(new ElementSelector("a/*"), () -> new YAction());
146 
147         for (String s : cc.combinations("a/b")) {
148             Supplier<Action> r = srs.matchActions(new ElementPath(s));
149             assertNotNull(r);
150             assertTrue(r.get() instanceof YAction);
151         }
152     }
153 
154     @Test
155     public void testDeepSuffix() throws Exception {
156         srs.addRule(new ElementSelector("a"), () -> new XAction(1));
157         srs.addRule(new ElementSelector("a/b/*"), () -> new XAction(2));
158 
159         for (String s : cc.combinations("a/other")) {
160             Supplier<Action> r = srs.matchActions(new ElementPath(s));
161             assertNull(r);
162         }
163     }
164 
165     @Test
166     public void testPrefixSuffixInteraction1() throws Exception {
167         srs.addRule(new ElementSelector("a"), () -> new ZAction());
168         srs.addRule(new ElementSelector("a/*"), () -> new YAction());
169         srs.addRule(new ElementSelector("*/a/b"), () -> new XAction(3));
170 
171         for (String s : cc.combinations("a/b")) {
172             Supplier<Action> r = srs.matchActions(new ElementPath(s));
173             assertNotNull(r);
174 
175             Action ra = r.get();
176 
177             assertTrue(ra instanceof XAction);
178             XAction xaction = (XAction) ra;
179             assertEquals(3, xaction.id);
180         }
181     }
182 
183     @Test
184     public void testPrefixSuffixInteraction2() throws Exception {
185         srs.addRule(new ElementSelector("tG"), () -> new XAction());
186         srs.addRule(new ElementSelector("tG/tS"), () -> new YAction());
187         srs.addRule(new ElementSelector("tG/tS/test"), () -> new ZAction());
188         srs.addRule(new ElementSelector("tG/tS/test/*"), () -> new XAction(9));
189 
190         for (String s : cc.combinations("tG/tS/toto")) {
191             Supplier<Action> r = srs.matchActions(new ElementPath(s));
192             assertNull(r);
193         }
194     }
195 
196     @Test
197     public void withTransparentParts() throws Exception {
198 
199         srs.addTransparentPathPart("if");
200         srs.addTransparentPathPart("then");
201         srs.addTransparentPathPart("else");
202 
203         {
204             ElementPath ep = new ElementPath("x/if/then/if");
205             ElementPath witness = new ElementPath("x/");
206 
207             ElementPath cleanedEP = srs.removeTransparentPathParts(ep);
208             assertEquals(witness, cleanedEP);
209         }
210 
211         {
212             ElementPath ep = new ElementPath("x/if/then/stack");
213             ElementPath witness = new ElementPath("x/stack");
214 
215             ElementPath cleanedEP = srs.removeTransparentPathParts(ep);
216             assertEquals(witness, cleanedEP);
217         }
218 
219         {
220             ElementPath ep = new ElementPath("x/if/then/if/else/stack");
221             ElementPath witness = new ElementPath("x/stack");
222 
223             ElementPath cleanedEP = srs.removeTransparentPathParts(ep);
224             assertEquals(witness, cleanedEP);
225         }
226     }
227 
228     @Test
229     public void withRenamedParts() throws Exception {
230         srs.addPathPathMapping("included", "configure");
231 
232         {
233             ElementPath ep = new ElementPath("included/a/b");
234             ElementPath witness = new ElementPath("configure/a/b");
235 
236             ElementPath renamedEP = srs.renamePathParts(ep);
237             assertEquals(witness, renamedEP);
238         }
239     }
240 
241     class XAction extends Action {
242         int id = 0;
243 
244         XAction() {
245         }
246 
247         XAction(int id) {
248             this.id = id;
249         }
250 
251         public void begin(SaxEventInterpretationContext ec, String name, Attributes attributes) {
252         }
253 
254         public void end(SaxEventInterpretationContext ec, String name) {
255         }
256 
257         public void finish(SaxEventInterpretationContext ec) {
258         }
259 
260         public String toString() {
261             return "XAction(" + id + ")";
262         }
263     }
264 
265     class YAction extends Action {
266         public void begin(SaxEventInterpretationContext ec, String name, Attributes attributes) {
267         }
268 
269         public void end(SaxEventInterpretationContext ec, String name) {
270         }
271 
272         public void finish(SaxEventInterpretationContext ec) {
273         }
274     }
275 
276     class ZAction extends Action {
277         public void begin(SaxEventInterpretationContext ec, String name, Attributes attributes) {
278         }
279 
280         public void end(SaxEventInterpretationContext ec, String name) {
281         }
282 
283         public void finish(SaxEventInterpretationContext ec) {
284         }
285     }
286 
287 }