0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.commons.beanutils;
0019:
0020: import java.lang.reflect.InvocationTargetException;
0021: import java.util.ArrayList;
0022: import java.util.HashMap;
0023: import java.util.List;
0024: import java.util.Map;
0025:
0026: import junit.framework.TestCase;
0027: import junit.framework.Test;
0028: import junit.framework.TestSuite;
0029:
0030: /**
0031: * Test accessing DynaBeans transparently via PropertyUtils.
0032: *
0033: * @author Craig R. McClanahan
0034: * @version $Revision: 471138 $ $Date: 2006-11-04 08:06:03 +0000 (Sat, 04 Nov 2006) $
0035: */
0036:
0037: public class DynaPropertyUtilsTestCase extends TestCase {
0038:
0039: // ----------------------------------------------------- Instance Variables
0040:
0041: /**
0042: * The basic test bean for each test.
0043: */
0044: protected DynaBean bean = null;
0045:
0046: /**
0047: * The set of properties that should be described.
0048: */
0049: protected String describes[] = { "booleanProperty",
0050: "booleanSecond", "doubleProperty", "floatProperty",
0051: "intArray", "intIndexed", "intProperty", "listIndexed",
0052: "longProperty", "mappedObjects", "mappedProperty",
0053: "mappedIntProperty", "nested", "nullProperty",
0054: // "readOnlyProperty",
0055: "shortProperty", "stringArray", "stringIndexed",
0056: "stringProperty" };
0057:
0058: /**
0059: * The nested bean pointed at by the "nested" property.
0060: */
0061: protected TestBean nested = null;
0062:
0063: // ----------------------------------------------------------- Constructors
0064:
0065: /**
0066: * Construct a new instance of this test case.
0067: *
0068: * @param name Name of the test case
0069: */
0070: public DynaPropertyUtilsTestCase(String name) {
0071:
0072: super (name);
0073:
0074: }
0075:
0076: // --------------------------------------------------- Overall Test Methods
0077:
0078: /**
0079: * Set up instance variables required by this test case.
0080: */
0081: public void setUp() throws Exception {
0082:
0083: // Instantiate a new DynaBean instance
0084: DynaClass dynaClass = createDynaClass();
0085: bean = dynaClass.newInstance();
0086:
0087: // Initialize the DynaBean's property values (like TestBean)
0088: bean.set("booleanProperty", new Boolean(true));
0089: bean.set("booleanSecond", new Boolean(true));
0090: bean.set("doubleProperty", new Double(321.0));
0091: bean.set("floatProperty", new Float((float) 123.0));
0092: int intArray[] = { 0, 10, 20, 30, 40 };
0093: bean.set("intArray", intArray);
0094: int intIndexed[] = { 0, 10, 20, 30, 40 };
0095: bean.set("intIndexed", intIndexed);
0096: bean.set("intProperty", new Integer(123));
0097: List listIndexed = new ArrayList();
0098: listIndexed.add("String 0");
0099: listIndexed.add("String 1");
0100: listIndexed.add("String 2");
0101: listIndexed.add("String 3");
0102: listIndexed.add("String 4");
0103: bean.set("listIndexed", listIndexed);
0104: bean.set("longProperty", new Long(321));
0105: HashMap mapProperty = new HashMap();
0106: mapProperty.put("First Key", "First Value");
0107: mapProperty.put("Second Key", "Second Value");
0108: bean.set("mapProperty", mapProperty);
0109: HashMap mappedObjects = new HashMap();
0110: mappedObjects.put("First Key", "First Value");
0111: mappedObjects.put("Second Key", "Second Value");
0112: bean.set("mappedObjects", mappedObjects);
0113: HashMap mappedProperty = new HashMap();
0114: mappedProperty.put("First Key", "First Value");
0115: mappedProperty.put("Second Key", "Second Value");
0116: bean.set("mappedProperty", mappedProperty);
0117: HashMap mappedIntProperty = new HashMap();
0118: mappedIntProperty.put("One", new Integer(1));
0119: mappedIntProperty.put("Two", new Integer(2));
0120: bean.set("mappedIntProperty", mappedIntProperty);
0121: nested = new TestBean();
0122: bean.set("nested", nested);
0123: // Property "nullProperty" is not initialized, so it should return null
0124: bean.set("shortProperty", new Short((short) 987));
0125: String stringArray[] = { "String 0", "String 1", "String 2",
0126: "String 3", "String 4" };
0127: bean.set("stringArray", stringArray);
0128: String stringIndexed[] = { "String 0", "String 1", "String 2",
0129: "String 3", "String 4" };
0130: bean.set("stringIndexed", stringIndexed);
0131: bean.set("stringProperty", "This is a string");
0132:
0133: }
0134:
0135: /**
0136: * Return the tests included in this test suite.
0137: */
0138: public static Test suite() {
0139:
0140: return (new TestSuite(DynaPropertyUtilsTestCase.class));
0141:
0142: }
0143:
0144: /**
0145: * Tear down instance variables required by this test case.
0146: */
0147: public void tearDown() {
0148:
0149: bean = null;
0150: nested = null;
0151:
0152: }
0153:
0154: // ------------------------------------------------ Individual Test Methods
0155:
0156: /**
0157: * Test copyProperties() when the origin is a a <code>Map</code>.
0158: */
0159: public void testCopyPropertiesMap() {
0160:
0161: Map map = new HashMap();
0162: map.put("booleanProperty", Boolean.FALSE);
0163: map.put("doubleProperty", new Double(333.0));
0164: map.put("dupProperty",
0165: new String[] { "New 0", "New 1", "New 2" });
0166: map.put("floatProperty", new Float((float) 222.0));
0167: map.put("intArray", new int[] { 0, 100, 200 });
0168: map.put("intProperty", new Integer(111));
0169: map.put("longProperty", new Long(444));
0170: map.put("shortProperty", new Short((short) 555));
0171: map.put("stringProperty", "New String Property");
0172:
0173: try {
0174: PropertyUtils.copyProperties(bean, map);
0175: } catch (Throwable t) {
0176: fail("Threw " + t.toString());
0177: }
0178:
0179: // Scalar properties
0180: assertEquals("booleanProperty", false, ((Boolean) bean
0181: .get("booleanProperty")).booleanValue());
0182: assertEquals("doubleProperty", 333.0, ((Double) bean
0183: .get("doubleProperty")).doubleValue(), 0.005);
0184: assertEquals("floatProperty", (float) 222.0, ((Float) bean
0185: .get("floatProperty")).floatValue(), (float) 0.005);
0186: assertEquals("intProperty", 111, ((Integer) bean
0187: .get("intProperty")).intValue());
0188: assertEquals("longProperty", 444, ((Long) bean
0189: .get("longProperty")).longValue());
0190: assertEquals("shortProperty", (short) 555, ((Short) bean
0191: .get("shortProperty")).shortValue());
0192: assertEquals("stringProperty", "New String Property",
0193: (String) bean.get("stringProperty"));
0194:
0195: // Indexed Properties
0196: String dupProperty[] = (String[]) bean.get("dupProperty");
0197: assertNotNull("dupProperty present", dupProperty);
0198: assertEquals("dupProperty length", 3, dupProperty.length);
0199: assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0200: assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0201: assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0202: int intArray[] = (int[]) bean.get("intArray");
0203: assertNotNull("intArray present", intArray);
0204: assertEquals("intArray length", 3, intArray.length);
0205: assertEquals("intArray[0]", 0, intArray[0]);
0206: assertEquals("intArray[1]", 100, intArray[1]);
0207: assertEquals("intArray[2]", 200, intArray[2]);
0208:
0209: }
0210:
0211: /**
0212: * Test the describe() method.
0213: */
0214: public void testDescribe() {
0215:
0216: Map map = null;
0217: try {
0218: map = PropertyUtils.describe(bean);
0219: } catch (Exception e) {
0220: fail("Threw exception " + e);
0221: }
0222:
0223: // Verify existence of all the properties that should be present
0224: for (int i = 0; i < describes.length; i++) {
0225: assertTrue("Property '" + describes[i] + "' is present",
0226: map.containsKey(describes[i]));
0227: }
0228: assertTrue("Property 'writeOnlyProperty' is not present", !map
0229: .containsKey("writeOnlyProperty"));
0230:
0231: // Verify the values of scalar properties
0232: assertEquals("Value of 'booleanProperty'", Boolean.TRUE, map
0233: .get("booleanProperty"));
0234: assertEquals("Value of 'doubleProperty'", new Double(321.0),
0235: map.get("doubleProperty"));
0236: assertEquals("Value of 'floatProperty'", new Float(
0237: (float) 123.0), map.get("floatProperty"));
0238: assertEquals("Value of 'intProperty'", new Integer(123), map
0239: .get("intProperty"));
0240: assertEquals("Value of 'longProperty'", new Long(321), map
0241: .get("longProperty"));
0242: assertEquals("Value of 'shortProperty'",
0243: new Short((short) 987), map.get("shortProperty"));
0244: assertEquals("Value of 'stringProperty'", "This is a string",
0245: (String) map.get("stringProperty"));
0246:
0247: }
0248:
0249: /**
0250: * Corner cases on getIndexedProperty invalid arguments.
0251: */
0252: public void testGetIndexedArguments() {
0253:
0254: // Use explicit index argument
0255:
0256: try {
0257: PropertyUtils.getIndexedProperty(null, "intArray", 0);
0258: fail("Should throw IllegalArgumentException 1");
0259: } catch (IllegalArgumentException e) {
0260: // Expected response
0261: } catch (Throwable t) {
0262: fail("Threw " + t
0263: + " instead of IllegalArgumentException 1");
0264: }
0265:
0266: try {
0267: PropertyUtils.getIndexedProperty(bean, null, 0);
0268: fail("Should throw IllegalArgumentException 2");
0269: } catch (IllegalArgumentException e) {
0270: // Expected response
0271: } catch (Throwable t) {
0272: fail("Threw " + t
0273: + " instead of IllegalArgumentException 2");
0274: }
0275:
0276: // Use index expression
0277:
0278: try {
0279: PropertyUtils.getIndexedProperty(null, "intArray[0]");
0280: fail("Should throw IllegalArgumentException 3");
0281: } catch (IllegalArgumentException e) {
0282: // Expected response
0283: } catch (Throwable t) {
0284: fail("Threw " + t
0285: + " instead of IllegalArgumentException 3");
0286: }
0287:
0288: try {
0289: PropertyUtils.getIndexedProperty(bean, "[0]");
0290: fail("Should throw NoSuchMethodException 4");
0291: } catch (NoSuchMethodException e) {
0292: // Expected response
0293: } catch (Throwable t) {
0294: fail("Threw " + t + " instead of NoSuchMethodException 4");
0295: }
0296:
0297: try {
0298: PropertyUtils.getIndexedProperty(bean, "intArray");
0299: fail("Should throw IllegalArgumentException 5");
0300: } catch (IllegalArgumentException e) {
0301: // Expected response
0302: } catch (Throwable t) {
0303: fail("Threw " + t
0304: + " instead of IllegalArgumentException 5");
0305: }
0306:
0307: // Use explicit index argument
0308:
0309: try {
0310: PropertyUtils.getIndexedProperty(null, "intIndexed", 0);
0311: fail("Should throw IllegalArgumentException 1");
0312: } catch (IllegalArgumentException e) {
0313: // Expected response
0314: } catch (Throwable t) {
0315: fail("Threw " + t
0316: + " instead of IllegalArgumentException 1");
0317: }
0318:
0319: try {
0320: PropertyUtils.getIndexedProperty(bean, null, 0);
0321: fail("Should throw IllegalArgumentException 2");
0322: } catch (IllegalArgumentException e) {
0323: // Expected response
0324: } catch (Throwable t) {
0325: fail("Threw " + t
0326: + " instead of IllegalArgumentException 2");
0327: }
0328:
0329: // Use index expression
0330:
0331: try {
0332: PropertyUtils.getIndexedProperty(null, "intIndexed[0]");
0333: fail("Should throw IllegalArgumentException 3");
0334: } catch (IllegalArgumentException e) {
0335: // Expected response
0336: } catch (Throwable t) {
0337: fail("Threw " + t
0338: + " instead of IllegalArgumentException 3");
0339: }
0340:
0341: try {
0342: PropertyUtils.getIndexedProperty(bean, "[0]");
0343: fail("Should throw NoSuchMethodException 4");
0344: } catch (NoSuchMethodException e) {
0345: // Expected response
0346: } catch (Throwable t) {
0347: fail("Threw " + t + " instead of NoSuchMethodException 4");
0348: }
0349:
0350: try {
0351: PropertyUtils.getIndexedProperty(bean, "intIndexed");
0352: fail("Should throw IllegalArgumentException 5");
0353: } catch (IllegalArgumentException e) {
0354: // Expected response
0355: } catch (Throwable t) {
0356: fail("Threw " + t
0357: + " instead of IllegalArgumentException 5");
0358: }
0359:
0360: }
0361:
0362: /**
0363: * Positive and negative tests on getIndexedProperty valid arguments.
0364: */
0365: public void testGetIndexedValues() {
0366:
0367: Object value = null;
0368:
0369: // Use explicit key argument
0370:
0371: for (int i = 0; i < 5; i++) {
0372:
0373: try {
0374: value = PropertyUtils.getIndexedProperty(bean,
0375: "intArray", i);
0376: assertNotNull("intArray returned value " + i, value);
0377: assertTrue("intArray returned Integer " + i,
0378: value instanceof Integer);
0379: assertEquals("intArray returned correct " + i, i * 10,
0380: ((Integer) value).intValue());
0381: } catch (Throwable t) {
0382: fail("intArray " + i + " threw " + t);
0383: }
0384:
0385: try {
0386: value = PropertyUtils.getIndexedProperty(bean,
0387: "intIndexed", i);
0388: assertNotNull("intIndexed returned value " + i, value);
0389: assertTrue("intIndexed returned Integer " + i,
0390: value instanceof Integer);
0391: assertEquals("intIndexed returned correct " + i,
0392: i * 10, ((Integer) value).intValue());
0393: } catch (Throwable t) {
0394: fail("intIndexed " + i + " threw " + t);
0395: }
0396:
0397: try {
0398: value = PropertyUtils.getIndexedProperty(bean,
0399: "listIndexed", i);
0400: assertNotNull("listIndexed returned value " + i, value);
0401: assertTrue("list returned String " + i,
0402: value instanceof String);
0403: assertEquals("listIndexed returned correct " + i,
0404: "String " + i, (String) value);
0405: } catch (Throwable t) {
0406: fail("listIndexed " + i + " threw " + t);
0407: }
0408:
0409: try {
0410: value = PropertyUtils.getIndexedProperty(bean,
0411: "stringArray", i);
0412: assertNotNull("stringArray returned value " + i, value);
0413: assertTrue("stringArray returned String " + i,
0414: value instanceof String);
0415: assertEquals("stringArray returned correct " + i,
0416: "String " + i, (String) value);
0417: } catch (Throwable t) {
0418: fail("stringArray " + i + " threw " + t);
0419: }
0420:
0421: try {
0422: value = PropertyUtils.getIndexedProperty(bean,
0423: "stringIndexed", i);
0424: assertNotNull("stringIndexed returned value " + i,
0425: value);
0426: assertTrue("stringIndexed returned String " + i,
0427: value instanceof String);
0428: assertEquals("stringIndexed returned correct " + i,
0429: "String " + i, (String) value);
0430: } catch (Throwable t) {
0431: fail("stringIndexed " + i + " threw " + t);
0432: }
0433:
0434: }
0435:
0436: // Use key expression
0437:
0438: for (int i = 0; i < 5; i++) {
0439:
0440: try {
0441: value = PropertyUtils.getIndexedProperty(bean,
0442: "intArray[" + i + "]");
0443: assertNotNull("intArray returned value " + i, value);
0444: assertTrue("intArray returned Integer " + i,
0445: value instanceof Integer);
0446: assertEquals("intArray returned correct " + i, i * 10,
0447: ((Integer) value).intValue());
0448: } catch (Throwable t) {
0449: fail("intArray " + i + " threw " + t);
0450: }
0451:
0452: try {
0453: value = PropertyUtils.getIndexedProperty(bean,
0454: "intIndexed[" + i + "]");
0455: assertNotNull("intIndexed returned value " + i, value);
0456: assertTrue("intIndexed returned Integer " + i,
0457: value instanceof Integer);
0458: assertEquals("intIndexed returned correct " + i,
0459: i * 10, ((Integer) value).intValue());
0460: } catch (Throwable t) {
0461: fail("intIndexed " + i + " threw " + t);
0462: }
0463:
0464: try {
0465: value = PropertyUtils.getIndexedProperty(bean,
0466: "listIndexed[" + i + "]");
0467: assertNotNull("listIndexed returned value " + i, value);
0468: assertTrue("listIndexed returned String " + i,
0469: value instanceof String);
0470: assertEquals("listIndexed returned correct " + i,
0471: "String " + i, (String) value);
0472: } catch (Throwable t) {
0473: fail("listIndexed " + i + " threw " + t);
0474: }
0475:
0476: try {
0477: value = PropertyUtils.getIndexedProperty(bean,
0478: "stringArray[" + i + "]");
0479: assertNotNull("stringArray returned value " + i, value);
0480: assertTrue("stringArray returned String " + i,
0481: value instanceof String);
0482: assertEquals("stringArray returned correct " + i,
0483: "String " + i, (String) value);
0484: } catch (Throwable t) {
0485: fail("stringArray " + i + " threw " + t);
0486: }
0487:
0488: try {
0489: value = PropertyUtils.getIndexedProperty(bean,
0490: "stringIndexed[" + i + "]");
0491: assertNotNull("stringIndexed returned value " + i,
0492: value);
0493: assertTrue("stringIndexed returned String " + i,
0494: value instanceof String);
0495: assertEquals("stringIndexed returned correct " + i,
0496: "String " + i, (String) value);
0497: } catch (Throwable t) {
0498: fail("stringIndexed " + i + " threw " + t);
0499: }
0500:
0501: }
0502:
0503: // Index out of bounds tests
0504:
0505: try {
0506: value = PropertyUtils.getIndexedProperty(bean, "intArray",
0507: -1);
0508: fail("Should have thrown ArrayIndexOutOfBoundsException");
0509: } catch (ArrayIndexOutOfBoundsException t) {
0510: // Expected results
0511: } catch (Throwable t) {
0512: fail("Threw " + t
0513: + " instead of ArrayIndexOutOfBoundsException");
0514: }
0515:
0516: try {
0517: value = PropertyUtils.getIndexedProperty(bean, "intArray",
0518: 5);
0519: fail("Should have thrown ArrayIndexOutOfBoundsException");
0520: } catch (ArrayIndexOutOfBoundsException t) {
0521: // Expected results
0522: } catch (Throwable t) {
0523: fail("Threw " + t
0524: + " instead of ArrayIndexOutOfBoundsException");
0525: }
0526:
0527: try {
0528: value = PropertyUtils.getIndexedProperty(bean,
0529: "intIndexed", -1);
0530: fail("Should have thrown ArrayIndexOutOfBoundsException");
0531: } catch (ArrayIndexOutOfBoundsException t) {
0532: // Expected results
0533: } catch (Throwable t) {
0534: fail("Threw " + t
0535: + " instead of ArrayIndexOutOfBoundsException");
0536: }
0537:
0538: try {
0539: value = PropertyUtils.getIndexedProperty(bean,
0540: "intIndexed", 5);
0541: fail("Should have thrown ArrayIndexOutOfBoundsException");
0542: } catch (ArrayIndexOutOfBoundsException t) {
0543: // Expected results
0544: } catch (Throwable t) {
0545: fail("Threw " + t
0546: + " instead of ArrayIndexOutOfBoundsException");
0547: }
0548:
0549: try {
0550: value = PropertyUtils.getIndexedProperty(bean,
0551: "listIndexed", -1);
0552: fail("Should have thrown IndexOutOfBoundsException");
0553: } catch (IndexOutOfBoundsException t) {
0554: // Expected results
0555: } catch (Throwable t) {
0556: fail("Threw " + t + " instead of IndexOutOfBoundsException");
0557: }
0558:
0559: try {
0560: value = PropertyUtils.getIndexedProperty(bean,
0561: "listIndexed", 5);
0562: fail("Should have thrown IndexOutOfBoundsException");
0563: } catch (IndexOutOfBoundsException t) {
0564: // Expected results
0565: } catch (Throwable t) {
0566: fail("Threw " + t + " instead of IndexOutOfBoundsException");
0567: }
0568:
0569: try {
0570: value = PropertyUtils.getIndexedProperty(bean,
0571: "stringArray", -1);
0572: fail("Should have thrown ArrayIndexOutOfBoundsException");
0573: } catch (ArrayIndexOutOfBoundsException t) {
0574: // Expected results
0575: } catch (Throwable t) {
0576: fail("Threw " + t
0577: + " instead of ArrayIndexOutOfBoundsException");
0578: }
0579:
0580: try {
0581: value = PropertyUtils.getIndexedProperty(bean,
0582: "stringArray", 5);
0583: fail("Should have thrown ArrayIndexOutOfBoundsException");
0584: } catch (ArrayIndexOutOfBoundsException t) {
0585: // Expected results
0586: } catch (Throwable t) {
0587: fail("Threw " + t
0588: + " instead of ArrayIndexOutOfBoundsException");
0589: }
0590:
0591: try {
0592: value = PropertyUtils.getIndexedProperty(bean,
0593: "stringIndexed", -1);
0594: fail("Should have thrown ArrayIndexOutOfBoundsException");
0595: } catch (ArrayIndexOutOfBoundsException t) {
0596: // Expected results
0597: } catch (Throwable t) {
0598: fail("Threw " + t
0599: + " instead of ArrayIndexOutOfBoundsException");
0600: }
0601:
0602: try {
0603: value = PropertyUtils.getIndexedProperty(bean,
0604: "stringIndexed", 5);
0605: fail("Should have thrown ArrayIndexOutOfBoundsException");
0606: } catch (ArrayIndexOutOfBoundsException t) {
0607: // Expected results
0608: } catch (Throwable t) {
0609: fail("Threw " + t
0610: + " instead of ArrayIndexOutOfBoundsException");
0611: }
0612:
0613: }
0614:
0615: /**
0616: * Corner cases on getMappedProperty invalid arguments.
0617: */
0618: public void testGetMappedArguments() {
0619:
0620: // Use explicit key argument
0621:
0622: try {
0623: PropertyUtils.getMappedProperty(null, "mappedProperty",
0624: "First Key");
0625: fail("Should throw IllegalArgumentException 1");
0626: } catch (IllegalArgumentException e) {
0627: // Expected response
0628: } catch (Throwable t) {
0629: fail("Threw " + t
0630: + " instead of IllegalArgumentException 1");
0631: }
0632:
0633: try {
0634: PropertyUtils.getMappedProperty(bean, null, "First Key");
0635: fail("Should throw IllegalArgumentException 2");
0636: } catch (IllegalArgumentException e) {
0637: // Expected response
0638: } catch (Throwable t) {
0639: fail("Threw " + t
0640: + " instead of IllegalArgumentException 2");
0641: }
0642:
0643: try {
0644: PropertyUtils.getMappedProperty(bean, "mappedProperty",
0645: null);
0646: fail("Should throw IllegalArgumentException 3");
0647: } catch (IllegalArgumentException e) {
0648: // Expected response
0649: } catch (Throwable t) {
0650: fail("Threw " + t
0651: + " instead of IllegalArgumentException 3");
0652: }
0653:
0654: // Use key expression
0655:
0656: try {
0657: PropertyUtils.getMappedProperty(null,
0658: "mappedProperty(First Key)");
0659: fail("Should throw IllegalArgumentException 4");
0660: } catch (IllegalArgumentException e) {
0661: // Expected response
0662: } catch (Throwable t) {
0663: fail("Threw " + t
0664: + " instead of IllegalArgumentException 4");
0665: }
0666:
0667: try {
0668: PropertyUtils.getMappedProperty(bean, "(Second Key)");
0669: fail("Should throw IllegalArgumentException 5");
0670: } catch (NoSuchMethodException e) {
0671: // Expected response
0672: } catch (Throwable t) {
0673: fail("Threw " + t + " instead of NoSuchMethodException 5");
0674: }
0675:
0676: try {
0677: PropertyUtils.getMappedProperty(bean, "mappedProperty");
0678: fail("Should throw IllegalArgumentException 6");
0679: } catch (IllegalArgumentException e) {
0680: // Expected response
0681: } catch (Throwable t) {
0682: fail("Threw " + t
0683: + " instead of IllegalArgumentException 6");
0684: }
0685:
0686: }
0687:
0688: /**
0689: * Test getting mapped values with periods in the key.
0690: */
0691: public void testGetMappedPeriods() {
0692:
0693: bean.set("mappedProperty", "key.with.a.dot", "Special Value");
0694: assertEquals("Can retrieve directly", "Special Value",
0695: (String) bean.get("mappedProperty", "key.with.a.dot"));
0696: try {
0697: assertEquals("Can retrieve via getMappedProperty",
0698: "Special Value", PropertyUtils.getMappedProperty(
0699: bean, "mappedProperty", "key.with.a.dot"));
0700: } catch (Exception e) {
0701: fail("Thew exception: " + e);
0702: }
0703: try {
0704: assertEquals("Can retrieve via getNestedProperty",
0705: "Special Value", PropertyUtils.getNestedProperty(
0706: bean, "mappedProperty(key.with.a.dot)"));
0707: } catch (Exception e) {
0708: fail("Thew exception: " + e);
0709: }
0710:
0711: bean.set("mappedObjects", "nested.property", new TestBean());
0712: assertNotNull("Can retrieve directly", bean.get(
0713: "mappedObjects", "nested.property"));
0714: try {
0715: assertEquals(
0716: "Can retrieve nested",
0717: "This is a string",
0718: PropertyUtils
0719: .getNestedProperty(bean,
0720: "mappedObjects(nested.property).stringProperty"));
0721: } catch (Exception e) {
0722: fail("Thew exception: " + e);
0723: }
0724:
0725: }
0726:
0727: /**
0728: * Test getting mapped values with slashes in the key. This is different
0729: * from periods because slashes are not syntactically significant.
0730: */
0731: public void testGetMappedSlashes() {
0732:
0733: bean.set("mappedProperty", "key/with/a/slash", "Special Value");
0734: assertEquals("Can retrieve directly", "Special Value", bean
0735: .get("mappedProperty", "key/with/a/slash"));
0736: try {
0737: assertEquals("Can retrieve via getMappedProperty",
0738: "Special Value", PropertyUtils.getMappedProperty(
0739: bean, "mappedProperty", "key/with/a/slash"));
0740: } catch (Exception e) {
0741: fail("Thew exception: " + e);
0742: }
0743: try {
0744: assertEquals("Can retrieve via getNestedProperty",
0745: "Special Value", PropertyUtils.getNestedProperty(
0746: bean, "mappedProperty(key/with/a/slash)"));
0747: } catch (Exception e) {
0748: fail("Thew exception: " + e);
0749: }
0750:
0751: bean.set("mappedObjects", "nested/property", new TestBean());
0752: assertNotNull("Can retrieve directly", bean.get(
0753: "mappedObjects", "nested/property"));
0754: try {
0755: assertEquals(
0756: "Can retrieve nested",
0757: "This is a string",
0758: PropertyUtils
0759: .getNestedProperty(bean,
0760: "mappedObjects(nested/property).stringProperty"));
0761: } catch (Exception e) {
0762: fail("Thew exception: " + e);
0763: }
0764:
0765: }
0766:
0767: /**
0768: * Positive and negative tests on getMappedProperty valid arguments.
0769: */
0770: public void testGetMappedValues() {
0771:
0772: Object value = null;
0773:
0774: // Use explicit key argument
0775:
0776: try {
0777: value = PropertyUtils.getMappedProperty(bean,
0778: "mappedProperty", "First Key");
0779: assertEquals("Can find first value", "First Value", value);
0780: } catch (Throwable t) {
0781: fail("Finding first value threw " + t);
0782: }
0783:
0784: try {
0785: value = PropertyUtils.getMappedProperty(bean,
0786: "mappedProperty", "Second Key");
0787: assertEquals("Can find second value", "Second Value", value);
0788: } catch (Throwable t) {
0789: fail("Finding second value threw " + t);
0790: }
0791:
0792: try {
0793: value = PropertyUtils.getMappedProperty(bean,
0794: "mappedProperty", "Third Key");
0795: assertNull("Can not find third value", value);
0796: } catch (Throwable t) {
0797: fail("Finding third value threw " + t);
0798: }
0799:
0800: // Use key expression with parentheses
0801:
0802: try {
0803: value = PropertyUtils.getMappedProperty(bean,
0804: "mappedProperty(First Key)");
0805: assertEquals("Can find first value", "First Value", value);
0806: } catch (Throwable t) {
0807: fail("Finding first value threw " + t);
0808: }
0809:
0810: try {
0811: value = PropertyUtils.getMappedProperty(bean,
0812: "mappedProperty(Second Key)");
0813: assertEquals("Can find second value", "Second Value", value);
0814: } catch (Throwable t) {
0815: fail("Finding second value threw " + t);
0816: }
0817:
0818: try {
0819: value = PropertyUtils.getMappedProperty(bean,
0820: "mappedProperty(Third Key)");
0821: assertNull("Can not find third value", value);
0822: } catch (Throwable t) {
0823: fail("Finding third value threw " + t);
0824: }
0825:
0826: // Use key expression with dotted syntax
0827:
0828: try {
0829: value = PropertyUtils.getNestedProperty(bean,
0830: "mapProperty.First Key");
0831: assertEquals("Can find first value", "First Value", value);
0832: } catch (Throwable t) {
0833: fail("Finding first value threw " + t);
0834: }
0835:
0836: try {
0837: value = PropertyUtils.getNestedProperty(bean,
0838: "mapProperty.Second Key");
0839: assertEquals("Can find second value", "Second Value", value);
0840: } catch (Throwable t) {
0841: fail("Finding second value threw " + t);
0842: }
0843:
0844: try {
0845: value = PropertyUtils.getNestedProperty(bean,
0846: "mapProperty.Third Key");
0847: assertNull("Can not find third value", value);
0848: } catch (Throwable t) {
0849: fail("Finding third value threw " + t);
0850: }
0851:
0852: }
0853:
0854: /**
0855: * Corner cases on getNestedProperty invalid arguments.
0856: */
0857: public void testGetNestedArguments() {
0858:
0859: try {
0860: PropertyUtils.getNestedProperty(null, "stringProperty");
0861: fail("Should throw IllegalArgumentException 1");
0862: } catch (IllegalArgumentException e) {
0863: // Expected response
0864: } catch (Throwable t) {
0865: fail("Threw " + t
0866: + " instead of IllegalArgumentException 1");
0867: }
0868:
0869: try {
0870: PropertyUtils.getNestedProperty(bean, null);
0871: fail("Should throw IllegalArgumentException 2");
0872: } catch (IllegalArgumentException e) {
0873: // Expected response
0874: } catch (Throwable t) {
0875: fail("Threw " + t
0876: + " instead of IllegalArgumentException 2");
0877: }
0878:
0879: }
0880:
0881: /**
0882: * Test getNestedProperty on a boolean property.
0883: */
0884: public void testGetNestedBoolean() {
0885:
0886: try {
0887: Object value = PropertyUtils.getNestedProperty(bean,
0888: "nested.booleanProperty");
0889: assertNotNull("Got a value", value);
0890: assertTrue("Got correct type", (value instanceof Boolean));
0891: TestBean nested = (TestBean) bean.get("nested");
0892: assertTrue("Got correct value", ((Boolean) value)
0893: .booleanValue() == nested.getBooleanProperty());
0894: } catch (IllegalAccessException e) {
0895: fail("IllegalAccessException");
0896: } catch (IllegalArgumentException e) {
0897: fail("IllegalArgumentException");
0898: } catch (InvocationTargetException e) {
0899: fail("InvocationTargetException");
0900: } catch (NoSuchMethodException e) {
0901: fail("NoSuchMethodException");
0902: }
0903:
0904: }
0905:
0906: /**
0907: * Test getNestedProperty on a double property.
0908: */
0909: public void testGetNestedDouble() {
0910:
0911: try {
0912: Object value = PropertyUtils.getNestedProperty(bean,
0913: "nested.doubleProperty");
0914: assertNotNull("Got a value", value);
0915: assertTrue("Got correct type", (value instanceof Double));
0916: TestBean nested = (TestBean) bean.get("nested");
0917: assertEquals("Got correct value", ((Double) value)
0918: .doubleValue(), nested.getDoubleProperty(), 0.005);
0919: } catch (IllegalAccessException e) {
0920: fail("IllegalAccessException");
0921: } catch (IllegalArgumentException e) {
0922: fail("IllegalArgumentException");
0923: } catch (InvocationTargetException e) {
0924: fail("InvocationTargetException");
0925: } catch (NoSuchMethodException e) {
0926: fail("NoSuchMethodException");
0927: }
0928:
0929: }
0930:
0931: /**
0932: * Test getNestedProperty on a float property.
0933: */
0934: public void testGetNestedFloat() {
0935:
0936: try {
0937: Object value = PropertyUtils.getNestedProperty(bean,
0938: "nested.floatProperty");
0939: assertNotNull("Got a value", value);
0940: assertTrue("Got correct type", (value instanceof Float));
0941: TestBean nested = (TestBean) bean.get("nested");
0942: assertEquals("Got correct value", ((Float) value)
0943: .floatValue(), nested.getFloatProperty(),
0944: (float) 0.005);
0945: } catch (IllegalAccessException e) {
0946: fail("IllegalAccessException");
0947: } catch (IllegalArgumentException e) {
0948: fail("IllegalArgumentException");
0949: } catch (InvocationTargetException e) {
0950: fail("InvocationTargetException");
0951: } catch (NoSuchMethodException e) {
0952: fail("NoSuchMethodException");
0953: }
0954:
0955: }
0956:
0957: /**
0958: * Test getNestedProperty on an int property.
0959: */
0960: public void testGetNestedInt() {
0961:
0962: try {
0963: Object value = PropertyUtils.getNestedProperty(bean,
0964: "nested.intProperty");
0965: assertNotNull("Got a value", value);
0966: assertTrue("Got correct type", (value instanceof Integer));
0967: TestBean nested = (TestBean) bean.get("nested");
0968: assertEquals("Got correct value", ((Integer) value)
0969: .intValue(), nested.getIntProperty());
0970: } catch (IllegalAccessException e) {
0971: fail("IllegalAccessException");
0972: } catch (IllegalArgumentException e) {
0973: fail("IllegalArgumentException");
0974: } catch (InvocationTargetException e) {
0975: fail("InvocationTargetException");
0976: } catch (NoSuchMethodException e) {
0977: fail("NoSuchMethodException");
0978: }
0979:
0980: }
0981:
0982: /**
0983: * Test getNestedProperty on a long property.
0984: */
0985: public void testGetNestedLong() {
0986:
0987: try {
0988: Object value = PropertyUtils.getNestedProperty(bean,
0989: "nested.longProperty");
0990: assertNotNull("Got a value", value);
0991: assertTrue("Got correct type", (value instanceof Long));
0992: TestBean nested = (TestBean) bean.get("nested");
0993: assertEquals("Got correct value", ((Long) value)
0994: .longValue(), nested.getLongProperty());
0995: } catch (IllegalAccessException e) {
0996: fail("IllegalAccessException");
0997: } catch (IllegalArgumentException e) {
0998: fail("IllegalArgumentException");
0999: } catch (InvocationTargetException e) {
1000: fail("InvocationTargetException");
1001: } catch (NoSuchMethodException e) {
1002: fail("NoSuchMethodException");
1003: }
1004:
1005: }
1006:
1007: /**
1008: * Test getNestedProperty on a read-only String property.
1009: */
1010: public void testGetNestedReadOnly() {
1011:
1012: try {
1013: Object value = PropertyUtils.getNestedProperty(bean,
1014: "nested.readOnlyProperty");
1015: assertNotNull("Got a value", value);
1016: assertTrue("Got correct type", (value instanceof String));
1017: TestBean nested = (TestBean) bean.get("nested");
1018: assertEquals("Got correct value", (String) value, nested
1019: .getReadOnlyProperty());
1020: } catch (IllegalAccessException e) {
1021: fail("IllegalAccessException");
1022: } catch (IllegalArgumentException e) {
1023: fail("IllegalArgumentException");
1024: } catch (InvocationTargetException e) {
1025: fail("InvocationTargetException");
1026: } catch (NoSuchMethodException e) {
1027: fail("NoSuchMethodException");
1028: }
1029:
1030: }
1031:
1032: /**
1033: * Test getNestedProperty on a short property.
1034: */
1035: public void testGetNestedShort() {
1036:
1037: try {
1038: Object value = PropertyUtils.getNestedProperty(bean,
1039: "nested.shortProperty");
1040: assertNotNull("Got a value", value);
1041: assertTrue("Got correct type", (value instanceof Short));
1042: TestBean nested = (TestBean) bean.get("nested");
1043: assertEquals("Got correct value", ((Short) value)
1044: .shortValue(), nested.getShortProperty());
1045: } catch (IllegalAccessException e) {
1046: fail("IllegalAccessException");
1047: } catch (IllegalArgumentException e) {
1048: fail("IllegalArgumentException");
1049: } catch (InvocationTargetException e) {
1050: fail("InvocationTargetException");
1051: } catch (NoSuchMethodException e) {
1052: fail("NoSuchMethodException");
1053: }
1054:
1055: }
1056:
1057: /**
1058: * Test getNestedProperty on a String property.
1059: */
1060: public void testGetNestedString() {
1061:
1062: try {
1063: Object value = PropertyUtils.getNestedProperty(bean,
1064: "nested.stringProperty");
1065: assertNotNull("Got a value", value);
1066: assertTrue("Got correct type", (value instanceof String));
1067: TestBean nested = (TestBean) bean.get("nested");
1068: assertEquals("Got correct value", ((String) value), nested
1069: .getStringProperty());
1070: } catch (IllegalAccessException e) {
1071: fail("IllegalAccessException");
1072: } catch (IllegalArgumentException e) {
1073: fail("IllegalArgumentException");
1074: } catch (InvocationTargetException e) {
1075: fail("InvocationTargetException");
1076: } catch (NoSuchMethodException e) {
1077: fail("NoSuchMethodException");
1078: }
1079:
1080: }
1081:
1082: /**
1083: * Negative test getNestedProperty on an unknown property.
1084: */
1085: public void testGetNestedUnknown() {
1086:
1087: try {
1088: PropertyUtils.getNestedProperty(bean, "nested.unknown");
1089: fail("Should have thrown NoSuchMethodException");
1090: } catch (IllegalAccessException e) {
1091: fail("IllegalAccessException");
1092: } catch (IllegalArgumentException e) {
1093: fail("IllegalArgumentException");
1094: } catch (InvocationTargetException e) {
1095: fail("InvocationTargetException");
1096: } catch (NoSuchMethodException e) {
1097: // Correct result for this test
1098: }
1099:
1100: }
1101:
1102: /**
1103: * Corner cases on getSimpleProperty invalid arguments.
1104: */
1105: public void testGetSimpleArguments() {
1106:
1107: try {
1108: PropertyUtils.getSimpleProperty(null, "stringProperty");
1109: fail("Should throw IllegalArgumentException 1");
1110: } catch (IllegalArgumentException e) {
1111: // Expected response
1112: } catch (Throwable t) {
1113: fail("Threw " + t
1114: + " instead of IllegalArgumentException 1");
1115: }
1116:
1117: try {
1118: PropertyUtils.getSimpleProperty(bean, null);
1119: fail("Should throw IllegalArgumentException 2");
1120: } catch (IllegalArgumentException e) {
1121: // Expected response
1122: } catch (Throwable t) {
1123: fail("Threw " + t
1124: + " instead of IllegalArgumentException 2");
1125: }
1126:
1127: }
1128:
1129: /**
1130: * Test getSimpleProperty on a boolean property.
1131: */
1132: public void testGetSimpleBoolean() {
1133:
1134: try {
1135: Object value = PropertyUtils.getSimpleProperty(bean,
1136: "booleanProperty");
1137: assertNotNull("Got a value", value);
1138: assertTrue("Got correct type", (value instanceof Boolean));
1139: assertTrue("Got correct value", ((Boolean) value)
1140: .booleanValue() == true);
1141: } catch (IllegalAccessException e) {
1142: fail("IllegalAccessException");
1143: } catch (IllegalArgumentException e) {
1144: fail("IllegalArgumentException");
1145: } catch (InvocationTargetException e) {
1146: fail("InvocationTargetException");
1147: } catch (NoSuchMethodException e) {
1148: fail("NoSuchMethodException");
1149: }
1150:
1151: }
1152:
1153: /**
1154: * Test getSimpleProperty on a double property.
1155: */
1156: public void testGetSimpleDouble() {
1157:
1158: try {
1159: Object value = PropertyUtils.getSimpleProperty(bean,
1160: "doubleProperty");
1161: assertNotNull("Got a value", value);
1162: assertTrue("Got correct type", (value instanceof Double));
1163: assertEquals("Got correct value", ((Double) value)
1164: .doubleValue(), 321.0, 0.005);
1165: } catch (IllegalAccessException e) {
1166: fail("IllegalAccessException");
1167: } catch (IllegalArgumentException e) {
1168: fail("IllegalArgumentException");
1169: } catch (InvocationTargetException e) {
1170: fail("InvocationTargetException");
1171: } catch (NoSuchMethodException e) {
1172: fail("NoSuchMethodException");
1173: }
1174:
1175: }
1176:
1177: /**
1178: * Test getSimpleProperty on a float property.
1179: */
1180: public void testGetSimpleFloat() {
1181:
1182: try {
1183: Object value = PropertyUtils.getSimpleProperty(bean,
1184: "floatProperty");
1185: assertNotNull("Got a value", value);
1186: assertTrue("Got correct type", (value instanceof Float));
1187: assertEquals("Got correct value", ((Float) value)
1188: .floatValue(), (float) 123.0, (float) 0.005);
1189: } catch (IllegalAccessException e) {
1190: fail("IllegalAccessException");
1191: } catch (IllegalArgumentException e) {
1192: fail("IllegalArgumentException");
1193: } catch (InvocationTargetException e) {
1194: fail("InvocationTargetException");
1195: } catch (NoSuchMethodException e) {
1196: fail("NoSuchMethodException");
1197: }
1198:
1199: }
1200:
1201: /**
1202: * Negative test getSimpleProperty on an indexed property.
1203: */
1204: public void testGetSimpleIndexed() {
1205:
1206: Object value = null;
1207: try {
1208: value = PropertyUtils.getSimpleProperty(bean,
1209: "intIndexed[0]");
1210: fail("Should have thrown IllegalArgumentException");
1211: } catch (IllegalAccessException e) {
1212: fail("IllegalAccessException");
1213: } catch (IllegalArgumentException e) {
1214: // Correct result for this test
1215: } catch (InvocationTargetException e) {
1216: fail("InvocationTargetException");
1217: } catch (NoSuchMethodException e) {
1218: fail("NoSuchMethodException");
1219: }
1220:
1221: }
1222:
1223: /**
1224: * Test getSimpleProperty on an int property.
1225: */
1226: public void testGetSimpleInt() {
1227:
1228: try {
1229: Object value = PropertyUtils.getSimpleProperty(bean,
1230: "intProperty");
1231: assertNotNull("Got a value", value);
1232: assertTrue("Got correct type", (value instanceof Integer));
1233: assertEquals("Got correct value", ((Integer) value)
1234: .intValue(), 123);
1235: } catch (IllegalAccessException e) {
1236: fail("IllegalAccessException");
1237: } catch (IllegalArgumentException e) {
1238: fail("IllegalArgumentException");
1239: } catch (InvocationTargetException e) {
1240: fail("InvocationTargetException");
1241: } catch (NoSuchMethodException e) {
1242: fail("NoSuchMethodException");
1243: }
1244:
1245: }
1246:
1247: /**
1248: * Test getSimpleProperty on a long property.
1249: */
1250: public void testGetSimpleLong() {
1251:
1252: try {
1253: Object value = PropertyUtils.getSimpleProperty(bean,
1254: "longProperty");
1255: assertNotNull("Got a value", value);
1256: assertTrue("Got correct type", (value instanceof Long));
1257: assertEquals("Got correct value", ((Long) value)
1258: .longValue(), 321);
1259: } catch (IllegalAccessException e) {
1260: fail("IllegalAccessException");
1261: } catch (IllegalArgumentException e) {
1262: fail("IllegalArgumentException");
1263: } catch (InvocationTargetException e) {
1264: fail("InvocationTargetException");
1265: } catch (NoSuchMethodException e) {
1266: fail("NoSuchMethodException");
1267: }
1268:
1269: }
1270:
1271: /**
1272: * Negative test getSimpleProperty on a nested property.
1273: */
1274: public void testGetSimpleNested() {
1275:
1276: Object value = null;
1277: try {
1278: value = PropertyUtils.getSimpleProperty(bean,
1279: "nested.stringProperty");
1280: fail("Should have thrown IllegaArgumentException");
1281: } catch (IllegalAccessException e) {
1282: fail("IllegalAccessException");
1283: } catch (IllegalArgumentException e) {
1284: // Correct result for this test
1285: } catch (InvocationTargetException e) {
1286: fail("InvocationTargetException");
1287: } catch (NoSuchMethodException e) {
1288: fail("NoSuchMethodException");
1289: }
1290:
1291: }
1292:
1293: /**
1294: * Test getSimpleProperty on a short property.
1295: */
1296: public void testGetSimpleShort() {
1297:
1298: try {
1299: Object value = PropertyUtils.getSimpleProperty(bean,
1300: "shortProperty");
1301: assertNotNull("Got a value", value);
1302: assertTrue("Got correct type", (value instanceof Short));
1303: assertEquals("Got correct value", ((Short) value)
1304: .shortValue(), (short) 987);
1305: } catch (IllegalAccessException e) {
1306: fail("IllegalAccessException");
1307: } catch (IllegalArgumentException e) {
1308: fail("IllegalArgumentException");
1309: } catch (InvocationTargetException e) {
1310: fail("InvocationTargetException");
1311: } catch (NoSuchMethodException e) {
1312: fail("NoSuchMethodException");
1313: }
1314:
1315: }
1316:
1317: /**
1318: * Test getSimpleProperty on a String property.
1319: */
1320: public void testGetSimpleString() {
1321:
1322: try {
1323: Object value = PropertyUtils.getSimpleProperty(bean,
1324: "stringProperty");
1325: assertNotNull("Got a value", value);
1326: assertTrue("Got correct type", (value instanceof String));
1327: assertEquals("Got correct value", (String) value,
1328: "This is a string");
1329: } catch (IllegalAccessException e) {
1330: fail("IllegalAccessException");
1331: } catch (IllegalArgumentException e) {
1332: fail("IllegalArgumentException");
1333: } catch (InvocationTargetException e) {
1334: fail("InvocationTargetException");
1335: } catch (NoSuchMethodException e) {
1336: fail("NoSuchMethodException");
1337: }
1338:
1339: }
1340:
1341: /**
1342: * Negative test getSimpleProperty on an unknown property.
1343: */
1344: public void testGetSimpleUnknown() {
1345:
1346: try {
1347: PropertyUtils.getSimpleProperty(bean, "unknown");
1348: fail("Should have thrown NoSuchMethodException");
1349: } catch (IllegalAccessException e) {
1350: fail("IllegalAccessException");
1351: } catch (IllegalArgumentException e) {
1352: fail("IllegalArgumentException");
1353: } catch (InvocationTargetException e) {
1354: fail("InvocationTargetException");
1355: } catch (NoSuchMethodException e) {
1356: // Correct result for this test
1357: assertEquals("Unknown property 'unknown' on dynaclass '"
1358: + ((DynaBean) bean).getDynaClass() + "'", e
1359: .getMessage());
1360: }
1361:
1362: }
1363:
1364: /**
1365: * Corner cases on setIndexedProperty invalid arguments.
1366: */
1367: public void testSetIndexedArguments() {
1368:
1369: // Use explicit index argument
1370:
1371: try {
1372: PropertyUtils.setIndexedProperty(null, "intArray", 0,
1373: new Integer(1));
1374: fail("Should throw IllegalArgumentException 1");
1375: } catch (IllegalArgumentException e) {
1376: // Expected response
1377: } catch (Throwable t) {
1378: fail("Threw " + t
1379: + " instead of IllegalArgumentException 1");
1380: }
1381:
1382: try {
1383: PropertyUtils.setIndexedProperty(bean, null, 0,
1384: new Integer(1));
1385: fail("Should throw IllegalArgumentException 2");
1386: } catch (IllegalArgumentException e) {
1387: // Expected response
1388: } catch (Throwable t) {
1389: fail("Threw " + t
1390: + " instead of IllegalArgumentException 2");
1391: }
1392:
1393: // Use index expression
1394:
1395: try {
1396: PropertyUtils.setIndexedProperty(null, "intArray[0]",
1397: new Integer(1));
1398: fail("Should throw IllegalArgumentException 3");
1399: } catch (IllegalArgumentException e) {
1400: // Expected response
1401: } catch (Throwable t) {
1402: fail("Threw " + t
1403: + " instead of IllegalArgumentException 3");
1404: }
1405:
1406: try {
1407: PropertyUtils.setIndexedProperty(bean, "[0]",
1408: new Integer(1));
1409: fail("Should throw NoSuchMethodException 4");
1410: } catch (NoSuchMethodException e) {
1411: // Expected response
1412: } catch (Throwable t) {
1413: fail("Threw " + t + " instead of NoSuchMethodException 4");
1414: }
1415:
1416: try {
1417: PropertyUtils.setIndexedProperty(bean, "intArray",
1418: new Integer(1));
1419: fail("Should throw IllegalArgumentException 5");
1420: } catch (IllegalArgumentException e) {
1421: // Expected response
1422: } catch (Throwable t) {
1423: fail("Threw " + t
1424: + " instead of IllegalArgumentException 5");
1425: }
1426:
1427: // Use explicit index argument
1428:
1429: try {
1430: PropertyUtils.setIndexedProperty(null, "intIndexed", 0,
1431: new Integer(1));
1432: fail("Should throw IllegalArgumentException 1");
1433: } catch (IllegalArgumentException e) {
1434: // Expected response
1435: } catch (Throwable t) {
1436: fail("Threw " + t
1437: + " instead of IllegalArgumentException 1");
1438: }
1439:
1440: try {
1441: PropertyUtils.setIndexedProperty(bean, null, 0,
1442: new Integer(1));
1443: fail("Should throw IllegalArgumentException 2");
1444: } catch (IllegalArgumentException e) {
1445: // Expected response
1446: } catch (Throwable t) {
1447: fail("Threw " + t
1448: + " instead of IllegalArgumentException 2");
1449: }
1450:
1451: // Use index expression
1452:
1453: try {
1454: PropertyUtils.setIndexedProperty(null, "intIndexed[0]",
1455: new Integer(1));
1456: fail("Should throw IllegalArgumentException 3");
1457: } catch (IllegalArgumentException e) {
1458: // Expected response
1459: } catch (Throwable t) {
1460: fail("Threw " + t
1461: + " instead of IllegalArgumentException 3");
1462: }
1463:
1464: try {
1465: PropertyUtils.setIndexedProperty(bean, "[0]",
1466: new Integer(1));
1467: fail("Should throw NoSuchMethodException 4");
1468: } catch (NoSuchMethodException e) {
1469: // Expected response
1470: } catch (Throwable t) {
1471: fail("Threw " + t + " instead of NoSuchMethodException 4");
1472: }
1473:
1474: try {
1475: PropertyUtils.setIndexedProperty(bean, "intIndexed",
1476: new Integer(1));
1477: fail("Should throw IllegalArgumentException 5");
1478: } catch (IllegalArgumentException e) {
1479: // Expected response
1480: } catch (Throwable t) {
1481: fail("Threw " + t
1482: + " instead of IllegalArgumentException 5");
1483: }
1484:
1485: }
1486:
1487: /**
1488: * Positive and negative tests on setIndexedProperty valid arguments.
1489: */
1490: public void testSetIndexedValues() {
1491:
1492: Object value = null;
1493:
1494: // Use explicit index argument
1495:
1496: try {
1497: PropertyUtils.setIndexedProperty(bean, "intArray", 0,
1498: new Integer(1));
1499: value = PropertyUtils.getIndexedProperty(bean, "intArray",
1500: 0);
1501: assertNotNull("Returned new value 0", value);
1502: assertTrue("Returned Integer new value 0",
1503: value instanceof Integer);
1504: assertEquals("Returned correct new value 0", 1,
1505: ((Integer) value).intValue());
1506: } catch (Throwable t) {
1507: fail("Threw " + t);
1508: }
1509:
1510: try {
1511: PropertyUtils.setIndexedProperty(bean, "intIndexed", 1,
1512: new Integer(11));
1513: value = PropertyUtils.getIndexedProperty(bean,
1514: "intIndexed", 1);
1515: assertNotNull("Returned new value 1", value);
1516: assertTrue("Returned Integer new value 1",
1517: value instanceof Integer);
1518: assertEquals("Returned correct new value 1", 11,
1519: ((Integer) value).intValue());
1520: } catch (Throwable t) {
1521: fail("Threw " + t);
1522: }
1523:
1524: try {
1525: PropertyUtils.setIndexedProperty(bean, "listIndexed", 2,
1526: "New Value 2");
1527: value = PropertyUtils.getIndexedProperty(bean,
1528: "listIndexed", 2);
1529: assertNotNull("Returned new value 2", value);
1530: assertTrue("Returned String new value 2",
1531: value instanceof String);
1532: assertEquals("Returned correct new value 2", "New Value 2",
1533: (String) value);
1534: } catch (Throwable t) {
1535: fail("Threw " + t);
1536: }
1537:
1538: try {
1539: PropertyUtils.setIndexedProperty(bean, "stringArray", 2,
1540: "New Value 2");
1541: value = PropertyUtils.getIndexedProperty(bean,
1542: "stringArray", 2);
1543: assertNotNull("Returned new value 2", value);
1544: assertTrue("Returned String new value 2",
1545: value instanceof String);
1546: assertEquals("Returned correct new value 2", "New Value 2",
1547: (String) value);
1548: } catch (Throwable t) {
1549: fail("Threw " + t);
1550: }
1551:
1552: try {
1553: PropertyUtils.setIndexedProperty(bean, "stringArray", 3,
1554: "New Value 3");
1555: value = PropertyUtils.getIndexedProperty(bean,
1556: "stringArray", 3);
1557: assertNotNull("Returned new value 3", value);
1558: assertTrue("Returned String new value 3",
1559: value instanceof String);
1560: assertEquals("Returned correct new value 3", "New Value 3",
1561: (String) value);
1562: } catch (Throwable t) {
1563: fail("Threw " + t);
1564: }
1565:
1566: // Use index expression
1567:
1568: try {
1569: PropertyUtils.setIndexedProperty(bean, "intArray[4]",
1570: new Integer(1));
1571: value = PropertyUtils.getIndexedProperty(bean,
1572: "intArray[4]");
1573: assertNotNull("Returned new value 4", value);
1574: assertTrue("Returned Integer new value 4",
1575: value instanceof Integer);
1576: assertEquals("Returned correct new value 4", 1,
1577: ((Integer) value).intValue());
1578: } catch (Throwable t) {
1579: fail("Threw " + t);
1580: }
1581:
1582: try {
1583: PropertyUtils.setIndexedProperty(bean, "intIndexed[3]",
1584: new Integer(11));
1585: value = PropertyUtils.getIndexedProperty(bean,
1586: "intIndexed[3]");
1587: assertNotNull("Returned new value 5", value);
1588: assertTrue("Returned Integer new value 5",
1589: value instanceof Integer);
1590: assertEquals("Returned correct new value 5", 11,
1591: ((Integer) value).intValue());
1592: } catch (Throwable t) {
1593: fail("Threw " + t);
1594: }
1595:
1596: try {
1597: PropertyUtils.setIndexedProperty(bean, "listIndexed[1]",
1598: "New Value 2");
1599: value = PropertyUtils.getIndexedProperty(bean,
1600: "listIndexed[1]");
1601: assertNotNull("Returned new value 6", value);
1602: assertTrue("Returned String new value 6",
1603: value instanceof String);
1604: assertEquals("Returned correct new value 6", "New Value 2",
1605: (String) value);
1606: } catch (Throwable t) {
1607: fail("Threw " + t);
1608: }
1609:
1610: try {
1611: PropertyUtils.setIndexedProperty(bean, "stringArray[1]",
1612: "New Value 2");
1613: value = PropertyUtils.getIndexedProperty(bean,
1614: "stringArray[2]");
1615: assertNotNull("Returned new value 6", value);
1616: assertTrue("Returned String new value 6",
1617: value instanceof String);
1618: assertEquals("Returned correct new value 6", "New Value 2",
1619: (String) value);
1620: } catch (Throwable t) {
1621: fail("Threw " + t);
1622: }
1623:
1624: try {
1625: PropertyUtils.setIndexedProperty(bean, "stringArray[0]",
1626: "New Value 3");
1627: value = PropertyUtils.getIndexedProperty(bean,
1628: "stringArray[0]");
1629: assertNotNull("Returned new value 7", value);
1630: assertTrue("Returned String new value 7",
1631: value instanceof String);
1632: assertEquals("Returned correct new value 7", "New Value 3",
1633: (String) value);
1634: } catch (Throwable t) {
1635: fail("Threw " + t);
1636: }
1637:
1638: // Index out of bounds tests
1639:
1640: try {
1641: PropertyUtils.setIndexedProperty(bean, "intArray", -1,
1642: new Integer(0));
1643: fail("Should have thrown ArrayIndexOutOfBoundsException");
1644: } catch (ArrayIndexOutOfBoundsException t) {
1645: // Expected results
1646: } catch (Throwable t) {
1647: fail("Threw " + t
1648: + " instead of ArrayIndexOutOfBoundsException");
1649: }
1650:
1651: try {
1652: PropertyUtils.setIndexedProperty(bean, "intArray", 5,
1653: new Integer(0));
1654: fail("Should have thrown ArrayIndexOutOfBoundsException");
1655: } catch (ArrayIndexOutOfBoundsException t) {
1656: // Expected results
1657: } catch (Throwable t) {
1658: fail("Threw " + t
1659: + " instead of ArrayIndexOutOfBoundsException");
1660: }
1661:
1662: try {
1663: PropertyUtils.setIndexedProperty(bean, "intIndexed", -1,
1664: new Integer(0));
1665: fail("Should have thrown ArrayIndexOutOfBoundsException");
1666: } catch (ArrayIndexOutOfBoundsException t) {
1667: // Expected results
1668: } catch (Throwable t) {
1669: fail("Threw " + t
1670: + " instead of ArrayIndexOutOfBoundsException");
1671: }
1672:
1673: try {
1674: PropertyUtils.setIndexedProperty(bean, "intIndexed", 5,
1675: new Integer(0));
1676: fail("Should have thrown ArrayIndexOutOfBoundsException");
1677: } catch (ArrayIndexOutOfBoundsException t) {
1678: // Expected results
1679: } catch (Throwable t) {
1680: fail("Threw " + t
1681: + " instead of ArrayIndexOutOfBoundsException");
1682: }
1683:
1684: try {
1685: PropertyUtils.setIndexedProperty(bean, "listIndexed", 5,
1686: "New String");
1687: fail("Should have thrown IndexOutOfBoundsException");
1688: } catch (IndexOutOfBoundsException t) {
1689: // Expected results
1690: } catch (Throwable t) {
1691: fail("Threw " + t + " instead of IndexOutOfBoundsException");
1692: }
1693:
1694: try {
1695: PropertyUtils.setIndexedProperty(bean, "listIndexed", -1,
1696: "New String");
1697: fail("Should have thrown IndexOutOfBoundsException");
1698: } catch (IndexOutOfBoundsException t) {
1699: // Expected results
1700: } catch (Throwable t) {
1701: fail("Threw " + t + " instead of IndexOutOfBoundsException");
1702: }
1703:
1704: try {
1705: PropertyUtils.setIndexedProperty(bean, "stringArray", -1,
1706: "New String");
1707: fail("Should have thrown ArrayIndexOutOfBoundsException");
1708: } catch (ArrayIndexOutOfBoundsException t) {
1709: // Expected results
1710: } catch (Throwable t) {
1711: fail("Threw " + t
1712: + " instead of ArrayIndexOutOfBoundsException");
1713: }
1714:
1715: try {
1716: PropertyUtils.setIndexedProperty(bean, "stringArray", 5,
1717: "New String");
1718: fail("Should have thrown ArrayIndexOutOfBoundsException");
1719: } catch (ArrayIndexOutOfBoundsException t) {
1720: // Expected results
1721: } catch (Throwable t) {
1722: fail("Threw " + t
1723: + " instead of ArrayIndexOutOfBoundsException");
1724: }
1725:
1726: try {
1727: PropertyUtils.setIndexedProperty(bean, "stringIndexed", -1,
1728: "New String");
1729: fail("Should have thrown ArrayIndexOutOfBoundsException");
1730: } catch (ArrayIndexOutOfBoundsException t) {
1731: // Expected results
1732: } catch (Throwable t) {
1733: fail("Threw " + t
1734: + " instead of ArrayIndexOutOfBoundsException");
1735: }
1736:
1737: try {
1738: PropertyUtils.setIndexedProperty(bean, "stringIndexed", 5,
1739: "New String");
1740: fail("Should have thrown ArrayIndexOutOfBoundsException");
1741: } catch (ArrayIndexOutOfBoundsException t) {
1742: // Expected results
1743: } catch (Throwable t) {
1744: fail("Threw " + t
1745: + " instead of ArrayIndexOutOfBoundsException");
1746: }
1747:
1748: }
1749:
1750: /**
1751: * Corner cases on getMappedProperty invalid arguments.
1752: */
1753: public void testSetMappedArguments() {
1754:
1755: // Use explicit key argument
1756:
1757: try {
1758: PropertyUtils.setMappedProperty(null, "mappedProperty",
1759: "First Key", "First Value");
1760: fail("Should throw IllegalArgumentException 1");
1761: } catch (IllegalArgumentException e) {
1762: // Expected response
1763: } catch (Throwable t) {
1764: fail("Threw " + t
1765: + " instead of IllegalArgumentException 1");
1766: }
1767:
1768: try {
1769: PropertyUtils.setMappedProperty(bean, null, "First Key",
1770: "First Value");
1771: fail("Should throw IllegalArgumentException 2");
1772: } catch (IllegalArgumentException e) {
1773: // Expected response
1774: } catch (Throwable t) {
1775: fail("Threw " + t
1776: + " instead of IllegalArgumentException 2");
1777: }
1778:
1779: try {
1780: PropertyUtils.setMappedProperty(bean, "mappedProperty",
1781: null, "First Value");
1782: fail("Should throw IllegalArgumentException 3");
1783: } catch (IllegalArgumentException e) {
1784: // Expected response
1785: } catch (Throwable t) {
1786: fail("Threw " + t
1787: + " instead of IllegalArgumentException 3");
1788: }
1789:
1790: // Use key expression
1791:
1792: try {
1793: PropertyUtils.setMappedProperty(null,
1794: "mappedProperty(First Key)", "First Value");
1795: fail("Should throw IllegalArgumentException 4");
1796: } catch (IllegalArgumentException e) {
1797: // Expected response
1798: } catch (Throwable t) {
1799: fail("Threw " + t
1800: + " instead of IllegalArgumentException 4");
1801: }
1802:
1803: try {
1804: PropertyUtils.setMappedProperty(bean, "(Second Key)",
1805: "Second Value");
1806: fail("Should throw IllegalArgumentException 5");
1807: } catch (NoSuchMethodException e) {
1808: // Expected response
1809: } catch (Throwable t) {
1810: fail("Threw " + t + " instead of NoSuchMethodException 5");
1811: }
1812:
1813: try {
1814: PropertyUtils.setMappedProperty(bean, "mappedProperty",
1815: "Third Value");
1816: fail("Should throw IllegalArgumentException 6");
1817: } catch (IllegalArgumentException e) {
1818: // Expected response
1819: } catch (Throwable t) {
1820: fail("Threw " + t
1821: + " instead of IllegalArgumentException 6");
1822: }
1823:
1824: }
1825:
1826: /**
1827: * Positive and negative tests on setMappedProperty valid arguments.
1828: */
1829: public void testSetMappedValues() {
1830:
1831: Object value = null;
1832:
1833: // Use explicit key argument
1834:
1835: try {
1836: value = PropertyUtils.getMappedProperty(bean,
1837: "mappedProperty", "Fourth Key");
1838: assertNull("Can not find fourth value", value);
1839: } catch (Throwable t) {
1840: fail("Finding fourth value threw " + t);
1841: }
1842:
1843: try {
1844: PropertyUtils.setMappedProperty(bean, "mappedProperty",
1845: "Fourth Key", "Fourth Value");
1846: } catch (Throwable t) {
1847: fail("Setting fourth value threw " + t);
1848: }
1849:
1850: try {
1851: value = PropertyUtils.getMappedProperty(bean,
1852: "mappedProperty", "Fourth Key");
1853: assertEquals("Can find fourth value", "Fourth Value", value);
1854: } catch (Throwable t) {
1855: fail("Finding fourth value threw " + t);
1856: }
1857:
1858: // Use key expression with parentheses
1859:
1860: try {
1861: value = PropertyUtils.getMappedProperty(bean,
1862: "mappedProperty(Fifth Key)");
1863: assertNull("Can not find fifth value", value);
1864: } catch (Throwable t) {
1865: fail("Finding fifth value threw " + t);
1866: }
1867:
1868: try {
1869: PropertyUtils.setMappedProperty(bean,
1870: "mappedProperty(Fifth Key)", "Fifth Value");
1871: } catch (Throwable t) {
1872: fail("Setting fifth value threw " + t);
1873: }
1874:
1875: try {
1876: value = PropertyUtils.getMappedProperty(bean,
1877: "mappedProperty(Fifth Key)");
1878: assertEquals("Can find fifth value", "Fifth Value", value);
1879: } catch (Throwable t) {
1880: fail("Finding fifth value threw " + t);
1881: }
1882:
1883: // Use key expression with dotted expression
1884:
1885: try {
1886: value = PropertyUtils.getNestedProperty(bean,
1887: "mapProperty.Sixth Key");
1888: assertNull("Can not find sixth value", value);
1889: } catch (Throwable t) {
1890: fail("Finding fifth value threw " + t);
1891: }
1892:
1893: try {
1894: PropertyUtils.setNestedProperty(bean,
1895: "mapProperty.Sixth Key", "Sixth Value");
1896: } catch (Throwable t) {
1897: fail("Setting sixth value threw " + t);
1898: }
1899:
1900: try {
1901: value = PropertyUtils.getNestedProperty(bean,
1902: "mapProperty.Sixth Key");
1903: assertEquals("Can find sixth value", "Sixth Value", value);
1904: } catch (Throwable t) {
1905: fail("Finding sixth value threw " + t);
1906: }
1907:
1908: }
1909:
1910: /**
1911: * Corner cases on setNestedProperty invalid arguments.
1912: */
1913: public void testSetNestedArguments() {
1914:
1915: try {
1916: PropertyUtils.setNestedProperty(null, "stringProperty", "");
1917: fail("Should throw IllegalArgumentException 1");
1918: } catch (IllegalArgumentException e) {
1919: // Expected response
1920: } catch (Throwable t) {
1921: fail("Threw " + t
1922: + " instead of IllegalArgumentException 1");
1923: }
1924:
1925: try {
1926: PropertyUtils.setNestedProperty(bean, null, "");
1927: fail("Should throw IllegalArgumentException 2");
1928: } catch (IllegalArgumentException e) {
1929: // Expected response
1930: } catch (Throwable t) {
1931: fail("Threw " + t
1932: + " instead of IllegalArgumentException 2");
1933: }
1934:
1935: }
1936:
1937: /**
1938: * Test setNextedProperty on a boolean property.
1939: */
1940: public void testSetNestedBoolean() {
1941:
1942: try {
1943: boolean oldValue = nested.getBooleanProperty();
1944: boolean newValue = !oldValue;
1945: PropertyUtils.setNestedProperty(bean,
1946: "nested.booleanProperty", new Boolean(newValue));
1947: assertTrue("Matched new value", newValue == nested
1948: .getBooleanProperty());
1949: } catch (IllegalAccessException e) {
1950: fail("IllegalAccessException");
1951: } catch (IllegalArgumentException e) {
1952: fail("IllegalArgumentException");
1953: } catch (InvocationTargetException e) {
1954: fail("InvocationTargetException");
1955: } catch (NoSuchMethodException e) {
1956: fail("NoSuchMethodException");
1957: }
1958:
1959: }
1960:
1961: /**
1962: * Test setNestedProperty on a double property.
1963: */
1964: public void testSetNestedDouble() {
1965:
1966: try {
1967: double oldValue = nested.getDoubleProperty();
1968: double newValue = oldValue + 1.0;
1969: PropertyUtils.setNestedProperty(bean,
1970: "nested.doubleProperty", new Double(newValue));
1971: assertEquals("Matched new value", newValue, nested
1972: .getDoubleProperty(), 0.005);
1973: } catch (IllegalAccessException e) {
1974: fail("IllegalAccessException");
1975: } catch (IllegalArgumentException e) {
1976: fail("IllegalArgumentException");
1977: } catch (InvocationTargetException e) {
1978: fail("InvocationTargetException");
1979: } catch (NoSuchMethodException e) {
1980: fail("NoSuchMethodException");
1981: }
1982:
1983: }
1984:
1985: /**
1986: * Test setNestedProperty on a float property.
1987: */
1988: public void testSetNestedFloat() {
1989:
1990: try {
1991: float oldValue = nested.getFloatProperty();
1992: float newValue = oldValue + (float) 1.0;
1993: PropertyUtils.setNestedProperty(bean,
1994: "nested.floatProperty", new Float(newValue));
1995: assertEquals("Matched new value", newValue, nested
1996: .getFloatProperty(), (float) 0.005);
1997: } catch (IllegalAccessException e) {
1998: fail("IllegalAccessException");
1999: } catch (IllegalArgumentException e) {
2000: fail("IllegalArgumentException");
2001: } catch (InvocationTargetException e) {
2002: fail("InvocationTargetException");
2003: } catch (NoSuchMethodException e) {
2004: fail("NoSuchMethodException");
2005: }
2006:
2007: }
2008:
2009: /**
2010: * Test setNestedProperty on a int property.
2011: */
2012: public void testSetNestedInt() {
2013:
2014: try {
2015: int oldValue = nested.getIntProperty();
2016: int newValue = oldValue + 1;
2017: PropertyUtils.setNestedProperty(bean, "nested.intProperty",
2018: new Integer(newValue));
2019: assertEquals("Matched new value", newValue, nested
2020: .getIntProperty());
2021: } catch (IllegalAccessException e) {
2022: fail("IllegalAccessException");
2023: } catch (IllegalArgumentException e) {
2024: fail("IllegalArgumentException");
2025: } catch (InvocationTargetException e) {
2026: fail("InvocationTargetException");
2027: } catch (NoSuchMethodException e) {
2028: fail("NoSuchMethodException");
2029: }
2030:
2031: }
2032:
2033: /**
2034: * Test setNestedProperty on a long property.
2035: */
2036: public void testSetNestedLong() {
2037:
2038: try {
2039: long oldValue = nested.getLongProperty();
2040: long newValue = oldValue + 1;
2041: PropertyUtils.setNestedProperty(bean,
2042: "nested.longProperty", new Long(newValue));
2043: assertEquals("Matched new value", newValue, nested
2044: .getLongProperty());
2045: } catch (IllegalAccessException e) {
2046: fail("IllegalAccessException");
2047: } catch (IllegalArgumentException e) {
2048: fail("IllegalArgumentException");
2049: } catch (InvocationTargetException e) {
2050: fail("InvocationTargetException");
2051: } catch (NoSuchMethodException e) {
2052: fail("NoSuchMethodException");
2053: }
2054:
2055: }
2056:
2057: /**
2058: * Test setNestedProperty on a read-only String property.
2059: */
2060: public void testSetNestedReadOnly() {
2061:
2062: try {
2063: String oldValue = nested.getWriteOnlyPropertyValue();
2064: String newValue = oldValue + " Extra Value";
2065: PropertyUtils.setNestedProperty(bean,
2066: "nested.readOnlyProperty", newValue);
2067: fail("Should have thrown NoSuchMethodException");
2068: } catch (IllegalAccessException e) {
2069: fail("IllegalAccessException");
2070: } catch (IllegalArgumentException e) {
2071: fail("IllegalArgumentException");
2072: } catch (InvocationTargetException e) {
2073: fail("InvocationTargetException");
2074: } catch (NoSuchMethodException e) {
2075: // Correct result for this test
2076: }
2077:
2078: }
2079:
2080: /**
2081: * Test setNestedProperty on a short property.
2082: */
2083: public void testSetNestedShort() {
2084:
2085: try {
2086: short oldValue = nested.getShortProperty();
2087: short newValue = oldValue;
2088: newValue++;
2089: PropertyUtils.setNestedProperty(bean,
2090: "nested.shortProperty", new Short(newValue));
2091: assertEquals("Matched new value", newValue, nested
2092: .getShortProperty());
2093: } catch (IllegalAccessException e) {
2094: fail("IllegalAccessException");
2095: } catch (IllegalArgumentException e) {
2096: fail("IllegalArgumentException");
2097: } catch (InvocationTargetException e) {
2098: fail("InvocationTargetException");
2099: } catch (NoSuchMethodException e) {
2100: fail("NoSuchMethodException");
2101: }
2102:
2103: }
2104:
2105: /**
2106: * Test setNestedProperty on a String property.
2107: */
2108: public void testSetNestedString() {
2109:
2110: try {
2111: String oldValue = nested.getStringProperty();
2112: String newValue = oldValue + " Extra Value";
2113: PropertyUtils.setNestedProperty(bean,
2114: "nested.stringProperty", newValue);
2115: assertEquals("Matched new value", newValue, nested
2116: .getStringProperty());
2117: } catch (IllegalAccessException e) {
2118: fail("IllegalAccessException");
2119: } catch (IllegalArgumentException e) {
2120: fail("IllegalArgumentException");
2121: } catch (InvocationTargetException e) {
2122: fail("InvocationTargetException");
2123: } catch (NoSuchMethodException e) {
2124: fail("NoSuchMethodException");
2125: }
2126:
2127: }
2128:
2129: /**
2130: * Test setNestedProperty on an unknown property name.
2131: */
2132: public void testSetNestedUnknown() {
2133:
2134: try {
2135: String newValue = "New String Value";
2136: PropertyUtils.setNestedProperty(bean, "nested.unknown",
2137: newValue);
2138: fail("Should have thrown NoSuchMethodException");
2139: } catch (IllegalAccessException e) {
2140: fail("IllegalAccessException");
2141: } catch (IllegalArgumentException e) {
2142: fail("IllegalArgumentException");
2143: } catch (InvocationTargetException e) {
2144: fail("InvocationTargetException");
2145: } catch (NoSuchMethodException e) {
2146: // Correct result for this test
2147: }
2148:
2149: }
2150:
2151: /**
2152: * Test setNestedProperty on a write-only String property.
2153: */
2154: public void testSetNestedWriteOnly() {
2155:
2156: try {
2157: String oldValue = nested.getWriteOnlyPropertyValue();
2158: String newValue = oldValue + " Extra Value";
2159: PropertyUtils.setNestedProperty(bean,
2160: "nested.writeOnlyProperty", newValue);
2161: assertEquals("Matched new value", newValue, nested
2162: .getWriteOnlyPropertyValue());
2163: } catch (IllegalAccessException e) {
2164: fail("IllegalAccessException");
2165: } catch (IllegalArgumentException e) {
2166: fail("IllegalArgumentException");
2167: } catch (InvocationTargetException e) {
2168: fail("InvocationTargetException");
2169: } catch (NoSuchMethodException e) {
2170: fail("NoSuchMethodException");
2171: }
2172:
2173: }
2174:
2175: /**
2176: * Corner cases on setSimpleProperty invalid arguments.
2177: */
2178: public void testSetSimpleArguments() {
2179:
2180: try {
2181: PropertyUtils.setSimpleProperty(null, "stringProperty", "");
2182: fail("Should throw IllegalArgumentException 1");
2183: } catch (IllegalArgumentException e) {
2184: // Expected response
2185: } catch (Throwable t) {
2186: fail("Threw " + t
2187: + " instead of IllegalArgumentException 1");
2188: }
2189:
2190: try {
2191: PropertyUtils.setSimpleProperty(bean, null, "");
2192: fail("Should throw IllegalArgumentException 2");
2193: } catch (IllegalArgumentException e) {
2194: // Expected response
2195: } catch (Throwable t) {
2196: fail("Threw " + t
2197: + " instead of IllegalArgumentException 2");
2198: }
2199:
2200: }
2201:
2202: /**
2203: * Test setSimpleProperty on a boolean property.
2204: */
2205: public void testSetSimpleBoolean() {
2206:
2207: try {
2208: boolean oldValue = ((Boolean) bean.get("booleanProperty"))
2209: .booleanValue();
2210: boolean newValue = !oldValue;
2211: PropertyUtils.setSimpleProperty(bean, "booleanProperty",
2212: new Boolean(newValue));
2213: assertTrue("Matched new value", newValue == ((Boolean) bean
2214: .get("booleanProperty")).booleanValue());
2215: } catch (IllegalAccessException e) {
2216: fail("IllegalAccessException");
2217: } catch (IllegalArgumentException e) {
2218: fail("IllegalArgumentException");
2219: } catch (InvocationTargetException e) {
2220: fail("InvocationTargetException");
2221: } catch (NoSuchMethodException e) {
2222: fail("NoSuchMethodException");
2223: }
2224:
2225: }
2226:
2227: /**
2228: * Test setSimpleProperty on a double property.
2229: */
2230: public void testSetSimpleDouble() {
2231:
2232: try {
2233: double oldValue = ((Double) bean.get("doubleProperty"))
2234: .doubleValue();
2235: double newValue = oldValue + 1.0;
2236: PropertyUtils.setSimpleProperty(bean, "doubleProperty",
2237: new Double(newValue));
2238: assertEquals("Matched new value", newValue, ((Double) bean
2239: .get("doubleProperty")).doubleValue(), 0.005);
2240: } catch (IllegalAccessException e) {
2241: fail("IllegalAccessException");
2242: } catch (IllegalArgumentException e) {
2243: fail("IllegalArgumentException");
2244: } catch (InvocationTargetException e) {
2245: fail("InvocationTargetException");
2246: } catch (NoSuchMethodException e) {
2247: fail("NoSuchMethodException");
2248: }
2249:
2250: }
2251:
2252: /**
2253: * Test setSimpleProperty on a float property.
2254: */
2255: public void testSetSimpleFloat() {
2256:
2257: try {
2258: float oldValue = ((Float) bean.get("floatProperty"))
2259: .floatValue();
2260: float newValue = oldValue + (float) 1.0;
2261: PropertyUtils.setSimpleProperty(bean, "floatProperty",
2262: new Float(newValue));
2263: assertEquals("Matched new value", newValue, ((Float) bean
2264: .get("floatProperty")).floatValue(), (float) 0.005);
2265: } catch (IllegalAccessException e) {
2266: fail("IllegalAccessException");
2267: } catch (IllegalArgumentException e) {
2268: fail("IllegalArgumentException");
2269: } catch (InvocationTargetException e) {
2270: fail("InvocationTargetException");
2271: } catch (NoSuchMethodException e) {
2272: fail("NoSuchMethodException");
2273: }
2274:
2275: }
2276:
2277: /**
2278: * Negative test setSimpleProperty on an indexed property.
2279: */
2280: public void testSetSimpleIndexed() {
2281:
2282: try {
2283: PropertyUtils.setSimpleProperty(bean, "stringIndexed[0]",
2284: "New String Value");
2285: fail("Should have thrown IllegalArgumentException");
2286: } catch (IllegalAccessException e) {
2287: fail("IllegalAccessException");
2288: } catch (IllegalArgumentException e) {
2289: // Correct result for this test
2290: } catch (InvocationTargetException e) {
2291: fail("InvocationTargetException");
2292: } catch (NoSuchMethodException e) {
2293: fail("NoSuchMethodException");
2294: }
2295:
2296: }
2297:
2298: /**
2299: * Test setSimpleProperty on a int property.
2300: */
2301: public void testSetSimpleInt() {
2302:
2303: try {
2304: int oldValue = ((Integer) bean.get("intProperty"))
2305: .intValue();
2306: int newValue = oldValue + 1;
2307: PropertyUtils.setSimpleProperty(bean, "intProperty",
2308: new Integer(newValue));
2309: assertEquals("Matched new value", newValue, ((Integer) bean
2310: .get("intProperty")).intValue());
2311: } catch (IllegalAccessException e) {
2312: fail("IllegalAccessException");
2313: } catch (IllegalArgumentException e) {
2314: fail("IllegalArgumentException");
2315: } catch (InvocationTargetException e) {
2316: fail("InvocationTargetException");
2317: } catch (NoSuchMethodException e) {
2318: fail("NoSuchMethodException");
2319: }
2320:
2321: }
2322:
2323: /**
2324: * Test setSimpleProperty on a long property.
2325: */
2326: public void testSetSimpleLong() {
2327:
2328: try {
2329: long oldValue = ((Long) bean.get("longProperty"))
2330: .longValue();
2331: long newValue = oldValue + 1;
2332: PropertyUtils.setSimpleProperty(bean, "longProperty",
2333: new Long(newValue));
2334: assertEquals("Matched new value", newValue, ((Long) bean
2335: .get("longProperty")).longValue());
2336: } catch (IllegalAccessException e) {
2337: fail("IllegalAccessException");
2338: } catch (IllegalArgumentException e) {
2339: fail("IllegalArgumentException");
2340: } catch (InvocationTargetException e) {
2341: fail("InvocationTargetException");
2342: } catch (NoSuchMethodException e) {
2343: fail("NoSuchMethodException");
2344: }
2345:
2346: }
2347:
2348: /**
2349: * Negative test setSimpleProperty on a nested property.
2350: */
2351: public void testSetSimpleNested() {
2352:
2353: try {
2354: PropertyUtils.setSimpleProperty(bean,
2355: "nested.stringProperty", "New String Value");
2356: fail("Should have thrown IllegalArgumentException");
2357: } catch (IllegalAccessException e) {
2358: fail("IllegalAccessException");
2359: } catch (IllegalArgumentException e) {
2360: // Correct result for this test
2361: } catch (InvocationTargetException e) {
2362: fail("InvocationTargetException");
2363: } catch (NoSuchMethodException e) {
2364: fail("NoSuchMethodException");
2365: }
2366:
2367: }
2368:
2369: /**
2370: * Test setSimpleProperty on a short property.
2371: */
2372: public void testSetSimpleShort() {
2373:
2374: try {
2375: short oldValue = ((Short) bean.get("shortProperty"))
2376: .shortValue();
2377: short newValue = oldValue;
2378: newValue++;
2379: PropertyUtils.setSimpleProperty(bean, "shortProperty",
2380: new Short(newValue));
2381: assertEquals("Matched new value", newValue, ((Short) bean
2382: .get("shortProperty")).shortValue());
2383: } catch (IllegalAccessException e) {
2384: fail("IllegalAccessException");
2385: } catch (IllegalArgumentException e) {
2386: fail("IllegalArgumentException");
2387: } catch (InvocationTargetException e) {
2388: fail("InvocationTargetException");
2389: } catch (NoSuchMethodException e) {
2390: fail("NoSuchMethodException");
2391: }
2392:
2393: }
2394:
2395: /**
2396: * Test setSimpleProperty on a String property.
2397: */
2398: public void testSetSimpleString() {
2399:
2400: try {
2401: String oldValue = (String) bean.get("stringProperty");
2402: String newValue = oldValue + " Extra Value";
2403: PropertyUtils.setSimpleProperty(bean, "stringProperty",
2404: newValue);
2405: assertEquals("Matched new value", newValue, (String) bean
2406: .get("stringProperty"));
2407: } catch (IllegalAccessException e) {
2408: fail("IllegalAccessException");
2409: } catch (IllegalArgumentException e) {
2410: fail("IllegalArgumentException");
2411: } catch (InvocationTargetException e) {
2412: fail("InvocationTargetException");
2413: } catch (NoSuchMethodException e) {
2414: fail("NoSuchMethodException");
2415: }
2416:
2417: }
2418:
2419: /**
2420: * Test setSimpleProperty on an unknown property name.
2421: */
2422: public void testSetSimpleUnknown() {
2423:
2424: try {
2425: String newValue = "New String Value";
2426: PropertyUtils.setSimpleProperty(bean, "unknown", newValue);
2427: fail("Should have thrown NoSuchMethodException");
2428: } catch (IllegalAccessException e) {
2429: fail("IllegalAccessException");
2430: } catch (IllegalArgumentException e) {
2431: fail("IllegalArgumentException");
2432: } catch (InvocationTargetException e) {
2433: fail("InvocationTargetException");
2434: } catch (NoSuchMethodException e) {
2435: // Correct result for this test
2436: assertEquals("Unknown property 'unknown' on dynaclass '"
2437: + ((DynaBean) bean).getDynaClass() + "'", e
2438: .getMessage());
2439: }
2440:
2441: }
2442:
2443: // ------------------------------------------------------ Protected Methods
2444:
2445: /**
2446: * Create and return a <code>DynaClass</code> instance for our test
2447: * <code>DynaBean</code>.
2448: */
2449: protected DynaClass createDynaClass() {
2450:
2451: int intArray[] = new int[0];
2452: String stringArray[] = new String[0];
2453:
2454: DynaClass dynaClass = new BasicDynaClass(
2455: "TestDynaClass",
2456: null,
2457: new DynaProperty[] {
2458: new DynaProperty("booleanProperty",
2459: Boolean.TYPE),
2460: new DynaProperty("booleanSecond", Boolean.TYPE),
2461: new DynaProperty("doubleProperty", Double.TYPE),
2462: new DynaProperty("dupProperty", stringArray
2463: .getClass()),
2464: new DynaProperty("floatProperty", Float.TYPE),
2465: new DynaProperty("intArray", intArray
2466: .getClass()),
2467: new DynaProperty("intIndexed", intArray
2468: .getClass()),
2469: new DynaProperty("intProperty", Integer.TYPE),
2470: new DynaProperty("listIndexed", List.class),
2471: new DynaProperty("longProperty", Long.TYPE),
2472: new DynaProperty("mapProperty", Map.class),
2473: new DynaProperty("mappedObjects", Map.class),
2474: new DynaProperty("mappedProperty", Map.class),
2475: new DynaProperty("mappedIntProperty", Map.class),
2476: new DynaProperty("nested", TestBean.class),
2477: new DynaProperty("nullProperty", String.class),
2478: new DynaProperty("shortProperty", Short.TYPE),
2479: new DynaProperty("stringArray", stringArray
2480: .getClass()),
2481: new DynaProperty("stringIndexed", stringArray
2482: .getClass()),
2483: new DynaProperty("stringProperty", String.class), });
2484: return (dynaClass);
2485:
2486: }
2487:
2488: }
|