hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r542934 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio: mockup/ protocol/
Date Wed, 30 May 2007 21:06:40 GMT
Author: olegk
Date: Wed May 30 14:06:39 2007
New Revision: 542934

URL: http://svn.apache.org/viewvc?view=rev&rev=542934
Log:
Refactored HttpCore NIO protocol handler tests

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
  (with props)
Removed:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.java
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpClient.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java?view=auto&rev=542934
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
Wed May 30 14:06:39 2007
@@ -0,0 +1,85 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.mockup;
+
+import java.io.IOException;
+
+import org.apache.http.HttpException;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.protocol.EventListener;
+
+public class CountingEventListener implements EventListener {
+
+    private volatile int connCount = 0;
+    
+    public CountingEventListener() {
+        super();
+    }
+    
+    public int getConnCount() {
+        return this.connCount;
+    }
+    
+    private void incrementConnCount() {
+        synchronized (this) {
+            this.connCount++;
+            this.notifyAll();
+        }
+    }
+    
+    public void await(int connCount, long timeout) throws InterruptedException {
+        synchronized (this) {
+            while (this.connCount < connCount) {
+                this.wait(timeout);
+            }
+        }
+    }
+    
+    public void connectionOpen(final NHttpConnection conn) {
+    }
+
+    public void connectionTimeout(final NHttpConnection conn) {
+    }
+
+    public void connectionClosed(final NHttpConnection conn) {
+        incrementConnCount();
+    }
+
+    public void fatalIOException(final IOException ex, final NHttpConnection conn) {
+        ex.printStackTrace();
+    }
+
+    public void fatalProtocolException(final HttpException ex, NHttpConnection conn) {
+        ex.printStackTrace();
+    }
+        
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java?view=auto&rev=542934
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
Wed May 30 14:06:39 2007
@@ -0,0 +1,49 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.mockup;
+
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerResolver;
+
+public class SimpleHttpRequestHandlerResolver implements HttpRequestHandlerResolver {
+
+    private final HttpRequestHandler handler;
+    
+    public SimpleHttpRequestHandlerResolver(final HttpRequestHandler handler) {
+        super();
+        this.handler = handler;
+    }
+    
+    public HttpRequestHandler lookup(final String requestURI) {
+        return this.handler;
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleHttpRequestHandlerResolver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpClient.java?view=diff&rev=542934&r1=542933&r2=542934
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpClient.java
Wed May 30 14:06:39 2007
@@ -34,69 +34,66 @@
 import java.io.IOException;
 import java.net.InetSocketAddress;
 
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.nio.DefaultClientIOEventDispatch;
 import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
-import org.apache.http.nio.protocol.BufferingHttpClientHandler;
-import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
 import org.apache.http.nio.reactor.ConnectingIOReactor;
 import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpProtocolParams;
-import org.apache.http.protocol.BasicHttpProcessor;
-import org.apache.http.protocol.RequestConnControl;
-import org.apache.http.protocol.RequestContent;
-import org.apache.http.protocol.RequestExpectContinue;
-import org.apache.http.protocol.RequestTargetHost;
-import org.apache.http.protocol.RequestUserAgent;
+import org.apache.http.params.HttpParams;
 
-public class TestHttpClient extends TestHttpServiceBase {
+public class TestHttpClient {
 
-    private HttpRequestExecutionHandler execHandler;
+    private final ConnectingIOReactor ioReactor;
+    private final HttpParams params;
     
-    public TestHttpClient() throws IOException {
-        super();
-        this.params
-            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
-            .setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000)
-            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
-            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
-            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
-            .setParameter(HttpProtocolParams.USER_AGENT, "TEST-CLIENT/1.1");
+    private volatile IOReactorThread thread;
 
-        this.ioReactor = new DefaultConnectingIOReactor(2, this.params);
+    public TestHttpClient(final HttpParams params) throws IOException {
+        super();
+        this.ioReactor = new DefaultConnectingIOReactor(2, params);
+        this.params = params;
     }
-    
-    public void setHttpRequestExecutionHandler(final HttpRequestExecutionHandler handler)
{
-        this.execHandler = handler;
+
+    public HttpParams getParams() {
+        return this.params;
     }
     
-    protected void execute() throws IOException {
-        BasicHttpProcessor httpproc = new BasicHttpProcessor();
-        httpproc.addInterceptor(new RequestContent());
-        httpproc.addInterceptor(new RequestTargetHost());
-        httpproc.addInterceptor(new RequestConnControl());
-        httpproc.addInterceptor(new RequestUserAgent());
-        httpproc.addInterceptor(new RequestExpectContinue());
-        
-        BufferingHttpClientHandler clientHandler = new BufferingHttpClientHandler(
-                httpproc,
-                this.execHandler,
-                new DefaultConnectionReuseStrategy(),
-                this.params);
-        
-        clientHandler.setEventListener(new EventLogger());
-
-        IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(
-                clientHandler, 
-                this.params);
-        
+    private void execute(final IOEventDispatch ioEventDispatch) throws IOException {
         this.ioReactor.execute(ioEventDispatch);
     }
     
     public void openConnection(final InetSocketAddress address, final Object attachment)
{
-        ((ConnectingIOReactor) this.ioReactor).connect(
-                address, null, attachment, null);
+        this.ioReactor.connect(address, null, attachment, null);
     }
+ 
+    public void start(final IOEventDispatch ioEventDispatch) {
+        this.thread = new IOReactorThread(ioEventDispatch);
+        this.thread.start();
+    }
+    
+    public void shutdown() throws IOException {
+        this.ioReactor.shutdown();
+        try {
+            this.thread.join(500);
+        } catch (InterruptedException ignore) {
+        }
+    }
+    
+    private class IOReactorThread extends Thread {
+
+        private final IOEventDispatch ioEventDispatch;
+        
+        public IOReactorThread(final IOEventDispatch ioEventDispatch) {
+            super();
+            this.ioEventDispatch = ioEventDispatch;
+        }
+        
+        public void run() {
+            try {
+                execute(this.ioEventDispatch);
+            } catch (IOException ex) {
+                ex.printStackTrace();
+            }
+        }
+
+    }    
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java?view=diff&rev=542934&r1=542933&r2=542934
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
Wed May 30 14:06:39 2007
@@ -33,96 +33,85 @@
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
-import java.net.SocketAddress;
 
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
 import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
-import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
 import org.apache.http.nio.reactor.IOEventDispatch;
 import org.apache.http.nio.reactor.ListeningIOReactor;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpProtocolParams;
-import org.apache.http.protocol.BasicHttpProcessor;
-import org.apache.http.protocol.HttpExpectationVerifier;
-import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.protocol.HttpRequestHandlerRegistry;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
+import org.apache.http.params.HttpParams;
 
 /**
  * Trivial test server based on HttpCore NIO
  * 
  * @author Oleg Kalnichevski
  */
-public class TestHttpServer extends TestHttpServiceBase {
+public class TestHttpServer {
 
-    private final HttpRequestHandlerRegistry reqistry;
-    private HttpExpectationVerifier expectationVerifier;
-    private volatile SocketAddress address;
-    private final Object mutex;
+    private final ListeningIOReactor ioReactor;
+    private final HttpParams params;
+    private final Object socketMutex;
+
+    private volatile IOReactorThread thread;
+    private volatile InetSocketAddress address;
     
-    public TestHttpServer() throws IOException {
+    public TestHttpServer(final HttpParams params) throws IOException {
         super();
-        this.params
-            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
-            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
-            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
-            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
-            .setParameter(HttpProtocolParams.ORIGIN_SERVER, "TEST-SERVER/1.1");
-        
-        this.reqistry = new HttpRequestHandlerRegistry();
-        this.ioReactor = new DefaultListeningIOReactor(2, this.params);
-        this.mutex = new Object();
-    }
-    
-    public void registerHandler(
-            final String pattern, 
-            final HttpRequestHandler handler) {
-        this.reqistry.register(pattern, handler);
+        this.ioReactor = new DefaultListeningIOReactor(2, params);
+        this.params = params;
+        this.socketMutex = new Object();
     }
-    
-    public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier)
{
-        this.expectationVerifier = expectationVerifier;
+
+    public HttpParams getParams() {
+        return this.params;
     }
     
-    protected void execute() throws IOException {
-        synchronized (this.mutex) {
-            this.address = ((ListeningIOReactor) this.ioReactor).listen(new InetSocketAddress(0));
-            this.mutex.notifyAll();
+    private void execute(final IOEventDispatch ioEventDispatch) throws IOException {
+        synchronized (this.socketMutex) {
+            this.address = (InetSocketAddress) this.ioReactor.listen(
+                    new InetSocketAddress(0));
+            this.socketMutex.notifyAll();
         }
-        BasicHttpProcessor httpproc = new BasicHttpProcessor();
-        httpproc.addInterceptor(new ResponseDate());
-        httpproc.addInterceptor(new ResponseServer());
-        httpproc.addInterceptor(new ResponseContent());
-        httpproc.addInterceptor(new ResponseConnControl());
-        
-        BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
-                httpproc,
-                new DefaultHttpResponseFactory(),
-                new DefaultConnectionReuseStrategy(),
-                this.params);
-        
-        serviceHandler.setEventListener(new EventLogger());
-        serviceHandler.setExpectationVerifier(this.expectationVerifier);
-        serviceHandler.setHandlerResolver(this.reqistry);
-        
-        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
-                serviceHandler, 
-                this.params);
         this.ioReactor.execute(ioEventDispatch);
     }
     
-    public SocketAddress getSocketAddress() throws InterruptedException {
-        synchronized (this.mutex) {
+    public InetSocketAddress getSocketAddress() throws InterruptedException {
+        synchronized (this.socketMutex) {
             while (this.address == null) {
-                this.mutex.wait();
+                this.socketMutex.wait();
             }
         }
         return this.address;
     }
+
+    public void start(final IOEventDispatch ioEventDispatch) {
+        this.thread = new IOReactorThread(ioEventDispatch);
+        this.thread.start();
+    }
+    
+    public void shutdown() throws IOException {
+        this.ioReactor.shutdown();
+        try {
+            this.thread.join(500);
+        } catch (InterruptedException ignore) {
+        }
+    }
+    
+    private class IOReactorThread extends Thread {
+
+        private final IOEventDispatch ioEventDispatch;
+        
+        public IOReactorThread(final IOEventDispatch ioEventDispatch) {
+            super();
+            this.ioEventDispatch = ioEventDispatch;
+        }
+        
+        public void run() {
+            try {
+                execute(this.ioEventDispatch);
+            } catch (IOException ex) {
+                ex.printStackTrace();
+            }
+        }
+
+    }    
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java?view=diff&rev=542934&r1=542933&r2=542934
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
Wed May 30 14:06:39 2007
@@ -52,16 +52,38 @@
 import org.apache.http.HttpVersion;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.nio.DefaultClientIOEventDispatch;
+import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
 import org.apache.http.message.BasicHttpEntityEnclosingRequest;
 import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.nio.NHttpClientHandler;
 import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.NHttpServiceHandler;
+import org.apache.http.nio.mockup.CountingEventListener;
+import org.apache.http.nio.mockup.SimpleHttpRequestHandlerResolver;
 import org.apache.http.nio.mockup.TestHttpClient;
 import org.apache.http.nio.mockup.TestHttpServer;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExecutionContext;
 import org.apache.http.protocol.HttpExpectationVerifier;
 import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
 import org.apache.http.util.EncodingUtils;
 import org.apache.http.util.EntityUtils;
 
@@ -95,15 +117,78 @@
     private TestHttpClient client;
     
     protected void setUp() throws Exception {
-        this.server = new TestHttpServer();
-        this.client = new TestHttpClient();
+        HttpParams serverParams = new BasicHttpParams();
+        serverParams
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.ORIGIN_SERVER, "TEST-SERVER/1.1");
+        
+        this.server = new TestHttpServer(serverParams);
+        
+        HttpParams clientParams = new BasicHttpParams();
+        clientParams
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
+            .setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.USER_AGENT, "TEST-CLIENT/1.1");
+        
+        this.client = new TestHttpClient(clientParams);
     }
 
     protected void tearDown() throws Exception {
         this.server.shutdown();
         this.client.shutdown();
     }
+    
+    private NHttpServiceHandler createHttpServiceHandler(
+            final HttpRequestHandler requestHandler,
+            final HttpExpectationVerifier expectationVerifier,
+            final EventListener eventListener) {
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        httpproc.addInterceptor(new ResponseDate());
+        httpproc.addInterceptor(new ResponseServer());
+        httpproc.addInterceptor(new ResponseContent());
+        httpproc.addInterceptor(new ResponseConnControl());
+
+        BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
+                httpproc,
+                new DefaultHttpResponseFactory(),
+                new DefaultConnectionReuseStrategy(),
+                this.server.getParams());
+
+        serviceHandler.setHandlerResolver(
+                new SimpleHttpRequestHandlerResolver(requestHandler));
+        serviceHandler.setExpectationVerifier(expectationVerifier);
+        serviceHandler.setEventListener(eventListener);
+        
+        return serviceHandler;
+    }
+    
+    private NHttpClientHandler createHttpClientHandler(
+            final HttpRequestExecutionHandler requestExecutionHandler,
+            final EventListener eventListener) {
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        httpproc.addInterceptor(new RequestContent());
+        httpproc.addInterceptor(new RequestTargetHost());
+        httpproc.addInterceptor(new RequestConnControl());
+        httpproc.addInterceptor(new RequestUserAgent());
+        httpproc.addInterceptor(new RequestExpectContinue());
+        
+        BufferingHttpClientHandler clientHandler = new BufferingHttpClientHandler(
+                httpproc,
+                requestExecutionHandler,
+                new DefaultConnectionReuseStrategy(),
+                this.client.getParams());
+
+        clientHandler.setEventListener(eventListener);
 
+        return clientHandler;
+    }
+    
     /**
      * This test case executes a series of simple (non-pipelined) GET requests 
      * over multiple connections. 
@@ -129,8 +214,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -150,10 +234,9 @@
                 response.setEntity(entity);
             }
             
-        });
+        };
         
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -199,10 +282,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
+        
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
         
-        this.server.start();
-        this.client.start();
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -212,8 +315,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -260,8 +363,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -281,10 +383,9 @@
                 }
             }
             
-        });
+        };
         
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -334,10 +435,30 @@
                 }
             }
             
-        });
+        };
         
-        this.server.start();
-        this.client.start();
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
+        
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
+        
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -347,8 +468,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -395,8 +516,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -415,10 +535,9 @@
                 }
             }
             
-        });
+        };
         
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -469,10 +588,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
         
-        this.server.start();
-        this.client.start();
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
+        
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -482,8 +621,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -530,8 +669,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
             
 
             public void handle(
@@ -552,13 +690,13 @@
                 }
             }
             
-        });
+        };
         
-        // Initialize the client side request executor
         // Set protocol level to HTTP/1.0
         this.client.getParams().setParameter(
                 HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -608,10 +746,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
         
-        this.server.start();
-        this.client.start();
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
+        
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -621,8 +779,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -669,8 +827,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -689,12 +846,12 @@
                 }
             }
             
-        });
+        };
 
         // Activate 'expect: continue' handshake
         this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
true);
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -745,10 +902,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
         
-        this.server.start();
-        this.client.start();
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
+        
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -758,8 +935,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -790,8 +967,7 @@
         final int reqNo = 3;
         final List responses = new ArrayList(reqNo);
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -802,9 +978,9 @@
                 response.setEntity(outgoing);
             }
             
-        });
+        };
         
-        this.server.setExpectationVerifier(new HttpExpectationVerifier() {
+        HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier() {
 
             public void verify(
                     final HttpRequest request, 
@@ -828,12 +1004,12 @@
                 }
             }
             
-        });
+        };
 
         // Activate 'expect: continue' handshake
         this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
true);
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -887,10 +1063,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
         
-        this.server.start();
-        this.client.start();
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                expectationVerifier,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
+        
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
         
@@ -898,7 +1094,7 @@
                 new InetSocketAddress("localhost", serverAddress.getPort()), 
                 responses);
      
-        this.client.await(1, 1000);
+        clientEventListener.await(1, 1000);
         
         this.client.shutdown();
         this.server.shutdown();
@@ -939,8 +1135,7 @@
             responseData[i] = new ArrayList();
         }
         
-        // Initialize the server-side request handler
-        this.server.registerHandler("*", new HttpRequestHandler() {
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
 
             public void handle(
                     final HttpRequest request, 
@@ -961,10 +1156,9 @@
                 response.setEntity(entity);
             }
             
-        });
+        };
         
-        // Initialize the client side request executor
-        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
 
             public void initalizeContext(final HttpContext context, final Object attachment)
{
                 context.setAttribute("LIST", (List) attachment);
@@ -1004,10 +1198,30 @@
                 }
             }
             
-        });
+        };
+        
+        CountingEventListener serverEventListener = new CountingEventListener();
+        CountingEventListener clientEventListener = new CountingEventListener();
+        
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+
+        IOEventDispatch serverEventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                this.server.getParams());
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler, 
+                clientEventListener);
+
+        IOEventDispatch clientEventDispatch = new DefaultClientIOEventDispatch(
+                clientHandler, 
+                this.client.getParams());        
         
-        this.server.start();
-        this.client.start();
+        this.server.start(serverEventDispatch);
+        this.client.start(clientEventDispatch);
         
         InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
 
@@ -1019,8 +1233,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo, 1000);
-        assertEquals(connNo, this.client.getConnCount());
+        clientEventListener.await(connNo, 1000);
+        assertEquals(connNo, clientEventListener.getConnCount());
 
         List[] responseDataGET = responseData; 
 
@@ -1037,8 +1251,8 @@
                     responseData[i]);
         }
      
-        this.client.await(connNo * 2, 1000);
-        assertEquals(connNo * 2, this.client.getConnCount());
+        clientEventListener.await(connNo * 2, 1000);
+        assertEquals(connNo * 2, clientEventListener.getConnCount());
         
         this.client.shutdown();
         this.server.shutdown();



Mime
View raw message