Author: frankbille
Date: Sun Oct 29 12:05:53 2006
New Revision: 468969

URL: http://svn.apache.org/viewvc?view=rev&rev=468969
Log:
WICKET-24 Added a helper method in FormTester for adding a file to a 
FileUploadField.

Added:
    
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
    
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
    
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
    
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.html
    
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.java
Modified:
    
incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
    
incubator/wicket/trunk/wicket/src/main/java/wicket/util/tester/FormTester.java

Modified: 
incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java?view=diff&rev=468969&r1=468968&r2=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
 (original)
+++ 
incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
 Sun Oct 29 12:05:53 2006
@@ -905,6 +905,15 @@
        }
 
        /**
+        * @return True if there has been added files to this request using
+        *         [EMAIL PROTECTED] #addFile(String, File, String)}
+        */
+       public boolean hasUploadedFiles()
+       {
+               return uploadedFiles != null;
+       }
+
+       /**
         * Reset the request back to a default state.
         */
        public void initialize()

Modified: 
incubator/wicket/trunk/wicket/src/main/java/wicket/util/tester/FormTester.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/util/tester/FormTester.java?view=diff&rev=468969&r1=468968&r2=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/main/java/wicket/util/tester/FormTester.java 
(original)
+++ 
incubator/wicket/trunk/wicket/src/main/java/wicket/util/tester/FormTester.java 
Sun Oct 29 12:05:53 2006
@@ -41,7 +41,13 @@
 import wicket.markup.html.form.Radio;
 import wicket.markup.html.form.RadioChoice;
 import wicket.markup.html.form.RadioGroup;
+import wicket.markup.html.form.upload.FileUploadField;
+import wicket.protocol.http.MockHttpServletRequest;
+import wicket.protocol.http.WebRequestCycle;
+import wicket.protocol.http.servlet.MultipartServletWebRequest;
+import wicket.util.file.File;
 import wicket.util.string.Strings;
+import wicket.util.upload.FileUploadException;
 
 /**
  * A helper for testing validaiton and submission of Form component.
@@ -126,9 +132,9 @@
                                }
                                else
                                {
-                                       String path=foundRadio.getPath();
-                                       
path=path.substring(formComponent.getPath().length()+1, path.length());
-                                       
+                                       String path = foundRadio.getPath();
+                                       path = 
path.substring(formComponent.getPath().length() + 1, path.length());
+
                                        
assignValueToFormComponent(formComponent, path);
                                }
                        }
@@ -444,6 +450,34 @@
        }
 
        /**
+        * Set the file on a [EMAIL PROTECTED] FileUploadField}.
+        * 
+        * @param formComponentId
+        *            relative path (from form) to formComponent. The form 
component
+        *            must be of a type FileUploadField.
+        * @param file
+        *            The file to upload.
+        * @param contentType
+        *            The content type of the file. Must be a correct mimetype.
+        */
+       public void setFile(final String formComponentId, final File file, 
final String contentType)
+       {
+               checkClosed();
+
+               FormComponent formComponent = 
(FormComponent)workingForm.get(formComponentId);
+
+               if (formComponent instanceof FileUploadField == false)
+               {
+                       throw new IllegalArgumentException("'" + 
formComponentId + "' is not "
+                                       + "a FileUploadField. You can only 
attach a file to form "
+                                       + "component of this type.");
+               }
+
+               MockHttpServletRequest servletRequest = 
wicketTester.getServletRequest();
+               servletRequest.addFile(formComponent.getInputName(), file, 
contentType);
+       }
+
+       /**
         * submit the form. note that submit() can be executed only once.
         */
        public void submit()
@@ -451,8 +485,21 @@
                checkClosed();
                try
                {
-                       
wicketTester.getServletRequest().setRequestToComponent(workingForm);
-                       wicketTester.processRequestCycle();
+                       MockHttpServletRequest servletRequest = 
wicketTester.getServletRequest();
+                       WebRequestCycle requestCycle = 
wicketTester.createRequestCycle();
+                       servletRequest.setRequestToComponent(workingForm);
+
+                       if (servletRequest.hasUploadedFiles())
+                       {
+                               requestCycle.setRequest(new 
MultipartServletWebRequest(servletRequest, workingForm
+                                               .getMaxSize()));
+                       }
+
+                       wicketTester.processRequestCycle(requestCycle);
+               }
+               catch (FileUploadException e)
+               {
+                       throw new IllegalStateException(e);
                }
                finally
                {

Added: 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/FormTesterTest.java?view=auto&rev=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
 (added)
+++ 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
 Sun Oct 29 12:05:53 2006
@@ -0,0 +1,94 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 
2006) eelco12 $
+ * $Revision: 5004 $
+ * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
+ * 
+ * 
==============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package wicket.util.tester;
+
+import wicket.WicketTestCase;
+import wicket.markup.html.form.upload.FileUpload;
+import wicket.util.file.File;
+import wicket.util.tester.MockFormFileUploadPage.MockDomainObjectFileUpload;
+import wicket.util.tester.MockFormPage.MockDomainObject;
+
+/**
+ * Test of FormTester.
+ * 
+ * @author frankbille
+ */
+public class FormTesterTest extends WicketTestCase
+{
+       /**
+        * Test that normal use of the formtester (no file uploads) works.
+        */
+       public void testFormTester()
+       {
+               application.startPage(MockFormPage.class);
+               MockFormPage page = 
(MockFormPage)application.getLastRenderedPage();
+               MockDomainObject domainObject = page.getDomainObject();
+               
+               assertNotNull(domainObject);
+               assertNull(domainObject.getText());
+               assertNull(domainObject.getTextarea());
+               assertFalse(domainObject.isCheckbox());
+               
+               FormTester formTester = application.newFormTester("form");
+               formTester.setValue("text", "Mock text value");
+               formTester.setValue("textarea", "Mock textarea value");
+               formTester.setValue("checkbox", "true");
+               formTester.submit();
+
+               assertNotNull(domainObject);
+               assertNotNull(domainObject.getText());
+               assertNotNull(domainObject.getTextarea());
+               assertTrue(domainObject.isCheckbox());
+       }
+       
+       /**
+        * Test that the user can use
+        * [EMAIL PROTECTED] FormTester#setFile(String, wicket.util.file.File, 
String)} to test
+        * that upload to a FileUploadField works.
+        */
+       public void testAddFile()
+       {
+               application.startPage(MockFormFileUploadPage.class);
+               MockFormFileUploadPage page = 
(MockFormFileUploadPage)application.getLastRenderedPage();
+               MockDomainObjectFileUpload domainObject = 
page.getDomainObject();
+               
+               application.createRequestCycle();
+               
+               assertNull(page.getFileUpload());
+               assertNotNull(domainObject);
+               assertNull(domainObject.getText());
+               
+               
+               FormTester formTester = application.newFormTester("form");
+               formTester.setFile("file", new File("pom.xml"), "text/xml");
+               formTester.setValue("text", "Mock value");
+               formTester.submit();
+               
+               
+               assertNotNull(domainObject);
+               assertNotNull(domainObject.getText());
+               assertEquals("Mock value", domainObject.getText());
+               
+               FileUpload fileUpload = page.getFileUpload();
+               assertNotNull(fileUpload);
+               
+               assertEquals("pom.xml", fileUpload.getClientFileName());
+               assertEquals("text/xml", fileUpload.getContentType());
+       }
+}

Added: 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html?view=auto&rev=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
 (added)
+++ 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
 Sun Oct 29 12:05:53 2006
@@ -0,0 +1,5 @@
+<form wicket:id="form">
+<input type="file" wicket:id="file" />
+<input type="text" wicket:id="text" />
+<input type="submit" value="Upload" />
+</form>

Added: 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java?view=auto&rev=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
 (added)
+++ 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
 Sun Oct 29 12:05:53 2006
@@ -0,0 +1,109 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 
2006) eelco12 $
+ * $Revision: 5004 $
+ * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
+ * 
+ * 
==============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package wicket.util.tester;
+
+import wicket.markup.html.WebPage;
+import wicket.markup.html.form.Form;
+import wicket.markup.html.form.TextField;
+import wicket.markup.html.form.upload.FileUpload;
+import wicket.markup.html.form.upload.FileUploadField;
+import wicket.model.CompoundPropertyModel;
+import wicket.util.lang.Bytes;
+
+/**
+ * Mock form for use when testing FormTester's addFile functionality.
+ * 
+ * @author frankbille
+ */
+public class MockFormFileUploadPage extends WebPage
+{
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Model object used in this test.
+        * 
+        * @author frankbille
+        */
+       public static class MockDomainObjectFileUpload
+       {
+               private String text;
+
+               /**
+                * @return text
+                */
+               public String getText()
+               {
+                       return text;
+               }
+
+               /**
+                * @param text
+                */
+               public void setText(String text)
+               {
+                       this.text = text;
+               }
+       }
+
+       private MockDomainObjectFileUpload domainObject;
+
+       private FileUploadField fileUploadField;
+
+       private FileUpload fileUpload;
+
+       /**
+        * Construct.
+        */
+       public MockFormFileUploadPage()
+       {
+               domainObject = new MockDomainObjectFileUpload();
+               Form<MockDomainObjectFileUpload> form = new 
Form<MockDomainObjectFileUpload>(this, "form",
+                               new 
CompoundPropertyModel<MockDomainObjectFileUpload>(domainObject))
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       protected void onSubmit()
+                       {
+                               fileUpload = fileUploadField.getFileUpload();
+                       }
+               };
+               form.setMultiPart(true);
+               form.setMaxSize(Bytes.kilobytes(100));
+               new TextField<String>(form, "text");
+               fileUploadField = new FileUploadField(form, "file");
+       }
+
+       /**
+        * @return domainObject
+        */
+       public MockDomainObjectFileUpload getDomainObject()
+       {
+               return domainObject;
+       }
+
+       /**
+        * @return fileUpload
+        */
+       public FileUpload getFileUpload()
+       {
+               return fileUpload;
+       }
+
+}

Added: 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.html?view=auto&rev=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.html
 (added)
+++ 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.html
 Sun Oct 29 12:05:53 2006
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
+<html xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:wicket="http://www.wicketframework.org";>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<title>Insert title here</title>
+</head>
+<body>
+<form wicket:id="form">
+<input type="text" wicket:id="text" />
+<input type="checkbox" wicket:id="checkbox" />
+<textarea wicket:id="textarea"></textarea>
+</form>
+</body>
+</html>
\ No newline at end of file

Added: 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.java?view=auto&rev=468969
==============================================================================
--- 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.java
 (added)
+++ 
incubator/wicket/trunk/wicket/src/test/java/wicket/util/tester/MockFormPage.java
 Sun Oct 29 12:05:53 2006
@@ -0,0 +1,117 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 
2006) eelco12 $
+ * $Revision: 5004 $
+ * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
+ * 
+ * 
==============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package wicket.util.tester;
+
+import wicket.markup.html.WebPage;
+import wicket.markup.html.form.CheckBox;
+import wicket.markup.html.form.Form;
+import wicket.markup.html.form.TextArea;
+import wicket.markup.html.form.TextField;
+import wicket.model.CompoundPropertyModel;
+
+/**
+ * Mock page for testing basic FormTester functionality.
+ * 
+ * @author frankbille
+ */
+public class MockFormPage extends WebPage
+{
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Domain object
+        */
+       public class MockDomainObject
+       {
+               private String text;
+               private boolean checkbox;
+               private String textarea;
+
+               /**
+                * @return checkbox
+                */
+               public boolean isCheckbox()
+               {
+                       return checkbox;
+               }
+
+               /**
+                * @param checkbox
+                */
+               public void setCheckbox(boolean checkbox)
+               {
+                       this.checkbox = checkbox;
+               }
+
+               /**
+                * @return text
+                */
+               public String getText()
+               {
+                       return text;
+               }
+
+               /**
+                * @param text
+                */
+               public void setText(String text)
+               {
+                       this.text = text;
+               }
+
+               /**
+                * @return textarea
+                */
+               public String getTextarea()
+               {
+                       return textarea;
+               }
+
+               /**
+                * @param textarea
+                */
+               public void setTextarea(String textarea)
+               {
+                       this.textarea = textarea;
+               }
+       }
+
+       private MockDomainObject domainObject;
+
+       /**
+        * Construct.
+        */
+       public MockFormPage()
+       {
+               domainObject = new MockDomainObject();
+               Form<MockDomainObject> form = new Form<MockDomainObject>(this, 
"form", new CompoundPropertyModel<MockDomainObject>(domainObject));
+               
+               new TextField<String>(form, "text");
+               new CheckBox(form, "checkbox");
+               new TextArea<String>(form, "textarea");
+       }
+       
+       /**
+        * @return domainObject
+        */
+       public MockDomainObject getDomainObject()
+       {
+               return domainObject;
+       }
+}


Reply via email to