Package org.strecks.validator.internal

Source Code of org.strecks.validator.internal.TestValidationAnnotationReaderImpl

/*
* 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.validator.internal;

import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.strecks.bind.internal.BindConvertInfo;
import org.strecks.converter.Converter;
import org.strecks.converter.SafeBeanUtilsConverter;
import org.strecks.converter.handler.ConversionHandler;
import org.strecks.converter.handler.DefaultConversionHandler;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.form.controller.BadlyTypedValidatorForm;
import org.strecks.validator.IntegerValidator;
import org.strecks.validator.MaxLengthValidator;
import org.strecks.validator.Validator;
import org.strecks.validator.annotation.ValidateInteger;
import org.strecks.validator.internal.impl.BeanWithGetterValidator;
import org.strecks.validator.internal.impl.BeanWithNoGetter;
import org.strecks.validator.internal.impl.BeanWithNoValidators;
import org.strecks.validator.message.DefaultMessageParameterProvider;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestValidationAnnotationReaderImpl
{

  @Test
  public void testNewMethodValidatorsNoConversion() throws Exception
  {
    Converter converter = createStrictMock(Converter.class);

    List<ValidatorWrapper> list = new ArrayList<ValidatorWrapper>();

    ValidatorWrapper v1 = getNonConvertableWrapper(String.class);
    ValidatorWrapper v2 = getNonConvertableWrapper(Object.class);

    list.add(v1);
    list.add(v2);

    replay(converter);

    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    MethodValidators methodValidators = reader.newMethodValidators(this.getClass(), new OrderedProperty("prop", 1),
        list, converter);
    assert !methodValidators.getRequiresConversion();
    assert converter == methodValidators.getConverter();

    verify(converter);
  }

  @Test
  public void testNewMethodValidatorsWithConvesion() throws Exception
  {

    Converter converter = createStrictMock(Converter.class);

    List<ValidatorWrapper> list = new ArrayList<ValidatorWrapper>();

    ValidatorWrapper v1 = getConvertableWrapper(Integer.class);
    ValidatorWrapper v2 = getConvertableWrapper(Number.class);
    ValidatorWrapper v3 = getConvertableWrapper(Object.class);

    list.add(v1);
    list.add(v2);
    list.add(v3);

    // record expectation
    converter.setTargetClass(Integer.class);

    replay(converter);

    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    MethodValidators methodValidators = reader.newMethodValidators(this.getClass(), new OrderedProperty("prop", 1),
        list, converter);
    assert methodValidators.getRequiresConversion();
    assert converter == methodValidators.getConverter();

    verify(converter);
  }

  @Test
  public void testAddValidator() throws Exception
  {
    ValidatorWrapper wrapper = createStrictMock(ValidatorWrapper.class);
    ValidationAnnotationReader reader = new ValidationAnnotationReader();

    List<ValidatorWrapper> validators = reader.addValidator(null, wrapper);
    assert validators != null;
    assertEquals(validators.size(), 1);
    assert validators.iterator().next() == wrapper;
  }

  @Test
  public void testAddValidatorEmpty() throws Exception
  {
    ValidatorWrapper wrapper = createStrictMock(ValidatorWrapper.class);
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    List<ValidatorWrapper> validatorsIn = new ArrayList<ValidatorWrapper>();
    List<ValidatorWrapper> validatorsOut = reader.addValidator(validatorsIn, wrapper);

    assert validatorsIn == validatorsOut;

    assert validatorsOut != null;
    assertEquals(validatorsOut.size(), 1);
    assert validatorsOut.iterator().next() == wrapper;
  }

  @Test
  public void testGetConversionHandler() throws Exception
  {
    BindConvertInfo bci = createStrictMock(BindConvertInfo.class);
    ConversionHandler conversionHandler = createStrictMock(ConversionHandler.class);

    ValidationAnnotationReader reader = new ValidationAnnotationReader();

    expect(bci.getConversionHandler()).andReturn(conversionHandler);
    replay(bci);
    replay(conversionHandler);

    assert reader.getConversionHandler(bci) == conversionHandler;

    verify(bci);
    verify(conversionHandler);
  }

  @Test
  public void testGetConversionHandlerNull() throws Exception
  {
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    assert reader.getConversionHandler(null) instanceof DefaultConversionHandler;
  }

  @Test
  public void testGetConverterNull() throws Exception
  {
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    Converter converter = reader.getConverter(this.getClass(), "prop", null);
    assert converter instanceof SafeBeanUtilsConverter;
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testGetConverterNone() throws Exception
  {
    BindConvertInfo info = createStrictMock(BindConvertInfo.class);
    Map converterMap = createStrictMock(Map.class);
    Converter converter = createStrictMock(Converter.class);

    ValidationAnnotationReader reader = new ValidationAnnotationReader();

    expect(info.getConverterMap()).andReturn(converterMap);
    expect(converterMap.get("prop")).andReturn(converter);

    replay(info);
    replay(converterMap);

    assert converter == reader.getConverter(this.getClass(), "prop", info);

    verify(info);
    verify(converterMap);
  }

  @Test
  public void testGetGetter() throws Exception
  {
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    Method setter = BeanWithGetterValidator.class.getMethod("setProperty", new Class[]
    {
      String.class
    });

    assert null != reader.getGetter(setter);
  }

  @Test
  public void testGetNoGetter() throws Exception
  {
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    Method setter = BeanWithNoGetter.class.getMethod("setStringProperty", new Class[]
    {
      String.class
    });

    try
    {
      reader.getGetter(setter);
      Assert.fail("Should have caught " + ApplicationConfigurationException.class);
    }
    catch (ApplicationConfigurationException e)
    {

      Assert.assertEquals(e.getMessage(),
          "Setter method public void org.strecks.validator.internal.impl.BeanWithNoGetter.setStringProperty(java.lang.String) "
              + "has no corresponding getter method");
    }
  }

  @Test
  public void testValidateWithGetterMethodAnnotation()
  {

    try
    {
      BeanWithGetterValidator bean = new BeanWithGetterValidator();
      ValidationAnnotationReader reader = new ValidationAnnotationReader();
      reader.readValidationHandlers(bean, null);
      Assert.fail("Should have caught " + ApplicationConfigurationException.class);
    }
    catch (ApplicationConfigurationException e)
    {
      e.printStackTrace();
    }
  }

  @Test
  public void testBadlyTypedValidatorMethod() throws Exception
  {

    try
    {
      BadlyTypedValidatorForm bean = new BadlyTypedValidatorForm();

      Method method = bean.getClass().getMethod("getRequiredInteger");
      MaxLengthValidator validator = new MaxLengthValidator();

      ValidationAnnotationReader reader = new ValidationAnnotationReader();
      reader.checkValidatorType("propertyName", method, validator.getClass(), Validator.class,
          ValidateInteger.class);

      Assert.fail("Should have caught " + ApplicationConfigurationException.class);

    }
    catch (ApplicationConfigurationException e)
    {
      Assert
          .assertEquals(
              e.getMessage(),
              "Class org.strecks.form.controller.BadlyTypedValidatorForm has property propertyName whose return type java.lang.Integer does not match the type java.lang.String of the validator used by annotation @ValidateInteger");

    }
  }

  @Test
  public void testBeanWithNoAnnotations() throws Exception
  {
    ValidationAnnotationReader reader = new ValidationAnnotationReader();
    ValidationInfo info = reader.readValidationHandlers(new BeanWithNoValidators(), null);

    Map<OrderedProperty, MethodValidators> map = info.getValidators();
    assert map != null;
    assert map.size() == 0;
  }

  private ValidatorWrapper getConvertableWrapper(Class<?> type)
  {
    DefaultMessageParameterProvider provider = new DefaultMessageParameterProvider();
    ValidatorWrapper wrapper = new ValidatorWrapper("key1", 20, Collections.emptyList(), new IntegerValidator(),
        this.getClass().getMethods()[0], provider, true, type);
    return wrapper;
  }

  private ValidatorWrapper getNonConvertableWrapper(Class<?> type)
  {
    DefaultMessageParameterProvider provider = new DefaultMessageParameterProvider();
    ValidatorWrapper wrapper = new ValidatorWrapper("key1", 20, Collections.emptyList(), new IntegerValidator(),
        this.getClass().getMethods()[0], provider);
    return wrapper;
  }

}
TOP

Related Classes of org.strecks.validator.internal.TestValidationAnnotationReaderImpl

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.