Package org.strecks.validator.strategy

Source Code of org.strecks.validator.strategy.TestDefaultValidationStrategy

package org.strecks.validator.strategy;

import static org.testng.Assert.assertEquals;

import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.strecks.converter.ConversionState;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.form.controller.BadlyTypedValidatorForm;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.form.controller.FormTestUtils;
import org.strecks.form.impl.SimpleStrutsForm;
import org.strecks.form.strategy.impl.ExtendedProjectForm1;
import org.strecks.form.strategy.impl.ProjectForm;
import org.strecks.validator.IntegerValidator;
import org.strecks.validator.internal.ValidationInfo;
import org.strecks.web.form.AbstractTestFormValidation;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestDefaultValidationStrategy extends AbstractTestFormValidation
{

  private ProjectForm form;

  private ValidationInfo validationInfo;

  private DefaultValidationStrategy strategy;
 
  private Map<String, Object> convertedValues;

  @BeforeMethod
  public void beforeTest()
  {

    form = new ProjectForm();
    DelegatingForm delegator = FormTestUtils.getDelegatingForm(form);

    validationInfo = delegator.getValidationInfo();
    strategy = new DefaultValidationStrategy();

    convertedValues = new HashMap<String, Object>();
   
  }

  @Test
  public void testSimpleValidate()
  {
    form.setProjectName("project name");
    form.setReadyDate("15 October 2005");
    convertedValues.put("readyDate", Date.valueOf("2005-10-15"));
    assert null == strategy.validate(validationInfo, form, null, null, convertedValues);
  }

  @Test
  public void testMissingData()
  {
    form.setReadyDate("duff date");
    convertedValues.put("readyDate", ConversionState.FAILURE);
    ActionErrors errors = strategy.validate(validationInfo, form, null, null, convertedValues);
    assertEquals(getValidationMessage(errors, "projectName"), "Project name is required");
    assertEquals(getValidationMessage(errors, "readyDate"),
        "Value 'duff date' is an invalid date. Use the format 'd MMM yyyy' (e.g. 19 October 2005)");
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testExtendedForm1()
  {
    ExtendedProjectForm1 form = new ExtendedProjectForm1();
    form.setReadyDate("duff date");

    convertedValues.put("readyDate", ConversionState.FAILURE);
    ActionErrors errors = strategy.validate(validationInfo, form, null, null, convertedValues);

    Iterator<ActionMessage> iterator = errors.get();
    assertEquals(iterator.next().getKey(), "project.name.required");
    assertEquals(iterator.next().getKey(), "another.error");
    assertEquals(iterator.next().getKey(), "invalid.full.date.format");
  }

  @Test
  public void testValidate()
  {
    strategy.validate(SimpleStrutsForm.class, "validField", 1, new IntegerValidator());
  }

  @Test
  public void testValidateInvalidType()
  {
    try
    {
      strategy.validate(BadlyTypedValidatorForm.class, "requiredInteger", new Integer(1), new IntegerValidator());
    }
    catch (ApplicationRuntimeException e)
    {
      Assert.assertEquals(e.getMessage(),
          "Mismatch between parameterization type of validator: org.strecks.validator.IntegerValidator(class java.lang.String), "
              + "and type of property being validated: property requiredInteger in "
              + "class org.strecks.form.controller.BadlyTypedValidatorForm(class java.lang.Integer)");
    }
  }

}
TOP

Related Classes of org.strecks.validator.strategy.TestDefaultValidationStrategy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.