KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > RestFilterTest


1 package org.apache.activemq.util;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.HttpURLConnection JavaDoc;
8 import java.net.URL JavaDoc;
9
10 import junit.framework.TestCase;
11
12 import org.mortbay.util.IO;
13
14 public class RestFilterTest extends TestCase {
15     
16     public RestFilterTest(String JavaDoc s) {
17         super(s);
18     }
19     
20     public void test() throws Exception JavaDoc {
21         byte[] fileContents = new byte[] {'a', 'b', 'c'};
22         URL JavaDoc url = new URL JavaDoc("http://localhost:8080/fileserver/repository/file.txt");
23
24         // 1. upload
25
HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
26         connection.setRequestMethod("PUT");
27         connection.setDoOutput(true);
28         connection.setChunkedStreamingMode(fileContents.length);
29         OutputStream JavaDoc os = connection.getOutputStream();
30         IO.copy(new ByteArrayInputStream(fileContents), os);
31         os.close();
32         assertTrue(isSuccessfulCode(connection.getResponseCode()));
33         connection.disconnect();
34         
35         // 2. download
36
connection = (HttpURLConnection JavaDoc)url.openConnection();
37         InputStream JavaDoc is = connection.getInputStream();
38         ByteArrayOutputStream baos = new ByteArrayOutputStream();
39         IO.copy(is, baos);
40         is.close();
41         assertTrue(isSuccessfulCode(connection.getResponseCode()));
42         connection.disconnect();
43         assertEquals(fileContents.length, baos.size());
44         
45         // 3. remove
46
connection = (HttpURLConnection JavaDoc)url.openConnection();
47         connection.setRequestMethod("DELETE");
48         is = connection.getInputStream();
49         is.close();
50         assertTrue(isSuccessfulCode(connection.getResponseCode()));
51     }
52     
53     private boolean isSuccessfulCode(int responseCode) {
54         return responseCode >= 200 && responseCode < 300; // 2xx => successful
55
}
56 }
57
Popular Tags