001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.web.form;
016:
017: import java.util.Iterator;
018:
019: import org.apache.struts.action.ActionErrors;
020: import org.apache.struts.action.ActionMessage;
021: import org.strecks.form.controller.DelegatingForm;
022: import org.strecks.form.controller.FormTestUtils;
023: import org.strecks.web.form.impl.NewProjectForm;
024: import org.testng.annotations.BeforeMethod;
025: import org.testng.annotations.Test;
026:
027: /**
028: * @author Phil Zoio
029: */
030: public class TestProjectFormValidation extends
031: AbstractTestFormValidation {
032:
033: private NewProjectForm form;
034:
035: private DelegatingForm delegator;
036:
037: @BeforeMethod
038: public void beforeTest() {
039:
040: form = new NewProjectForm();
041:
042: form.setProjectName("project name");
043: form.setSelectedProjectType("1");
044: form.setReadyDate("15 October 2005");
045:
046: delegator = FormTestUtils.getDelegatingForm(form);
047:
048: }
049:
050: @Test
051: public void testValidationOK() {
052:
053: assert null == delegator.validate(null, null);
054:
055: }
056:
057: @Test
058: public void testProjectMissingData() {
059: form.setProjectName(null);
060: form.setSelectedProjectType("");
061: ActionErrors errors = delegator.validate(null, null);
062: assert "Project name is required".equals(getValidationMessage(
063: errors, "projectName"));
064: assert "A non-empty project type must be selected"
065: .equals(getValidationMessage(errors,
066: "selectedProjectType"));
067: }
068:
069: @Test
070: public void testProjectNameEmpty() {
071: form.setProjectName("");
072: ActionErrors errors = delegator.validate(null, null);
073: assert "Project name is required".equals(getValidationMessage(
074: errors, "projectName"));
075: }
076:
077: @Test
078: public void testDateInvalid() {
079: form.setReadyDate("invalid ready date");
080:
081: ActionErrors errors = delegator.validate(null, null);
082: System.out.println(errors);
083: String validationMessage = getValidationMessage(errors,
084: "readyDate");
085: assert "Value 'invalid ready date' is an invalid date. Use the format 'd MMM yyyy' (e.g. 19 October 2005)"
086: .equals(validationMessage);
087: }
088:
089: @Test
090: public void testDateEmpty() {
091: form.setReadyDate("");
092:
093: // we still okay with this at the moment
094: assert null == delegator.validate(null, null);
095: }
096:
097: @Test
098: public void testMultiple() {
099: form.setProjectName("");
100: form.setReadyDate("aaa");
101:
102: // we still okay with this at the moment
103: ActionErrors validate = delegator.validate(null, null);
104:
105: for (Iterator iterator = validate.get(); iterator.hasNext();) {
106: ActionMessage message = (ActionMessage) iterator.next();
107: System.out.println("Message 1: " + message);
108: }
109: }
110:
111: }
|