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.Calendar;
0022: import java.util.HashMap;
0023: import java.util.Iterator;
0024: import java.util.Locale;
0025: import java.util.Map;
0026: import java.util.StringTokenizer;
0027:
0028: import org.apache.commons.beanutils.converters.ArrayConverter;
0029: import org.apache.commons.beanutils.converters.DateConverter;
0030:
0031: import junit.framework.Test;
0032: import junit.framework.TestCase;
0033: import junit.framework.TestSuite;
0034:
0035: /**
0036: * <p>
0037: * Test Case for the BeanUtils class. The majority of these tests use
0038: * instances of the TestBean class, so be sure to update the tests if you
0039: * change the characteristics of that class.
0040: * </p>
0041: *
0042: * <p>
0043: * Template for this stolen from Craigs PropertyUtilsTestCase
0044: * </p>
0045: *
0046: * <p>
0047: * Note that the tests are dependant upon the static aspects
0048: * (such as array sizes...) of the TestBean.java class, so ensure
0049: * than all changes to TestBean are reflected here.
0050: * </p>
0051: *
0052: * <p>
0053: * So far, this test case has tests for the following methods of the
0054: * <code>BeanUtils</code> class:
0055: * </p>
0056: * <ul>
0057: * <li>getArrayProperty(Object bean, String name)</li>
0058: * </ul>
0059: *
0060: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
0061: * @version $Revision: 552381 $
0062: */
0063:
0064: public class BeanUtilsTestCase extends TestCase {
0065:
0066: // ---------------------------------------------------- Instance Variables
0067:
0068: /**
0069: * The test bean for each test.
0070: */
0071: protected TestBean bean = null;
0072:
0073: /**
0074: * The set of properties that should be described.
0075: */
0076: protected String describes[] = { "booleanProperty",
0077: "booleanSecond", "byteProperty", "doubleProperty",
0078: "dupProperty", "floatProperty", "intArray",
0079: // "intIndexed",
0080: "longProperty", "listIndexed",
0081: "longProperty",
0082: // "mappedProperty",
0083: // "mappedIntProperty",
0084: "nested", "nullProperty", "readOnlyProperty",
0085: "shortProperty", "stringArray",
0086: // "stringIndexed",
0087: "stringProperty" };
0088:
0089: /** Test Calendar value */
0090: protected java.util.Calendar testCalendar;
0091:
0092: /** Test java.util.Date value */
0093: protected java.util.Date testUtilDate;
0094:
0095: /** Test String Date value */
0096: protected String testStringDate;
0097:
0098: // ---------------------------------------------------------- Constructors
0099:
0100: /**
0101: * Construct a new instance of this test case.
0102: *
0103: * @param name Name of the test case
0104: */
0105: public BeanUtilsTestCase(String name) {
0106: super (name);
0107: }
0108:
0109: // -------------------------------------------------- Overall Test Methods
0110:
0111: /**
0112: * Set up instance variables required by this test case.
0113: */
0114: public void setUp() {
0115: ConvertUtils.deregister();
0116: BeanUtilsBean.setInstance(new BeanUtilsBean());
0117: setUpShared();
0118: }
0119:
0120: /**
0121: * Shared Set up.
0122: */
0123: protected void setUpShared() {
0124: bean = new TestBean();
0125:
0126: DateConverter dateConverter = new DateConverter(null);
0127: dateConverter.setLocale(Locale.US);
0128: dateConverter.setPattern("dd.MM.yyyy");
0129: ConvertUtils.register(dateConverter, java.util.Date.class);
0130:
0131: ArrayConverter dateArrayConverter = new ArrayConverter(
0132: java.util.Date[].class, dateConverter, 0);
0133: ConvertUtils.register(dateArrayConverter,
0134: java.util.Date[].class);
0135:
0136: testCalendar = Calendar.getInstance();
0137: testCalendar.set(1992, 11, 28, 0, 0, 0);
0138: testCalendar.set(Calendar.MILLISECOND, 0);
0139: testUtilDate = testCalendar.getTime();
0140: testStringDate = "28.12.1992";
0141: }
0142:
0143: /**
0144: * Return the tests included in this test suite.
0145: */
0146: public static Test suite() {
0147: return (new TestSuite(BeanUtilsTestCase.class));
0148: }
0149:
0150: /**
0151: * Tear down instance variables required by this test case.
0152: */
0153: public void tearDown() {
0154: bean = null;
0155: }
0156:
0157: // ------------------------------------------------ Individual Test Methods
0158:
0159: /**
0160: * Test the copyProperties() method from a DynaBean.
0161: */
0162: public void testCopyPropertiesDynaBean() {
0163:
0164: // Set up an origin bean with customized properties
0165: DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
0166: DynaBean orig = null;
0167: try {
0168: orig = dynaClass.newInstance();
0169: } catch (Exception e) {
0170: fail("newInstance(): " + e);
0171: }
0172: orig.set("booleanProperty", Boolean.FALSE);
0173: orig.set("byteProperty", new Byte((byte) 111));
0174: orig.set("doubleProperty", new Double(333.33));
0175: orig.set("dupProperty", new String[] { "New 0", "New 1",
0176: "New 2" });
0177: orig.set("intArray", new int[] { 100, 200, 300 });
0178: orig.set("intProperty", new Integer(333));
0179: orig.set("longProperty", new Long(3333));
0180: orig.set("shortProperty", new Short((short) 33));
0181: orig.set("stringArray", new String[] { "New 0", "New 1" });
0182: orig.set("stringProperty", "Custom string");
0183:
0184: // Copy the origin bean to our destination test bean
0185: try {
0186: BeanUtils.copyProperties(bean, orig);
0187: } catch (Exception e) {
0188: fail("Threw exception: " + e);
0189: }
0190:
0191: // Validate the results for scalar properties
0192: assertEquals("Copied boolean property", false, bean
0193: .getBooleanProperty());
0194: assertEquals("Copied byte property", (byte) 111, bean
0195: .getByteProperty());
0196: assertEquals("Copied double property", 333.33, bean
0197: .getDoubleProperty(), 0.005);
0198: assertEquals("Copied int property", 333, bean.getIntProperty());
0199: assertEquals("Copied long property", 3333, bean
0200: .getLongProperty());
0201: assertEquals("Copied short property", (short) 33, bean
0202: .getShortProperty());
0203: assertEquals("Copied string property", "Custom string", bean
0204: .getStringProperty());
0205:
0206: // Validate the results for array properties
0207: String dupProperty[] = bean.getDupProperty();
0208: assertNotNull("dupProperty present", dupProperty);
0209: assertEquals("dupProperty length", 3, dupProperty.length);
0210: assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0211: assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0212: assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0213: int intArray[] = bean.getIntArray();
0214: assertNotNull("intArray present", intArray);
0215: assertEquals("intArray length", 3, intArray.length);
0216: assertEquals("intArray[0]", 100, intArray[0]);
0217: assertEquals("intArray[1]", 200, intArray[1]);
0218: assertEquals("intArray[2]", 300, intArray[2]);
0219: String stringArray[] = bean.getStringArray();
0220: assertNotNull("stringArray present", stringArray);
0221: assertEquals("stringArray length", 2, stringArray.length);
0222: assertEquals("stringArray[0]", "New 0", stringArray[0]);
0223: assertEquals("stringArray[1]", "New 1", stringArray[1]);
0224:
0225: }
0226:
0227: /**
0228: * Test copyProperties() when the origin is a a <code>Map</code>.
0229: */
0230: public void testCopyPropertiesMap() {
0231:
0232: Map map = new HashMap();
0233: map.put("booleanProperty", "false");
0234: map.put("byteProperty", "111");
0235: map.put("doubleProperty", "333.0");
0236: map.put("dupProperty",
0237: new String[] { "New 0", "New 1", "New 2" });
0238: map.put("floatProperty", "222.0");
0239: map.put("intArray", new String[] { "0", "100", "200" });
0240: map.put("intProperty", "111");
0241: map.put("longProperty", "444");
0242: map.put("shortProperty", "555");
0243: map.put("stringProperty", "New String Property");
0244:
0245: try {
0246: BeanUtils.copyProperties(bean, map);
0247: } catch (Throwable t) {
0248: fail("Threw " + t.toString());
0249: }
0250:
0251: // Scalar properties
0252: assertEquals("booleanProperty", false, bean
0253: .getBooleanProperty());
0254: assertEquals("byteProperty", (byte) 111, bean.getByteProperty());
0255: assertEquals("doubleProperty", 333.0, bean.getDoubleProperty(),
0256: 0.005);
0257: assertEquals("floatProperty", (float) 222.0, bean
0258: .getFloatProperty(), (float) 0.005);
0259: assertEquals("longProperty", 111, bean.getIntProperty());
0260: assertEquals("longProperty", 444, bean.getLongProperty());
0261: assertEquals("shortProperty", (short) 555, bean
0262: .getShortProperty());
0263: assertEquals("stringProperty", "New String Property", bean
0264: .getStringProperty());
0265:
0266: // Indexed Properties
0267: String dupProperty[] = bean.getDupProperty();
0268: assertNotNull("dupProperty present", dupProperty);
0269: assertEquals("dupProperty length", 3, dupProperty.length);
0270: assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0271: assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0272: assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0273: int intArray[] = bean.getIntArray();
0274: assertNotNull("intArray present", intArray);
0275: assertEquals("intArray length", 3, intArray.length);
0276: assertEquals("intArray[0]", 0, intArray[0]);
0277: assertEquals("intArray[1]", 100, intArray[1]);
0278: assertEquals("intArray[2]", 200, intArray[2]);
0279:
0280: }
0281:
0282: /**
0283: * Test the copyProperties() method from a standard JavaBean.
0284: */
0285: public void testCopyPropertiesStandard() {
0286:
0287: // Set up an origin bean with customized properties
0288: TestBean orig = new TestBean();
0289: orig.setBooleanProperty(false);
0290: orig.setByteProperty((byte) 111);
0291: orig.setDoubleProperty(333.33);
0292: orig.setDupProperty(new String[] { "New 0", "New 1", "New 2" });
0293: orig.setIntArray(new int[] { 100, 200, 300 });
0294: orig.setIntProperty(333);
0295: orig.setLongProperty(3333);
0296: orig.setShortProperty((short) 33);
0297: orig.setStringArray(new String[] { "New 0", "New 1" });
0298: orig.setStringProperty("Custom string");
0299:
0300: // Copy the origin bean to our destination test bean
0301: try {
0302: BeanUtils.copyProperties(bean, orig);
0303: } catch (Exception e) {
0304: fail("Threw exception: " + e);
0305: }
0306:
0307: // Validate the results for scalar properties
0308: assertEquals("Copied boolean property", false, bean
0309: .getBooleanProperty());
0310: assertEquals("Copied byte property", (byte) 111, bean
0311: .getByteProperty());
0312: assertEquals("Copied double property", 333.33, bean
0313: .getDoubleProperty(), 0.005);
0314: assertEquals("Copied int property", 333, bean.getIntProperty());
0315: assertEquals("Copied long property", 3333, bean
0316: .getLongProperty());
0317: assertEquals("Copied short property", (short) 33, bean
0318: .getShortProperty());
0319: assertEquals("Copied string property", "Custom string", bean
0320: .getStringProperty());
0321:
0322: // Validate the results for array properties
0323: String dupProperty[] = bean.getDupProperty();
0324: assertNotNull("dupProperty present", dupProperty);
0325: assertEquals("dupProperty length", 3, dupProperty.length);
0326: assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0327: assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0328: assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0329: int intArray[] = bean.getIntArray();
0330: assertNotNull("intArray present", intArray);
0331: assertEquals("intArray length", 3, intArray.length);
0332: assertEquals("intArray[0]", 100, intArray[0]);
0333: assertEquals("intArray[1]", 200, intArray[1]);
0334: assertEquals("intArray[2]", 300, intArray[2]);
0335: String stringArray[] = bean.getStringArray();
0336: assertNotNull("stringArray present", stringArray);
0337: assertEquals("stringArray length", 2, stringArray.length);
0338: assertEquals("stringArray[0]", "New 0", stringArray[0]);
0339: assertEquals("stringArray[1]", "New 1", stringArray[1]);
0340:
0341: }
0342:
0343: /**
0344: * Test the describe() method.
0345: */
0346: public void testDescribe() {
0347:
0348: Map map = null;
0349: try {
0350: map = BeanUtils.describe(bean);
0351: } catch (Exception e) {
0352: fail("Threw exception " + e);
0353: }
0354:
0355: // Verify existence of all the properties that should be present
0356: for (int i = 0; i < describes.length; i++) {
0357: assertTrue("Property '" + describes[i] + "' is present",
0358: map.containsKey(describes[i]));
0359: }
0360: assertTrue("Property 'writeOnlyProperty' is not present", !map
0361: .containsKey("writeOnlyProperty"));
0362:
0363: // Verify the values of scalar properties
0364: assertEquals("Value of 'booleanProperty'", "true", (String) map
0365: .get("booleanProperty"));
0366: assertEquals("Value of 'byteProperty'", "121", (String) map
0367: .get("byteProperty"));
0368: assertEquals("Value of 'doubleProperty'", "321.0", (String) map
0369: .get("doubleProperty"));
0370: assertEquals("Value of 'floatProperty'", "123.0", (String) map
0371: .get("floatProperty"));
0372: assertEquals("Value of 'intProperty'", "123", (String) map
0373: .get("intProperty"));
0374: assertEquals("Value of 'longProperty'", "321", (String) map
0375: .get("longProperty"));
0376: assertEquals("Value of 'shortProperty'", "987", (String) map
0377: .get("shortProperty"));
0378: assertEquals("Value of 'stringProperty'", "This is a string",
0379: (String) map.get("stringProperty"));
0380:
0381: }
0382:
0383: /**
0384: * tests the string and int arrays of TestBean
0385: */
0386: public void testGetArrayProperty() {
0387: try {
0388: String arr[] = BeanUtils.getArrayProperty(bean,
0389: "stringArray");
0390: String comp[] = bean.getStringArray();
0391:
0392: assertTrue("String array length = " + comp.length,
0393: (comp.length == arr.length));
0394:
0395: arr = BeanUtils.getArrayProperty(bean, "intArray");
0396: int iarr[] = bean.getIntArray();
0397:
0398: assertTrue("String array length = " + iarr.length,
0399: (iarr.length == arr.length));
0400:
0401: // Test property which isn't array or collection
0402: arr = BeanUtils.getArrayProperty(bean, "shortProperty");
0403: String shortAsString = "" + bean.getShortProperty();
0404: assertEquals("Short List Test lth", 1, arr.length);
0405: assertEquals("Short Test value", shortAsString, arr[0]);
0406:
0407: // Test comma delimited list
0408: String value1 = "ABC";
0409: bean.setStringProperty("ABC");
0410: arr = BeanUtils.getArrayProperty(bean, "stringProperty");
0411: assertEquals("Delimited List Test lth", 1, arr.length);
0412: assertEquals("Delimited List Test value1", "ABC", arr[0]);
0413:
0414: } catch (IllegalAccessException e) {
0415: fail("IllegalAccessException");
0416: } catch (InvocationTargetException e) {
0417: fail("InvocationTargetException");
0418: } catch (NoSuchMethodException e) {
0419: fail("NoSuchMethodException");
0420: }
0421:
0422: }
0423:
0424: /**
0425: * Test <code>getArrayProperty()</code> converting to a String.
0426: */
0427: public void testGetArrayPropertyDate() {
0428: String[] value = null;
0429: try {
0430: bean
0431: .setDateArrayProperty(new java.util.Date[] { testUtilDate });
0432: value = BeanUtils.getArrayProperty(bean,
0433: "dateArrayProperty");
0434: } catch (Throwable t) {
0435: fail("Threw " + t);
0436: }
0437: assertEquals("java.util.Date[] --> String[] length", 1,
0438: value.length);
0439: assertEquals("java.util.Date[] --> String[] value ",
0440: testUtilDate.toString(), value[0]);
0441: }
0442:
0443: /**
0444: * tests getting an indexed property
0445: */
0446: public void testGetIndexedProperty1() {
0447: try {
0448: String val = BeanUtils.getIndexedProperty(bean,
0449: "intIndexed[3]");
0450: String comp = String.valueOf(bean.getIntIndexed(3));
0451: assertTrue("intIndexed[3] == " + comp, val.equals(comp));
0452:
0453: val = BeanUtils
0454: .getIndexedProperty(bean, "stringIndexed[3]");
0455: comp = bean.getStringIndexed(3);
0456: assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
0457: } catch (IllegalAccessException e) {
0458: fail("IllegalAccessException");
0459: } catch (InvocationTargetException e) {
0460: fail("InvocationTargetException");
0461: } catch (NoSuchMethodException e) {
0462: fail("NoSuchMethodException");
0463: }
0464: }
0465:
0466: /**
0467: * Test <code>getArrayProperty()</code> converting to a String.
0468: */
0469: public void testGetIndexedPropertyDate() {
0470: String value = null;
0471: try {
0472: bean
0473: .setDateArrayProperty(new java.util.Date[] { testUtilDate });
0474: value = BeanUtils.getIndexedProperty(bean,
0475: "dateArrayProperty[0]");
0476: } catch (Throwable t) {
0477: fail("Threw " + t);
0478: }
0479: assertEquals("java.util.Date[0] --> String", testUtilDate
0480: .toString(), value);
0481: }
0482:
0483: /**
0484: * tests getting an indexed property
0485: */
0486: public void testGetIndexedProperty2() {
0487: try {
0488: String val = BeanUtils.getIndexedProperty(bean,
0489: "intIndexed", 3);
0490: String comp = String.valueOf(bean.getIntIndexed(3));
0491:
0492: assertTrue("intIndexed,3 == " + comp, val.equals(comp));
0493:
0494: val = BeanUtils
0495: .getIndexedProperty(bean, "stringIndexed", 3);
0496: comp = bean.getStringIndexed(3);
0497:
0498: assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
0499:
0500: } catch (IllegalAccessException e) {
0501: fail("IllegalAccessException");
0502: } catch (InvocationTargetException e) {
0503: fail("InvocationTargetException");
0504: } catch (NoSuchMethodException e) {
0505: fail("NoSuchMethodException");
0506: }
0507: }
0508:
0509: /**
0510: * tests getting a nested property
0511: */
0512: public void testGetNestedProperty() {
0513: try {
0514: String val = BeanUtils.getNestedProperty(bean,
0515: "nested.stringProperty");
0516: String comp = bean.getNested().getStringProperty();
0517: assertTrue("nested.StringProperty == " + comp, val
0518: .equals(comp));
0519: } catch (IllegalAccessException e) {
0520: fail("IllegalAccessException");
0521: } catch (InvocationTargetException e) {
0522: fail("InvocationTargetException");
0523: } catch (NoSuchMethodException e) {
0524: fail("NoSuchMethodException");
0525: }
0526: }
0527:
0528: /**
0529: * tests getting a 'whatever' property
0530: */
0531: public void testGetGeneralProperty() {
0532: try {
0533: String val = BeanUtils.getProperty(bean,
0534: "nested.intIndexed[2]");
0535: String comp = String.valueOf(bean.getIntIndexed(2));
0536:
0537: assertTrue("nested.intIndexed[2] == " + comp, val
0538: .equals(comp));
0539: } catch (IllegalAccessException e) {
0540: fail("IllegalAccessException");
0541: } catch (InvocationTargetException e) {
0542: fail("InvocationTargetException");
0543: } catch (NoSuchMethodException e) {
0544: fail("NoSuchMethodException");
0545: }
0546: }
0547:
0548: /**
0549: * tests getting a 'whatever' property
0550: */
0551: public void testGetSimpleProperty() {
0552: try {
0553: String val = BeanUtils.getSimpleProperty(bean,
0554: "shortProperty");
0555: String comp = String.valueOf(bean.getShortProperty());
0556:
0557: assertTrue("shortProperty == " + comp, val.equals(comp));
0558: } catch (IllegalAccessException e) {
0559: fail("IllegalAccessException");
0560: } catch (InvocationTargetException e) {
0561: fail("InvocationTargetException");
0562: } catch (NoSuchMethodException e) {
0563: fail("NoSuchMethodException");
0564: }
0565: }
0566:
0567: /**
0568: * Test <code>getSimpleProperty()</code> converting to a String.
0569: */
0570: public void testGetSimplePropertyDate() {
0571: String value = null;
0572: try {
0573: bean.setDateProperty(testUtilDate);
0574: value = BeanUtils.getSimpleProperty(bean, "dateProperty");
0575: } catch (Throwable t) {
0576: fail("Threw " + t);
0577: }
0578: assertEquals("java.util.Date --> String", testUtilDate
0579: .toString(), value);
0580: }
0581:
0582: /**
0583: * Test populate() method on individual array elements.
0584: */
0585: public void testPopulateArrayElements() {
0586:
0587: try {
0588:
0589: HashMap map = new HashMap();
0590: map.put("intIndexed[0]", "100");
0591: map.put("intIndexed[2]", "120");
0592: map.put("intIndexed[4]", "140");
0593:
0594: BeanUtils.populate(bean, map);
0595:
0596: assertEquals("intIndexed[0] is 100", 100, bean
0597: .getIntIndexed(0));
0598: assertEquals("intIndexed[1] is 10", 10, bean
0599: .getIntIndexed(1));
0600: assertEquals("intIndexed[2] is 120", 120, bean
0601: .getIntIndexed(2));
0602: assertEquals("intIndexed[3] is 30", 30, bean
0603: .getIntIndexed(3));
0604: assertEquals("intIndexed[4] is 140", 140, bean
0605: .getIntIndexed(4));
0606:
0607: map.clear();
0608: map.put("stringIndexed[1]", "New String 1");
0609: map.put("stringIndexed[3]", "New String 3");
0610:
0611: BeanUtils.populate(bean, map);
0612:
0613: assertEquals("stringIndexed[0] is \"String 0\"",
0614: "String 0", bean.getStringIndexed(0));
0615: assertEquals("stringIndexed[1] is \"New String 1\"",
0616: "New String 1", bean.getStringIndexed(1));
0617: assertEquals("stringIndexed[2] is \"String 2\"",
0618: "String 2", bean.getStringIndexed(2));
0619: assertEquals("stringIndexed[3] is \"New String 3\"",
0620: "New String 3", bean.getStringIndexed(3));
0621: assertEquals("stringIndexed[4] is \"String 4\"",
0622: "String 4", bean.getStringIndexed(4));
0623:
0624: } catch (IllegalAccessException e) {
0625: fail("IllegalAccessException");
0626: } catch (InvocationTargetException e) {
0627: fail("InvocationTargetException");
0628: }
0629:
0630: }
0631:
0632: /**
0633: * Test populate() method on array properties as a whole.
0634: */
0635: public void testPopulateArrayProperties() {
0636:
0637: try {
0638:
0639: HashMap map = new HashMap();
0640: int intArray[] = new int[] { 123, 456, 789 };
0641: map.put("intArray", intArray);
0642: String stringArray[] = new String[] { "New String 0",
0643: "New String 1" };
0644: map.put("stringArray", stringArray);
0645:
0646: BeanUtils.populate(bean, map);
0647:
0648: intArray = bean.getIntArray();
0649: assertNotNull("intArray is present", intArray);
0650: assertEquals("intArray length", 3, intArray.length);
0651: assertEquals("intArray[0]", 123, intArray[0]);
0652: assertEquals("intArray[1]", 456, intArray[1]);
0653: assertEquals("intArray[2]", 789, intArray[2]);
0654: stringArray = bean.getStringArray();
0655: assertNotNull("stringArray is present", stringArray);
0656: assertEquals("stringArray length", 2, stringArray.length);
0657: assertEquals("stringArray[0]", "New String 0",
0658: stringArray[0]);
0659: assertEquals("stringArray[1]", "New String 1",
0660: stringArray[1]);
0661:
0662: } catch (IllegalAccessException e) {
0663: fail("IllegalAccessException");
0664: } catch (InvocationTargetException e) {
0665: fail("InvocationTargetException");
0666: }
0667:
0668: }
0669:
0670: /**
0671: * Test populate() on mapped properties.
0672: */
0673: public void testPopulateMapped() {
0674:
0675: try {
0676:
0677: HashMap map = new HashMap();
0678: map.put("mappedProperty(First Key)", "New First Value");
0679: map.put("mappedProperty(Third Key)", "New Third Value");
0680:
0681: BeanUtils.populate(bean, map);
0682:
0683: assertEquals("mappedProperty(First Key)",
0684: "New First Value", bean
0685: .getMappedProperty("First Key"));
0686: assertEquals("mappedProperty(Second Key)", "Second Value",
0687: bean.getMappedProperty("Second Key"));
0688: assertEquals("mappedProperty(Third Key)",
0689: "New Third Value", bean
0690: .getMappedProperty("Third Key"));
0691: assertNull("mappedProperty(Fourth Key", bean
0692: .getMappedProperty("Fourth Key"));
0693:
0694: } catch (IllegalAccessException e) {
0695: fail("IllegalAccessException");
0696: } catch (InvocationTargetException e) {
0697: fail("InvocationTargetException");
0698: }
0699:
0700: }
0701:
0702: /**
0703: * Test populate() method on nested properties.
0704: */
0705: public void testPopulateNested() {
0706:
0707: try {
0708:
0709: HashMap map = new HashMap();
0710: map.put("nested.booleanProperty", "false");
0711: // booleanSecond is left at true
0712: map.put("nested.doubleProperty", "432.0");
0713: // floatProperty is left at 123.0
0714: map.put("nested.intProperty", "543");
0715: // longProperty is left at 321
0716: map.put("nested.shortProperty", "654");
0717: // stringProperty is left at "This is a string"
0718: map.put("nested.writeOnlyProperty",
0719: "New writeOnlyProperty value");
0720:
0721: BeanUtils.populate(bean, map);
0722:
0723: assertTrue("booleanProperty is false", !bean.getNested()
0724: .getBooleanProperty());
0725: assertTrue("booleanSecond is true", bean.getNested()
0726: .isBooleanSecond());
0727: assertEquals("doubleProperty is 432.0", 432.0, bean
0728: .getNested().getDoubleProperty(), 0.005);
0729: assertEquals("floatProperty is 123.0", (float) 123.0, bean
0730: .getNested().getFloatProperty(), (float) 0.005);
0731: assertEquals("intProperty is 543", 543, bean.getNested()
0732: .getIntProperty());
0733: assertEquals("longProperty is 321", 321, bean.getNested()
0734: .getLongProperty());
0735: assertEquals("shortProperty is 654", (short) 654, bean
0736: .getNested().getShortProperty());
0737: assertEquals("stringProperty is \"This is a string\"",
0738: "This is a string", bean.getNested()
0739: .getStringProperty());
0740: assertEquals(
0741: "writeOnlyProperty is \"New writeOnlyProperty value\"",
0742: "New writeOnlyProperty value", bean.getNested()
0743: .getWriteOnlyPropertyValue());
0744:
0745: } catch (IllegalAccessException e) {
0746: fail("IllegalAccessException");
0747: } catch (InvocationTargetException e) {
0748: fail("InvocationTargetException");
0749: }
0750:
0751: }
0752:
0753: /**
0754: * Test populate() method on scalar properties.
0755: */
0756: public void testPopulateScalar() {
0757:
0758: try {
0759:
0760: bean.setNullProperty("Non-null value");
0761:
0762: HashMap map = new HashMap();
0763: map.put("booleanProperty", "false");
0764: // booleanSecond is left at true
0765: map.put("byteProperty", "111");
0766: map.put("doubleProperty", "432.0");
0767: // floatProperty is left at 123.0
0768: map.put("intProperty", "543");
0769: map.put("longProperty", "");
0770: map.put("nullProperty", null);
0771: map.put("shortProperty", "654");
0772: // stringProperty is left at "This is a string"
0773: map.put("writeOnlyProperty", "New writeOnlyProperty value");
0774: map.put("readOnlyProperty", "New readOnlyProperty value");
0775:
0776: BeanUtils.populate(bean, map);
0777:
0778: assertTrue("booleanProperty is false", !bean
0779: .getBooleanProperty());
0780: assertTrue("booleanSecond is true", bean.isBooleanSecond());
0781: assertEquals("byteProperty is 111", (byte) 111, bean
0782: .getByteProperty());
0783: assertEquals("doubleProperty is 432.0", 432.0, bean
0784: .getDoubleProperty(), 0.005);
0785: assertEquals("floatProperty is 123.0", (float) 123.0, bean
0786: .getFloatProperty(), (float) 0.005);
0787: assertEquals("intProperty is 543", 543, bean
0788: .getIntProperty());
0789: assertEquals("longProperty is 0", 0, bean.getLongProperty());
0790: assertNull("nullProperty is null", bean.getNullProperty());
0791: assertEquals("shortProperty is 654", (short) 654, bean
0792: .getShortProperty());
0793: assertEquals("stringProperty is \"This is a string\"",
0794: "This is a string", bean.getStringProperty());
0795: assertEquals(
0796: "writeOnlyProperty is \"New writeOnlyProperty value\"",
0797: "New writeOnlyProperty value", bean
0798: .getWriteOnlyPropertyValue());
0799: assertEquals(
0800: "readOnlyProperty is \"Read Only String Property\"",
0801: "Read Only String Property", bean
0802: .getReadOnlyProperty());
0803:
0804: } catch (IllegalAccessException e) {
0805: fail("IllegalAccessException");
0806: } catch (InvocationTargetException e) {
0807: fail("InvocationTargetException");
0808: }
0809:
0810: }
0811:
0812: /**
0813: * Test calling setProperty() with null property values.
0814: */
0815: public void testSetPropertyNullValues() throws Exception {
0816:
0817: Object oldValue = null;
0818: Object newValue = null;
0819:
0820: // Scalar value into array
0821: oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0822: BeanUtils.setProperty(bean, "stringArray", (String) null);
0823: newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0824: assertNotNull("stringArray is not null", newValue);
0825: assertTrue("stringArray of correct type",
0826: newValue instanceof String[]);
0827: assertEquals("stringArray length", 1,
0828: ((String[]) newValue).length);
0829: PropertyUtils.setProperty(bean, "stringArray", oldValue);
0830:
0831: // Indexed value into array
0832: oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0833: BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
0834: newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0835: assertNotNull("stringArray is not null", newValue);
0836: assertTrue("stringArray of correct type",
0837: newValue instanceof String[]);
0838: assertEquals("stringArray length", 5,
0839: ((String[]) newValue).length);
0840: assertTrue("stringArray[2] is null",
0841: ((String[]) newValue)[2] == null);
0842: PropertyUtils.setProperty(bean, "stringArray", oldValue);
0843:
0844: // Value into scalar
0845: BeanUtils.setProperty(bean, "stringProperty", null);
0846: assertTrue("stringProperty is now null", BeanUtils.getProperty(
0847: bean, "stringProperty") == null);
0848:
0849: }
0850:
0851: /**
0852: * Test converting to and from primitive wrapper types.
0853: */
0854: public void testSetPropertyOnPrimitiveWrappers() throws Exception {
0855:
0856: BeanUtils.setProperty(bean, "intProperty", new Integer(1));
0857: assertEquals(1, bean.getIntProperty());
0858: BeanUtils.setProperty(bean, "stringProperty", new Integer(1));
0859: assertEquals(1, Integer.parseInt(bean.getStringProperty()));
0860:
0861: }
0862:
0863: /**
0864: * Test narrowing and widening conversions on byte.
0865: */
0866: public void testSetPropertyByte() throws Exception {
0867:
0868: BeanUtils.setProperty(bean, "byteProperty",
0869: new Byte((byte) 123));
0870: assertEquals((byte) 123, bean.getByteProperty());
0871: /*
0872: BeanUtils.setProperty(bean, "byteProperty", new Double((double) 123));
0873: assertEquals((byte) 123, bean.getByteProperty());
0874: BeanUtils.setProperty(bean, "byteProperty", new Float((float) 123));
0875: assertEquals((byte) 123, bean.getByteProperty());
0876: */
0877: BeanUtils.setProperty(bean, "byteProperty", new Integer(123));
0878: assertEquals((byte) 123, bean.getByteProperty());
0879: BeanUtils.setProperty(bean, "byteProperty", new Long(123));
0880: assertEquals((byte) 123, bean.getByteProperty());
0881: BeanUtils.setProperty(bean, "byteProperty", new Short(
0882: (short) 123));
0883: assertEquals((byte) 123, bean.getByteProperty());
0884:
0885: }
0886:
0887: /**
0888: * Test <code>setProperty()</code> conversion.
0889: */
0890: public void testSetPropertyConvert() {
0891: try {
0892: BeanUtils.setProperty(bean, "dateProperty", testCalendar);
0893: } catch (Throwable t) {
0894: fail("Threw " + t);
0895: }
0896: assertEquals("Calendar --> java.util.Date", testUtilDate, bean
0897: .getDateProperty());
0898: }
0899:
0900: /**
0901: * Test <code>setProperty()</code> converting from a String.
0902: */
0903: public void testSetPropertyConvertFromString() {
0904: try {
0905: BeanUtils.setProperty(bean, "dateProperty", testStringDate);
0906: } catch (Throwable t) {
0907: fail("Threw " + t);
0908: }
0909: assertEquals("String --> java.util.Date", testUtilDate, bean
0910: .getDateProperty());
0911: }
0912:
0913: /**
0914: * Test <code>setProperty()</code> converting to a String.
0915: */
0916: public void testSetPropertyConvertToString() {
0917: try {
0918: BeanUtils.setProperty(bean, "stringProperty", testUtilDate);
0919: } catch (Throwable t) {
0920: fail("Threw " + t);
0921: }
0922: assertEquals("java.util.Date --> String", testUtilDate
0923: .toString(), bean.getStringProperty());
0924: }
0925:
0926: /**
0927: * Test <code>setProperty()</code> converting to a String array.
0928: */
0929: public void testSetPropertyConvertToStringArray() {
0930: try {
0931: bean.setStringArray(null);
0932: BeanUtils.setProperty(bean, "stringArray",
0933: new java.util.Date[] { testUtilDate });
0934: } catch (Throwable t) {
0935: fail("Threw " + t);
0936: }
0937: assertEquals("java.util.Date[] --> String[] length", 1, bean
0938: .getStringArray().length);
0939: assertEquals("java.util.Date[] --> String[] value ",
0940: testUtilDate.toString(), bean.getStringArray()[0]);
0941: }
0942:
0943: /**
0944: * Test <code>setProperty()</code> converting to a String on indexed property
0945: */
0946: public void testSetPropertyConvertToStringIndexed() {
0947: try {
0948: bean.setStringArray(new String[1]);
0949: BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate);
0950: } catch (Throwable t) {
0951: fail("Threw " + t);
0952: }
0953: assertEquals("java.util.Date --> String[]", testUtilDate
0954: .toString(), bean.getStringArray()[0]);
0955: }
0956:
0957: /**
0958: * Test narrowing and widening conversions on double.
0959: */
0960: public void testSetPropertyDouble() throws Exception {
0961:
0962: BeanUtils.setProperty(bean, "doubleProperty", new Byte(
0963: (byte) 123));
0964: assertEquals(123, bean.getDoubleProperty(), 0.005);
0965: BeanUtils.setProperty(bean, "doubleProperty", new Double(123));
0966: assertEquals(123, bean.getDoubleProperty(), 0.005);
0967: BeanUtils.setProperty(bean, "doubleProperty", new Float(123));
0968: assertEquals(123, bean.getDoubleProperty(), 0.005);
0969: BeanUtils.setProperty(bean, "doubleProperty", new Integer(123));
0970: assertEquals(123, bean.getDoubleProperty(), 0.005);
0971: BeanUtils.setProperty(bean, "doubleProperty", new Long(123));
0972: assertEquals(123, bean.getDoubleProperty(), 0.005);
0973: BeanUtils.setProperty(bean, "doubleProperty", new Short(
0974: (short) 123));
0975: assertEquals(123, bean.getDoubleProperty(), 0.005);
0976:
0977: }
0978:
0979: /**
0980: * Test narrowing and widening conversions on float.
0981: */
0982: public void testSetPropertyFloat() throws Exception {
0983:
0984: BeanUtils.setProperty(bean, "floatProperty", new Byte(
0985: (byte) 123));
0986: assertEquals(123, bean.getFloatProperty(), 0.005);
0987: BeanUtils.setProperty(bean, "floatProperty", new Double(123));
0988: assertEquals(123, bean.getFloatProperty(), 0.005);
0989: BeanUtils.setProperty(bean, "floatProperty", new Float(123));
0990: assertEquals(123, bean.getFloatProperty(), 0.005);
0991: BeanUtils.setProperty(bean, "floatProperty", new Integer(123));
0992: assertEquals(123, bean.getFloatProperty(), 0.005);
0993: BeanUtils.setProperty(bean, "floatProperty", new Long(123));
0994: assertEquals(123, bean.getFloatProperty(), 0.005);
0995: BeanUtils.setProperty(bean, "floatProperty", new Short(
0996: (short) 123));
0997: assertEquals(123, bean.getFloatProperty(), 0.005);
0998:
0999: }
1000:
1001: /**
1002: * Test narrowing and widening conversions on int.
1003: */
1004: public void testSetPropertyInteger() throws Exception {
1005:
1006: BeanUtils.setProperty(bean, "longProperty",
1007: new Byte((byte) 123));
1008: assertEquals(123, bean.getIntProperty());
1009: /*
1010: BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
1011: assertEquals((int) 123, bean.getIntProperty());
1012: BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
1013: assertEquals((int) 123, bean.getIntProperty());
1014: */
1015: BeanUtils.setProperty(bean, "longProperty", new Integer(123));
1016: assertEquals(123, bean.getIntProperty());
1017: BeanUtils.setProperty(bean, "longProperty", new Long(123));
1018: assertEquals(123, bean.getIntProperty());
1019: BeanUtils.setProperty(bean, "longProperty", new Short(
1020: (short) 123));
1021: assertEquals(123, bean.getIntProperty());
1022:
1023: }
1024:
1025: /**
1026: * Test narrowing and widening conversions on long.
1027: */
1028: public void testSetPropertyLong() throws Exception {
1029:
1030: BeanUtils.setProperty(bean, "longProperty",
1031: new Byte((byte) 123));
1032: assertEquals(123, bean.getLongProperty());
1033: /*
1034: BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
1035: assertEquals((long) 123, bean.getLongProperty());
1036: BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
1037: assertEquals((long) 123, bean.getLongProperty());
1038: */
1039: BeanUtils.setProperty(bean, "longProperty", new Integer(123));
1040: assertEquals(123, bean.getLongProperty());
1041: BeanUtils.setProperty(bean, "longProperty", new Long(123));
1042: assertEquals(123, bean.getLongProperty());
1043: BeanUtils.setProperty(bean, "longProperty", new Short(
1044: (short) 123));
1045: assertEquals(123, bean.getLongProperty());
1046:
1047: }
1048:
1049: /**
1050: * Test setting a null property value.
1051: */
1052: public void testSetPropertyNull() throws Exception {
1053:
1054: bean.setNullProperty("non-null value");
1055: BeanUtils.setProperty(bean, "nullProperty", null);
1056: assertNull("nullProperty is null", bean.getNullProperty());
1057:
1058: }
1059:
1060: /**
1061: * Test narrowing and widening conversions on short.
1062: */
1063: public void testSetPropertyShort() throws Exception {
1064:
1065: BeanUtils.setProperty(bean, "shortProperty", new Byte(
1066: (byte) 123));
1067: assertEquals((short) 123, bean.getShortProperty());
1068: /*
1069: BeanUtils.setProperty(bean, "shortProperty", new Double((double) 123));
1070: assertEquals((short) 123, bean.getShortProperty());
1071: BeanUtils.setProperty(bean, "shortProperty", new Float((float) 123));
1072: assertEquals((short) 123, bean.getShortProperty());
1073: */
1074: BeanUtils.setProperty(bean, "shortProperty", new Integer(123));
1075: assertEquals((short) 123, bean.getShortProperty());
1076: BeanUtils.setProperty(bean, "shortProperty", new Long(123));
1077: assertEquals((short) 123, bean.getShortProperty());
1078: BeanUtils.setProperty(bean, "shortProperty", new Short(
1079: (short) 123));
1080: assertEquals((short) 123, bean.getShortProperty());
1081:
1082: }
1083:
1084: /**
1085: * Test setting a String value to a String array property
1086: */
1087: public void testSetPropertyStringToArray() throws Exception {
1088: BeanUtils.setProperty(bean, "stringArray", "ABC,DEF,GHI");
1089: String[] strArray = bean.getStringArray();
1090: assertEquals("length", 3, strArray.length);
1091: assertEquals("value[0]", "ABC", strArray[0]);
1092: assertEquals("value[1]", "DEF", strArray[1]);
1093: assertEquals("value[2]", "GHI", strArray[2]);
1094:
1095: BeanUtils.setProperty(bean, "intArray", "0, 10, 20, 30, 40");
1096: int[] intArray = bean.getIntArray();
1097: assertEquals("length", 5, intArray.length);
1098: assertEquals("value[0]", 0, intArray[0]);
1099: assertEquals("value[1]", 10, intArray[1]);
1100: assertEquals("value[2]", 20, intArray[2]);
1101: assertEquals("value[3]", 30, intArray[3]);
1102: assertEquals("value[4]", 40, intArray[4]);
1103: }
1104:
1105: /**
1106: * Test narrowing and widening conversions on byte.
1107: */
1108: public void testCopyPropertyByte() throws Exception {
1109:
1110: BeanUtils.copyProperty(bean, "byteProperty", new Byte(
1111: (byte) 123));
1112: assertEquals((byte) 123, bean.getByteProperty());
1113: BeanUtils.copyProperty(bean, "byteProperty", new Double(123));
1114: assertEquals((byte) 123, bean.getByteProperty());
1115: BeanUtils.copyProperty(bean, "byteProperty", new Float(123));
1116: assertEquals((byte) 123, bean.getByteProperty());
1117: BeanUtils.copyProperty(bean, "byteProperty", new Integer(123));
1118: assertEquals((byte) 123, bean.getByteProperty());
1119: BeanUtils.copyProperty(bean, "byteProperty", new Long(123));
1120: assertEquals((byte) 123, bean.getByteProperty());
1121: BeanUtils.copyProperty(bean, "byteProperty", new Short(
1122: (short) 123));
1123: assertEquals((byte) 123, bean.getByteProperty());
1124:
1125: }
1126:
1127: /**
1128: * Test <code>copyProperty()</code> conversion.
1129: */
1130: public void testCopyPropertyConvert() {
1131: try {
1132: BeanUtils.copyProperty(bean, "dateProperty", testCalendar);
1133: } catch (Throwable t) {
1134: fail("Threw " + t);
1135: }
1136: assertEquals("Calendar --> java.util.Date", testUtilDate, bean
1137: .getDateProperty());
1138: }
1139:
1140: /**
1141: * Test <code>copyProperty()</code> converting from a String.
1142: */
1143: public void testCopyPropertyConvertFromString() {
1144: try {
1145: BeanUtils
1146: .copyProperty(bean, "dateProperty", testStringDate);
1147: } catch (Throwable t) {
1148: fail("Threw " + t);
1149: }
1150: assertEquals("String --> java.util.Date", testUtilDate, bean
1151: .getDateProperty());
1152: }
1153:
1154: /**
1155: * Test <code>copyProperty()</code> converting to a String.
1156: */
1157: public void testCopyPropertyConvertToString() {
1158: try {
1159: BeanUtils
1160: .copyProperty(bean, "stringProperty", testUtilDate);
1161: } catch (Throwable t) {
1162: fail("Threw " + t);
1163: }
1164: assertEquals("java.util.Date --> String", testUtilDate
1165: .toString(), bean.getStringProperty());
1166: }
1167:
1168: /**
1169: * Test <code>copyProperty()</code> converting to a String.
1170: */
1171: public void testCopyPropertyConvertToStringArray() {
1172: try {
1173: bean.setStringArray(null);
1174: BeanUtils.copyProperty(bean, "stringArray",
1175: new java.util.Date[] { testUtilDate });
1176: } catch (Throwable t) {
1177: fail("Threw " + t);
1178: }
1179: assertEquals("java.util.Date[] --> String[] length", 1, bean
1180: .getStringArray().length);
1181: assertEquals("java.util.Date[] --> String[] value ",
1182: testUtilDate.toString(), bean.getStringArray()[0]);
1183: }
1184:
1185: /**
1186: * Test <code>copyProperty()</code> converting to a String on indexed property
1187: */
1188: public void testCopyPropertyConvertToStringIndexed() {
1189: try {
1190: bean.setStringArray(new String[1]);
1191: BeanUtils
1192: .copyProperty(bean, "stringArray[0]", testUtilDate);
1193: } catch (Throwable t) {
1194: fail("Threw " + t);
1195: }
1196: assertEquals("java.util.Date --> String[]", testUtilDate
1197: .toString(), bean.getStringArray()[0]);
1198: }
1199:
1200: /**
1201: * Test narrowing and widening conversions on double.
1202: */
1203: public void testCopyPropertyDouble() throws Exception {
1204:
1205: BeanUtils.copyProperty(bean, "doubleProperty", new Byte(
1206: (byte) 123));
1207: assertEquals(123, bean.getDoubleProperty(), 0.005);
1208: BeanUtils.copyProperty(bean, "doubleProperty", new Double(123));
1209: assertEquals(123, bean.getDoubleProperty(), 0.005);
1210: BeanUtils.copyProperty(bean, "doubleProperty", new Float(123));
1211: assertEquals(123, bean.getDoubleProperty(), 0.005);
1212: BeanUtils
1213: .copyProperty(bean, "doubleProperty", new Integer(123));
1214: assertEquals(123, bean.getDoubleProperty(), 0.005);
1215: BeanUtils.copyProperty(bean, "doubleProperty", new Long(123));
1216: assertEquals(123, bean.getDoubleProperty(), 0.005);
1217: BeanUtils.copyProperty(bean, "doubleProperty", new Short(
1218: (short) 123));
1219: assertEquals(123, bean.getDoubleProperty(), 0.005);
1220:
1221: }
1222:
1223: /**
1224: * Test narrowing and widening conversions on float.
1225: */
1226: public void testCopyPropertyFloat() throws Exception {
1227:
1228: BeanUtils.copyProperty(bean, "floatProperty", new Byte(
1229: (byte) 123));
1230: assertEquals(123, bean.getFloatProperty(), 0.005);
1231: BeanUtils.copyProperty(bean, "floatProperty", new Double(123));
1232: assertEquals(123, bean.getFloatProperty(), 0.005);
1233: BeanUtils.copyProperty(bean, "floatProperty", new Float(123));
1234: assertEquals(123, bean.getFloatProperty(), 0.005);
1235: BeanUtils.copyProperty(bean, "floatProperty", new Integer(123));
1236: assertEquals(123, bean.getFloatProperty(), 0.005);
1237: BeanUtils.copyProperty(bean, "floatProperty", new Long(123));
1238: assertEquals(123, bean.getFloatProperty(), 0.005);
1239: BeanUtils.copyProperty(bean, "floatProperty", new Short(
1240: (short) 123));
1241: assertEquals(123, bean.getFloatProperty(), 0.005);
1242:
1243: }
1244:
1245: /**
1246: * Test narrowing and widening conversions on int.
1247: */
1248: public void testCopyPropertyInteger() throws Exception {
1249:
1250: BeanUtils.copyProperty(bean, "longProperty", new Byte(
1251: (byte) 123));
1252: assertEquals(123, bean.getIntProperty());
1253: BeanUtils.copyProperty(bean, "longProperty", new Double(123));
1254: assertEquals(123, bean.getIntProperty());
1255: BeanUtils.copyProperty(bean, "longProperty", new Float(123));
1256: assertEquals(123, bean.getIntProperty());
1257: BeanUtils.copyProperty(bean, "longProperty", new Integer(123));
1258: assertEquals(123, bean.getIntProperty());
1259: BeanUtils.copyProperty(bean, "longProperty", new Long(123));
1260: assertEquals(123, bean.getIntProperty());
1261: BeanUtils.copyProperty(bean, "longProperty", new Short(
1262: (short) 123));
1263: assertEquals(123, bean.getIntProperty());
1264:
1265: }
1266:
1267: /**
1268: * Test narrowing and widening conversions on long.
1269: */
1270: public void testCopyPropertyLong() throws Exception {
1271:
1272: BeanUtils.copyProperty(bean, "longProperty", new Byte(
1273: (byte) 123));
1274: assertEquals(123, bean.getLongProperty());
1275: BeanUtils.copyProperty(bean, "longProperty", new Double(123));
1276: assertEquals(123, bean.getLongProperty());
1277: BeanUtils.copyProperty(bean, "longProperty", new Float(123));
1278: assertEquals(123, bean.getLongProperty());
1279: BeanUtils.copyProperty(bean, "longProperty", new Integer(123));
1280: assertEquals(123, bean.getLongProperty());
1281: BeanUtils.copyProperty(bean, "longProperty", new Long(123));
1282: assertEquals(123, bean.getLongProperty());
1283: BeanUtils.copyProperty(bean, "longProperty", new Short(
1284: (short) 123));
1285: assertEquals(123, bean.getLongProperty());
1286:
1287: }
1288:
1289: /**
1290: * Test narrowing and widening conversions on short.
1291: */
1292: public void testCopyPropertyShort() throws Exception {
1293:
1294: BeanUtils.copyProperty(bean, "shortProperty", new Byte(
1295: (byte) 123));
1296: assertEquals((short) 123, bean.getShortProperty());
1297: BeanUtils.copyProperty(bean, "shortProperty", new Double(123));
1298: assertEquals((short) 123, bean.getShortProperty());
1299: BeanUtils.copyProperty(bean, "shortProperty", new Float(123));
1300: assertEquals((short) 123, bean.getShortProperty());
1301: BeanUtils.copyProperty(bean, "shortProperty", new Integer(123));
1302: assertEquals((short) 123, bean.getShortProperty());
1303: BeanUtils.copyProperty(bean, "shortProperty", new Long(123));
1304: assertEquals((short) 123, bean.getShortProperty());
1305: BeanUtils.copyProperty(bean, "shortProperty", new Short(
1306: (short) 123));
1307: assertEquals((short) 123, bean.getShortProperty());
1308:
1309: }
1310:
1311: /**
1312: * Test copying a property using a nested indexed array expression,
1313: * with and without conversions.
1314: */
1315: public void testCopyPropertyNestedIndexedArray() throws Exception {
1316:
1317: int origArray[] = { 0, 10, 20, 30, 40 };
1318: int intArray[] = { 0, 0, 0 };
1319: bean.getNested().setIntArray(intArray);
1320: int intChanged[] = { 0, 0, 0 };
1321:
1322: // No conversion required
1323: BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(
1324: 1));
1325: checkIntArray(bean.getIntArray(), origArray);
1326: intChanged[1] = 1;
1327: checkIntArray(bean.getNested().getIntArray(), intChanged);
1328:
1329: // Widening conversion required
1330: BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte(
1331: (byte) 2));
1332: checkIntArray(bean.getIntArray(), origArray);
1333: intChanged[1] = 2;
1334: checkIntArray(bean.getNested().getIntArray(), intChanged);
1335:
1336: // Narrowing conversion required
1337: BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long(3));
1338: checkIntArray(bean.getIntArray(), origArray);
1339: intChanged[1] = 3;
1340: checkIntArray(bean.getNested().getIntArray(), intChanged);
1341:
1342: // String conversion required
1343: BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
1344: checkIntArray(bean.getIntArray(), origArray);
1345: intChanged[1] = 4;
1346: checkIntArray(bean.getNested().getIntArray(), intChanged);
1347:
1348: }
1349:
1350: /**
1351: * Test copying a property using a nested mapped map property.
1352: */
1353: public void testCopyPropertyNestedMappedMap() throws Exception {
1354:
1355: Map origMap = new HashMap();
1356: origMap.put("First Key", "First Value");
1357: origMap.put("Second Key", "Second Value");
1358: Map changedMap = new HashMap();
1359: changedMap.put("First Key", "First Value");
1360: changedMap.put("Second Key", "Second Value");
1361:
1362: // No conversion required
1363: BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
1364: "New Second Value");
1365: checkMap(bean.getMapProperty(), origMap);
1366: changedMap.put("Second Key", "New Second Value");
1367: checkMap(bean.getNested().getMapProperty(), changedMap);
1368:
1369: }
1370:
1371: /**
1372: * Test copying a property using a nested simple expression, with and
1373: * without conversions.
1374: */
1375: public void testCopyPropertyNestedSimple() throws Exception {
1376:
1377: bean.setIntProperty(0);
1378: bean.getNested().setIntProperty(0);
1379:
1380: // No conversion required
1381: BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(
1382: 1));
1383: assertNotNull(bean.getNested());
1384: assertEquals(0, bean.getIntProperty());
1385: assertEquals(1, bean.getNested().getIntProperty());
1386:
1387: // Widening conversion required
1388: BeanUtils.copyProperty(bean, "nested.intProperty", new Byte(
1389: (byte) 2));
1390: assertNotNull(bean.getNested());
1391: assertEquals(0, bean.getIntProperty());
1392: assertEquals(2, bean.getNested().getIntProperty());
1393:
1394: // Narrowing conversion required
1395: BeanUtils.copyProperty(bean, "nested.intProperty", new Long(3));
1396: assertNotNull(bean.getNested());
1397: assertEquals(0, bean.getIntProperty());
1398: assertEquals(3, bean.getNested().getIntProperty());
1399:
1400: // String conversion required
1401: BeanUtils.copyProperty(bean, "nested.intProperty", "4");
1402: assertNotNull(bean.getNested());
1403: assertEquals(0, bean.getIntProperty());
1404: assertEquals(4, bean.getNested().getIntProperty());
1405:
1406: }
1407:
1408: /**
1409: * Test copying a null property value.
1410: */
1411: public void testCopyPropertyNull() throws Exception {
1412:
1413: bean.setNullProperty("non-null value");
1414: BeanUtils.copyProperty(bean, "nullProperty", null);
1415: assertNull("nullProperty is null", bean.getNullProperty());
1416:
1417: }
1418:
1419: /**
1420: * Test copying a new value to a write-only property, with and without
1421: * conversions.
1422: */
1423: public void testCopyPropertyWriteOnly() throws Exception {
1424:
1425: bean.setWriteOnlyProperty("Original value");
1426:
1427: // No conversion required
1428: BeanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
1429: assertEquals("New value", bean.getWriteOnlyPropertyValue());
1430:
1431: // Integer->String conversion required
1432: BeanUtils.copyProperty(bean, "writeOnlyProperty", new Integer(
1433: 123));
1434: assertEquals("123", bean.getWriteOnlyPropertyValue());
1435:
1436: }
1437:
1438: /**
1439: * Test setting a new value to a write-only property, with and without
1440: * conversions.
1441: */
1442: public void testSetPropertyWriteOnly() throws Exception {
1443:
1444: bean.setWriteOnlyProperty("Original value");
1445:
1446: // No conversion required
1447: BeanUtils.setProperty(bean, "writeOnlyProperty", "New value");
1448: assertEquals("New value", bean.getWriteOnlyPropertyValue());
1449:
1450: // Integer->String conversion required
1451: BeanUtils.setProperty(bean, "writeOnlyProperty", new Integer(
1452: 123));
1453: assertEquals("123", bean.getWriteOnlyPropertyValue());
1454:
1455: }
1456:
1457: /** Tests that separate instances can register separate instances */
1458: public void testSeparateInstances() throws Exception {
1459: BeanUtilsBean utilsOne = new BeanUtilsBean(
1460: new ConvertUtilsBean(), new PropertyUtilsBean());
1461: BeanUtilsBean utilsTwo = new BeanUtilsBean(
1462: new ConvertUtilsBean(), new PropertyUtilsBean());
1463:
1464: TestBean bean = new TestBean();
1465:
1466: // Make sure what we're testing works
1467: bean.setBooleanProperty(false);
1468: utilsOne.setProperty(bean, "booleanProperty", "true");
1469: assertEquals("Set property failed (1)", bean
1470: .getBooleanProperty(), true);
1471:
1472: bean.setBooleanProperty(false);
1473: utilsTwo.setProperty(bean, "booleanProperty", "true");
1474: assertEquals("Set property failed (2)", bean
1475: .getBooleanProperty(), true);
1476:
1477: // now change the registered conversion
1478:
1479: utilsOne.getConvertUtils().register(
1480: new ThrowExceptionConverter(), Boolean.TYPE);
1481: try {
1482:
1483: bean.setBooleanProperty(false);
1484: utilsOne.setProperty(bean, "booleanProperty", "true");
1485: fail("Registered conversion not used.");
1486:
1487: } catch (PassTestException e) { /* Do nothing */
1488: }
1489:
1490: // make sure that this conversion has no been registered in the other instance
1491: try {
1492:
1493: bean.setBooleanProperty(false);
1494: utilsTwo.setProperty(bean, "booleanProperty", "true");
1495: assertEquals("Set property failed (3)", bean
1496: .getBooleanProperty(), true);
1497:
1498: } catch (PassTestException e) {
1499: fail("Registed converter is used by other instances");
1500: }
1501: }
1502:
1503: public void testArrayPropertyConversion() throws Exception {
1504: BeanUtilsBean beanUtils = new BeanUtilsBean(
1505: new ConvertUtilsBean(), new PropertyUtilsBean());
1506:
1507: TestBean bean = new TestBean();
1508: String[] results = beanUtils.getArrayProperty(bean, "intArray");
1509:
1510: int[] values = bean.getIntArray();
1511: assertEquals(
1512: "Converted array size not equal to property array size.",
1513: results.length, values.length);
1514: for (int i = 0, size = values.length; i < size; i++) {
1515: assertEquals("Value " + i + " incorrectly converted ",
1516: values[i] + "", results[i]);
1517: }
1518: }
1519:
1520: // Ensure that the actual int[] matches the expected int[]
1521: protected void checkIntArray(int actual[], int expected[]) {
1522: assertNotNull("actual array not null", actual);
1523: assertEquals("actual array length", expected.length,
1524: actual.length);
1525: for (int i = 0; i < actual.length; i++) {
1526: assertEquals("actual array value[" + i + "]", expected[i],
1527: actual[i]);
1528: }
1529: }
1530:
1531: // Ensure that the actual Map matches the expected Map
1532: protected void checkMap(Map actual, Map expected) {
1533: assertNotNull("actual map not null", actual);
1534: assertEquals("actual map size", expected.size(), actual.size());
1535: Iterator keys = expected.keySet().iterator();
1536: while (keys.hasNext()) {
1537: Object key = keys.next();
1538: assertEquals("actual map value(" + key + ")", expected
1539: .get(key), actual.get(key));
1540: }
1541: }
1542:
1543: public void testMappedProperty() throws Exception {
1544: MappedPropertyTestBean bean = new MappedPropertyTestBean();
1545:
1546: BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)",
1547: "some.dotty.value");
1548:
1549: assertEquals("Mapped property set correctly",
1550: "some.dotty.value", bean
1551: .getMapproperty("this.that.the-other"));
1552: }
1553:
1554: /**
1555: * Test for {@link BeanUtilsBean#initCause(Throwable, Throwable)} method.
1556: */
1557: public void testInitCause() {
1558: if (isPre14JVM()) {
1559: return;
1560: }
1561: String parentMsg = "PARENT-THROWABLE";
1562: String causeMsg = "THROWABLE-CAUSE";
1563: try {
1564: initCauseAndThrowException(parentMsg, causeMsg);
1565: } catch (Throwable thrownParent) {
1566: assertEquals("Parent", parentMsg, thrownParent.getMessage());
1567: try {
1568: assertEquals("Parent", parentMsg, thrownParent
1569: .getMessage());
1570: Throwable thrownCause = getCause(thrownParent);
1571: assertNotNull("Cause Null", thrownCause);
1572: assertEquals("Cause", causeMsg, thrownCause
1573: .getMessage());
1574: } catch (Throwable testError) {
1575: fail("If you're running JDK 1.3 then don't worry this should fail,"
1576: + " if not then needs checking out: "
1577: + testError);
1578: }
1579: }
1580: }
1581:
1582: /**
1583: * Use reflection to get the cause
1584: */
1585: private Throwable getCause(Throwable t) throws Throwable {
1586: return (Throwable) PropertyUtils.getProperty(t, "cause");
1587: }
1588:
1589: /**
1590: * Catch a cause, initialize using BeanUtils.initCause() and throw new exception
1591: */
1592: private void initCauseAndThrowException(String parent, String cause)
1593: throws Throwable {
1594: try {
1595: throwException(cause);
1596: } catch (Throwable e) {
1597: Throwable t = new Exception(parent);
1598: BeanUtils.initCause(t, e);
1599: throw t;
1600: }
1601: }
1602:
1603: /**
1604: * Throw an exception with the specified message.
1605: */
1606: private void throwException(String msg) throws Throwable {
1607: throw new Exception(msg);
1608: }
1609:
1610: /**
1611: * Test for JDK 1.4
1612: */
1613: private boolean isPre14JVM() {
1614: String version = System
1615: .getProperty("java.specification.version");
1616: StringTokenizer tokenizer = new StringTokenizer(version, ".");
1617: if (tokenizer.nextToken().equals("1")) {
1618: String minorVersion = tokenizer.nextToken();
1619: if (minorVersion.equals("0"))
1620: return true;
1621: if (minorVersion.equals("1"))
1622: return true;
1623: if (minorVersion.equals("2"))
1624: return true;
1625: if (minorVersion.equals("3"))
1626: return true;
1627: }
1628: return false;
1629: }
1630: }
|