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.Assert;
31  import org.junit.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          Assert.assertNotNull(h);
50          Assert.assertTrue(h1 == h);
51          h = matcher.lookup("/h2");
52          Assert.assertNotNull(h);
53          Assert.assertTrue(h2 == h);
54          h = matcher.lookup("/h3");
55          Assert.assertNotNull(h);
56          Assert.assertTrue(h3 == h);
57  
58          matcher.unregister("/h1");
59          h = matcher.lookup("/h1");
60          Assert.assertNull(h);
61      }
62  
63      @Test(expected = NullPointerException.class)
64      public void testRegisterNull() throws Exception {
65          final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
66          matcher.register(null, null);
67      }
68  
69      @Test
70      public void testWildCardMatching1a() throws Exception {
71          final Object h1 = new Object();
72          final Object h2 = new Object();
73          final Object h3 = new Object();
74          final Object def = new Object();
75  
76          final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
77          matcher.register(".*", def);
78          matcher.register("/one/.*", h1);
79          matcher.register("/one/two/.*", h2);
80          matcher.register("/one/two/three/.*", h3);
81  
82          Object h;
83  
84          h = matcher.lookup("/one/request");
85          Assert.assertNotNull(h);
86          Assert.assertTrue(def == h);
87  
88          h = matcher.lookup("/one/two/request");
89          Assert.assertNotNull(h);
90          Assert.assertTrue(def == h);
91  
92          h = matcher.lookup("/one/two/three/request");
93          Assert.assertNotNull(h);
94          Assert.assertTrue(def == h);
95  
96          h = matcher.lookup("default/request");
97          Assert.assertNotNull(h);
98          Assert.assertTrue(def == h);
99      }
100 
101     @Test
102     public void testWildCardMatching1b() throws Exception {
103         final Object h1 = new Object();
104         final Object h2 = new Object();
105         final Object h3 = new Object();
106         final Object def = new Object();
107 
108         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
109         matcher.register("/one/two/three/.*", h3);
110         matcher.register("/one/two/.*", h2);
111         matcher.register("/one/.*", h1);
112         matcher.register(".*", def);
113 
114         Object h;
115 
116         h = matcher.lookup("/one/request");
117         Assert.assertNotNull(h);
118         Assert.assertTrue(h1 == h);
119 
120         h = matcher.lookup("/one/two/request");
121         Assert.assertNotNull(h);
122         Assert.assertTrue(h2 == h);
123 
124         h = matcher.lookup("/one/two/three/request");
125         Assert.assertNotNull(h);
126         Assert.assertTrue(h3 == h);
127 
128         h = matcher.lookup("default/request");
129         Assert.assertNotNull(h);
130         Assert.assertTrue(def == h);
131     }
132 
133     @Test
134     public void testWildCardMatching2a() throws Exception {
135         final Object h1 = new Object();
136         final Object h2 = new Object();
137         final Object def = new Object();
138 
139         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
140         matcher.register(".*", def);
141         matcher.register(".*\\.view", h1);
142         matcher.register(".*\\.form", h2);
143 
144         Object h;
145 
146         h = matcher.lookup("/that.view");
147         Assert.assertNotNull(h);
148         Assert.assertTrue(def == h);
149 
150         h = matcher.lookup("/that.form");
151         Assert.assertNotNull(h);
152         Assert.assertTrue(def == h);
153 
154         h = matcher.lookup("/whatever");
155         Assert.assertNotNull(h);
156         Assert.assertTrue(def == h);
157     }
158 
159     @Test
160     public void testWildCardMatching2b() throws Exception {
161         final Object h1 = new Object();
162         final Object h2 = new Object();
163         final Object def = new Object();
164 
165         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
166         matcher.register(".*\\.form", h2);
167         matcher.register(".*\\.view", h1);
168         matcher.register(".*", def);
169 
170         Object h;
171 
172         h = matcher.lookup("/that.view");
173         Assert.assertNotNull(h);
174         Assert.assertTrue(h1 == h);
175 
176         h = matcher.lookup("/that.form");
177         Assert.assertNotNull(h);
178         Assert.assertTrue(h2 == h);
179 
180         h = matcher.lookup("/whatever");
181         Assert.assertNotNull(h);
182         Assert.assertTrue(def == h);
183     }
184 
185     @Test
186     public void testSuffixPatternOverPrefixPatternMatch() throws Exception {
187         final Object h1 = new Object();
188         final Object h2 = new Object();
189 
190         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
191         matcher.register("/ma.*", h1);
192         matcher.register(".*tch", h2);
193 
194         final Object h = matcher.lookup("/match");
195         Assert.assertNotNull(h);
196         Assert.assertTrue(h1 == h);
197     }
198 
199     @Test(expected = NullPointerException.class)
200     public void testRegisterInvalidInput() throws Exception {
201         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
202         matcher.register(null, null);
203     }
204 
205     @Test(expected = NullPointerException.class)
206     public void testLookupInvalidInput() throws Exception {
207         final LookupRegistry<Object> matcher = new UriRegexMatcher<>();
208         matcher.lookup(null);
209     }
210 
211 }