001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.ByteArrayOutputStream;
035: import java.io.File;
036: import java.io.FileWriter;
037: import java.io.IOException;
038: import java.io.PrintWriter;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: import org.apache.commons.httpclient.methods.multipart.FilePart;
045: import org.apache.commons.httpclient.methods.multipart.StringPart;
046:
047: /**
048: * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
049: * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
050: */
051: public class TestPartsNoHost extends TestCase {
052:
053: static final String PART_DATA = "This is the part data.";
054: static final String NAME = "name";
055:
056: // ------------------------------------------------------------ Constructor
057:
058: public TestPartsNoHost(String testName) {
059: super (testName);
060: }
061:
062: // ------------------------------------------------------- TestCase Methods
063:
064: public static Test suite() {
065: return new TestSuite(TestPartsNoHost.class);
066: }
067:
068: // ----------------------------------------------------------------- Tests
069:
070: public void testFilePartResendsFileData() throws Exception {
071: File file = createTempTestFile();
072: FilePart part = new FilePart(NAME, file);
073: ByteArrayOutputStream stream = new ByteArrayOutputStream();
074: part.send(stream);
075: String resp1 = stream.toString();
076: stream = new ByteArrayOutputStream();
077: part.send(stream);
078: String resp2 = stream.toString();
079: file.delete();
080: assertEquals(resp1, resp2);
081: }
082:
083: public void testStringPartResendsData() throws Exception {
084: StringPart part = new StringPart(NAME, PART_DATA);
085: ByteArrayOutputStream stream = new ByteArrayOutputStream();
086: part.send(stream);
087: String resp1 = stream.toString();
088: stream = new ByteArrayOutputStream();
089: part.send(stream);
090: String resp2 = stream.toString();
091: assertEquals(resp1, resp2);
092: }
093:
094: public void testFilePartNullFileResendsData() throws Exception {
095: FilePart part = new FilePart(NAME, "emptyfile.ext", null);
096: ByteArrayOutputStream stream = new ByteArrayOutputStream();
097: part.send(stream);
098: String resp1 = stream.toString();
099: stream = new ByteArrayOutputStream();
100: part.send(stream);
101: String resp2 = stream.toString();
102: assertEquals(resp1, resp2);
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: }
|