Search in sources :

Example 1 with BeanUtilsBean

use of org.apache.commons.beanutils.BeanUtilsBean in project checkstyle by checkstyle.

the class AutomaticBean method createBeanUtilsBean.

/**
     * Creates a BeanUtilsBean that is configured to use
     * type converters that throw a ConversionException
     * instead of using the default value when something
     * goes wrong.
     *
     * @return a configured BeanUtilsBean
     */
private static BeanUtilsBean createBeanUtilsBean() {
    final ConvertUtilsBean cub = new ConvertUtilsBean();
    registerIntegralTypes(cub);
    registerCustomTypes(cub);
    return new BeanUtilsBean(cub, new PropertyUtilsBean());
}
Also used : BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) ConvertUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean)

Example 2 with BeanUtilsBean

use of org.apache.commons.beanutils.BeanUtilsBean in project databus by linkedin.

the class TestConfigManager method testPaddedInt.

@Test
public void testPaddedInt() throws Exception {
    ConvertUtilsBean convertUtils = new ConvertUtilsBean();
    Integer intValue = (Integer) convertUtils.convert("456", int.class);
    assertEquals("correct int value", 456, intValue.intValue());
    BeanUtilsBean beanUtils = new BeanUtilsBean();
    PropertyDescriptor propDesc = beanUtils.getPropertyUtils().getPropertyDescriptor(_configBuilder, "intSetting");
    assertEquals("correct setting type", int.class, propDesc.getPropertyType());
    _configManager.setSetting("com.linkedin.databus2.intSetting", " 123 ");
    DynamicConfig config = _configManager.getReadOnlyConfig();
    assertEquals("correct int value", 123, config.getIntSetting());
}
Also used : BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) PropertyDescriptor(java.beans.PropertyDescriptor) ConvertUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean) Test(org.testng.annotations.Test)

Example 3 with BeanUtilsBean

use of org.apache.commons.beanutils.BeanUtilsBean in project head by mifos.

the class ConversionUtil method populateBusinessObject.

public static void populateBusinessObject(ActionForm actionForm, AbstractBusinessObject object, Locale locale) throws ValueObjectConversionException {
    try {
        if (null != object) {
            ConvertUtilsBean conBean = new ConvertUtilsBean();
            MifosSqlDateConverter converter = new MifosSqlDateConverter();
            MifosDoubleConverter mifosDoubleConverter = new MifosDoubleConverter();
            MifosStringToJavaUtilDateConverter stringToJavaDateConverter = new MifosStringToJavaUtilDateConverter();
            converter.setLocale(locale);
            conBean.register(stringToJavaDateConverter, java.util.Date.class);
            conBean.register(converter, java.sql.Date.class);
            conBean.register(mifosDoubleConverter, Double.class);
            BeanUtilsBean bean = new BeanUtilsBean(conBean, BeanUtilsBean.getInstance().getPropertyUtils());
            bean.copyProperties(object, actionForm);
        } else {
            throw new IllegalArgumentException("business object was null");
        }
    } catch (InvocationTargetException e) {
        throw new ValueObjectConversionException(e);
    } catch (IllegalAccessException e) {
        throw new ValueObjectConversionException(e);
    } catch (Exception e) {
        throw new ValueObjectConversionException(e);
    }
}
Also used : BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) ValueObjectConversionException(org.mifos.framework.exceptions.ValueObjectConversionException) ConvertUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValueObjectConversionException(org.mifos.framework.exceptions.ValueObjectConversionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with BeanUtilsBean

use of org.apache.commons.beanutils.BeanUtilsBean in project apex-core by apache.

the class InjectConfigTest method testBeanUtils.

@Test
public void testBeanUtils() throws Exception {
    // http://www.cowtowncoder.com/blog/archives/2011/02/entry_440.html
    BeanUtilsTestBean testBean = new BeanUtilsTestBean();
    testBean.url = new URL("http://localhost:12345/context");
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> properties = mapper.convertValue(testBean, Map.class);
    LOG.debug("testBean source: {}", properties);
    BeanUtilsTestBean testBean2 = new BeanUtilsTestBean();
    testBean2.string2 = "testBean2";
    Assert.assertFalse("contains transientProperty", properties.containsKey("transientProperty"));
    Assert.assertTrue("contains string2", properties.containsKey("string2"));
    // remove null
    properties.remove("string2");
    //properties.put("string3", "");
    BeanUtilsBean bub = new BeanUtilsBean();
    try {
        bub.getProperty(testBean, "invalidProperty");
        Assert.fail("exception expected");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Unknown property 'invalidProperty'"));
    }
    bub.setProperty(properties, "mapProperty.someKey", "someValue");
    JsonNode sourceTree = mapper.convertValue(testBean2, JsonNode.class);
    JsonNode updateTree = mapper.convertValue(properties, JsonNode.class);
    merge(sourceTree, updateTree);
    //   mapper.readerForUpdating(testBean2).readValue(sourceTree);
    //   Assert.assertEquals("preserve existing value", "testBean2", testBean2.string2);
    //   Assert.assertEquals("map property", "someValue", testBean2.mapProperty.get("someKey"));
    //   LOG.debug("testBean cloned: {}", mapper.convertValue(testBean2, Map.class));
    PropertyUtilsBean propertyUtilsBean = BeanUtilsBean.getInstance().getPropertyUtils();
    // set value on non-existing property
    try {
        propertyUtilsBean.setProperty(testBean, "nonExistingProperty.someProperty", "ddd");
        Assert.fail("should throw exception");
    } catch (NoSuchMethodException e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Unknown property 'nonExistingProperty'"));
    }
    // set value on read-only property
    try {
        testBean.getMapProperty().put("s", "s1Val");
        PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean, "mapProperty");
        Class<?> type = propertyUtilsBean.getPropertyType(testBean, "mapProperty.s");
        propertyUtilsBean.setProperty(testBean, "mapProperty", Integer.valueOf(1));
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Property 'mapProperty' has no setter method"));
    }
    // type mismatch
    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "s1");
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
    }
    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "1");
    } catch (IllegalArgumentException e) {
        // BeanUtils does not report invalid properties, but it handles type conversion, which above doesn't
        Assert.assertEquals("", 0, testBean.getIntProp());
        // by default beanutils ignores conversion error
        bub.setProperty(testBean, "intProp", "1");
        Assert.assertEquals("", 1, testBean.getIntProp());
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) JsonNode(org.codehaus.jackson.JsonNode) URL(java.net.URL) BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

BeanUtilsBean (org.apache.commons.beanutils.BeanUtilsBean)4 ConvertUtilsBean (org.apache.commons.beanutils.ConvertUtilsBean)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 PropertyUtilsBean (org.apache.commons.beanutils.PropertyUtilsBean)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Test (org.junit.Test)1 ValueObjectConversionException (org.mifos.framework.exceptions.ValueObjectConversionException)1 Test (org.testng.annotations.Test)1