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  
32  package org.apache.commons.httpclient;
33  
34  import java.io.ByteArrayOutputStream;
35  import java.io.File;
36  import java.io.FileWriter;
37  import java.io.IOException;
38  import java.io.PrintWriter;
39  
40  import junit.framework.Test;
41  import junit.framework.TestCase;
42  import junit.framework.TestSuite;
43  
44  import org.apache.commons.httpclient.methods.multipart.FilePart;
45  import org.apache.commons.httpclient.methods.multipart.StringPart;
46  
47  /**
48   * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
49   * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
50   */
51  public class TestPartsNoHost extends TestCase {
52  
53      static final String PART_DATA = "This is the part data.";
54      static final String NAME = "name";
55  
56      
57  
58      public TestPartsNoHost(String testName) {
59          super(testName);
60      }
61  
62      
63  
64      public static Test suite() {
65          return new TestSuite(TestPartsNoHost.class);
66      }
67  
68      
69  
70      public void testFilePartResendsFileData() throws Exception {
71          File file = createTempTestFile();
72          FilePart part = new FilePart(NAME, file);
73          ByteArrayOutputStream stream = new ByteArrayOutputStream();
74          part.send(stream);
75          String resp1 = stream.toString();
76          stream = new ByteArrayOutputStream();
77          part.send(stream);
78          String resp2 = stream.toString();
79          file.delete();
80          assertEquals(resp1, resp2);
81      }
82  
83      public void testStringPartResendsData() throws Exception {
84          StringPart part = new StringPart(NAME, PART_DATA);
85          ByteArrayOutputStream stream = new ByteArrayOutputStream();
86          part.send(stream);
87          String resp1 = stream.toString();
88          stream = new ByteArrayOutputStream();
89          part.send(stream);
90          String resp2 = stream.toString();
91          assertEquals(resp1, resp2);
92      }
93  
94      public void testFilePartNullFileResendsData() throws Exception {
95          FilePart part = new FilePart(NAME, "emptyfile.ext", null);
96          ByteArrayOutputStream stream = new ByteArrayOutputStream();
97          part.send(stream);
98          String resp1 = stream.toString();
99          stream = new ByteArrayOutputStream();
100         part.send(stream);
101         String resp2 = stream.toString();
102         assertEquals(resp1, resp2);
103     }
104 
105 
106     /** Writes PART_DATA out to a temporary file and returns the file it
107      * was written to.
108      * @return the File object representing the file the data was
109      * written to.
110      */
111     private File createTempTestFile() throws IOException {
112         File file = File.createTempFile("FilePartTest", ".txt");
113         PrintWriter out = new PrintWriter(new FileWriter(file));
114         out.println(PART_DATA);
115         out.flush();
116         out.close();
117         return file;
118     }
119 }