Author: niallp Date: Thu Nov 10 22:50:39 2005 New Revision: 332476 URL: http://svn.apache.org/viewcvs?rev=332476&view=rev Log: Fix Bug 18811 Misleading error message in ConvertingWrapDynaBean (and also in WrapDynaBean) - reported by Aslak Hellesøy.
Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertingWrapDynaBean.java jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertingWrapDynaBean.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertingWrapDynaBean.java?rev=332476&r1=332475&r2=332476&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertingWrapDynaBean.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertingWrapDynaBean.java Thu Nov 10 22:50:39 2005 @@ -1,5 +1,5 @@ /* - * Copyright 2001-2004 The Apache Software Foundation. + * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.apache.commons.beanutils; +import java.lang.reflect.InvocationTargetException; /** * <p>Implementation of <code>DynaBean</code> that wraps a standard JavaBean @@ -56,20 +57,22 @@ * @param name Name of the property whose value is to be set * @param value Value to which this property is to be set * - * @exception ConversionException if the specified value cannot be - * converted to the type required for this property - * @exception IllegalArgumentException if there is no property - * of the specified name - * @exception NullPointerException if an attempt is made to set a - * primitive property to null + * @exception IllegalArgumentException if there are any problems + * copying the property. */ public void set(String name, Object value) { try { BeanUtils.copyProperty(instance, name, value); + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error setting property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no write method"); + ("Error setting property '" + name + + "', exception - " + t); } } Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java?rev=332476&r1=332475&r2=332476&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java Thu Nov 10 22:50:39 2005 @@ -18,7 +18,7 @@ package org.apache.commons.beanutils; import java.io.Serializable; - +import java.lang.reflect.InvocationTargetException; /** @@ -115,9 +115,15 @@ Object value = null; try { value = PropertyUtils.getSimpleProperty(instance, name); + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error reading property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no read method"); + ("Error reading property '" + name + + "', exception - " + t); } return (value); @@ -146,9 +152,15 @@ value = PropertyUtils.getIndexedProperty(instance, name, index); } catch (IndexOutOfBoundsException e) { throw e; + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error reading indexed property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no indexed read method"); + ("Error reading indexed property '" + name + + "', exception - " + t); } return (value); @@ -172,9 +184,15 @@ Object value = null; try { value = PropertyUtils.getMappedProperty(instance, name, key); + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error reading mapped property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no mapped read method"); + ("Error reading mapped property '" + name + + "', exception - " + t); } return (value); @@ -233,9 +251,15 @@ try { PropertyUtils.setSimpleProperty(instance, name, value); + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error setting property '" + name + + "' nested exception -" + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no write method"); + ("Error setting property '" + name + + "', exception - " + t); } } @@ -263,9 +287,15 @@ PropertyUtils.setIndexedProperty(instance, name, index, value); } catch (IndexOutOfBoundsException e) { throw e; + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error setting indexed property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no indexed write method"); + ("Error setting indexed property '" + name + + "', exception - " + t); } } @@ -289,9 +319,15 @@ try { PropertyUtils.setMappedProperty(instance, name, key, value); + } catch (InvocationTargetException ite) { + Throwable cause = ite.getTargetException(); + throw new IllegalArgumentException + ("Error setting mapped property '" + name + + "' nested exception - " + cause); } catch (Throwable t) { throw new IllegalArgumentException - ("Property '" + name + "' has no mapped write method"); + ("Error setting mapped property '" + name + + "', exception - " + t); } } Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java?rev=332476&r1=332475&r2=332476&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java Thu Nov 10 22:50:39 2005 @@ -93,6 +93,86 @@ /** + * The <code>set()</code> method. + */ + public void testSimpleProperties() { + + // Invalid getter + try { + Object result = bean.get("invalidProperty"); + fail("Invalid get should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException t) { + ; // Expected result + } + + // Invalid setter + try { + bean.set("invalidProperty", "XYZ"); + fail("Invalid set should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException t) { + ; // Expected result + } + + // Set up initial Value + String testValue = "Original Value"; + String testProperty = "stringProperty"; + TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance(); + instance.setStringProperty(testValue); + assertEquals("Check String property", testValue, instance.getStringProperty()); + + // Test Valid Get & Set + try { + testValue = "Some new value"; + bean.set(testProperty, testValue); + assertEquals("Test Set", testValue, instance.getStringProperty()); + assertEquals("Test Get", testValue, bean.get(testProperty)); + } catch (IllegalArgumentException t) { + fail("Get threw exception: " + t); + } + + } + + /** + * The <code>set()</code> method. + */ + public void testIndexedProperties() { + + // Invalid getter + try { + Object result = bean.get("invalidProperty", 0); + fail("Invalid get should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException t) { + ; // Expected result + } + + // Invalid setter + try { + bean.set("invalidProperty", 0, "XYZ"); + fail("Invalid set should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException t) { + ; // Expected result + } + + // Set up initial Value + String testValue = "Original Value"; + String testProperty = "stringIndexed"; + TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance(); + instance.setStringIndexed(0, testValue); + assertEquals("Check String property", testValue, instance.getStringIndexed(0)); + + // Test Valid Get & Set + try { + testValue = "Some new value"; + bean.set(testProperty, 0, testValue); + assertEquals("Test Set", testValue, instance.getStringIndexed(0)); + assertEquals("Test Get", testValue, bean.get(testProperty, 0)); + } catch (IllegalArgumentException t) { + fail("Get threw exception: " + t); + } + + } + + /** * The <code>contains()</code> method is not supported by the * <code>WrapDynaBean</code> implementation class. */ --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]