1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.File ; 35 import java.io.FileWriter ; 36 import java.io.IOException ; 37 import java.io.PrintWriter ; 38 39 import junit.framework.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 import org.apache.commons.httpclient.methods.multipart.FilePart; 44 import org.apache.commons.httpclient.methods.multipart.StringPart; 45 46 50 public class TestPartsNoHost extends TestCase { 51 52 static final String PART_DATA = "This is the part data."; 53 static final String NAME = "name"; 54 55 57 public TestPartsNoHost(String testName) { 58 super(testName); 59 } 60 61 63 public static Test suite() { 64 return new TestSuite(TestPartsNoHost.class); 65 } 66 67 69 public void testFilePartResendsFileData() throws Exception { 70 File file = createTempTestFile(); 71 FilePart part = new FilePart(NAME, file); 72 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 73 part.send(stream); 74 String resp1 = stream.toString(); 75 stream = new ByteArrayOutputStream (); 76 part.send(stream); 77 String resp2 = stream.toString(); 78 file.delete(); 79 assertEquals(resp1, resp2); 80 } 81 82 public void testStringPartResendsData() throws Exception { 83 StringPart part = new StringPart(NAME, PART_DATA); 84 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 85 part.send(stream); 86 String resp1 = stream.toString(); 87 stream = new ByteArrayOutputStream (); 88 part.send(stream); 89 String resp2 = stream.toString(); 90 assertEquals(resp1, resp2); 91 } 92 93 public void testFilePartNullFileResendsData() throws Exception { 94 FilePart part = new FilePart(NAME, "emptyfile.ext", null); 95 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 96 part.send(stream); 97 String resp1 = stream.toString(); 98 stream = new ByteArrayOutputStream (); 99 part.send(stream); 100 String resp2 = stream.toString(); 101 assertEquals(resp1, resp2); 102 } 103 104 105 110 private File createTempTestFile() throws IOException { 111 File file = File.createTempFile("FilePartTest", ".txt"); 112 PrintWriter out = new PrintWriter (new FileWriter (file)); 113 out.println(PART_DATA); 114 out.flush(); 115 out.close(); 116 return file; 117 } 118 } 119 | Popular Tags |