1 package org.apache.activemq.util; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 import java.net.HttpURLConnection ; 8 import java.net.URL ; 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 s) { 17 super(s); 18 } 19 20 public void test() throws Exception { 21 byte[] fileContents = new byte[] {'a', 'b', 'c'}; 22 URL url = new URL ("http://localhost:8080/fileserver/repository/file.txt"); 23 24 HttpURLConnection connection = (HttpURLConnection )url.openConnection(); 26 connection.setRequestMethod("PUT"); 27 connection.setDoOutput(true); 28 connection.setChunkedStreamingMode(fileContents.length); 29 OutputStream os = connection.getOutputStream(); 30 IO.copy(new ByteArrayInputStream(fileContents), os); 31 os.close(); 32 assertTrue(isSuccessfulCode(connection.getResponseCode())); 33 connection.disconnect(); 34 35 connection = (HttpURLConnection )url.openConnection(); 37 InputStream 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 connection = (HttpURLConnection )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; } 56 } 57 | Popular Tags |