KickJava   Java API By Example, From Geeks To Geeks.

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


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

31
32 package org.apache.commons.httpclient;
33 import java.io.BufferedReader JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36
37 import junit.framework.Test;
38 import junit.framework.TestSuite;
39
40 import org.apache.commons.httpclient.methods.GetMethod;
41 import org.apache.commons.httpclient.server.HttpRequestHandler;
42 import org.apache.commons.httpclient.server.ResponseWriter;
43 import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
44 import org.apache.commons.httpclient.server.SimpleRequest;
45
46 /**
47  * Tests ability to abort method execution.
48  *
49  * @author Oleg Kalnichevski
50  *
51  * @version $Revision: 480424 $
52  */

53 public class TestMethodAbort extends HttpClientTestBase {
54
55     // ------------------------------------------------------------ Constructor
56
public TestMethodAbort(final String JavaDoc testName) throws IOException JavaDoc {
57         super(testName);
58     }
59
60     // ------------------------------------------------------------------- Main
61
public static void main(String JavaDoc args[]) {
62         String JavaDoc[] testCaseName = { TestMethodAbort.class.getName() };
63         junit.textui.TestRunner.main(testCaseName);
64     }
65
66     // ------------------------------------------------------- TestCase Methods
67

68     public static Test suite() {
69         return new TestSuite(TestMethodAbort.class);
70     }
71
72     private class ProduceGarbageHandler implements HttpRequestHandler {
73
74         public ProduceGarbageHandler() {
75             super();
76         }
77
78         public boolean processRequest(
79             final SimpleHttpServerConnection conn,
80             final SimpleRequest request) throws IOException JavaDoc
81         {
82
83             final String JavaDoc garbage = "garbage!\r\n";
84             final long count = 1000000000;
85
86             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
87             ResponseWriter out = conn.getWriter();
88             out.println(httpversion + " 200 OK");
89             out.println("Content-Type: text/plain");
90             out.println("Content-Length: " + count * garbage.length()) ;
91             out.println("Connection: close");
92             out.println();
93             for (int i = 0; i < count; i++) {
94                 out.print(garbage);
95             }
96             return true;
97         }
98     }
99
100     public void testAbortMethod() throws IOException JavaDoc {
101         this.server.setRequestHandler(new ProduceGarbageHandler());
102         final GetMethod httpget = new GetMethod("/test/");
103         
104         Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
105             public void run() {
106                 try {
107                     Thread.sleep(500);
108                 } catch (InterruptedException JavaDoc e) {
109                 }
110                 httpget.abort();
111             }
112             
113         });
114         thread.setDaemon(true);
115         thread.start();
116         
117         try {
118             this.client.executeMethod(httpget);
119             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
120                 httpget.getResponseBodyAsStream()));
121             String JavaDoc line = null;
122             while ((line = in.readLine()) != null) {
123             }
124             fail("IOException must have been thrown");
125         } catch (IOException JavaDoc e) {
126             // expected
127
} finally {
128             httpget.releaseConnection();
129         }
130         assertTrue(httpget.isAborted());
131     }
132
133     public void testAbortedMethodExecute() throws IOException JavaDoc {
134         final GetMethod httpget = new GetMethod("/test/");
135         
136         try {
137             httpget.abort();
138             try {
139                 this.client.executeMethod(httpget);
140                 fail("IllegalStateException must have been thrown");
141             } catch (IllegalStateException JavaDoc e) {
142             }
143         } finally {
144             httpget.releaseConnection();
145         }
146         assertTrue(httpget.isAborted());
147     }
148 }
149
Popular Tags