1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java,v 1.7 2004/02/22 18:08:49 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
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      // ------------------------------------------------------------ Constructor
56  
57      public TestPartsNoHost(String testName) {
58          super(testName);
59      }
60  
61      // ------------------------------------------------------- TestCase Methods
62  
63      public static Test suite() {
64          return new TestSuite(TestPartsNoHost.class);
65      }
66  
67      // ----------------------------------------------------------------- Tests
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 }