create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / communication / HttpURLConnectionMock.java
1 package net.sf.openrocket.communication;
2
3
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;
13 import java.net.URL;
14 import java.security.Permission;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Locale;
18 import java.util.Map;
19
20 import net.sf.openrocket.util.BugException;
21
22 public class HttpURLConnectionMock extends HttpURLConnection {
23         
24         private static final URL MOCK_URL;
25         static {
26                 try {
27                         MOCK_URL = new URL("http://localhost/");
28                 } catch (MalformedURLException e) {
29                         throw new BugException(e);
30                 }
31         }
32         
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 = "";
39         
40         private volatile boolean doInput = false;
41         private volatile boolean doOutput = false;
42         
43         private volatile byte[] content = null;
44         private volatile String contentType = null;
45         private volatile boolean useCaches = false;
46         
47         
48         private volatile InputStream inputStream = null;
49         private volatile ByteArrayOutputStream outputStream = null;
50         
51         private volatile String trueUrl = null;
52         
53         
54         private volatile boolean connected = false;
55         private volatile int connectionDelay = 0;
56         
57         private volatile boolean failed = false;
58         
59         
60         
61         
62         public HttpURLConnectionMock() {
63                 super(MOCK_URL);
64         }
65         
66         public HttpURLConnectionMock(URL u) {
67                 super(u);
68         }
69         
70         
71         
72         public String getTrueUrl() {
73                 return trueUrl;
74         }
75         
76         public void setTrueUrl(String url) {
77                 assertNull(this.trueUrl);
78                 this.trueUrl = url;
79         }
80         
81         
82         public boolean hasFailed() {
83                 return failed;
84         }
85         
86         
87         public void setConnectionDelay(int delay) {
88                 this.connectionDelay = delay;
89         }
90         
91         
92         
93         @Override
94         public void connect() {
95                 if (!connected) {
96                         try {
97                                 Thread.sleep(connectionDelay);
98                         } catch (InterruptedException e) {
99                         }
100                         connected = true;
101                 }
102         }
103         
104         @Override
105         public void disconnect() {
106                 
107         }
108         
109         @Override
110         public boolean usingProxy() {
111                 return false;
112         }
113         
114         
115         
116         
117         @Override
118         public boolean getInstanceFollowRedirects() {
119                 return this.instanceFollowRedirects;
120         }
121         
122         @Override
123         public void setInstanceFollowRedirects(boolean followRedirects) {
124                 assertFalse(connected);
125                 this.instanceFollowRedirects = followRedirects;
126         }
127         
128         @Override
129         public String getRequestMethod() {
130                 return this.requestMethod;
131         }
132         
133         @Override
134         public void setRequestMethod(String method) throws ProtocolException {
135                 assertFalse(connected);
136                 this.requestMethod = method;
137         }
138         
139         @Override
140         public int getResponseCode() throws IOException {
141                 connect();
142                 return this.responseCode;
143         }
144         
145         public void setResponseCode(int code) {
146                 this.responseCode = code;
147         }
148         
149         
150         @Override
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);
155         }
156         
157         
158         @Override
159         public void setRequestProperty(String key, String value) {
160                 assertFalse(connected);
161                 this.requestProperties.put(key.toLowerCase(Locale.ENGLISH), value);
162         }
163         
164         
165         @Override
166         public String getRequestProperty(String key) {
167                 return this.requestProperties.get(key.toLowerCase(Locale.ENGLISH));
168         }
169         
170         
171         @Override
172         public int getConnectTimeout() {
173                 return this.connectTimeout;
174         }
175         
176         @Override
177         public void setConnectTimeout(int timeout) {
178                 assertFalse(connected);
179                 this.connectTimeout = timeout;
180         }
181         
182         
183         
184         @Override
185         public String getContentEncoding() {
186                 connect();
187                 return this.contentEncoding;
188         }
189         
190         public void setContentEncoding(String encoding) {
191                 this.contentEncoding = encoding;
192         }
193         
194         
195         
196         @Override
197         public int getContentLength() {
198                 connect();
199                 if (content == null)
200                         return 0;
201                 return content.length;
202         }
203         
204         public void setContent(byte[] content) {
205                 this.content = content;
206         }
207         
208         public void setContent(String content) {
209                 try {
210                         this.content = content.getBytes("UTF-8");
211                 } catch (UnsupportedEncodingException e) {
212                         fail("UTF-8");
213                 }
214         }
215         
216         
217         @Override
218         public String getContentType() {
219                 connect();
220                 return this.contentType;
221         }
222         
223         public void setContentType(String type) {
224                 this.contentType = type;
225         }
226         
227         
228         
229         @Override
230         public boolean getDoInput() {
231                 return this.doInput;
232         }
233         
234         
235         @Override
236         public void setDoInput(boolean doinput) {
237                 assertFalse(connected);
238                 this.doInput = doinput;
239         }
240         
241         
242         @Override
243         public boolean getDoOutput() {
244                 return this.doOutput;
245         }
246         
247         
248         @Override
249         public void setDoOutput(boolean dooutput) {
250                 assertFalse(connected);
251                 this.doOutput = dooutput;
252         }
253         
254         
255         @Override
256         public InputStream getInputStream() throws IOException {
257                 assertTrue(doInput);
258                 assertNull(inputStream);
259                 assertNotNull(content);
260                 
261                 connect();
262                 inputStream = new ByteArrayInputStream(content);
263                 return inputStream;
264         }
265         
266         
267         @Override
268         public OutputStream getOutputStream() throws IOException {
269                 assertTrue(doOutput);
270                 assertNull(outputStream);
271                 outputStream = new ByteArrayOutputStream();
272                 return outputStream;
273         }
274         
275         public byte[] getOutputStreamData() {
276                 return outputStream.toByteArray();
277         }
278         
279         public String getOutputStreamString() {
280                 try {
281                         return outputStream.toString("UTF-8");
282                 } catch (UnsupportedEncodingException e) {
283                         fail("UTF-8");
284                         return null;
285                 }
286         }
287         
288         
289         
290         @Override
291         public void setUseCaches(boolean usecaches) {
292                 assertFalse(connected);
293                 this.useCaches = usecaches;
294         }
295         
296         
297         
298         @Override
299         public boolean getUseCaches() {
300                 return this.useCaches;
301         }
302         
303         
304         
305         
306         
307         private void assertNull(Object o) {
308                 try {
309                         org.junit.Assert.assertNull(o);
310                 } catch (AssertionError e) {
311                         failed = true;
312                         throw e;
313                 }
314         }
315         
316         private void assertNotNull(Object o) {
317                 try {
318                         org.junit.Assert.assertNotNull(o);
319                 } catch (AssertionError e) {
320                         failed = true;
321                         throw e;
322                 }
323         }
324         
325         private void assertTrue(boolean o) {
326                 try {
327                         org.junit.Assert.assertTrue(o);
328                 } catch (AssertionError e) {
329                         failed = true;
330                         throw e;
331                 }
332         }
333         
334         private void assertFalse(boolean o) {
335                 try {
336                         org.junit.Assert.assertFalse(o);
337                 } catch (AssertionError e) {
338                         failed = true;
339                         throw e;
340                 }
341         }
342         
343         private void fail(String msg) {
344                 failed = true;
345                 org.junit.Assert.fail(msg);
346         }
347         
348         
349         
350         
351         
352         @Override
353         public InputStream getErrorStream() {
354                 throw new UnsupportedOperationException();
355         }
356         
357         
358         
359         @Override
360         public String getHeaderField(int n) {
361                 throw new UnsupportedOperationException();
362         }
363         
364         
365         
366         @Override
367         public long getHeaderFieldDate(String name, long Default) {
368                 throw new UnsupportedOperationException();
369         }
370         
371         
372         
373         @Override
374         public String getHeaderFieldKey(int n) {
375                 throw new UnsupportedOperationException();
376         }
377         
378         
379         @Override
380         public Permission getPermission() throws IOException {
381                 throw new UnsupportedOperationException();
382         }
383         
384         
385         @Override
386         public String getResponseMessage() throws IOException {
387                 throw new UnsupportedOperationException();
388         }
389         
390         
391         
392         @Override
393         public void setChunkedStreamingMode(int chunklen) {
394                 throw new UnsupportedOperationException();
395         }
396         
397         
398         
399         @Override
400         public void setFixedLengthStreamingMode(int contentLength) {
401                 throw new UnsupportedOperationException();
402         }
403         
404         
405         
406         
407         
408         @Override
409         public boolean getAllowUserInteraction() {
410                 throw new UnsupportedOperationException();
411         }
412         
413         
414         
415         @Override
416         public Object getContent() throws IOException {
417                 throw new UnsupportedOperationException();
418         }
419         
420         
421         
422         @SuppressWarnings("unchecked")
423         @Override
424         public Object getContent(Class[] classes) throws IOException {
425                 throw new UnsupportedOperationException();
426         }
427         
428         
429         @Override
430         public long getDate() {
431                 throw new UnsupportedOperationException();
432         }
433         
434         
435         
436         @Override
437         public boolean getDefaultUseCaches() {
438                 throw new UnsupportedOperationException();
439         }
440         
441         
442         @Override
443         public long getExpiration() {
444                 throw new UnsupportedOperationException();
445         }
446         
447         
448         
449         @Override
450         public String getHeaderField(String name) {
451                 throw new UnsupportedOperationException();
452         }
453         
454         
455         
456         @Override
457         public int getHeaderFieldInt(String name, int Default) {
458                 throw new UnsupportedOperationException();
459         }
460         
461         
462         
463         @Override
464         public Map<String, List<String>> getHeaderFields() {
465                 throw new UnsupportedOperationException();
466         }
467         
468         
469         
470         @Override
471         public long getIfModifiedSince() {
472                 throw new UnsupportedOperationException();
473         }
474         
475         
476         @Override
477         public long getLastModified() {
478                 throw new UnsupportedOperationException();
479         }
480         
481         @Override
482         public int getReadTimeout() {
483                 throw new UnsupportedOperationException();
484         }
485         
486         
487         
488         @Override
489         public Map<String, List<String>> getRequestProperties() {
490                 throw new UnsupportedOperationException();
491         }
492         
493         
494         @Override
495         public URL getURL() {
496                 throw new UnsupportedOperationException();
497         }
498         
499         
500         
501         @Override
502         public void setAllowUserInteraction(boolean allowuserinteraction) {
503                 throw new UnsupportedOperationException();
504         }
505         
506         @Override
507         public void setDefaultUseCaches(boolean defaultusecaches) {
508                 throw new UnsupportedOperationException();
509         }
510         
511         
512         @Override
513         public void setIfModifiedSince(long ifmodifiedsince) {
514                 throw new UnsupportedOperationException();
515         }
516         
517         
518         @Override
519         public void setReadTimeout(int timeout) {
520                 throw new UnsupportedOperationException();
521         }
522         
523         
524         
525         
526         
527         @Override
528         public String toString() {
529                 throw new UnsupportedOperationException();
530         }
531         
532         
533         
534         
535 }