KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestIdleConnectionTimeout


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java,v 1.5 2004/11/07 12:31:42 olegk Exp $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30 package org.apache.commons.httpclient;
31
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
37 import org.apache.commons.httpclient.util.IdleConnectionHandler;
38 import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
39
40 /**
41  */

42 public class TestIdleConnectionTimeout extends TestCase {
43     /**
44      *
45      */

46     public TestIdleConnectionTimeout() {
47         super();
48     }
49     /**
50      * @param arg0
51      */

52     public TestIdleConnectionTimeout(String JavaDoc arg0) {
53         super(arg0);
54     }
55     
56     // ------------------------------------------------------- TestCase Methods
57

58     public static Test suite() {
59         return new TestSuite(TestIdleConnectionTimeout.class);
60     }
61
62     /**
63      * Tests that the IdleConnectionHandler correctly closes connections.
64      */

65     public void testHandler() {
66         
67         TimeoutHttpConnection connection = new TimeoutHttpConnection();
68         
69         IdleConnectionHandler handler = new IdleConnectionHandler();
70         
71         handler.add(connection);
72         
73         synchronized(this) {
74             try {
75                 this.wait(250);
76             } catch (InterruptedException JavaDoc e) {
77                 e.printStackTrace();
78             }
79         }
80         
81         handler.closeIdleConnections(100);
82         
83         assertTrue("Connection not closed", connection.isClosed());
84
85         connection.setClosed(false);
86         
87         handler.remove(connection);
88         
89         synchronized(this) {
90             try {
91                 this.wait(250);
92             } catch (InterruptedException JavaDoc e) {
93                 e.printStackTrace();
94             }
95         }
96
97         handler.closeIdleConnections(100);
98         
99         assertFalse("Connection closed", connection.isClosed());
100     }
101
102     /**
103      * Tests that the IdleConnectionTimeoutThread works correctly.
104      */

105     public void testTimeoutThread() {
106         
107         TimeoutHttpConnectionManager cm = new TimeoutHttpConnectionManager();
108         
109         IdleConnectionTimeoutThread timeoutThread = new IdleConnectionTimeoutThread();
110         timeoutThread.addConnectionManager(cm);
111         timeoutThread.setTimeoutInterval(100);
112         timeoutThread.start();
113         
114         synchronized(this) {
115             try {
116                 this.wait(250);
117             } catch (InterruptedException JavaDoc e) {
118                 e.printStackTrace();
119             }
120         }
121         
122         assertTrue("closeIdleConnections() not called", cm.closed);
123
124         timeoutThread.removeConnectionManager(cm);
125         cm.closed = false;
126         
127         synchronized(this) {
128             try {
129                 this.wait(250);
130             } catch (InterruptedException JavaDoc e) {
131                 e.printStackTrace();
132             }
133         }
134
135         assertFalse("closeIdleConnections() called", cm.closed);
136         
137         timeoutThread.shutdown();
138     }
139     
140     private static class TimeoutHttpConnectionManager implements HttpConnectionManager {
141         
142         public boolean closed = false;
143         
144         public void closeIdleConnections(long idleTimeout) {
145             this.closed = true;
146         }
147
148         /**
149          * @deprecated
150          */

151         public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
152             throws HttpException {
153             return null;
154         }
155
156         public HttpConnection getConnection(HostConfiguration hostConfiguration) {
157             return null;
158         }
159
160         public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
161             long timeout) throws ConnectionPoolTimeoutException {
162             return null;
163         }
164
165         public HttpConnectionManagerParams getParams() {
166             return null;
167         }
168
169         public void releaseConnection(HttpConnection conn) {
170         }
171
172         public void setParams(HttpConnectionManagerParams params) {
173         }
174 }
175     
176     private static class TimeoutHttpConnection extends HttpConnection {
177         
178         private boolean closed = false;;
179         
180         public TimeoutHttpConnection() {
181             super("fake-host", 80);
182         }
183         
184         /**
185          * @return Returns the closed.
186          */

187         public boolean isClosed() {
188             return closed;
189         }
190         /**
191          * @param closed The closed to set.
192          */

193         public void setClosed(boolean closed) {
194             this.closed = closed;
195         }
196         
197         /* (non-Javadoc)
198          * @see org.apache.commons.httpclient.HttpConnection#close()
199          */

200         public void close() {
201             closed = true;
202         }
203     }
204     
205 }
206
Popular Tags