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 public class HttpURLConnectionMock extends HttpURLConnection {
21 private static final URL MOCK_URL;
24 MOCK_URL = new URL("http://localhost/");
25 } catch (MalformedURLException e) {
26 throw new RuntimeException(e);
30 private volatile boolean instanceFollowRedirects = false;
31 private volatile String requestMethod = "";
32 private volatile int responseCode;
33 private Map<String, String> requestProperties = new HashMap<String, String>();
34 private volatile int connectTimeout = -1;
35 private volatile String contentEncoding = "";
37 private volatile boolean doInput = false;
38 private volatile boolean doOutput = false;
40 private volatile byte[] content = null;
41 private volatile String contentType = null;
42 private volatile boolean useCaches = false;
45 private volatile InputStream inputStream = null;
46 private volatile ByteArrayOutputStream outputStream = null;
48 private volatile String trueUrl = null;
51 private volatile boolean connected = false;
52 private volatile int connectionDelay = 0;
54 private volatile boolean failed = false;
59 public HttpURLConnectionMock() {
63 public HttpURLConnectionMock(URL u) {
69 public String getTrueUrl() {
73 public void setTrueUrl(String url) {
74 assertNull(this.trueUrl);
79 public boolean hasFailed() {
84 public void setConnectionDelay(int delay) {
85 this.connectionDelay = delay;
91 public void connect() {
94 Thread.sleep(connectionDelay);
95 } catch (InterruptedException e) {
102 public void disconnect() {
107 public boolean usingProxy() {
115 public boolean getInstanceFollowRedirects() {
116 return this.instanceFollowRedirects;
120 public void setInstanceFollowRedirects(boolean followRedirects) {
121 assertFalse(connected);
122 this.instanceFollowRedirects = followRedirects;
126 public String getRequestMethod() {
127 return this.requestMethod;
131 public void setRequestMethod(String method) throws ProtocolException {
132 assertFalse(connected);
133 this.requestMethod = method;
137 public int getResponseCode() throws IOException {
139 return this.responseCode;
142 public void setResponseCode(int code) {
143 this.responseCode = code;
148 public void addRequestProperty(String key, String value) {
149 assertFalse(connected);
150 assertFalse(this.requestProperties.containsKey(key.toLowerCase()));
151 this.requestProperties.put(key.toLowerCase(), value);
156 public void setRequestProperty(String key, String value) {
157 assertFalse(connected);
158 this.requestProperties.put(key.toLowerCase(), value);
163 public String getRequestProperty(String key) {
164 return this.requestProperties.get(key.toLowerCase());
169 public int getConnectTimeout() {
170 return this.connectTimeout;
174 public void setConnectTimeout(int timeout) {
175 assertFalse(connected);
176 this.connectTimeout = timeout;
182 public String getContentEncoding() {
184 return this.contentEncoding;
187 public void setContentEncoding(String encoding) {
188 this.contentEncoding = encoding;
194 public int getContentLength() {
198 return content.length;
201 public void setContent(byte[] content) {
202 this.content = content;
205 public void setContent(String content) {
207 this.content = content.getBytes("UTF-8");
208 } catch (UnsupportedEncodingException e) {
215 public String getContentType() {
217 return this.contentType;
220 public void setContentType(String type) {
221 this.contentType = type;
227 public boolean getDoInput() {
233 public void setDoInput(boolean doinput) {
234 assertFalse(connected);
235 this.doInput = doinput;
240 public boolean getDoOutput() {
241 return this.doOutput;
246 public void setDoOutput(boolean dooutput) {
247 assertFalse(connected);
248 this.doOutput = dooutput;
253 public InputStream getInputStream() throws IOException {
255 assertNull(inputStream);
256 assertNotNull(content);
259 inputStream = new ByteArrayInputStream(content);
265 public OutputStream getOutputStream() throws IOException {
266 assertTrue(doOutput);
267 assertNull(outputStream);
268 outputStream = new ByteArrayOutputStream();
272 public byte[] getOutputStreamData() {
273 return outputStream.toByteArray();
276 public String getOutputStreamString() {
278 return outputStream.toString("UTF-8");
279 } catch (UnsupportedEncodingException e) {
288 public void setUseCaches(boolean usecaches) {
289 assertFalse(connected);
290 this.useCaches = usecaches;
296 public boolean getUseCaches() {
297 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);
366 public InputStream getErrorStream() {
367 throw new UnsupportedOperationException();
373 public String getHeaderField(int n) {
374 throw new UnsupportedOperationException();
380 public long getHeaderFieldDate(String name, long Default) {
381 throw new UnsupportedOperationException();
387 public String getHeaderFieldKey(int n) {
388 throw new UnsupportedOperationException();
393 public Permission getPermission() throws IOException {
394 throw new UnsupportedOperationException();
399 public String getResponseMessage() throws IOException {
400 throw new UnsupportedOperationException();
406 public void setChunkedStreamingMode(int chunklen) {
407 throw new UnsupportedOperationException();
413 public void setFixedLengthStreamingMode(int contentLength) {
414 throw new UnsupportedOperationException();
422 public boolean getAllowUserInteraction() {
423 throw new UnsupportedOperationException();
429 public Object getContent() throws IOException {
430 throw new UnsupportedOperationException();
435 @SuppressWarnings("unchecked")
437 public Object getContent(Class[] classes) throws IOException {
438 throw new UnsupportedOperationException();
443 public long getDate() {
444 throw new UnsupportedOperationException();
450 public boolean getDefaultUseCaches() {
451 throw new UnsupportedOperationException();
456 public long getExpiration() {
457 throw new UnsupportedOperationException();
463 public String getHeaderField(String name) {
464 throw new UnsupportedOperationException();
470 public int getHeaderFieldInt(String name, int Default) {
471 throw new UnsupportedOperationException();
477 public Map<String, List<String>> getHeaderFields() {
478 throw new UnsupportedOperationException();
484 public long getIfModifiedSince() {
485 throw new UnsupportedOperationException();
490 public long getLastModified() {
491 throw new UnsupportedOperationException();
495 public int getReadTimeout() {
496 throw new UnsupportedOperationException();
502 public Map<String, List<String>> getRequestProperties() {
503 throw new UnsupportedOperationException();
508 public URL getURL() {
509 throw new UnsupportedOperationException();
515 public void setAllowUserInteraction(boolean allowuserinteraction) {
516 throw new UnsupportedOperationException();
520 public void setDefaultUseCaches(boolean defaultusecaches) {
521 throw new UnsupportedOperationException();
526 public void setIfModifiedSince(long ifmodifiedsince) {
527 throw new UnsupportedOperationException();
532 public void setReadTimeout(int timeout) {
533 throw new UnsupportedOperationException();
541 public String toString() {
542 throw new UnsupportedOperationException();