Package org.strecks.converter

Source Code of org.strecks.converter.TestBind

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

import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;
import org.testng.annotations.Test;

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

  @Test
  public void testSimpleBinding() throws Exception
  {

    NestedBindableBean nested = new NestedBindableBean();
    BindableBean bindable = new BindableBean();

    bindable.setNestedBindableBean(nested);
    bindable.setIntegerValue("5");

    Object integerValue = BeanUtilsBean.getInstance().getConvertUtils().convert(bindable.getIntegerValue(),
        Integer.class);
    PropertyUtils.setProperty(bindable, "nestedBindableBean.integerValue", integerValue);

    assert nested.getIntegerValue() == 5;

  }

  @Test(expectedExceptions=IllegalArgumentException.class)
  public void testNullBinding() throws Exception
  {

    BindableBean bindable = new BindableBean();

    PropertyUtils.setProperty(bindable, "nestedBindableBean.integerValue", 5);

  }

  @Test
  public void testGetProperty() throws Exception
  {

    NestedBindableBean nested = new NestedBindableBean();
    BindableBean bindable = new BindableBean();

    bindable.setNestedBindableBean(nested);
    bindable.setIntegerValue("5");

    Object property = PropertyUtils.getProperty(bindable, "nestedBindableBean.integerValue");
    System.out.println(property);

    PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(bindable,
        "nestedBindableBean.integerValue");
    System.out.println(propertyDescriptor);

    // now try get property descriptor null
    // bindable.setNestedBindableBean(null);

    propertyDescriptor = PropertyUtils.getPropertyDescriptor(bindable, "nestedBindableBean.integerValue");
    System.out.println(propertyDescriptor);

  }

  @Test(expectedExceptions=NestedNullException.class)
  public void testGetNullProperty() throws Exception
  {
    BindableBean bindable = new BindableBean();
    PropertyUtils.getProperty(bindable, "nestedBindableBean.integerValue");
  }

  @Test
  public void testPropertyClass() throws Exception
  {

    NestedBindableBean nested = new NestedBindableBean();
    BindableBean bindable = new BindableBean();
    bindable.setNestedBindableBean(nested);
    Object targetBean = PropertyUtils.getProperty(bindable, "nestedBindableBean");
    PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(targetBean, "integerValue");

    assert propertyDescriptor.getPropertyType().equals(Integer.class);

  }

  @Test(expectedExceptions=IllegalArgumentException.class)
  public void tryStringBinding() throws Exception
  {

    NestedBindableBean nested = new NestedBindableBean();
    BindableBean bindable = new BindableBean();

    bindable.setNestedBindableBean(nested);
    bindable.setIntegerValue("5");

    // now try binding a string
    bindable.setNestedBindableBean(nested);
    PropertyUtils.setProperty(bindable, "nestedBindableBean.integerValue", "6");

    assert nested.getIntegerValue() == 6;

  }

  @Test
  public void tryBooleanBinding() throws Exception
  {

    BindableBean bindable = new BindableBean();
    bindable.setBooleanValue(Boolean.FALSE);

    // now try binding a string
    PropertyUtils.setProperty(bindable, "booleanValue", null);

    assert bindable.getBooleanValue() == null;

  }

}
TOP

Related Classes of org.strecks.converter.TestBind

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.