You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/09/15 12:25:47 UTC
svn commit: r1385038 - in /httpcomponents/httpcore/trunk:
httpcore-nio/src/main/java/org/apache/http/nio/protocol/
httpcore-nio/src/test/java/org/apache/http/nio/protocol/
httpcore/src/main/java/org/apache/http/protocol/
httpcore/src/test/java/org/apac...
Author: olegk
Date: Sat Sep 15 10:25:47 2012
New Revision: 1385038
URL: http://svn.apache.org/viewvc?rev=1385038&view=rev
Log:
Tweaked UriHttpAsyncRequestHandlerMapper and UriHttpRequestHandlerMapper classes; added test cases
Added:
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java (with props)
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java (with props)
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java (with props)
Removed:
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java?rev=1385038&r1=1385037&r2=1385038&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java Sat Sep 15 10:25:47 2012
@@ -27,11 +27,10 @@
package org.apache.http.nio.protocol;
-import java.util.Map;
-
import org.apache.http.HttpRequest;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.protocol.UriPatternMatcher;
+import org.apache.http.util.Args;
/**
* Maintains a map of HTTP request handlers keyed by a request URI pattern.
@@ -54,8 +53,13 @@ public class UriHttpAsyncRequestHandlerM
private final UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher;
+ protected UriHttpAsyncRequestHandlerMapper(final UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher) {
+ super();
+ this.matcher = Args.notNull(matcher, "Pattern matcher");
+ }
+
public UriHttpAsyncRequestHandlerMapper() {
- matcher = new UriPatternMatcher<HttpAsyncRequestHandler<?>>();
+ this(new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
}
/**
@@ -79,23 +83,30 @@ public class UriHttpAsyncRequestHandlerM
}
/**
- * Sets handlers from the given map.
- * @param map the map containing handlers keyed by their URI patterns.
+ * Extracts request path from the given {@link HttpRequest}
*/
- public void setHandlers(final Map<String, HttpAsyncRequestHandler<?>> map) {
- matcher.setObjects(map);
+ protected String getRequestPath(final HttpRequest request) {
+ String uriPath = request.getRequestLine().getUri();
+ int index = uriPath.indexOf("?");
+ if (index != -1) {
+ uriPath = uriPath.substring(0, index);
+ } else {
+ index = uriPath.indexOf("#");
+ if (index != -1) {
+ uriPath = uriPath.substring(0, index);
+ }
+ }
+ return uriPath;
}
-
+
/**
- * Get the handler map.
- * @return The map of handlers and their associated URI patterns.
+ * Looks up a handler matching the given request URI.
+ *
+ * @param requestURI the request path
+ * @return handler or <code>null</code> if no match is found.
*/
- public Map<String, HttpAsyncRequestHandler<?>> getHandlers() {
- return matcher.getObjects();
- }
-
public HttpAsyncRequestHandler<?> lookup(final HttpRequest request) {
- return matcher.lookup(request.getRequestLine().getUri());
+ return matcher.lookup(getRequestPath(request));
}
}
Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java?rev=1385038&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java Sat Sep 15 10:25:47 2012
@@ -0,0 +1,107 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.protocol;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.protocol.UriPatternMatcher;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestUriHttpAsyncRequestHandlerMapper {
+
+ @Test
+ public void testRegisterUnregister() throws Exception {
+ HttpAsyncRequestHandler<?> h = Mockito.mock(HttpAsyncRequestHandler.class);
+
+ UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
+ new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
+ UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
+
+ registry.register("/h1", h);
+ registry.unregister("/h1");
+
+ Mockito.verify(matcher).register("/h1", h);
+ Mockito.verify(matcher).unregister("/h1");
+ }
+
+ @Test
+ public void testLookup() throws Exception {
+ UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
+ new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
+ UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
+
+ HttpRequest request = new BasicHttpRequest("GET", "/");
+ registry.lookup(request);
+ registry.unregister("/h1");
+
+ Mockito.verify(matcher).lookup("/");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testRegisterNull() throws Exception {
+ UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+ registry.register(null, null);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testLookupNull() throws Exception {
+ UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+ registry.register(null, null);
+ }
+
+ @Test
+ public void testWildCardMatchingWithQuery() throws Exception {
+ HttpAsyncRequestHandler<?> h1 = Mockito.mock(HttpAsyncRequestHandler.class);
+ HttpAsyncRequestHandler<?> h2 = Mockito.mock(HttpAsyncRequestHandler.class);
+ HttpAsyncRequestHandler<?> def = Mockito.mock(HttpAsyncRequestHandler.class);
+
+ UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
+ new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
+ UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
+ registry.register("*", def);
+ registry.register("*.view", h1);
+ registry.register("*.form", h2);
+
+ HttpAsyncRequestHandler<?> h;
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/that.view?param=value"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/that.form?whatever"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h2 == h);
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/whatever"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(def == h);
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestUriHttpAsyncRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java?rev=1385038&r1=1385037&r2=1385038&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java Sat Sep 15 10:25:47 2012
@@ -27,10 +27,9 @@
package org.apache.http.protocol;
-import java.util.Map;
-
import org.apache.http.HttpRequest;
import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.util.Args;
/**
* Maintains a map of HTTP request handlers keyed by a request URI pattern.
@@ -54,8 +53,13 @@ public class UriHttpRequestHandlerMapper
private final UriPatternMatcher<HttpRequestHandler> matcher;
+ protected UriHttpRequestHandlerMapper(final UriPatternMatcher<HttpRequestHandler> matcher) {
+ super();
+ this.matcher = Args.notNull(matcher, "Pattern matcher");
+ }
+
public UriHttpRequestHandlerMapper() {
- matcher = new UriPatternMatcher<HttpRequestHandler>();
+ this(new UriPatternMatcher<HttpRequestHandler>());
}
/**
@@ -66,12 +70,8 @@ public class UriHttpRequestHandlerMapper
* @param handler the handler.
*/
public void register(final String pattern, final HttpRequestHandler handler) {
- if (pattern == null) {
- throw new IllegalArgumentException("URI request pattern may not be null");
- }
- if (handler == null) {
- throw new IllegalArgumentException("Request handler may not be null");
- }
+ Args.notNull(pattern, "Pattern");
+ Args.notNull(handler, "Handler");
matcher.register(pattern, handler);
}
@@ -85,23 +85,31 @@ public class UriHttpRequestHandlerMapper
}
/**
- * Sets handlers from the given map.
- * @param map the map containing handlers keyed by their URI patterns.
+ * Extracts request path from the given {@link HttpRequest}
*/
- public void setHandlers(final Map<String, HttpRequestHandler> map) {
- matcher.setObjects(map);
+ protected String getRequestPath(final HttpRequest request) {
+ String uriPath = request.getRequestLine().getUri();
+ int index = uriPath.indexOf("?");
+ if (index != -1) {
+ uriPath = uriPath.substring(0, index);
+ } else {
+ index = uriPath.indexOf("#");
+ if (index != -1) {
+ uriPath = uriPath.substring(0, index);
+ }
+ }
+ return uriPath;
}
-
+
/**
- * Get the handler map.
- * @return The map of handlers and their associated URI patterns.
+ * Looks up a handler matching the given request URI.
+ *
+ * @param requestURI the request path
+ * @return handler or <code>null</code> if no match is found.
*/
- public Map<String, HttpRequestHandler> getHandlers() {
- return matcher.getObjects();
- }
-
public HttpRequestHandler lookup(final HttpRequest request) {
- return matcher.lookup(request.getRequestLine().getUri());
+ Args.notNull(request, "HTTP request");
+ return matcher.lookup(getRequestPath(request));
}
}
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java?rev=1385038&r1=1385037&r2=1385038&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java Sat Sep 15 10:25:47 2012
@@ -85,7 +85,7 @@ public class UriPatternMatcher<T> {
}
/**
- * @deprecated (4.1) use {@link #setObjects(Map)}
+ * @deprecated (4.1) do not use
*/
@Deprecated
public synchronized void setHandlers(final Map<String, T> map) {
@@ -95,9 +95,9 @@ public class UriPatternMatcher<T> {
}
/**
- * Sets objects from the given map.
- * @param map the map containing objects keyed by their URI patterns.
+ * @deprecated (4.1) do not use
*/
+ @Deprecated
public synchronized void setObjects(final Map<String, T> map) {
Args.notNull(map, "Map of handlers");
this.map.clear();
@@ -105,11 +105,9 @@ public class UriPatternMatcher<T> {
}
/**
- * Returns the objects map.
- * @return The map of objects.
- *
- * @since 4.2
+ * @deprecated (4.1) do not use
*/
+ @Deprecated
public synchronized Map<String, T> getObjects() {
return this.map;
}
@@ -117,25 +115,19 @@ public class UriPatternMatcher<T> {
/**
* Looks up an object matching the given request URI.
*
- * @param requestURI the request URI
+ * @param requestURI the request path
* @return object or <code>null</code> if no match is found.
*/
- public synchronized T lookup(String requestURI) {
- Args.notNull(requestURI, "Request URI");
- //Strip away the query part part if found
- int index = requestURI.indexOf("?");
- if (index != -1) {
- requestURI = requestURI.substring(0, index);
- }
-
+ public synchronized T lookup(final String path) {
+ Args.notNull(path, "Request path");
// direct match?
- T obj = this.map.get(requestURI);
+ T obj = this.map.get(path);
if (obj == null) {
// pattern match?
String bestMatch = null;
for (Iterator<String> it = this.map.keySet().iterator(); it.hasNext();) {
String pattern = it.next();
- if (matchUriRequestPattern(pattern, requestURI)) {
+ if (matchUriRequestPattern(pattern, path)) {
// we have a match. is it any better?
if (bestMatch == null
|| (bestMatch.length() < pattern.length())
@@ -157,13 +149,13 @@ public class UriPatternMatcher<T> {
* @return <code>true</code> if the request URI matches the pattern,
* <code>false</code> otherwise.
*/
- protected boolean matchUriRequestPattern(final String pattern, final String requestUri) {
+ protected boolean matchUriRequestPattern(final String pattern, final String path) {
if (pattern.equals("*")) {
return true;
} else {
return
- (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) ||
- (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length())));
+ (pattern.endsWith("*") && path.startsWith(pattern.substring(0, pattern.length() - 1))) ||
+ (pattern.startsWith("*") && path.endsWith(pattern.substring(1, pattern.length())));
}
}
Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java?rev=1385038&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java Sat Sep 15 10:25:47 2012
@@ -0,0 +1,103 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestUriHttpRequestHandlerMapper {
+
+ @Test
+ public void testRegisterUnregister() throws Exception {
+ HttpRequestHandler h = Mockito.mock(HttpRequestHandler.class);
+
+ UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
+
+ registry.register("/h1", h);
+ registry.unregister("/h1");
+
+ Mockito.verify(matcher).register("/h1", h);
+ Mockito.verify(matcher).unregister("/h1");
+ }
+
+ @Test
+ public void testLookup() throws Exception {
+ UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
+
+ HttpRequest request = new BasicHttpRequest("GET", "/");
+ registry.lookup(request);
+ registry.unregister("/h1");
+
+ Mockito.verify(matcher).lookup("/");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testRegisterNull() throws Exception {
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
+ registry.register(null, null);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testLookupNull() throws Exception {
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
+ registry.register(null, null);
+ }
+
+ @Test
+ public void testWildCardMatchingWithQuery() throws Exception {
+ HttpRequestHandler h1 = Mockito.mock(HttpRequestHandler.class);
+ HttpRequestHandler h2 = Mockito.mock(HttpRequestHandler.class);
+ HttpRequestHandler def = Mockito.mock(HttpRequestHandler.class);
+
+ UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
+ registry.register("*", def);
+ registry.register("*.view", h1);
+ registry.register("*.form", h2);
+
+ HttpRequestHandler h;
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/that.view?param=value"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/that.form?whatever"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h2 == h);
+
+ h = registry.lookup(new BasicHttpRequest("GET", "/whatever"));
+ Assert.assertNotNull(h);
+ Assert.assertTrue(def == h);
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriHttpRequestHandlerMapper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java?rev=1385038&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java Sat Sep 15 10:25:47 2012
@@ -0,0 +1,155 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUriPatternMatcher {
+
+ @Test
+ public void testRegisterUnregister() throws Exception {
+ Object h1 = new Object();
+ Object h2 = new Object();
+ Object h3 = new Object();
+
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register("/h1", h1);
+ matcher.register("/h2", h2);
+ matcher.register("/h3", h3);
+
+ Object h;
+
+ h = matcher.lookup("/h1");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+ h = matcher.lookup("/h2");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h2 == h);
+ h = matcher.lookup("/h3");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h3 == h);
+
+ matcher.unregister("/h1");
+ h = matcher.lookup("/h1");
+ Assert.assertNull(h);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testRegisterNull() throws Exception {
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register(null, null);
+ }
+
+ @Test
+ public void testWildCardMatching1() throws Exception {
+ Object h1 = new Object();
+ Object h2 = new Object();
+ Object h3 = new Object();
+ Object def = new Object();
+
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register("*", def);
+ matcher.register("/one/*", h1);
+ matcher.register("/one/two/*", h2);
+ matcher.register("/one/two/three/*", h3);
+
+ Object h;
+
+ h = matcher.lookup("/one/request");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+
+ h = matcher.lookup("/one/two/request");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h2 == h);
+
+ h = matcher.lookup("/one/two/three/request");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h3 == h);
+
+ h = matcher.lookup("default/request");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(def == h);
+ }
+
+ @Test
+ public void testWildCardMatching2() throws Exception {
+ Object h1 = new Object();
+ Object h2 = new Object();
+ Object def = new Object();
+
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register("*", def);
+ matcher.register("*.view", h1);
+ matcher.register("*.form", h2);
+
+ Object h;
+
+ h = matcher.lookup("/that.view");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+
+ h = matcher.lookup("/that.form");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h2 == h);
+
+ h = matcher.lookup("/whatever");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(def == h);
+ }
+
+ @Test
+ public void testSuffixPatternOverPrefixPatternMatch() throws Exception {
+ Object h1 = new Object();
+ Object h2 = new Object();
+
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register("/ma*", h1);
+ matcher.register("*tch", h2);
+
+ Object h;
+
+ h = matcher.lookup("/match");
+ Assert.assertNotNull(h);
+ Assert.assertTrue(h1 == h);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testRegisterInvalidInput() throws Exception {
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.register(null, null);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testLookupInvalidInput() throws Exception {
+ UriPatternMatcher<Object> matcher = new UriPatternMatcher<Object>();
+ matcher.lookup(null);
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestUriPatternMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain