Author: frankbille
Date: Sun Jan 14 01:53:42 2007
New Revision: 496050

URL: http://svn.apache.org/viewvc?view=rev&rev=496050
Log:
WICKET-24: FileUpload testing helper

Added:
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
   (with props)
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
   (with props)
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
   (with props)
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html
   (with props)
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java
   (with props)
Modified:
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/tester/FormTester.java

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java?view=diff&rev=496050&r1=496049&r2=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
 Sun Jan 14 01:53:42 2007
@@ -898,6 +898,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/branches/wicket-1.x/wicket/src/main/java/wicket/util/tester/FormTester.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/tester/FormTester.java?view=diff&rev=496050&r1=496049&r2=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/tester/FormTester.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/tester/FormTester.java
 Sun Jan 14 01:53:42 2007
@@ -39,12 +39,19 @@
 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.
  * 
  * @author Ingram Chen
+ * @author Frank Bille (frankbille)
  */
 public class FormTester
 {
@@ -458,6 +465,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()
@@ -465,8 +500,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 WicketRuntimeException(e);
                }
                finally
                {

Added: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java?view=auto&rev=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
 (added)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
 Sun Jan 14 01:53:42 2007
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+{
+       /**
+        * Construct.
+        */
+       public FormTesterTest()
+       {
+               super("Form tester test");
+       }
+
+       /**
+        * 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());
+       }
+}

Propchange: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/FormTesterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html?view=auto&rev=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
 (added)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
 Sun Jan 14 01:53:42 2007
@@ -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>

Propchange: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java?view=auto&rev=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
 (added)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
 Sun Jan 14 01:53:42 2007
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 form = new Form("form", new 
CompoundPropertyModel(domainObject))
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       protected void onSubmit()
+                       {
+                               fileUpload = fileUploadField.getFileUpload();
+                       }
+               };
+               add(form);
+               form.setMultiPart(true);
+               form.setMaxSize(Bytes.kilobytes(100));
+               form.add(new TextField("text"));
+               fileUploadField = new FileUploadField("file");
+               form.add(fileUploadField);
+       }
+
+       /**
+        * @return domainObject
+        */
+       public MockDomainObjectFileUpload getDomainObject()
+       {
+               return domainObject;
+       }
+
+       /**
+        * @return fileUpload
+        */
+       public FileUpload getFileUpload()
+       {
+               return fileUpload;
+       }
+
+}

Propchange: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormFileUploadPage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html?view=auto&rev=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html
 (added)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html
 Sun Jan 14 01:53:42 2007
@@ -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>

Propchange: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java?view=auto&rev=496050
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java
 (added)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java
 Sun Jan 14 01:53:42 2007
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 form = new Form("form", new 
CompoundPropertyModel(domainObject));
+               add(form);
+               
+               form.add(new TextField("text"));
+               form.add(new CheckBox("checkbox"));
+               form.add(new TextArea("textarea"));
+       }
+       
+       /**
+        * @return domainObject
+        */
+       public MockDomainObject getDomainObject()
+       {
+               return domainObject;
+       }
+}

Propchange: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/tester/MockFormPage.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to