代码改变世界

BeanUtils设置字段值失败问题

2016-01-27 14:18  chen.simon  阅读(955)  评论(0编辑  收藏  举报
package org.apache.commons.beanutils;

import static org.junit.Assert.*;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  Chenxiaguang
 * @version  [版本号, 2016-1-27]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class BeanUtilsTest
{

    /**
     * Test method for {@link org.apache.commons.beanutils.BeanUtils#setProperty(java.lang.Object, java.lang.String, java.lang.Object)}.
     */
    @Test
    public void testSetProperty()
    {
        TestBeanA testBeanA = new TestBeanA();
        try
        {
            BeanInfo beanInfo = null;
            try {
                Class<?> beanClass = TestBeanA.class;
                beanInfo = Introspector.getBeanInfo(beanClass);
            } catch (IntrospectionException e) {
                return;
            }
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            BeanUtils.setProperty(testBeanA, "Aac", "a");// fail
            BeanUtils.setProperty(testBeanA, "aac", "a");// ok
            BeanUtils.setProperty(testBeanA, "aBc", "b");// fail
            BeanUtils.setProperty(testBeanA, "ABc", "b");// ok
            BeanUtils.setProperty(testBeanA, "acc", "b");// ok
            BeanUtils.setProperty(testBeanA, "D", "b");// fail
            BeanUtils.setProperty(testBeanA, "d", "b");// ok
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
    }

}

 

用于测试的bean

package org.apache.commons.beanutils;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  Chenxiaguang
 * @version  [版本号, 2016-1-27]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class TestBeanA
{

    private String Aac;
    
    private String ABc;
    
    private String acc;
    
    private String D;

    public String getAac()
    {
        return Aac;
    }

    public void setAac(String aac)
    {
        Aac = aac;
    }

    public String getABc()
    {
        return ABc;
    }

    public void setABc(String aBc)
    {
        ABc = aBc;
    }

    public String getAcc()
    {
        return acc;
    }

    public void setAcc(String acc)
    {
        this.acc = acc;
    }

    public String getD()
    {
        return D;
    }

    public void setD(String d)
    {
        D = d;
    }
    
    
}
--EOF--