001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.io.IOException;
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpRequest;
040: import org.apache.http.HttpResponse;
041:
042: import junit.framework.Test;
043: import junit.framework.TestCase;
044: import junit.framework.TestSuite;
045:
046: /**
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: */
049: public class TestHttpRequestHandlerRegistry extends TestCase {
050:
051: public TestHttpRequestHandlerRegistry(String testName) {
052: super (testName);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestHttpRequestHandlerRegistry.class
057: .getName() };
058: junit.textui.TestRunner.main(testCaseName);
059: }
060:
061: public static Test suite() {
062: return new TestSuite(TestHttpRequestHandlerRegistry.class);
063: }
064:
065: private static class DummyHttpRequestHandler implements
066: HttpRequestHandler {
067:
068: public void handle(final HttpRequest request,
069: final HttpResponse response, final HttpContext context)
070: throws HttpException, IOException {
071: }
072:
073: }
074:
075: public void testRegisterUnregister() throws Exception {
076: HttpRequestHandler h1 = new DummyHttpRequestHandler();
077: HttpRequestHandler h2 = new DummyHttpRequestHandler();
078: HttpRequestHandler h3 = new DummyHttpRequestHandler();
079:
080: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
081: registry.register("/h1", h1);
082: registry.register("/h2", h2);
083: registry.register("/h3", h3);
084:
085: HttpRequestHandler h;
086:
087: h = registry.lookup("/h1");
088: assertNotNull(h);
089: assertTrue(h1 == h);
090: h = registry.lookup("/h2");
091: assertNotNull(h);
092: assertTrue(h2 == h);
093: h = registry.lookup("/h3");
094: assertNotNull(h);
095: assertTrue(h3 == h);
096:
097: registry.unregister("/h1");
098: h = registry.lookup("/h1");
099: assertNull(h);
100:
101: Map map = new HashMap();
102: map.put("/a1", h1);
103: map.put("/a2", h2);
104: map.put("/a3", h3);
105: registry.setHandlers(map);
106:
107: h = registry.lookup("/h2");
108: assertNull(h);
109: h = registry.lookup("/h3");
110: assertNull(h);
111:
112: h = registry.lookup("/a1");
113: assertNotNull(h);
114: assertTrue(h1 == h);
115: }
116:
117: public void testWildCardMatching1() throws Exception {
118: HttpRequestHandler h1 = new DummyHttpRequestHandler();
119: HttpRequestHandler h2 = new DummyHttpRequestHandler();
120: HttpRequestHandler h3 = new DummyHttpRequestHandler();
121: HttpRequestHandler def = new DummyHttpRequestHandler();
122:
123: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
124: registry.register("*", def);
125: registry.register("/one/*", h1);
126: registry.register("/one/two/*", h2);
127: registry.register("/one/two/three/*", h3);
128:
129: HttpRequestHandler h;
130:
131: h = registry.lookup("/one/request");
132: assertNotNull(h);
133: assertTrue(h1 == h);
134:
135: h = registry.lookup("/one/two/request");
136: assertNotNull(h);
137: assertTrue(h2 == h);
138:
139: h = registry.lookup("/one/two/three/request");
140: assertNotNull(h);
141: assertTrue(h3 == h);
142:
143: h = registry.lookup("default/request");
144: assertNotNull(h);
145: assertTrue(def == h);
146: }
147:
148: public void testWildCardMatching2() throws Exception {
149: HttpRequestHandler h1 = new DummyHttpRequestHandler();
150: HttpRequestHandler h2 = new DummyHttpRequestHandler();
151: HttpRequestHandler def = new DummyHttpRequestHandler();
152:
153: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
154: registry.register("*", def);
155: registry.register("*.view", h1);
156: registry.register("*.form", h2);
157:
158: HttpRequestHandler h;
159:
160: h = registry.lookup("/that.view");
161: assertNotNull(h);
162: assertTrue(h1 == h);
163:
164: h = registry.lookup("/that.form");
165: assertNotNull(h);
166: assertTrue(h2 == h);
167:
168: h = registry.lookup("/whatever");
169: assertNotNull(h);
170: assertTrue(def == h);
171: }
172:
173: public void testWildCardMatchingWithQuery() throws Exception {
174: HttpRequestHandler h1 = new DummyHttpRequestHandler();
175: HttpRequestHandler h2 = new DummyHttpRequestHandler();
176: HttpRequestHandler def = new DummyHttpRequestHandler();
177:
178: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
179: registry.register("*", def);
180: registry.register("*.view", h1);
181: registry.register("*.form", h2);
182:
183: HttpRequestHandler h;
184:
185: h = registry.lookup("/that.view?param=value");
186: assertNotNull(h);
187: assertTrue(h1 == h);
188:
189: h = registry.lookup("/that.form?whatever");
190: assertNotNull(h);
191: assertTrue(h2 == h);
192:
193: h = registry.lookup("/whatever");
194: assertNotNull(h);
195: assertTrue(def == h);
196: }
197:
198: public void testSuffixPatternOverPrefixPatternMatch()
199: throws Exception {
200: HttpRequestHandler h1 = new DummyHttpRequestHandler();
201: HttpRequestHandler h2 = new DummyHttpRequestHandler();
202:
203: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
204: registry.register("/ma*", h1);
205: registry.register("*tch", h2);
206:
207: HttpRequestHandler h;
208:
209: h = registry.lookup("/match");
210: assertNotNull(h);
211: assertTrue(h1 == h);
212: }
213:
214: public void testInvalidInput() throws Exception {
215: HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
216: try {
217: registry.register(null, null);
218: fail("IllegalArgumentException should have been thrown");
219: } catch (IllegalArgumentException ex) {
220: // expected
221: }
222:
223: try {
224: registry.register("", null);
225: fail("IllegalArgumentException should have been thrown");
226: } catch (IllegalArgumentException ex) {
227: // expected
228: }
229:
230: registry.unregister(null);
231:
232: try {
233: registry.setHandlers(null);
234: fail("IllegalArgumentException should have been thrown");
235: } catch (IllegalArgumentException ex) {
236: // expected
237: }
238:
239: try {
240: registry.lookup(null);
241: fail("IllegalArgumentException should have been thrown");
242: } catch (IllegalArgumentException ex) {
243: // expected
244: }
245: }
246:
247: }
|