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 $
4
* $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5
* ====================================================================
7
* Copyright 1999-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
20
* ====================================================================
22
* This software consists of voluntary contributions made by many
23
* individuals on behalf of the Apache Software Foundation. For more
24
* information on the Apache Software Foundation, please see
25
* <http://www.apache.org/>.
27
* [Additional notices, if required by prior licensing conditions]
31
package org.apache.commons.httpclient;
32
import java.io.BufferedReader;
33
import java.io.IOException;
34
import java.io.InputStreamReader;
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
39
import org.apache.commons.httpclient.methods.GetMethod;
40
import org.apache.commons.httpclient.server.HttpRequestHandler;
41
import org.apache.commons.httpclient.server.ResponseWriter;
42
import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
43
import org.apache.commons.httpclient.server.SimpleRequest;
46
* Tests ability to abort method execution.
48
* @author Oleg Kalnichevski
50
* @version $Revision: 155418 $
52
public class TestMethodAbort extends HttpClientTestBase {
54
// ------------------------------------------------------------ Constructor
55
public TestMethodAbort(final String testName) throws IOException {
59
// ------------------------------------------------------------------- Main
60
public static void main(String args[]) {
61
String[] testCaseName = { TestMethodAbort.class.getName() };
62
junit.textui.TestRunner.main(testCaseName);
65
// ------------------------------------------------------- TestCase Methods
67
public static Test suite() {
68
return new TestSuite(TestMethodAbort.class);
71
private class ProduceGarbageHandler implements HttpRequestHandler {
73
public ProduceGarbageHandler() {
77
public boolean processRequest(
78
final SimpleHttpServerConnection conn,
79
final SimpleRequest request) throws IOException
82
final String garbage = "garbage!\r\n";
83
final long count = 1000000000;
85
HttpVersion httpversion = request.getRequestLine().getHttpVersion();
86
ResponseWriter out = conn.getWriter();
87
out.println(httpversion + " 200 OK");
88
out.println("Content-Type: text/plain");
89
out.println("Content-Length: " + count * garbage.length()) ;
90
out.println("Connection: close");
92
for (int i = 0; i < count; i++) {
99
public void testAbortMethod() throws IOException {
100
this.server.setRequestHandler(new ProduceGarbageHandler());
101
final GetMethod httpget = new GetMethod("/test/");
103
Thread thread = new Thread(new Runnable() {
107
} catch (InterruptedException e) {
113
thread.setDaemon(true);
117
this.client.executeMethod(httpget);
118
BufferedReader in = new BufferedReader(new InputStreamReader(
119
httpget.getResponseBodyAsStream()));
121
while ((line = in.readLine()) != null) {
123
fail("IOException must have been thrown");
124
} catch (IOException e) {
127
httpget.releaseConnection();
129
assertTrue(httpget.isAborted());
132
public void testAbortedMethodExecute() throws IOException {
133
final GetMethod httpget = new GetMethod("/test/");
138
this.client.executeMethod(httpget);
139
fail("IllegalStateException must have been thrown");
140
} catch (IllegalStateException e) {
143
httpget.releaseConnection();
145
assertTrue(httpget.isAborted());