1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 TestUriPatternMatcher {
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 UriPatternMatcher<Object> matcher = new UriPatternMatcher<>();
42 Assertions.assertEquals(0, matcher.entrySet().size());
43 matcher.register("/h1", h1);
44 Assertions.assertEquals(1, matcher.entrySet().size());
45 matcher.register("/h2", h2);
46 Assertions.assertEquals(2, matcher.entrySet().size());
47 matcher.register("/h3", h3);
48 Assertions.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 UriPatternMatcher<>();
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 Assertions.assertNotNull(h);
66 Assertions.assertSame(h1, h);
67 h = matcher.lookup("/h2");
68 Assertions.assertNotNull(h);
69 Assertions.assertSame(h2, h);
70 h = matcher.lookup("/h3");
71 Assertions.assertNotNull(h);
72 Assertions.assertSame(h3, h);
73
74 matcher.unregister("/h1");
75 h = matcher.lookup("/h1");
76 Assertions.assertNull(h);
77 }
78
79 @Test
80 public void testRegisterNull() throws Exception {
81 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
82 Assertions.assertThrows(NullPointerException.class, () ->
83 matcher.register(null, null));
84 }
85
86 @Test
87 public void testWildCardMatching1() throws Exception {
88 final Object h1 = new Object();
89 final Object h2 = new Object();
90 final Object h3 = new Object();
91 final Object def = new Object();
92
93 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
94 matcher.register("*", def);
95 matcher.register("/one/*", h1);
96 matcher.register("/one/two/*", h2);
97 matcher.register("/one/two/three/*", h3);
98
99 Object h;
100
101 h = matcher.lookup("/one/request");
102 Assertions.assertNotNull(h);
103 Assertions.assertSame(h1, h);
104
105 h = matcher.lookup("/one/two/request");
106 Assertions.assertNotNull(h);
107 Assertions.assertSame(h2, h);
108
109 h = matcher.lookup("/one/two/three/request");
110 Assertions.assertNotNull(h);
111 Assertions.assertSame(h3, h);
112
113 h = matcher.lookup("default/request");
114 Assertions.assertNotNull(h);
115 Assertions.assertSame(def, h);
116 }
117
118 @Test
119 public void testWildCardMatching2() throws Exception {
120 final Object h1 = new Object();
121 final Object h2 = new Object();
122 final Object def = new Object();
123
124 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
125 matcher.register("*", def);
126 matcher.register("*.view", h1);
127 matcher.register("*.form", h2);
128
129 Object h;
130
131 h = matcher.lookup("/that.view");
132 Assertions.assertNotNull(h);
133 Assertions.assertSame(h1, h);
134
135 h = matcher.lookup("/that.form");
136 Assertions.assertNotNull(h);
137 Assertions.assertSame(h2, h);
138
139 h = matcher.lookup("/whatever");
140 Assertions.assertNotNull(h);
141 Assertions.assertSame(def, h);
142 }
143
144 @Test
145 public void testSuffixPatternOverPrefixPatternMatch() throws Exception {
146 final Object h1 = new Object();
147 final Object h2 = new Object();
148
149 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
150 matcher.register("/ma*", h1);
151 matcher.register("*tch", h2);
152
153 final Object h = matcher.lookup("/match");
154 Assertions.assertNotNull(h);
155 Assertions.assertSame(h1, h);
156 }
157
158 @Test
159 public void testRegisterInvalidInput() throws Exception {
160 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
161 Assertions.assertThrows(NullPointerException.class, () ->
162 matcher.register(null, null));
163 }
164
165 @Test
166 public void testLookupInvalidInput() throws Exception {
167 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
168 Assertions.assertThrows(NullPointerException.class, () ->
169 matcher.lookup(null));
170 }
171
172 @Test
173 public void testMatchExact() {
174 final Object h1 = new Object();
175 final Object h2 = new Object();
176
177 final LookupRegistry<Object> matcher = new UriPatternMatcher<>();
178 matcher.register("exact", h1);
179 matcher.register("*", h2);
180
181 final Object h = matcher.lookup("exact");
182 Assertions.assertNotNull(h);
183 Assertions.assertSame(h1, h);
184 }
185
186 }