1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java,v 1.6 2003/03/11 19:54:48 olegk Exp $ 3 * $Revision: 1.6 $ 4 * $Date: 2003/03/11 19:54:48 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import java.io.ByteArrayOutputStream; 66 import java.io.File; 67 import java.io.FileWriter; 68 import java.io.IOException; 69 import java.io.PrintWriter; 70 71 import junit.framework.Test; 72 import junit.framework.TestCase; 73 import junit.framework.TestSuite; 74 75 import org.apache.commons.httpclient.methods.multipart.FilePart; 76 import org.apache.commons.httpclient.methods.multipart.StringPart; 77 78 /*** 79 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 80 * @version $Revision: 1.6 $ $Date: 2003/03/11 19:54:48 $ 81 */ 82 public class TestPartsNoHost extends TestCase { 83 84 static final String PART_DATA = "This is the part data."; 85 static final String NAME = "name"; 86 87 // ------------------------------------------------------------ Constructor 88 89 public TestPartsNoHost(String testName) { 90 super(testName); 91 } 92 93 // ------------------------------------------------------- TestCase Methods 94 95 public static Test suite() { 96 return new TestSuite(TestPartsNoHost.class); 97 } 98 99 // ----------------------------------------------------------------- Tests 100 101 public void testFilePartResendsFileData() throws Exception { 102 File file = createTempTestFile(); 103 FilePart part = new FilePart(NAME, file); 104 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 105 part.send(stream); 106 String resp1 = stream.toString(); 107 stream = new ByteArrayOutputStream(); 108 part.send(stream); 109 String resp2 = stream.toString(); 110 file.delete(); 111 assertEquals(resp1, resp2); 112 } 113 114 public void testStringPartResendsData() throws Exception { 115 StringPart part = new StringPart(NAME, PART_DATA); 116 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 117 part.send(stream); 118 String resp1 = stream.toString(); 119 stream = new ByteArrayOutputStream(); 120 part.send(stream); 121 String resp2 = stream.toString(); 122 assertEquals(resp1, resp2); 123 } 124 125 public void testFilePartNullFileResendsData() throws Exception { 126 FilePart part = new FilePart(NAME, "emptyfile.ext", null); 127 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 128 part.send(stream); 129 String resp1 = stream.toString(); 130 stream = new ByteArrayOutputStream(); 131 part.send(stream); 132 String resp2 = stream.toString(); 133 assertEquals(resp1, resp2); 134 } 135 136 137 /*** Writes PART_DATA out to a temporary file and returns the file it 138 * was written to. 139 * @return the File object representing the file the data was 140 * written to. 141 */ 142 private File createTempTestFile() throws IOException { 143 File file = File.createTempFile("FilePartTest", ".txt"); 144 PrintWriter out = new PrintWriter(new FileWriter(file)); 145 out.println(PART_DATA); 146 out.flush(); 147 out.close(); 148 return file; 149 } 150 }

This page was automatically generated by Maven