/*
* Copyright 2005-2006 the original author or authors.
*
* 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 org.strecks.web.form;
import java.util.Iterator;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.form.controller.FormTestUtils;
import org.strecks.web.form.impl.NewProjectForm;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestProjectFormValidation extends AbstractTestFormValidation
{
private NewProjectForm form;
private DelegatingForm delegator;
@BeforeMethod
public void beforeTest()
{
form = new NewProjectForm();
form.setProjectName("project name");
form.setSelectedProjectType("1");
form.setReadyDate("15 October 2005");
delegator = FormTestUtils.getDelegatingForm(form);
}
@Test
public void testValidationOK()
{
assert null == delegator.validate(null, null);
}
@Test
public void testProjectMissingData()
{
form.setProjectName(null);
form.setSelectedProjectType("");
ActionErrors errors = delegator.validate(null, null);
assert "Project name is required".equals(getValidationMessage(errors, "projectName"));
assert "A non-empty project type must be selected".equals(getValidationMessage(errors, "selectedProjectType"));
}
@Test
public void testProjectNameEmpty()
{
form.setProjectName("");
ActionErrors errors = delegator.validate(null, null);
assert "Project name is required".equals(getValidationMessage(errors, "projectName"));
}
@Test
public void testDateInvalid()
{
form.setReadyDate("invalid ready date");
ActionErrors errors = delegator.validate(null, null);
System.out.println(errors);
String validationMessage = getValidationMessage(errors, "readyDate");
assert "Value 'invalid ready date' is an invalid date. Use the format 'd MMM yyyy' (e.g. 19 October 2005)"
.equals(validationMessage);
}
@Test
public void testDateEmpty()
{
form.setReadyDate("");
// we still okay with this at the moment
assert null == delegator.validate(null, null);
}
@Test
public void testMultiple()
{
form.setProjectName("");
form.setReadyDate("aaa");
// we still okay with this at the moment
ActionErrors validate = delegator.validate(null, null);
for (Iterator iterator = validate.get(); iterator.hasNext();)
{
ActionMessage message = (ActionMessage) iterator.next();
System.out.println("Message 1: " + message);
}
}
}