commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mart...@apache.org
Subject cvs commit: jakarta-commons/fileupload/src/test/org/apache/commons/fileupload FileUploadTest.java TestAll.java
Date Thu, 14 Oct 2004 04:48:16 GMT
martinc     2004/10/13 21:48:16

  Modified:    fileupload/src/test/org/apache/commons/fileupload
                        FileUploadTest.java TestAll.java
  Log:
  Bug #30059 - More unit tests from Justin Sampson. Thanks!
  
  Revision  Changes    Path
  1.11      +73 -12    jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/FileUploadTest.java
  
  Index: FileUploadTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/FileUploadTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- FileUploadTest.java	25 Feb 2004 21:07:12 -0000	1.10
  +++ FileUploadTest.java	14 Oct 2004 04:48:15 -0000	1.11
  @@ -15,8 +15,11 @@
    */
   package org.apache.commons.fileupload;
   
  -import junit.framework.TestCase;
  +import java.io.IOException;
  +import java.io.UnsupportedEncodingException;
  +import java.util.List;
   import javax.servlet.http.HttpServletRequest;
  +import junit.framework.TestCase;
   
   /**
    * Unit tests {@link org.apache.commons.fileupload.DiskFileUpload}.
  @@ -77,22 +80,80 @@
           super(name);
       }
   
  -    public void testParseRequest() throws FileUploadException
  -    {
   
  -    	String[] fileNames =
  -    	{
  -			"filename1",
  -    		"filename2"
  -    	};
  +    public void testFileUpload() throws IOException, FileUploadException {
  +        List fileItems = parseUpload("-----1234\r\n" +
  +                        "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n"
+
  +                        "Content-Type: text/whatever\r\n" +
  +                        "\r\n" +
  +                        "This is the content of the file\n" +
  +                        "\r\n" +
  +                        "-----1234\r\n" +
  +                        "Content-Disposition: form-data; name=\"field\"\r\n" +
  +                        "\r\n" +
  +                        "fieldValue\r\n" +
  +                        "-----1234\r\n" +
  +                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
  +                        "\r\n" +
  +                        "value1\r\n" +
  +                        "-----1234\r\n" +
  +                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
  +                        "\r\n" +
  +                        "value2\r\n" +
  +                        "-----1234--\r\n");
  +        assertEquals(4, fileItems.size());
  +
  +        FileItem file = (FileItem) fileItems.get(0);
  +        assertEquals("file", file.getFieldName());
  +        assertFalse(file.isFormField());
  +        assertEquals("This is the content of the file\n", file.getString());
  +        assertEquals("text/whatever", file.getContentType());
  +        assertEquals("foo.tab", file.getName());
  +
  +        FileItem field = (FileItem) fileItems.get(1);
  +        assertEquals("field", field.getFieldName());
  +        assertTrue(field.isFormField());
  +        assertEquals("fieldValue", field.getString());
  +
  +        FileItem multi0 = (FileItem) fileItems.get(2);
  +        assertEquals("multi", multi0.getFieldName());
  +        assertTrue(multi0.isFormField());
  +        assertEquals("value1", multi0.getString());
  +
  +        FileItem multi1 = (FileItem) fileItems.get(3);
  +        assertEquals("multi", multi1.getFieldName());
  +        assertTrue(multi1.isFormField());
  +        assertEquals("value2", multi1.getString());
  +    }
  +
  +    /**
  +     * This is what the browser does if you submit the form without choosing a file.
  +     */
  +    public void testEmptyFile() throws UnsupportedEncodingException, FileUploadException
{
  +        List fileItems = parseUpload ("-----1234\r\n" +
  +                "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" +
  +                "\r\n" +
  +                "\r\n" +
  +                "-----1234--\r\n");
  +        assertEquals(1, fileItems.size());
  +
  +        FileItem file = (FileItem) fileItems.get(0);
  +        assertFalse(file.isFormField());
  +        assertEquals("", file.getString());
  +        assertEquals("", file.getName());
  +    }
   
  -		FileUploadBase fu = new DiskFileUpload();
  +    private List parseUpload(String content) throws UnsupportedEncodingException, FileUploadException
{
  +        byte[] bytes = content.getBytes("US-ASCII");
   
  -		HttpServletRequest req = HttpServletRequestFactory.createValidHttpServletRequest(fileNames);
  +        String contentType = "multipart/form-data; boundary=---1234";
   
  -		// todo java.util.List lst = fu.parseRequest(req);
  -		// todo assertNotNull(lst);
  +        FileUploadBase upload = new DiskFileUpload();
  +        HttpServletRequest request = new MyHttpServletRequest(bytes, contentType);
   
  +        List fileItems = upload.parseRequest(request);
  +        return fileItems;
       }
  +
   }
   
  
  
  
  1.6       +1 -0      jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/TestAll.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestAll.java	12 Mar 2004 07:34:46 -0000	1.5
  +++ TestAll.java	14 Oct 2004 04:48:15 -0000	1.6
  @@ -34,6 +34,7 @@
           suite.addTest(new TestSuite(MultipartStreamTest.class));
           suite.addTest(new TestSuite(FileUploadTest.class));
           suite.addTest(new TestSuite(DeferredFileOutputStreamTest.class));
  +        suite.addTest(new TestSuite(DefaultFileItemTest.class));
           return suite;
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message