Package org.strecks.util

Source Code of org.strecks.util.TestPropertyValueSetter

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

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.injection.handler.impl.TestAction;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

  private TestAction action;

  @BeforeMethod
  public void setUp()
  {
    action = new TestAction();
  }

  @Test
  public void testInvalidType1() throws Exception
  {
    try
    {
      TestAction action = new TestAction();
      Method setterMethod = getSetter(action, "setIntegerInput", Integer.class);
      PropertyValueSetter.setPropertyValue(action, setterMethod, Integer.class,
          "should be an int but is a String");
    }
    catch (ApplicationRuntimeException e)
    {
      Assert.assertEquals(e.getMessage(), "Application error: could not set "
          + "'should be an int but is a String' (type java.lang.String)"
          + " using method setIntegerInput of "
          + "class org.strecks.injection.handler.impl.TestAction: illegal argument supplied");
    }
  }

  @Test
  public void testInvalidType2()
  {
    try
    {
      TestAction action = new TestAction();
      PropertyValueSetter.setPropertyValue(action, "integerInput", Integer.class,
          "should be an int but is a String");
    }
    catch (ApplicationRuntimeException e)
    {
      Assert.assertEquals(e.getMessage(), "Application error: could not set "
          + "'should be an int but is a String' (type java.lang.String) "
          + "for property integerInput of class "
          + "org.strecks.injection.handler.impl.TestAction: illegal argument supplied");
    }
  }

  @Test
  public void testPropertyValueSetter1() throws Exception
  {
    TestAction action = new TestAction();
    Method setterMethod = getSetter(action, "setIntegerInput", Integer.class);
    PropertyValueSetter.setPropertyValue(action, setterMethod, Integer.class, new Integer(1));
    assert action.getIntegerInput().equals(1);
  }

  @Test
  public void testPropertyValueSetter2() throws Exception
  {
    TestAction action = new TestAction();
    Method setterMethod = getSetter(action, "setIntegerInput", Integer.class);
    PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(setterMethod);

    PropertyValueSetter.setPropertyValue(action, descriptor, new Integer(1));
    assert action.getIntegerInput().equals(1);
  }

  @Test
  public void testPropertyValueSetter3() throws Exception
  {
    TestAction action = new TestAction();
    PropertyValueSetter.setPropertyValue(action, "integerInput", Integer.class, new Integer(1));
    assert action.getIntegerInput().equals(1);
  }

  @Test
  public void testPrimitives1() throws NoSuchMethodException
  {
    Method setterMethod = getSetter(action, "setByteInput", Byte.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Byte.TYPE, null);
    assert action.getByteInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Byte.TYPE, (byte) 1);
    assert action.getByteInput() == 1;

    setterMethod = getSetter(action, "setBooleanInput", Boolean.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Boolean.TYPE, null);
    assert action.isBooleanInput() == false;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Boolean.TYPE, true);
    assert action.isBooleanInput() == true;

    setterMethod = getSetter(action, "setShortInput", Short.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Short.TYPE, null);
    assert action.getShortInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Short.TYPE, (short) 2);
    assert action.getShortInput() == 2;

    setterMethod = getSetter(action, "setIntInput", Integer.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Integer.TYPE, null);
    assert action.getIntInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Integer.TYPE, (int) 3);
    assert action.getIntInput() == 3;

    setterMethod = getSetter(action, "setPrimitiveLongInput", Long.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Long.TYPE, null);
    assert action.getPrimitiveLongInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Long.TYPE, 4L);
    assert action.getPrimitiveLongInput() == 4;

    setterMethod = getSetter(action, "setFloatInput", Float.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Float.TYPE, null);
    assert action.getFloatInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Float.TYPE, 5.0F);
    assert action.getFloatInput() == 5.0F;

    setterMethod = getSetter(action, "setDoubleInput", Double.TYPE);

    PropertyValueSetter.setPropertyValue(action, setterMethod, Double.TYPE, null);
    assert action.getDoubleInput() == 0;

    PropertyValueSetter.setPropertyValue(action, setterMethod, Double.TYPE, 6.2);
    assert action.getDoubleInput() == 6.2;
  }

  @Test
  public void testPrimitives2() throws NoSuchMethodException
  {
    TestAction action = new TestAction();

    PropertyValueSetter.setPropertyValue(action, "byteInput", Byte.TYPE, null);
    assert action.getByteInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "byteInput", Byte.TYPE, (byte) 1);
    assert action.getByteInput() == 1;

    PropertyValueSetter.setPropertyValue(action, "booleanInput", Boolean.TYPE, null);
    assert action.isBooleanInput() == false;

    PropertyValueSetter.setPropertyValue(action, "booleanInput", Boolean.TYPE, true);
    assert action.isBooleanInput() == true;

    PropertyValueSetter.setPropertyValue(action, "shortInput", Short.TYPE, null);
    assert action.getShortInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "shortInput", Short.TYPE, (short) 2);
    assert action.getShortInput() == 2;

    PropertyValueSetter.setPropertyValue(action, "intInput", Integer.TYPE, null);
    assert action.getIntInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "intInput", Integer.TYPE, (int) 3);
    assert action.getIntInput() == 3;

    PropertyValueSetter.setPropertyValue(action, "primitiveLongInput", Long.TYPE, null);
    assert action.getPrimitiveLongInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "primitiveLongInput", Long.TYPE, 4L);
    assert action.getPrimitiveLongInput() == 4;

    PropertyValueSetter.setPropertyValue(action, "floatInput", Float.TYPE, null);
    assert action.getFloatInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "floatInput", Float.TYPE, 5.0F);
    assert action.getFloatInput() == 5.0F;

    PropertyValueSetter.setPropertyValue(action, "doubleInput", Double.TYPE, null);
    assert action.getDoubleInput() == 0;

    PropertyValueSetter.setPropertyValue(action, "doubleInput", Double.TYPE, 6.2);
    assert action.getDoubleInput() == 6.2;
  }

  @Test
  public void testNullPrimitives() throws Exception
  {
    testNullPrimitive("byteInput");
    testNullPrimitive("booleanInput");
    testNullPrimitive("shortInput");
    testNullPrimitive("intInput");
    testNullPrimitive("primitiveLongInput");
    testNullPrimitive("floatInput");
    testNullPrimitive("doubleInput");
  }

  private Method getSetter(TestAction action, String name, Class clazz) throws NoSuchMethodException
  {
    Method setterMethod = action.getClass().getMethod(name, new Class[]
    {
      clazz
    });
    return setterMethod;
  }

  private void testNullPrimitive(String string) throws IllegalAccessException, InvocationTargetException,
      NoSuchMethodException
  {
    try
    {
      PropertyUtils.setProperty(action, string, null);
      Assert.fail(string);
    }
    catch (IllegalArgumentException e)
    {
    }
  }

}
TOP

Related Classes of org.strecks.util.TestPropertyValueSetter

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.