~ubuntu-branches/ubuntu/hardy/commons-httpclient/hardy

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/httpclient/TestMethodAbort.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2006-09-15 20:07:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060915200743-t2md4cgfsb07wgn7
Tags: 3.0.1-0.1
* Non-maintainer upload.
* Bump debhelper Build-Depends to (>= 4.1.0) as required by cdbs' 
  debhelper.mk
* Put the coppyright holders in debian/copyright
* Include the jar file in the package. (Closes: #381354)
* Only include one copy of the docs.
  done by James Westby <jw+debian@jameswestby.net>  Mon, 14 Aug 2006 02:29:47 +0100

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: 155418 $
 
4
 * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
 
5
 * ====================================================================
 
6
 *
 
7
 *  Copyright 1999-2004 The Apache Software Foundation
 
8
 *
 
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
 
12
 *
 
13
 *      http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
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
 * ====================================================================
 
21
 *
 
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/>.
 
26
 *
 
27
 * [Additional notices, if required by prior licensing conditions]
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.commons.httpclient;
 
32
import java.io.BufferedReader;
 
33
import java.io.IOException;
 
34
import java.io.InputStreamReader;
 
35
 
 
36
import junit.framework.Test;
 
37
import junit.framework.TestSuite;
 
38
 
 
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;
 
44
 
 
45
/**
 
46
 * Tests ability to abort method execution.
 
47
 *
 
48
 * @author Oleg Kalnichevski
 
49
 * 
 
50
 * @version $Revision: 155418 $
 
51
 */
 
52
public class TestMethodAbort extends HttpClientTestBase {
 
53
 
 
54
    // ------------------------------------------------------------ Constructor
 
55
    public TestMethodAbort(final String testName) throws IOException {
 
56
        super(testName);
 
57
    }
 
58
 
 
59
    // ------------------------------------------------------------------- Main
 
60
    public static void main(String args[]) {
 
61
        String[] testCaseName = { TestMethodAbort.class.getName() };
 
62
        junit.textui.TestRunner.main(testCaseName);
 
63
    }
 
64
 
 
65
    // ------------------------------------------------------- TestCase Methods
 
66
 
 
67
    public static Test suite() {
 
68
        return new TestSuite(TestMethodAbort.class);
 
69
    }
 
70
 
 
71
    private class ProduceGarbageHandler implements HttpRequestHandler {
 
72
 
 
73
        public ProduceGarbageHandler() {
 
74
            super();
 
75
        }
 
76
 
 
77
        public boolean processRequest(
 
78
            final SimpleHttpServerConnection conn,
 
79
            final SimpleRequest request) throws IOException
 
80
        {
 
81
 
 
82
            final String garbage = "garbage!\r\n";  
 
83
            final long count = 1000000000;  
 
84
 
 
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");
 
91
            out.println();
 
92
            for (int i = 0; i < count; i++) {
 
93
                out.print(garbage);
 
94
            }
 
95
            return true;
 
96
        }
 
97
    }
 
98
 
 
99
    public void testAbortMethod() throws IOException {
 
100
        this.server.setRequestHandler(new ProduceGarbageHandler());
 
101
        final GetMethod httpget = new GetMethod("/test/");
 
102
        
 
103
        Thread thread = new Thread(new Runnable() {
 
104
            public void run() {            
 
105
                try {
 
106
                    Thread.sleep(500);
 
107
                } catch (InterruptedException e) {
 
108
                }
 
109
                httpget.abort();
 
110
            }
 
111
            
 
112
        });
 
113
        thread.setDaemon(true); 
 
114
        thread.start();
 
115
        
 
116
        try {
 
117
            this.client.executeMethod(httpget);
 
118
            BufferedReader in = new BufferedReader(new InputStreamReader(
 
119
                httpget.getResponseBodyAsStream()));
 
120
            String line = null;
 
121
            while ((line = in.readLine()) != null) {  
 
122
            }
 
123
            fail("IOException must have been thrown");
 
124
        } catch (IOException e) {
 
125
            // expected
 
126
        } finally {
 
127
            httpget.releaseConnection();
 
128
        }
 
129
        assertTrue(httpget.isAborted());
 
130
    }
 
131
 
 
132
    public void testAbortedMethodExecute() throws IOException {
 
133
        final GetMethod httpget = new GetMethod("/test/");
 
134
        
 
135
        try {
 
136
            httpget.abort();
 
137
            try {
 
138
                this.client.executeMethod(httpget);
 
139
                fail("IllegalStateException must have been thrown");
 
140
            } catch (IllegalStateException e) {
 
141
            }
 
142
        } finally {
 
143
            httpget.releaseConnection();
 
144
        }
 
145
        assertTrue(httpget.isAborted());
 
146
    }
 
147
}