KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestPartsNoHost


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java,v 1.6.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.6.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
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 JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
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: 1.6.2.1 $ $Date: 2004/02/22 18:21:16 $
49  */

50 public class TestPartsNoHost extends TestCase {
51
52     static final String JavaDoc PART_DATA = "This is the part data.";
53     static final String JavaDoc NAME = "name";
54
55     // ------------------------------------------------------------ Constructor
56

57     public TestPartsNoHost(String JavaDoc 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 JavaDoc {
70         File JavaDoc file = createTempTestFile();
71         FilePart part = new FilePart(NAME, file);
72         ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
73         part.send(stream);
74         String JavaDoc resp1 = stream.toString();
75         stream = new ByteArrayOutputStream JavaDoc();
76         part.send(stream);
77         String JavaDoc resp2 = stream.toString();
78         file.delete();
79         assertEquals(resp1, resp2);
80     }
81
82     public void testStringPartResendsData() throws Exception JavaDoc {
83         StringPart part = new StringPart(NAME, PART_DATA);
84         ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
85         part.send(stream);
86         String JavaDoc resp1 = stream.toString();
87         stream = new ByteArrayOutputStream JavaDoc();
88         part.send(stream);
89         String JavaDoc resp2 = stream.toString();
90         assertEquals(resp1, resp2);
91     }
92
93     public void testFilePartNullFileResendsData() throws Exception JavaDoc {
94         FilePart part = new FilePart(NAME, "emptyfile.ext", null);
95         ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
96         part.send(stream);
97         String JavaDoc resp1 = stream.toString();
98         stream = new ByteArrayOutputStream JavaDoc();
99         part.send(stream);
100         String JavaDoc 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 JavaDoc createTempTestFile() throws IOException JavaDoc {
111         File JavaDoc file = File.createTempFile("FilePartTest", ".txt");
112         PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
113         out.println(PART_DATA);
114         out.flush();
115         out.close();
116         return file;
117     }
118 }
119
Popular Tags