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