Revision: 516
Author:   henryju
Date:     2006-06-28 04:31:24 -0700 (Wed, 28 Jun 2006)
ViewCVS:  http://svn.sourceforge.net/jwebunit/?rev=516&view=rev

Log Message:
-----------
Add a test case to check support of form input of file.

Modified Paths:
--------------
    branches/1.x/jwebunit-commons-tests/pom.xml
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ParamsServlet.java
    
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html

Added Paths:
-----------
    
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/InputFileForm.html
Modified: branches/1.x/jwebunit-commons-tests/pom.xml
===================================================================
--- branches/1.x/jwebunit-commons-tests/pom.xml 2006-06-27 15:08:38 UTC (rev 
515)
+++ branches/1.x/jwebunit-commons-tests/pom.xml 2006-06-28 11:31:24 UTC (rev 
516)
@@ -1,35 +1,37 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project>
-    <parent>
-        <artifactId>jwebunit</artifactId>
-        <groupId>net.sourceforge.jwebunit</groupId>
-        <version>1.3-SNAPSHOT</version>
-        <relativePath>../</relativePath>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>jwebunit-commons-tests</artifactId>
-    <name>Commons Tests</name>
-    <description>
-        All test cases that each plugin should pass.
-    </description>
-    <properties>
-        <topDirectoryLocation>..</topDirectoryLocation>
-    </properties>
-    <dependencies>
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <version>3.8.1</version>
-        </dependency>
-        <dependency>
-            <groupId>org.mortbay.jetty</groupId>
-            <artifactId>jetty</artifactId>
-            <version>6.0.0beta15</version>
-        </dependency>
-        <dependency>
-            <groupId>net.sourceforge.jwebunit</groupId>
-            <artifactId>jwebunit-core</artifactId>
-            <version>1.3-SNAPSHOT</version>
-        </dependency>
-    </dependencies>
-</project>
+<?xml version="1.0" encoding="UTF-8"?><project>
+  <parent>
+    <artifactId>jwebunit</artifactId>
+    <groupId>net.sourceforge.jwebunit</groupId>
+    <version>1.3-SNAPSHOT</version>
+    <relativePath>../</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>jwebunit-commons-tests</artifactId>
+  <name>Commons Tests</name>
+  <description>All test cases that each plugin should pass.</description>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+    </dependency>
+    <dependency>
+      <groupId>org.mortbay.jetty</groupId>
+      <artifactId>jetty</artifactId>
+      <version>6.0.0beta15</version>
+    </dependency>
+    <dependency>
+      <groupId>net.sourceforge.jwebunit</groupId>
+      <artifactId>jwebunit-core</artifactId>
+      <version>1.3-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>commons-fileupload</groupId>
+      <artifactId>commons-fileupload</artifactId>
+      <version>1.1</version>
+    </dependency>
+  </dependencies>
+  <properties>
+    <topDirectoryLocation>..</topDirectoryLocation>
+  </properties>
+</project>
\ No newline at end of file

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
    2006-06-27 15:08:38 UTC (rev 515)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
    2006-06-28 11:31:24 UTC (rev 516)
@@ -4,6 +4,8 @@
  
******************************************************************************/
 package net.sourceforge.jwebunit.tests;
 
+import java.net.URL;
+
 import net.sourceforge.jwebunit.tests.util.JettySetup;
 import junit.framework.AssertionFailedError;
 import junit.framework.Test;
@@ -34,7 +36,6 @@
                setTextField("color", "blue");
                submit("button");
         assertTextPresent("Submitted parameters");
-        //dumpResponse(System.out);
                assertTextPresent("Params are: color=blue");
                clickLink("return");
         setTextField("color", "red");
