    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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    * ====================================================================
    6    *
    7    *  Licensed to the Apache Software Foundation (ASF) under one or more
    8    *  contributor license agreements.  See the NOTICE file distributed with
    9    *  this work for additional information regarding copyright ownership.
    10   *  The ASF licenses this file to You under the Apache License, Version 2.0
    11   *  (the "License"); you may not use this file except in compliance with
    12   *  the License.  You may obtain a copy of the License at
    13   *
    14   *      http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   *  Unless required by applicable law or agreed to in writing, software
    17   *  distributed under the License is distributed on an "AS IS" BASIS,
    18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   *  See the License for the specific language governing permissions and
    20   *  limitations under the License.
    21   * ====================================================================
    22   *
    23   * This software consists of voluntary contributions made by many
    24   * individuals on behalf of the Apache Software Foundation.  For more
    25   * information on the Apache Software Foundation, please see
    26   * <http://www.apache.org/>.
    27   *
    28   * [Additional notices, if required by prior licensing conditions]
    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      // ------------------------------------------------------------ Constructor
    57
    58      public TestPartsNoHost(String testName) {
    59          super(testName);
    60      }
    61
    62      // ------------------------------------------------------- TestCase Methods
    63
    64      public static Test suite() {
    65          return new TestSuite(TestPartsNoHost.class);
    66      }
    67
    68      // ----------------------------------------------------------------- Tests
    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 }