1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran.spi;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import java.util.List;
19  
20  import org.junit.Test;
21  import org.xml.sax.Attributes;
22  
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.joran.action.Action;
25  
26  /**
27   * 
28   * @author Ceki Gulcu
29   */
30  public class SimpleStoreTest  {
31  
32    @Test
33    public void test1() throws Exception {
34      // Document doc = getW3Document("file:input/joran/parser1.xml");
35      SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
36      srs.addRule(new Pattern("a/b"), new XAction());
37  
38      List r = srs.matchActions(new Pattern("a/b"));
39      assertNotNull(r);
40      assertEquals(1, r.size());
41  
42      if (!(r.get(0) instanceof XAction)) {
43        fail("Wrong type");
44      }
45  
46      srs = new SimpleRuleStore(new ContextBase());
47      srs.addRule(new Pattern("a/b"), new XAction());
48      srs.addRule(new Pattern("a/b"), new YAction());
49  
50      r = srs.matchActions(new Pattern("a/b"));
51      assertNotNull(r);
52      assertEquals(2, r.size());
53  
54      if (!(r.get(0) instanceof XAction)) {
55        fail("Wrong type");
56      }
57  
58      if (!(r.get(1) instanceof YAction)) {
59        fail("Wrong type");
60      }
61  
62    }
63  
64    @Test
65    public void testSlashSuffix() throws Exception {
66      SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
67      Pattern pa = new Pattern("a/");
68      srs.addRule(pa, new XAction());
69      
70      List r = srs.matchActions(new Pattern("a"));
71      assertNotNull(r);
72      assertEquals(1, r.size());
73  
74      if (!(r.get(0) instanceof XAction)) {
75        fail("Wrong type");
76      }
77  
78   
79    }
80    
81    @Test
82    public void testTail1() throws Exception {
83      SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
84      srs.addRule(new Pattern("*/b"), new XAction());
85  
86      List r = srs.matchActions(new Pattern("a/b"));
87      assertNotNull(r);
88  
89      assertEquals(1, r.size());
90  
91      if (!(r.get(0) 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 Pattern("*/c"), new XAction());
101 
102     List r = srs.matchActions(new Pattern("a/b/c"));
103     assertNotNull(r);
104 
105     assertEquals(1, r.size());
106 
107     if (!(r.get(0) instanceof XAction)) {
108       fail("Wrong type");
109     }
110   }
111   
112   @Test
113   public void testTail3() throws Exception {
114     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
115     srs.addRule(new Pattern("*/b"), new XAction());
116     srs.addRule(new Pattern("*/a/b"), new YAction());
117 
118     List r = srs.matchActions(new Pattern("a/b"));
119     assertNotNull(r);
120 
121     // System.out.println("restulg list is: "+r);
122     assertEquals(1, r.size());
123 
124     if (!(r.get(0) instanceof YAction)) {
125       fail("Wrong type");
126     }
127   }
128 
129   @Test
130   public void testTail4() throws Exception {
131     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
132     srs.addRule(new Pattern("*/b"), new XAction());
133     srs.addRule(new Pattern("*/a/b"), new YAction());
134     srs.addRule(new Pattern("a/b"), new ZAction());
135 
136     List r = srs.matchActions(new Pattern("a/b"));
137     assertNotNull(r);
138 
139     // System.out.println("result list is: "+r);
140     assertEquals(1, r.size());
141 
142     if (!(r.get(0) instanceof ZAction)) {
143       fail("Wrong type");
144     }
145   }
146   
147   @Test
148   public void testSuffix() throws Exception {
149     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
150     srs.addRule(new Pattern("a"), new XAction());
151     srs.addRule(new Pattern("a/*"), new YAction());
152 
153     List r = srs.matchActions(new Pattern("a/b"));
154     assertNotNull(r);
155     assertEquals(1, r.size());
156     assertTrue(r.get(0) instanceof YAction);
157   }
158   
159   @Test
160   public void testDeepSuffix() throws Exception {
161     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
162     srs.addRule(new Pattern("a"), new XAction(1));
163     srs.addRule(new Pattern("a/b/*"), new XAction(2));
164 
165     List r = srs.matchActions(new Pattern("a/other"));
166     assertNull(r);
167   }
168 
169   @Test
170   public void testPrefixSuffixInteraction1() throws Exception {
171     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
172     srs.addRule(new Pattern("a"), new ZAction());
173     srs.addRule(new Pattern("a/*"), new YAction());
174     srs.addRule(new Pattern("*/a/b"), new XAction(3));
175 
176     List r = srs.matchActions(new Pattern("a/b"));
177     assertNotNull(r);
178 
179     assertEquals(1, r.size());
180     
181     assertTrue(r.get(0) instanceof XAction);
182     XAction xaction = (XAction) r.get(0);
183     assertEquals(3, xaction.id);
184   }
185 
186   @Test
187   public void testPrefixSuffixInteraction2() throws Exception {
188     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
189     srs.addRule(new Pattern("testGroup"), new XAction());
190     srs.addRule(new Pattern("testGroup/testShell"), new YAction());
191     srs.addRule(new Pattern("testGroup/testShell/test"), new ZAction());
192     srs.addRule(new Pattern("testGroup/testShell/test/*"), new XAction(9));
193     
194     List r = srs.matchActions(new Pattern("testGroup/testShell/toto"));
195     assertNull(r);
196   }
197   
198   class XAction extends Action {
199     int id = 0;
200     XAction() {
201     }
202     XAction(int id) {
203       this.id = id;
204     }
205 
206     public void begin(InterpretationContext ec, String name, Attributes attributes) {
207     }
208 
209     public void end(InterpretationContext ec, String name) {
210     }
211 
212     public void finish(InterpretationContext ec) {
213     }
214     
215     public String toString() {
216      return "XAction("+id+")";
217     }    
218   }
219 
220   class YAction extends Action {
221     public void begin(InterpretationContext ec, String name, Attributes attributes) {
222     }
223 
224     public void end(InterpretationContext ec, String name) {
225     }
226 
227     public void finish(InterpretationContext ec) {
228     }
229   }
230 
231   class ZAction extends Action {
232     public void begin(InterpretationContext ec, String name, Attributes attributes) {
233     }
234 
235     public void end(InterpretationContext ec, String name) {
236     }
237 
238     public void finish(InterpretationContext ec) {
239     }
240   }
241 
242 }