View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.protocol;
29  
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.Test;
32  
33  public class TestUriRegexMatcher {
34  
35      @Test
36      public void testRegisterUnregister() throws Exception {
37          final Object h1 = new Object();
38          final Object h2 = new Object();
39          final Object h3 = new Object();
40  
41          final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
42          matcher.register("/h1", h1);
43          matcher.register("/h2", h2);
44          matcher.register("/h3", h3);
45  
46          Object h;
47  
48          h = matcher.lookup("/h1");
49          Assertions.assertNotNull(h);
50          Assertions.assertSame(h1, h);
51          h = matcher.lookup("/h2");
52          Assertions.assertNotNull(h);
53          Assertions.assertSame(h2, h);
54          h = matcher.lookup("/h3");
55          Assertions.assertNotNull(h);
56          Assertions.assertSame(h3, h);
57  
58          matcher.unregister("/h1");
59          h = matcher.lookup("/h1");
60          Assertions.assertNull(h);
61      }
62  
63      @Test
64      public void testRegisterNull() throws Exception {
65          final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
66          Assertions.assertThrows(NullPointerException.class, () ->
67                  matcher.register(null, null));
68      }
69  
70      @Test
71      public void testWildCardMatching1a() throws Exception {
72          final Object h1 = new Object();
73          final Object h2 = new Object();
74          final Object h3 = new Object();
75          final Object def = new Object();
76  
77          final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
78          matcher.register(".*", def);
79          matcher.register("/one/.*", h1);
80          matcher.register("/one/two/.*", h2);
81          matcher.register("/one/two/three/.*", h3);
82  
83          Object h;
84  
85          h = matcher.lookup("/one/request");
86          Assertions.assertNotNull(h);
87          Assertions.assertSame(def, h);
88  
89          h = matcher.lookup("/one/two/request");
90          Assertions.assertNotNull(h);
91          Assertions.assertSame(def, h);
92  
93          h = matcher.lookup("/one/two/three/request");
94          Assertions.assertNotNull(h);
95          Assertions.assertSame(def, h);
96  
97          h = matcher.lookup("default/request");
98          Assertions.assertNotNull(h);
99          Assertions.assertSame(def, h);
100     }
101 
102     @Test
103     public void testWildCardMatching1b() throws Exception {
104         final Object h1 = new Object();
105         final Object h2 = new Object();
106         final Object h3 = new Object();
107         final Object def = new Object();
108 
109         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
110         matcher.register("/one/two/three/.*", h3);
111         matcher.register("/one/two/.*", h2);
112         matcher.register("/one/.*", h1);
113         matcher.register(".*", def);
114 
115         Object h;
116 
117         h = matcher.lookup("/one/request");
118         Assertions.assertNotNull(h);
119         Assertions.assertSame(h1, h);
120 
121         h = matcher.lookup("/one/two/request");
122         Assertions.assertNotNull(h);
123         Assertions.assertSame(h2, h);
124 
125         h = matcher.lookup("/one/two/three/request");
126         Assertions.assertNotNull(h);
127         Assertions.assertSame(h3, h);
128 
129         h = matcher.lookup("default/request");
130         Assertions.assertNotNull(h);
131         Assertions.assertSame(def, h);
132     }
133 
134     @Test
135     public void testWildCardMatching2a() throws Exception {
136         final Object h1 = new Object();
137         final Object h2 = new Object();
138         final Object def = new Object();
139 
140         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
141         matcher.register(".*", def);
142         matcher.register(".*\\.view", h1);
143         matcher.register(".*\\.form", h2);
144 
145         Object h;
146 
147         h = matcher.lookup("/that.view");
148         Assertions.assertNotNull(h);
149         Assertions.assertSame(def, h);
150 
151         h = matcher.lookup("/that.form");
152         Assertions.assertNotNull(h);
153         Assertions.assertSame(def, h);
154 
155         h = matcher.lookup("/whatever");
156         Assertions.assertNotNull(h);
157         Assertions.assertSame(def, h);
158     }
159 
160     @Test
161     public void testWildCardMatching2b() throws Exception {
162         final Object h1 = new Object();
163         final Object h2 = new Object();
164         final Object def = new Object();
165 
166         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
167         matcher.register(".*\\.form", h2);
168         matcher.register(".*\\.view", h1);
169         matcher.register(".*", def);
170 
171         Object h;
172 
173         h = matcher.lookup("/that.view");
174         Assertions.assertNotNull(h);
175         Assertions.assertSame(h1, h);
176 
177         h = matcher.lookup("/that.form");
178         Assertions.assertNotNull(h);
179         Assertions.assertSame(h2, h);
180 
181         h = matcher.lookup("/whatever");
182         Assertions.assertNotNull(h);
183         Assertions.assertSame(def, h);
184     }
185 
186     @Test
187     public void testSuffixPatternOverPrefixPatternMatch() throws Exception {
188         final Object h1 = new Object();
189         final Object h2 = new Object();
190 
191         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
192         matcher.register("/ma.*", h1);
193         matcher.register(".*tch", h2);
194 
195         final Object h = matcher.lookup("/match");
196         Assertions.assertNotNull(h);
197         Assertions.assertSame(h1, h);
198     }
199 
200     @Test
201     public void testRegisterInvalidInput() throws Exception {
202         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
203         Assertions.assertThrows(NullPointerException.class, () ->
204                 matcher.register(null, null));
205     }
206 
207     @Test
208     public void testLookupInvalidInput() throws Exception {
209         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
210         Assertions.assertThrows(NullPointerException.class, () ->
211                 matcher.lookup(null));
212     }
213 
214 }