/*
* 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 org.apache.wicket.util.tester;
import java.util.Locale;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.Session;
import org.apache.wicket.WicketTestCase;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.tester.MockFormFileUploadPage.MockDomainObjectFileUpload;
import org.apache.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()
{
tester.startPage(MockFormPage.class);
MockFormPage page = (MockFormPage)tester.getLastRenderedPage();
MockDomainObject domainObject = page.getDomainObject();
assertNotNull(domainObject);
assertNull(domainObject.getText());
assertNull(domainObject.getTextarea());
assertFalse(domainObject.isCheckbox());
FormTester formTester = tester.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());
}
public void testCheckboxValuesCanBeSelectedWithBoolean()
{
tester.startPage(MockFormPage.class);
MockFormPage page = (MockFormPage)tester.getLastRenderedPage();
MockDomainObject domainObject = page.getDomainObject();
assertFalse(domainObject.isCheckbox());
FormTester formTester = tester.newFormTester("form");
formTester.setValue("checkbox", true);
formTester.submit();
assertTrue(domainObject.isCheckbox());
formTester = tester.newFormTester("form");
formTester.setValue("checkbox", false);
formTester.submit();
assertFalse(domainObject.isCheckbox());
}
/**
* Test that the user can use
* {@link FormTester#setFile(String, org.apache.wicket.util.file.File, String)} to test that
* upload to a FileUploadField works.
*/
public void testAddFile()
{
tester.startPage(MockFormFileUploadPage.class);
MockFormFileUploadPage page = (MockFormFileUploadPage)tester.getLastRenderedPage();
MockDomainObjectFileUpload domainObject = page.getDomainObject();
tester.createRequestCycle();
assertNull(page.getFileUpload());
assertNotNull(domainObject);
assertNull(domainObject.getText());
FormTester formTester = tester.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);
assertTrue("setFile failed, no upload content detected.", fileUpload.getBytes().length > 0);
assertEquals("pom.xml", fileUpload.getClientFileName());
assertEquals("text/xml", fileUpload.getContentType());
}
/**
* Test that the user can use
* {@link FormTester#setFile(String, org.apache.wicket.util.file.File, String)} to test that
* upload to a FileUploadField works.
*/
public void testAddBinaryFile()
{
tester.startPage(MockFormFileUploadPage.class);
MockFormFileUploadPage page = (MockFormFileUploadPage)tester.getLastRenderedPage();
MockDomainObjectFileUpload domainObject = page.getDomainObject();
tester.createRequestCycle();
assertNull(page.getFileUpload());
assertNotNull(domainObject);
assertNull(domainObject.getText());
FormTester formTester = tester.newFormTester("form");
formTester.setFile("file", new File(getBasedir() +
"src/test/java/org/apache/wicket/util/tester/bg.jpg"), "image/jpeg");
formTester.setValue("text", "Mock value");
formTester.submit();
assertNotNull(domainObject);
assertNotNull(domainObject.getText());
assertEquals("Mock value", domainObject.getText());
FileUpload fileUpload = page.getFileUpload();
assertNotNull(fileUpload);
assertTrue("uploaded content does not have the right size, expected 428, got " +
fileUpload.getBytes().length, fileUpload.getBytes().length == 428);
assertEquals("bg.jpg", fileUpload.getClientFileName());
assertEquals("image/jpeg", fileUpload.getContentType());
}
/**
* Test that formTester deal with Multipart form correctly when no actual upload
*/
public void testSubmitWithoutUploadFile()
{
tester.startPage(MockFormFileUploadPage.class);
MockFormFileUploadPage page = (MockFormFileUploadPage)tester.getLastRenderedPage();
Session.get().setLocale(Locale.US);
FormTester formTester = tester.newFormTester("form");
// without file upload
formTester.submit();
assertNull(page.getFileUpload());
tester.assertErrorMessages(new String[] { "Field 'file' is required." });
}
/**
* Test that formTester deal with Multipart form correctly when no actual upload
*/
public void testSubmitMultipartForm()
{
tester.startPage(MockFormFileUploadPage.class, new PageParameters("required=false"));
MockFormFileUploadPage page = (MockFormFileUploadPage)tester.getLastRenderedPage();
MockDomainObjectFileUpload domainObject = page.getDomainObject();
Session.get().setLocale(Locale.US);
FormTester formTester = tester.newFormTester("form");
formTester.setValue("text", "Mock Value");
formTester.submit();
assertFalse(formTester.getForm().hasError());
assertNull(page.getFileUpload());
assertEquals("Mock Value", domainObject.getText());
}
/**
* @throws Exception
*/
public void testNoParametersCreatedForDisabledComponents() throws Exception
{
tester.startPage(new MockFormPage()
{
@Override
protected void onBeforeRender()
{
super.onBeforeRender();
// on first rendering there can't be any form parameters.
// on second rendering there must not be any since we disable the form.
// the components all get rendered as disabled, so the browser would not send
// any parameters. thus FormTester must not send any either.
assertTrue(getRequest().getRequestParameters().getParameters().isEmpty());
}
});
final Component form = tester.getComponentFromLastRenderedPage("form");
form.setEnabled(false);
assertFalse(form.isEnabled());
Component check = tester.getComponentFromLastRenderedPage("form:checkbox");
assertTrue(check.isEnabled());
assertFalse(check.isEnabledInHierarchy());
FormTester formTester = tester.newFormTester("form");
formTester.submit();
}
public void testNestedFormHandlingOnInnerSubmit() throws Exception
{
NestedFormPage page = (NestedFormPage)tester.startPage(NestedFormPage.class);
FormTester form = tester.newFormTester("outer:inner");
form.submit("submit");
assertFalse("should not directly submit inner form - browsers submit the outer form!",
page.iface.contains("inner"));
assertFalse("outer form should not be processed", page.outerSubmitted);
assertTrue("inner form should be processed", page.innerSubmitted);
}
public void testNestedFormHandlingOnInnerSubmitWithOuterForm() throws Exception
{
NestedFormPage page = (NestedFormPage)tester.startPage(NestedFormPage.class);
FormTester form = tester.newFormTester("outer");
form.submit("inner:submit");
assertFalse("should not directly submit inner form - browsers submit the outer form!",
page.iface.contains("inner"));
assertFalse("outer form should not be processed", page.outerSubmitted);
assertTrue("inner form should be processed", page.innerSubmitted);
}
public void testNestedFormHandlingOnOuterSubmit() throws Exception
{
NestedFormPage page = (NestedFormPage)tester.startPage(NestedFormPage.class);
FormTester form = tester.newFormTester("outer");
form.submit();
assertFalse("should not directly submit inner form - browsers submit the outer form!",
page.iface.contains("inner"));
assertTrue("outer form should be processed", page.outerSubmitted);
assertTrue("inner form should be processed", page.innerSubmitted);
}
}