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  
18  import org.junit.Test;
19  
20  /**
21   * Test pattern manipulation code.
22   * 
23   * @author Ceki Gulcu
24   */
25  public class ElementSelectorTest {
26  
27      @Test
28      public void test1() {
29          ElementSelector p = new ElementSelector("a");
30          assertEquals(1, p.size());
31          assertEquals("a", p.peekLast());
32          assertEquals("a", p.get(0));
33      }
34  
35      @Test
36      public void testSuffix() {
37          ElementSelector p = new ElementSelector("a/");
38          assertEquals(1, p.size());
39          assertEquals("a", p.peekLast());
40          assertEquals("a", p.get(0));
41      }
42  
43      @Test
44      public void test2() {
45          ElementSelector p = new ElementSelector("a/b");
46          assertEquals(2, p.size());
47          assertEquals("b", p.peekLast());
48          assertEquals("a", p.get(0));
49          assertEquals("b", p.get(1));
50      }
51  
52      @Test
53      public void test3() {
54          ElementSelector p = new ElementSelector("a123/b1234/cvvsdf");
55          assertEquals(3, p.size());
56          assertEquals("a123", p.get(0));
57          assertEquals("b1234", p.get(1));
58          assertEquals("cvvsdf", p.get(2));
59      }
60  
61      @Test
62      public void test4() {
63          ElementSelector p = new ElementSelector("/a123/b1234/cvvsdf");
64          assertEquals(3, p.size());
65          assertEquals("a123", p.get(0));
66          assertEquals("b1234", p.get(1));
67          assertEquals("cvvsdf", p.get(2));
68      }
69  
70      @Test
71      public void test5() {
72          ElementSelector p = new ElementSelector("//a");
73          assertEquals(1, p.size());
74          assertEquals("a", p.get(0));
75      }
76  
77      @Test
78      public void test6() {
79          ElementSelector p = new ElementSelector("//a//b");
80          assertEquals(2, p.size());
81          assertEquals("a", p.get(0));
82          assertEquals("b", p.get(1));
83      }
84  
85      // test tail matching
86      @Test
87      public void testTailMatch() {
88          {
89              ElementPath p = new ElementPath("/a/b");
90              ElementSelector ruleElementSelector = new ElementSelector("*");
91              assertEquals(0, ruleElementSelector.getTailMatchLength(p));
92          }
93  
94          {
95              ElementPath p = new ElementPath("/a");
96              ElementSelector ruleElementSelector = new ElementSelector("*/a");
97              assertEquals(1, ruleElementSelector.getTailMatchLength(p));
98          }
99  
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 }