001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.BufferedReader;
035: import java.io.IOException;
036: import java.io.InputStreamReader;
037:
038: import junit.framework.Test;
039: import junit.framework.TestSuite;
040:
041: import org.apache.commons.httpclient.methods.GetMethod;
042: import org.apache.commons.httpclient.server.HttpRequestHandler;
043: import org.apache.commons.httpclient.server.ResponseWriter;
044: import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
045: import org.apache.commons.httpclient.server.SimpleRequest;
046:
047: /**
048: * Tests ability to abort method execution.
049: *
050: * @author Oleg Kalnichevski
051: *
052: * @version $Revision: 480424 $
053: */
054: public class TestMethodAbort extends HttpClientTestBase {
055:
056: // ------------------------------------------------------------ Constructor
057: public TestMethodAbort(final String testName) throws IOException {
058: super (testName);
059: }
060:
061: // ------------------------------------------------------------------- Main
062: public static void main(String args[]) {
063: String[] testCaseName = { TestMethodAbort.class.getName() };
064: junit.textui.TestRunner.main(testCaseName);
065: }
066:
067: // ------------------------------------------------------- TestCase Methods
068:
069: public static Test suite() {
070: return new TestSuite(TestMethodAbort.class);
071: }
072:
073: private class ProduceGarbageHandler implements HttpRequestHandler {
074:
075: public ProduceGarbageHandler() {
076: super ();
077: }
078:
079: public boolean processRequest(
080: final SimpleHttpServerConnection conn,
081: final SimpleRequest request) throws IOException {
082:
083: final String garbage = "garbage!\r\n";
084: final long count = 1000000000;
085:
086: HttpVersion httpversion = request.getRequestLine()
087: .getHttpVersion();
088: ResponseWriter out = conn.getWriter();
089: out.println(httpversion + " 200 OK");
090: out.println("Content-Type: text/plain");
091: out.println("Content-Length: " + count * garbage.length());
092: out.println("Connection: close");
093: out.println();
094: for (int i = 0; i < count; i++) {
095: out.print(garbage);
096: }
097: return true;
098: }
099: }
100:
101: public void testAbortMethod() throws IOException {
102: this .server.setRequestHandler(new ProduceGarbageHandler());
103: final GetMethod httpget = new GetMethod("/test/");
104:
105: Thread thread = new Thread(new Runnable() {
106: public void run() {
107: try {
108: Thread.sleep(500);
109: } catch (InterruptedException e) {
110: }
111: httpget.abort();
112: }
113:
114: });
115: thread.setDaemon(true);
116: thread.start();
117:
118: try {
119: this .client.executeMethod(httpget);
120: BufferedReader in = new BufferedReader(
121: new InputStreamReader(httpget
122: .getResponseBodyAsStream()));
123: String line = null;
124: while ((line = in.readLine()) != null) {
125: }
126: fail("IOException must have been thrown");
127: } catch (IOException e) {
128: // expected
129: } finally {
130: httpget.releaseConnection();
131: }
132: assertTrue(httpget.isAborted());
133: }
134:
135: public void testAbortedMethodExecute() throws IOException {
136: final GetMethod httpget = new GetMethod("/test/");
137:
138: try {
139: httpget.abort();
140: try {
141: this .client.executeMethod(httpget);
142: fail("IllegalStateException must have been thrown");
143: } catch (IllegalStateException e) {
144: }
145: } finally {
146: httpget.releaseConnection();
147: }
148: assertTrue(httpget.isAborted());
149: }
150: }
|