1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55 public TestMethodAbort(final String testName) throws IOException {
56 super(testName);
57 }
58
59
60 public static void main(String args[]) {
61 String[] testCaseName = { TestMethodAbort.class.getName() };
62 junit.textui.TestRunner.main(testCaseName);
63 }
64
65
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
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 }