001: package org.strecks.validator.strategy;
002:
003: import static org.testng.Assert.assertEquals;
004:
005: import java.sql.Date;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import org.apache.struts.action.ActionErrors;
011: import org.apache.struts.action.ActionMessage;
012: import org.strecks.converter.ConversionState;
013: import org.strecks.exceptions.ApplicationRuntimeException;
014: import org.strecks.form.controller.BadlyTypedValidatorForm;
015: import org.strecks.form.controller.DelegatingForm;
016: import org.strecks.form.controller.FormTestUtils;
017: import org.strecks.form.impl.SimpleStrutsForm;
018: import org.strecks.form.strategy.impl.ExtendedProjectForm1;
019: import org.strecks.form.strategy.impl.ProjectForm;
020: import org.strecks.validator.IntegerValidator;
021: import org.strecks.validator.internal.ValidationInfo;
022: import org.strecks.web.form.AbstractTestFormValidation;
023: import org.testng.Assert;
024: import org.testng.annotations.BeforeMethod;
025: import org.testng.annotations.Test;
026:
027: /**
028: * @author Phil Zoio
029: */
030: public class TestDefaultValidationStrategy extends
031: AbstractTestFormValidation {
032:
033: private ProjectForm form;
034:
035: private ValidationInfo validationInfo;
036:
037: private DefaultValidationStrategy strategy;
038:
039: private Map<String, Object> convertedValues;
040:
041: @BeforeMethod
042: public void beforeTest() {
043:
044: form = new ProjectForm();
045: DelegatingForm delegator = FormTestUtils
046: .getDelegatingForm(form);
047:
048: validationInfo = delegator.getValidationInfo();
049: strategy = new DefaultValidationStrategy();
050:
051: convertedValues = new HashMap<String, Object>();
052:
053: }
054:
055: @Test
056: public void testSimpleValidate() {
057: form.setProjectName("project name");
058: form.setReadyDate("15 October 2005");
059: convertedValues.put("readyDate", Date.valueOf("2005-10-15"));
060: assert null == strategy.validate(validationInfo, form, null,
061: null, convertedValues);
062: }
063:
064: @Test
065: public void testMissingData() {
066: form.setReadyDate("duff date");
067: convertedValues.put("readyDate", ConversionState.FAILURE);
068: ActionErrors errors = strategy.validate(validationInfo, form,
069: null, null, convertedValues);
070: assertEquals(getValidationMessage(errors, "projectName"),
071: "Project name is required");
072: assertEquals(
073: getValidationMessage(errors, "readyDate"),
074: "Value 'duff date' is an invalid date. Use the format 'd MMM yyyy' (e.g. 19 October 2005)");
075: }
076:
077: @SuppressWarnings("unchecked")
078: @Test
079: public void testExtendedForm1() {
080: ExtendedProjectForm1 form = new ExtendedProjectForm1();
081: form.setReadyDate("duff date");
082:
083: convertedValues.put("readyDate", ConversionState.FAILURE);
084: ActionErrors errors = strategy.validate(validationInfo, form,
085: null, null, convertedValues);
086:
087: Iterator<ActionMessage> iterator = errors.get();
088: assertEquals(iterator.next().getKey(), "project.name.required");
089: assertEquals(iterator.next().getKey(), "another.error");
090: assertEquals(iterator.next().getKey(),
091: "invalid.full.date.format");
092: }
093:
094: @Test
095: public void testValidate() {
096: strategy.validate(SimpleStrutsForm.class, "validField", 1,
097: new IntegerValidator());
098: }
099:
100: @Test
101: public void testValidateInvalidType() {
102: try {
103: strategy.validate(BadlyTypedValidatorForm.class,
104: "requiredInteger", new Integer(1),
105: new IntegerValidator());
106: } catch (ApplicationRuntimeException e) {
107: Assert
108: .assertEquals(
109: e.getMessage(),
110: "Mismatch between parameterization type of validator: org.strecks.validator.IntegerValidator(class java.lang.String), "
111: + "and type of property being validated: property requiredInteger in "
112: + "class org.strecks.form.controller.BadlyTypedValidatorForm(class java.lang.Integer)");
113: }
114: }
115:
116: }
|