1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 /***
47 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
48 * @version $Revision: 155418 $ $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
49 */
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
56
57 public TestPartsNoHost(String testName) {
58 super(testName);
59 }
60
61
62
63 public static Test suite() {
64 return new TestSuite(TestPartsNoHost.class);
65 }
66
67
68
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 /*** Writes PART_DATA out to a temporary file and returns the file it
106 * was written to.
107 * @return the File object representing the file the data was
108 * written to.
109 */
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 }