@@ -43,16 +44,20 @@
        }
 
     public void testSetFileField() {
-        beginAt("/SingleNamedButtonForm.html");
-        setTextField("file", "c:\\test.txt");
+        beginAt("/InputFileForm.html");
+        URL url = getClass().getResource("data.txt");
+        assertNotNull(url);
+        //FIXME HtmlUnit bug, need to remove file:/
+         String filename = url.toString().substring(6);
+        setTextField("file", filename);
         submit("button");
         assertTextPresent("Submitted parameters");
-        assertTextPresent("file=test.txt");
+        assertMatch("file=data\\.txt\\{This file.*\\}");
     }
 
     public void testCheckBoxSelection() {
                beginAt("/SingleNamedButtonForm.html");
-               checkCheckbox("checkBox"); //Fail with httpunit because of 
hidden field with same name
+               checkCheckbox("checkBox");
         setTextField("color", "blue");
                submit();
         //checkBox contains 2 parameters: one for the hidden input and one for 
the checkbox

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ParamsServlet.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ParamsServlet.java
    2006-06-27 15:08:38 UTC (rev 515)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ParamsServlet.java
    2006-06-28 11:31:24 UTC (rev 516)
@@ -6,12 +6,20 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.List;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+
 public class ParamsServlet extends HttpServlet {
 
     private static final long serialVersionUID = 1L;
@@ -31,27 +39,72 @@
          * Prints POST and GET parameters as name=value1[,value2...] separated
          * with spaces
          */
-        java.util.Enumeration params = request.getParameterNames();
-        for (; params.hasMoreElements();) {
-            String p = params.nextElement().toString();
-            String[] v = request.getParameterValues(p);
-            out.write(" " + p + "=");
-            int n = v.length;
-            if (n > 0) {
-                out.write(v[0]!=null?v[0]:"");
-                for (int i = 1; i < n; i++) {
-                    out.write("," + (v[i]!=null?v[i]:""));
+
+        // Check that we have a file upload request
+        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
+        if (isMultipart) {
+            // Create a factory for disk-based file items
+            FileItemFactory factory = new DiskFileItemFactory();
+
+            // Create a new file upload handler
+            ServletFileUpload upload = new ServletFileUpload(factory);
+
+            // Parse the request
+            List /* FileItem */items = null;
+            try {
+                items = upload.parseRequest(request);
+            } catch (FileUploadException e) {
+                throw new ServletException(e);
+            }
+
+            String ref = null;
+            // Process the uploaded items
+            Iterator iter = items.iterator();
+            while (iter.hasNext()) {
+                FileItem item = (FileItem) iter.next();
+
+                if (item.isFormField()) {
+                    out.write(" " + item.getFieldName() + "="
+                            + item.getString());
+                    if (item.getFieldName().equals("myReferer")) {
+                        ref = item.getString();
+                    }
+                } else {
+                    String fieldName = item.getFieldName();
+                    String fileName = item.getName();
+                    String contentType = item.getContentType();
+                    boolean isInMemory = item.isInMemory();
+                    long sizeInBytes = item.getSize();
+                    out.write(" " + item.getFieldName() + "=" + item.getName()
+                            + "{" + new String(item.get()) + "}");
+
                 }
             }
-        }
-        out.write(" </p>\n");
-        String ref = request.getHeader("Referer");
-        if (ref==null) {
-            if (request.getParameterValues("myReferer")!=null) {
-                ref = request.getParameterValues("myReferer")[0];
+            out.write(" </p>\n");
+            out.write(HtmlHelper.getLinkParagraph("return", ref));
+        } else {
+            java.util.Enumeration params = request.getParameterNames();
+            for (; params.hasMoreElements();) {
+                String p = params.nextElement().toString();
+                String[] v = request.getParameterValues(p);
+                out.write(" " + p + "=");
+                int n = v.length;
+                if (n > 0) {
+                    out.write(v[0] != null ? v[0] : "");
+                    for (int i = 1; i < n; i++) {
+                        out.write("," + (v[i] != null ? v[i] : ""));
+                    }
+                }
             }
+            out.write(" </p>\n");
+            String ref = request.getHeader("Referer");
+            if (ref == null) {
+                if (request.getParameterValues("myReferer") != null) {
+                    ref = request.getParameterValues("myReferer")[0];
+                }
+            }
+            out.write(HtmlHelper.getLinkParagraph("return", ref));
         }
-        out.write(HtmlHelper.getLinkParagraph("return", ref));
         out.write(HtmlHelper.getEnd());
     }
 

Added: 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/InputFileForm.html
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/InputFileForm.html
                              (rev 0)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/InputFileForm.html
      2006-06-28 11:31:24 UTC (rev 516)
@@ -0,0 +1,13 @@
+<html>
+<head></head>
+<body>
+       <form method="post" enctype="multipart/form-data" 
action="../params.jsp">
+               <input type="text" name="color" />
+               <input type="hidden" name="checkBox"/>
+               <input type="checkbox" name="checkBox" />
+               <input type="submit" name="button">
+               <input type="hidden" name="myReferer" 
value="FormSubmissionTest/InputFileForm.html">
+               <input type="file" name="file" />
+       </form>
+</body>
+</html>
\ No newline at end of file

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
      2006-06-27 15:08:38 UTC (rev 515)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
      2006-06-28 11:31:24 UTC (rev 516)
@@ -1,13 +1,12 @@
 <html>
 <head></head>
 <body>
-       <form method="GET" action="../params.jsp">
+       <form method="get" action="../params.jsp">
                <input type="text" name="color" />
                <input type="hidden" name="checkBox"/>
                <input type="checkbox" name="checkBox" />
                <input type="submit" name="button">
                <input type="hidden" name="myReferer" 
value="FormSubmissionTest/SingleNamedButtonForm.html">
-               <input type="file" name="file" />
        </form>
 </body>
 </html>
\ No newline at end of file


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to