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