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