craigmcc 01/08/21 16:05:08
Modified: beanutils/src/java/org/apache/commons/beanutils
PropertyUtils.java
beanutils/src/test/org/apache/commons/beanutils
PropertyUtilsTestCase.java
Log:
Add unit tests for indexed properties. Along the way, fix a bug where
ArrayIndexOutOfBoundsException was being reported inconsistently depending
on whether the underlying property was really an array or just had indexed
getter and setter methods.
Revision Changes Path
1.8 +32 -6
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java
Index: PropertyUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PropertyUtils.java 2001/08/21 21:59:02 1.7
+++ PropertyUtils.java 2001/08/21 23:05:08 1.8
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.7 2001/08/21 21:59:02 craigmcc Exp $
- * $Revision: 1.7 $
- * $Date: 2001/08/21 21:59:02 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.8 2001/08/21 23:05:08 craigmcc Exp $
+ * $Revision: 1.8 $
+ * $Date: 2001/08/21 23:05:08 $
*
* ====================================================================
*
@@ -126,7 +126,7 @@
* @author Chris Audley
* @author Rey Fran�ois
* @author Gregor Ra�man
- * @version $Revision: 1.7 $ $Date: 2001/08/21 21:59:02 $
+ * @version $Revision: 1.8 $ $Date: 2001/08/21 23:05:08 $
*/
public class PropertyUtils {
@@ -295,6 +295,8 @@
* @param name <code>propertyname[index]</code> of the property value
* to be extracted
*
+ * @exception ArrayIndexOutOfBoundsException if the specified index
+ * is outside the valid range for the underlying array
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if <code>bean</code> or
@@ -343,6 +345,8 @@
* @param name Simple property name of the property value to be extracted
* @param index Index of the property value to be extracted
*
+ * @exception ArrayIndexOutOfBoundsException if the specified index
+ * is outside the valid range for the underlying array
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if <code>bean</code> or
@@ -376,7 +380,16 @@
if (readMethod != null) {
Object subscript[] = new Object[1];
subscript[0] = new Integer(index);
- return (readMethod.invoke(bean, subscript));
+ try {
+ return (readMethod.invoke(bean, subscript));
+ } catch (InvocationTargetException e) {
+ if (e.getTargetException() instanceof
+ ArrayIndexOutOfBoundsException)
+ throw (ArrayIndexOutOfBoundsException)
+ e.getTargetException();
+ else
+ throw e;
+ }
}
}
@@ -968,6 +981,8 @@
* @param value Value to which the specified property element
* should be set
*
+ * @exception ArrayIndexOutOfBoundsException if the specified index
+ * is outside the valid range for the underlying array
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if <code>bean</code> or
@@ -1018,6 +1033,8 @@
* @param index Index of the property value to be set
* @param value Value to which the indexed property element is to be set
*
+ * @exception ArrayIndexOutOfBoundsException if the specified index
+ * is outside the valid range for the underlying array
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if <code>bean</code> or
@@ -1052,7 +1069,16 @@
Object subscript[] = new Object[2];
subscript[0] = new Integer(index);
subscript[1] = value;
- writeMethod.invoke(bean, subscript);
+ try {
+ writeMethod.invoke(bean, subscript);
+ } catch (InvocationTargetException e) {
+ if (e.getTargetException() instanceof
+ ArrayIndexOutOfBoundsException)
+ throw (ArrayIndexOutOfBoundsException)
+ e.getTargetException();
+ else
+ throw e;
+ }
return;
}
}
1.9 +680 -4
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
Index: PropertyUtilsTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- PropertyUtilsTestCase.java 2001/08/21 21:59:02 1.8
+++ PropertyUtilsTestCase.java 2001/08/21 23:05:08 1.9
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
1.8 2001/08/21 21:59:02 craigmcc Exp $
- * $Revision: 1.8 $
- * $Date: 2001/08/21 21:59:02 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
1.9 2001/08/21 23:05:08 craigmcc Exp $
+ * $Revision: 1.9 $
+ * $Date: 2001/08/21 23:05:08 $
*
* ====================================================================
*
@@ -82,18 +82,24 @@
* <p>So far, this test case has tests for the following methods of the
* <code>PropertyUtils</code> class:</p>
* <ul>
+ * <li>getIndexedProperty(Object,String)</li>
+ * <li>getIndexedProperty(Object,String,int)</li>
* <li>getMappedProperty(Object,String)</li>
+ * <li>getMappedProperty(Object,String,String</li>
* <li>getNestedProperty(Object,String)</li>
* <li>getPropertyDescriptor(Object,String)</li>
* <li>getPropertyDescriptors(Object)</li>
* <li>getSimpleProperty(Object,String)</li>
+ * <li>setIndexedProperty(Object,String,Object)</li>
+ * <li>setIndexedProperty(Object,String,String,Object)</li>
* <li>setMappedProperty(Object,String,Object)</li>
+ * <li>setMappedProperty(Object,String,String,Object)</li>
* <li>setNestedProperty(Object,String,Object)</li>
* <li>setSimpleProperty(Object,String,Object)</li>
* </ul>
*
* @author Craig R. McClanahan
- * @version $Revision: 1.8 $ $Date: 2001/08/21 21:59:02 $
+ * @version $Revision: 1.9 $ $Date: 2001/08/21 23:05:08 $
*/
public class PropertyUtilsTestCase extends TestCase {
@@ -433,6 +439,326 @@
/**
+ * Corner cases on getIndexedProperty invalid arguments.
+ */
+ public void testGetIndexedArguments() {
+
+ // Use explicit index argument
+
+ try {
+ PropertyUtils.getIndexedProperty(null, "intArray", 0);
+ fail("Should throw IllegalArgumentException 1");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 1");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, null, 0);
+ fail("Should throw IllegalArgumentException 2");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 2");
+ }
+
+ // Use index expression
+
+ try {
+ PropertyUtils.getIndexedProperty(null,
+ "intArray[0]");
+ fail("Should throw IllegalArgumentException 3");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 3");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, "[0]");
+ fail("Should throw NoSuchMethodException 4");
+ } catch (NoSuchMethodException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of NoSuchMethodException 4");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, "intArray");
+ fail("Should throw IllegalArgumentException 5");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 5");
+ }
+
+ // Use explicit index argument
+
+ try {
+ PropertyUtils.getIndexedProperty(null, "intIndexed", 0);
+ fail("Should throw IllegalArgumentException 1");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 1");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, null, 0);
+ fail("Should throw IllegalArgumentException 2");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 2");
+ }
+
+ // Use index expression
+
+ try {
+ PropertyUtils.getIndexedProperty(null,
+ "intIndexed[0]");
+ fail("Should throw IllegalArgumentException 3");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 3");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, "[0]");
+ fail("Should throw NoSuchMethodException 4");
+ } catch (NoSuchMethodException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of NoSuchMethodException 4");
+ }
+
+ try {
+ PropertyUtils.getIndexedProperty(bean, "intIndexed");
+ fail("Should throw IllegalArgumentException 5");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 5");
+ }
+
+ }
+
+
+ /**
+ * Positive and negative tests on getIndexedProperty valid arguments.
+ */
+ public void testGetIndexedValues() {
+
+ Object value = null;
+
+ // Use explicit key argument
+
+ for (int i = 0; i < 5; i++) {
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean, "intArray", i);
+ assertNotNull("intArray returned value " + i, value);
+ assertTrue("intArray returned Integer " + i,
+ value instanceof Integer);
+ assertEquals("intArray returned correct " + i, i * 10,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("intArray " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean, "intIndexed", i);
+ assertNotNull("intIndexed returned value " + i, value);
+ assertTrue("intIndexed returned Integer " + i,
+ value instanceof Integer);
+ assertEquals("intIndexed returned correct " + i, i * 10,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("intIndexed " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean, "stringArray", i);
+ assertNotNull("stringArray returned value " + i, value);
+ assertTrue("intArray returned String " + i,
+ value instanceof String);
+ assertEquals("stringArray returned correct " + i,
+ "String " + i, (String) value);
+ } catch (Throwable t) {
+ fail("stringArray " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean, "stringIndexed", i);
+ assertNotNull("stringIndexed returned value " + i, value);
+ assertTrue("intArray returned String " + i,
+ value instanceof String);
+ assertEquals("stringIndexed returned correct " + i,
+ "String " + i, (String) value);
+ } catch (Throwable t) {
+ fail("stringIndexed " + i + " threw " + t);
+ }
+
+ }
+
+ // Use key expression
+
+ for (int i = 0; i < 5; i++) {
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intArray[" + i + "]");
+ assertNotNull("intArray returned value " + i, value);
+ assertTrue("intArray returned Integer " + i,
+ value instanceof Integer);
+ assertEquals("intArray returned correct " + i, i * 10,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("intArray " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intIndexed[" + i + "]");
+ assertNotNull("intIndexed returned value " + i, value);
+ assertTrue("intIndexed returned Integer " + i,
+ value instanceof Integer);
+ assertEquals("intIndexed returned correct " + i, i * 10,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("intIndexed " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray[" + i + "]");
+ assertNotNull("stringArray returned value " + i, value);
+ assertTrue("intArray returned String " + i,
+ value instanceof String);
+ assertEquals("stringArray returned correct " + i,
+ "String " + i, (String) value);
+ } catch (Throwable t) {
+ fail("stringArray " + i + " threw " + t);
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringIndexed[" + i + "]");
+ assertNotNull("stringIndexed returned value " + i, value);
+ assertTrue("intArray returned String " + i,
+ value instanceof String);
+ assertEquals("stringIndexed returned correct " + i,
+ "String " + i, (String) value);
+ } catch (Throwable t) {
+ fail("stringIndexed " + i + " threw " + t);
+ }
+
+ }
+
+ // Index out of bounds tests
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intArray", -1);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intArray", 5);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intIndexed", -1);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intIndexed", 5);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray", -1);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray", 5);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringIndexed", -1);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringIndexed", 5);
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ }
+
+
+ /**
* Corner cases on getMappedProperty invalid arguments.
*/
public void testGetMappedArguments() {
@@ -1298,6 +1624,356 @@
public void testGetWriteMethodPublicSubclass() {
testGetWriteMethod(beanPublicSubclass, properties, TEST_BEAN_CLASS);
+
+ }
+
+
+ /**
+ * Corner cases on setIndexedProperty invalid arguments.
+ */
+ public void testSetIndexedArguments() {
+
+ // Use explicit index argument
+
+ try {
+ PropertyUtils.setIndexedProperty(null, "intArray", 0,
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 1");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 1");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, null, 0,
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 2");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 2");
+ }
+
+ // Use index expression
+
+ try {
+ PropertyUtils.setIndexedProperty(null,
+ "intArray[0]",
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 3");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 3");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, "[0]",
+ new Integer(1));
+ fail("Should throw NoSuchMethodException 4");
+ } catch (NoSuchMethodException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of NoSuchMethodException 4");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, "intArray",
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 5");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 5");
+ }
+
+ // Use explicit index argument
+
+ try {
+ PropertyUtils.setIndexedProperty(null, "intIndexed", 0,
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 1");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 1");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, null, 0,
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 2");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 2");
+ }
+
+ // Use index expression
+
+ try {
+ PropertyUtils.setIndexedProperty(null,
+ "intIndexed[0]",
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 3");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 3");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, "[0]",
+ new Integer(1));
+ fail("Should throw NoSuchMethodException 4");
+ } catch (NoSuchMethodException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of NoSuchMethodException 4");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean, "intIndexed",
+ new Integer(1));
+ fail("Should throw IllegalArgumentException 5");
+ } catch (IllegalArgumentException e) {
+ ; // Expected response
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of IllegalArgumentException 5");
+ }
+
+ }
+
+
+ /**
+ * Positive and negative tests on setIndexedProperty valid arguments.
+ */
+ public void testSetIndexedValues() {
+
+ Object value = null;
+
+ // Use explicit index argument
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intArray", 0,
+ new Integer(1));
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intArray", 0);
+ assertNotNull("Returned new value 0", value);
+ assertTrue("Returned Integer new value 0",
+ value instanceof Integer);
+ assertEquals("Returned correct new value 0", 1,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intIndexed", 1,
+ new Integer(11));
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intIndexed", 1);
+ assertNotNull("Returned new value 1", value);
+ assertTrue("Returned Integer new value 1",
+ value instanceof Integer);
+ assertEquals("Returned correct new value 1", 11,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray", 2,
+ "New Value 2");
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray", 2);
+ assertNotNull("Returned new value 2", value);
+ assertTrue("Returned String new value 2",
+ value instanceof String);
+ assertEquals("Returned correct new value 2", "New Value 2",
+ (String) value);
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray", 3,
+ "New Value 3");
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray", 3);
+ assertNotNull("Returned new value 3", value);
+ assertTrue("Returned String new value 3",
+ value instanceof String);
+ assertEquals("Returned correct new value 3", "New Value 3",
+ (String) value);
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ // Use index expression
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intArray[4]",
+ new Integer(1));
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intArray[4]");
+ assertNotNull("Returned new value 4", value);
+ assertTrue("Returned Integer new value 4",
+ value instanceof Integer);
+ assertEquals("Returned correct new value 4", 1,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intIndexed[3]",
+ new Integer(11));
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "intIndexed[3]");
+ assertNotNull("Returned new value 5", value);
+ assertTrue("Returned Integer new value 5",
+ value instanceof Integer);
+ assertEquals("Returned correct new value 5", 11,
+ ((Integer) value).intValue());
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray[1]",
+ "New Value 2");
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray[2]");
+ assertNotNull("Returned new value 6", value);
+ assertTrue("Returned String new value 6",
+ value instanceof String);
+ assertEquals("Returned correct new value 6", "New Value 2",
+ (String) value);
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray[0]",
+ "New Value 3");
+ value =
+ PropertyUtils.getIndexedProperty(bean,
+ "stringArray[0]");
+ assertNotNull("Returned new value 7", value);
+ assertTrue("Returned String new value 7",
+ value instanceof String);
+ assertEquals("Returned correct new value 7", "New Value 3",
+ (String) value);
+ } catch (Throwable t) {
+ fail("Threw " + t);
+ }
+
+ // Index out of bounds tests
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intArray", -1,
+ new Integer(0));
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intArray", 5,
+ new Integer(0));
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intIndexed", -1,
+ new Integer(0));
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "intIndexed", 5,
+ new Integer(0));
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray", -1,
+ "New String");
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringArray", 5,
+ "New String");
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringIndexed", -1,
+ "New String");
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
+
+ try {
+ PropertyUtils.setIndexedProperty(bean,
+ "stringIndexed", 5,
+ "New String");
+ fail("Should have thrown ArrayIndexOutOfBoundsException");
+ } catch (ArrayIndexOutOfBoundsException t) {
+ ; // Expected results
+ } catch (Throwable t) {
+ fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
+ }
}