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 org.junit.jupiter.api.Test;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  
21  /**
22   * Test pattern manipulation code.
23   * 
24   * @author Ceki Gulcu
25   */
26  public class ElementSelectorTest {
27  
28      @Test
29      public void test1() {
30          ElementSelector p = new ElementSelector("a");
31          assertEquals(1, p.size());
32          assertEquals("a", p.peekLast());
33          assertEquals("a", p.get(0));
34      }
35  
36      @Test
37      public void testSuffix() {
38          ElementSelector p = new ElementSelector("a/");
39          assertEquals(1, p.size());
40          assertEquals("a", p.peekLast());
41          assertEquals("a", p.get(0));
42      }
43  
44      @Test
45      public void test2() {
46          ElementSelector p = new ElementSelector("a/b");
47          assertEquals(2, p.size());
48          assertEquals("b", p.peekLast());
49          assertEquals("a", p.get(0));
50          assertEquals("b", p.get(1));
51      }
52  
53      @Test
54      public void test3() {
55          ElementSelector p = new ElementSelector("a123/b1234/cvvsdf");
56          assertEquals(3, p.size());
57          assertEquals("a123", p.get(0));
58          assertEquals("b1234", p.get(1));
59          assertEquals("cvvsdf", p.get(2));
60      }
61  
62      @Test
63      public void test4() {
64          ElementSelector p = new ElementSelector("/a123/b1234/cvvsdf");
65          assertEquals(3, p.size());
66          assertEquals("a123", p.get(0));
67          assertEquals("b1234", p.get(1));
68          assertEquals("cvvsdf", p.get(2));
69      }
70  
71      @Test
72      public void test5() {
73          ElementSelector p = new ElementSelector("//a");
74          assertEquals(1, p.size());
75          assertEquals("a", p.get(0));
76      }
77  
78      @Test
79      public void test6() {
80          ElementSelector p = new ElementSelector("//a//b");
81          assertEquals(2, p.size());
82          assertEquals("a", p.get(0));
83          assertEquals("b", p.get(1));
84      }
85  
86      // test tail matching
87      @Test
88      public void testTailMatch() {
89          {
90              ElementPath p = new ElementPath("/a/b");
91              ElementSelector ruleElementSelector = new ElementSelector("*");
92              assertEquals(0, ruleElementSelector.getTailMatchLength(p));
93          }
94  
95          {
96              ElementPath p = new ElementPath("/a");
97              ElementSelector ruleElementSelector = new ElementSelector("*/a");
98              assertEquals(1, ruleElementSelector.getTailMatchLength(p));
99          }
100 
101         {
102             ElementPath p = new ElementPath("/A");
103             ElementSelector ruleElementSelector = new ElementSelector("*/a");
104             assertEquals(1, ruleElementSelector.getTailMatchLength(p));
105         }
106 
107         {
108             ElementPath p = new ElementPath("/a");
109             ElementSelector ruleElementSelector = new ElementSelector("*/A");
110             assertEquals(1, ruleElementSelector.getTailMatchLength(p));
111         }
112 
113         {
114             ElementPath p = new ElementPath("/a/b");
115             ElementSelector ruleElementSelector = new ElementSelector("*/b");
116             assertEquals(1, ruleElementSelector.getTailMatchLength(p));
117         }
118 
119         {
120             ElementPath p = new ElementPath("/a/B");
121             ElementSelector ruleElementSelector = new ElementSelector("*/b");
122             assertEquals(1, ruleElementSelector.getTailMatchLength(p));
123         }
124 
125         {
126             ElementPath p = new ElementPath("/a/b/c");
127             ElementSelector ruleElementSelector = new ElementSelector("*/b/c");
128             assertEquals(2, ruleElementSelector.getTailMatchLength(p));
129         }
130     }
131 
132     // test prefix matching
133     @Test
134     public void testPrefixMatch() {
135         {
136             ElementPath p = new ElementPath("/a/b");
137             ElementSelector ruleElementSelector = new ElementSelector("/x/*");
138             assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
139         }
140 
141         {
142             ElementPath p = new ElementPath("/a");
143             ElementSelector ruleElementSelector = new ElementSelector("/x/*");
144             assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
145         }
146 
147         {
148             ElementPath p = new ElementPath("/a/b");
149             ElementSelector ruleElementSelector = new ElementSelector("/a/*");
150             assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
151         }
152 
153         {
154             ElementPath p = new ElementPath("/a/b");
155             ElementSelector ruleElementSelector = new ElementSelector("/A/*");
156             assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
157         }
158 
159         {
160             ElementPath p = new ElementPath("/A/b");
161             ElementSelector ruleElementSelector = new ElementSelector("/a/*");
162             assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
163         }
164 
165         {
166             ElementPath p = new ElementPath("/a/b");
167             ElementSelector ruleElementSelector = new ElementSelector("/a/b/*");
168             assertEquals(2, ruleElementSelector.getPrefixMatchLength(p));
169         }
170 
171         {
172             ElementPath p = new ElementPath("/a/b");
173             ElementSelector ruleElementSelector = new ElementSelector("/*");
174             assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
175         }
176     }
177 
178 }