1 package net.sf.openrocket.communication;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.io.UnsupportedEncodingException;
10 import java.net.HttpURLConnection;
11 import java.net.MalformedURLException;
12 import java.net.ProtocolException;
14 import java.security.Permission;
15 import java.util.HashMap;
16 import java.util.List;
19 import net.sf.openrocket.util.BugException;
21 public class HttpURLConnectionMock extends HttpURLConnection {
23 private static final URL MOCK_URL;
26 MOCK_URL = new URL("http://localhost/");
27 } catch (MalformedURLException e) {
28 throw new BugException(e);
32 private volatile boolean instanceFollowRedirects = false;
33 private volatile String requestMethod = "";
34 private volatile int responseCode;
35 private Map<String, String> requestProperties = new HashMap<String, String>();
36 private volatile int connectTimeout = -1;
37 private volatile String contentEncoding = "";
39 private volatile boolean doInput = false;
40 private volatile boolean doOutput = false;
42 private volatile byte[] content = null;
43 private volatile String contentType = null;
44 private volatile boolean useCaches = false;
47 private volatile InputStream inputStream = null;
48 private volatile ByteArrayOutputStream outputStream = null;
50 private volatile String trueUrl = null;
53 private volatile boolean connected = false;
54 private volatile int connectionDelay = 0;
56 private volatile boolean failed = false;
61 public HttpURLConnectionMock() {
65 public HttpURLConnectionMock(URL u) {
71 public String getTrueUrl() {
75 public void setTrueUrl(String url) {
76 assertNull(this.trueUrl);
81 public boolean hasFailed() {
86 public void setConnectionDelay(int delay) {
87 this.connectionDelay = delay;
93 public void connect() {
96 Thread.sleep(connectionDelay);
97 } catch (InterruptedException e) {
104 public void disconnect() {
109 public boolean usingProxy() {
117 public boolean getInstanceFollowRedirects() {
118 return this.instanceFollowRedirects;
122 public void setInstanceFollowRedirects(boolean followRedirects) {
123 assertFalse(connected);
124 this.instanceFollowRedirects = followRedirects;
128 public String getRequestMethod() {
129 return this.requestMethod;
133 public void setRequestMethod(String method) throws ProtocolException {
134 assertFalse(connected);
135 this.requestMethod = method;
139 public int getResponseCode() throws IOException {
141 return this.responseCode;
144 public void setResponseCode(int code) {
145 this.responseCode = code;
150 public void addRequestProperty(String key, String value) {
151 assertFalse(connected);
152 assertFalse(this.requestProperties.containsKey(key.toLowerCase()));
153 this.requestProperties.put(key.toLowerCase(), value);
158 public void setRequestProperty(String key, String value) {
159 assertFalse(connected);
160 this.requestProperties.put(key.toLowerCase(), value);
165 public String getRequestProperty(String key) {
166 return this.requestProperties.get(key.toLowerCase());
171 public int getConnectTimeout() {
172 return this.connectTimeout;
176 public void setConnectTimeout(int timeout) {
177 assertFalse(connected);
178 this.connectTimeout = timeout;
184 public String getContentEncoding() {
186 return this.contentEncoding;
189 public void setContentEncoding(String encoding) {
190 this.contentEncoding = encoding;
196 public int getContentLength() {
200 return content.length;
203 public void setContent(byte[] content) {
204 this.content = content;
207 public void setContent(String content) {
209 this.content = content.getBytes("UTF-8");
210 } catch (UnsupportedEncodingException e) {
217 public String getContentType() {
219 return this.contentType;
222 public void setContentType(String type) {
223 this.contentType = type;
229 public boolean getDoInput() {
235 public void setDoInput(boolean doinput) {
236 assertFalse(connected);
237 this.doInput = doinput;
242 public boolean getDoOutput() {
243 return this.doOutput;
248 public void setDoOutput(boolean dooutput) {
249 assertFalse(connected);
250 this.doOutput = dooutput;
255 public InputStream getInputStream() throws IOException {
257 assertNull(inputStream);
258 assertNotNull(content);
261 inputStream = new ByteArrayInputStream(content);
267 public OutputStream getOutputStream() throws IOException {
268 assertTrue(doOutput);
269 assertNull(outputStream);
270 outputStream = new ByteArrayOutputStream();
274 public byte[] getOutputStreamData() {
275 return outputStream.toByteArray();
278 public String getOutputStreamString() {
280 return outputStream.toString("UTF-8");
281 } catch (UnsupportedEncodingException e) {
290 public void setUseCaches(boolean usecaches) {
291 assertFalse(connected);
292 this.useCaches = usecaches;
298 public boolean getUseCaches() {
299 return this.useCaches;
309 private void assertNull(Object o) {
311 org.junit.Assert.assertNull(o);
312 } catch (AssertionError e) {
318 private void assertNotNull(Object o) {
320 org.junit.Assert.assertNotNull(o);
321 } catch (AssertionError e) {
327 private void assertTrue(boolean o) {
329 org.junit.Assert.assertTrue(o);
330 } catch (AssertionError e) {
336 private void assertFalse(boolean o) {
338 org.junit.Assert.assertFalse(o);
339 } catch (AssertionError e) {
345 private void fail(String msg) {
347 org.junit.Assert.fail(msg);
368 public InputStream getErrorStream() {
369 throw new UnsupportedOperationException();
375 public String getHeaderField(int n) {
376 throw new UnsupportedOperationException();
382 public long getHeaderFieldDate(String name, long Default) {
383 throw new UnsupportedOperationException();
389 public String getHeaderFieldKey(int n) {
390 throw new UnsupportedOperationException();
395 public Permission getPermission() throws IOException {
396 throw new UnsupportedOperationException();
401 public String getResponseMessage() throws IOException {
402 throw new UnsupportedOperationException();
408 public void setChunkedStreamingMode(int chunklen) {
409 throw new UnsupportedOperationException();
415 public void setFixedLengthStreamingMode(int contentLength) {
416 throw new UnsupportedOperationException();
424 public boolean getAllowUserInteraction() {
425 throw new UnsupportedOperationException();
431 public Object getContent() throws IOException {
432 throw new UnsupportedOperationException();
437 @SuppressWarnings("unchecked")
439 public Object getContent(Class[] classes) throws IOException {
440 throw new UnsupportedOperationException();
445 public long getDate() {
446 throw new UnsupportedOperationException();
452 public boolean getDefaultUseCaches() {
453 throw new UnsupportedOperationException();
458 public long getExpiration() {
459 throw new UnsupportedOperationException();
465 public String getHeaderField(String name) {
466 throw new UnsupportedOperationException();
472 public int getHeaderFieldInt(String name, int Default) {
473 throw new UnsupportedOperationException();
479 public Map<String, List<String>> getHeaderFields() {
480 throw new UnsupportedOperationException();
486 public long getIfModifiedSince() {
487 throw new UnsupportedOperationException();
492 public long getLastModified() {
493 throw new UnsupportedOperationException();
497 public int getReadTimeout() {
498 throw new UnsupportedOperationException();
504 public Map<String, List<String>> getRequestProperties() {
505 throw new UnsupportedOperationException();
510 public URL getURL() {
511 throw new UnsupportedOperationException();
517 public void setAllowUserInteraction(boolean allowuserinteraction) {
518 throw new UnsupportedOperationException();
522 public void setDefaultUseCaches(boolean defaultusecaches) {
523 throw new UnsupportedOperationException();
528 public void setIfModifiedSince(long ifmodifiedsince) {
529 throw new UnsupportedOperationException();
534 public void setReadTimeout(int timeout) {
535 throw new UnsupportedOperationException();
543 public String toString() {
544 throw new UnsupportedOperationException();