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;
17 import java.util.Locale;
20 import net.sf.openrocket.util.BugException;
22 public class HttpURLConnectionMock extends HttpURLConnection {
24 private static final URL MOCK_URL;
27 MOCK_URL = new URL("http://localhost/");
28 } catch (MalformedURLException e) {
29 throw new BugException(e);
33 private volatile boolean instanceFollowRedirects = false;
34 private volatile String requestMethod = "";
35 private volatile int responseCode;
36 private Map<String, String> requestProperties = new HashMap<String, String>();
37 private volatile int connectTimeout = -1;
38 private volatile String contentEncoding = "";
40 private volatile boolean doInput = false;
41 private volatile boolean doOutput = false;
43 private volatile byte[] content = null;
44 private volatile String contentType = null;
45 private volatile boolean useCaches = false;
48 private volatile InputStream inputStream = null;
49 private volatile ByteArrayOutputStream outputStream = null;
51 private volatile String trueUrl = null;
54 private volatile boolean connected = false;
55 private volatile int connectionDelay = 0;
57 private volatile boolean failed = false;
62 public HttpURLConnectionMock() {
66 public HttpURLConnectionMock(URL u) {
72 public String getTrueUrl() {
76 public void setTrueUrl(String url) {
77 assertNull(this.trueUrl);
82 public boolean hasFailed() {
87 public void setConnectionDelay(int delay) {
88 this.connectionDelay = delay;
94 public void connect() {
97 Thread.sleep(connectionDelay);
98 } catch (InterruptedException e) {
105 public void disconnect() {
110 public boolean usingProxy() {
118 public boolean getInstanceFollowRedirects() {
119 return this.instanceFollowRedirects;
123 public void setInstanceFollowRedirects(boolean followRedirects) {
124 assertFalse(connected);
125 this.instanceFollowRedirects = followRedirects;
129 public String getRequestMethod() {
130 return this.requestMethod;
134 public void setRequestMethod(String method) throws ProtocolException {
135 assertFalse(connected);
136 this.requestMethod = method;
140 public int getResponseCode() throws IOException {
142 return this.responseCode;
145 public void setResponseCode(int code) {
146 this.responseCode = code;
151 public void addRequestProperty(String key, String value) {
152 assertFalse(connected);
153 assertFalse(this.requestProperties.containsKey(key.toLowerCase(Locale.ENGLISH)));
154 this.requestProperties.put(key.toLowerCase(Locale.ENGLISH), value);
159 public void setRequestProperty(String key, String value) {
160 assertFalse(connected);
161 this.requestProperties.put(key.toLowerCase(Locale.ENGLISH), value);
166 public String getRequestProperty(String key) {
167 return this.requestProperties.get(key.toLowerCase(Locale.ENGLISH));
172 public int getConnectTimeout() {
173 return this.connectTimeout;
177 public void setConnectTimeout(int timeout) {
178 assertFalse(connected);
179 this.connectTimeout = timeout;
185 public String getContentEncoding() {
187 return this.contentEncoding;
190 public void setContentEncoding(String encoding) {
191 this.contentEncoding = encoding;
197 public int getContentLength() {
201 return content.length;
204 public void setContent(byte[] content) {
205 this.content = content;
208 public void setContent(String content) {
210 this.content = content.getBytes("UTF-8");
211 } catch (UnsupportedEncodingException e) {
218 public String getContentType() {
220 return this.contentType;
223 public void setContentType(String type) {
224 this.contentType = type;
230 public boolean getDoInput() {
236 public void setDoInput(boolean doinput) {
237 assertFalse(connected);
238 this.doInput = doinput;
243 public boolean getDoOutput() {
244 return this.doOutput;
249 public void setDoOutput(boolean dooutput) {
250 assertFalse(connected);
251 this.doOutput = dooutput;
256 public InputStream getInputStream() throws IOException {
258 assertNull(inputStream);
259 assertNotNull(content);
262 inputStream = new ByteArrayInputStream(content);
268 public OutputStream getOutputStream() throws IOException {
269 assertTrue(doOutput);
270 assertNull(outputStream);
271 outputStream = new ByteArrayOutputStream();
275 public byte[] getOutputStreamData() {
276 return outputStream.toByteArray();
279 public String getOutputStreamString() {
281 return outputStream.toString("UTF-8");
282 } catch (UnsupportedEncodingException e) {
291 public void setUseCaches(boolean usecaches) {
292 assertFalse(connected);
293 this.useCaches = usecaches;
299 public boolean getUseCaches() {
300 return this.useCaches;
307 private void assertNull(Object o) {
309 org.junit.Assert.assertNull(o);
310 } catch (AssertionError e) {
316 private void assertNotNull(Object o) {
318 org.junit.Assert.assertNotNull(o);
319 } catch (AssertionError e) {
325 private void assertTrue(boolean o) {
327 org.junit.Assert.assertTrue(o);
328 } catch (AssertionError e) {
334 private void assertFalse(boolean o) {
336 org.junit.Assert.assertFalse(o);
337 } catch (AssertionError e) {
343 private void fail(String msg) {
345 org.junit.Assert.fail(msg);
353 public InputStream getErrorStream() {
354 throw new UnsupportedOperationException();
360 public String getHeaderField(int n) {
361 throw new UnsupportedOperationException();
367 public long getHeaderFieldDate(String name, long Default) {
368 throw new UnsupportedOperationException();
374 public String getHeaderFieldKey(int n) {
375 throw new UnsupportedOperationException();
380 public Permission getPermission() throws IOException {
381 throw new UnsupportedOperationException();
386 public String getResponseMessage() throws IOException {
387 throw new UnsupportedOperationException();
393 public void setChunkedStreamingMode(int chunklen) {
394 throw new UnsupportedOperationException();
400 public void setFixedLengthStreamingMode(int contentLength) {
401 throw new UnsupportedOperationException();
409 public boolean getAllowUserInteraction() {
410 throw new UnsupportedOperationException();
416 public Object getContent() throws IOException {
417 throw new UnsupportedOperationException();
422 @SuppressWarnings("unchecked")
424 public Object getContent(Class[] classes) throws IOException {
425 throw new UnsupportedOperationException();
430 public long getDate() {
431 throw new UnsupportedOperationException();
437 public boolean getDefaultUseCaches() {
438 throw new UnsupportedOperationException();
443 public long getExpiration() {
444 throw new UnsupportedOperationException();
450 public String getHeaderField(String name) {
451 throw new UnsupportedOperationException();
457 public int getHeaderFieldInt(String name, int Default) {
458 throw new UnsupportedOperationException();
464 public Map<String, List<String>> getHeaderFields() {
465 throw new UnsupportedOperationException();
471 public long getIfModifiedSince() {
472 throw new UnsupportedOperationException();
477 public long getLastModified() {
478 throw new UnsupportedOperationException();
482 public int getReadTimeout() {
483 throw new UnsupportedOperationException();
489 public Map<String, List<String>> getRequestProperties() {
490 throw new UnsupportedOperationException();
495 public URL getURL() {
496 throw new UnsupportedOperationException();
502 public void setAllowUserInteraction(boolean allowuserinteraction) {
503 throw new UnsupportedOperationException();
507 public void setDefaultUseCaches(boolean defaultusecaches) {
508 throw new UnsupportedOperationException();
513 public void setIfModifiedSince(long ifmodifiedsince) {
514 throw new UnsupportedOperationException();
519 public void setReadTimeout(int timeout) {
520 throw new UnsupportedOperationException();
528 public String toString() {
529 throw new UnsupportedOperationException();