Source Code Cross Referenced for DynaBeanUtilsTestCase.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.Iterator;
0024:        import java.util.List;
0025:        import java.util.Map;
0026:
0027:        import junit.framework.TestCase;
0028:        import junit.framework.Test;
0029:        import junit.framework.TestSuite;
0030:
0031:        /**
0032:         * Test case for BeanUtils when the underlying bean is actually a DynaBean.
0033:         *
0034:         * @author Craig R. McClanahan
0035:         * @version $Revision: 552279 $ $Date: 2007-07-01 12:04:26 +0100 (Sun, 01 Jul 2007) $
0036:         */
0037:
0038:        public class DynaBeanUtilsTestCase extends TestCase {
0039:
0040:            // ----------------------------------------------------- Instance Variables
0041:
0042:            /**
0043:             * The basic test bean for each test.
0044:             */
0045:            protected DynaBean bean = null;
0046:
0047:            /**
0048:             * The nested bean pointed at by the "nested" property.
0049:             */
0050:            protected TestBean nested = null;
0051:
0052:            /**
0053:             * The set of properties that should be described.
0054:             */
0055:            protected String describes[] = { "booleanProperty",
0056:                    "booleanSecond", "byteProperty", "doubleProperty",
0057:                    "dupProperty", "floatProperty", "intArray", "intIndexed",
0058:                    "intProperty", "listIndexed", "longProperty",
0059:                    "mapProperty", "mappedProperty", "mappedIntProperty",
0060:                    "nested", "nullProperty",
0061:                    //      "readOnlyProperty",
0062:                    "shortProperty", "stringArray", "stringIndexed",
0063:                    "stringProperty" };
0064:
0065:            // ----------------------------------------------------------- Constructors
0066:
0067:            /**
0068:             * Construct a new instance of this test case.
0069:             *
0070:             * @param name Name of the test case
0071:             */
0072:            public DynaBeanUtilsTestCase(String name) {
0073:
0074:                super (name);
0075:
0076:            }
0077:
0078:            // --------------------------------------------------- Overall Test Methods
0079:
0080:            /**
0081:             * Set up instance variables required by this test case.
0082:             */
0083:            public void setUp() throws Exception {
0084:
0085:                ConvertUtils.deregister();
0086:
0087:                // Instantiate a new DynaBean instance
0088:                DynaClass dynaClass = createDynaClass();
0089:                bean = dynaClass.newInstance();
0090:
0091:                // Initialize the DynaBean's property values (like TestBean)
0092:                bean.set("booleanProperty", new Boolean(true));
0093:                bean.set("booleanSecond", new Boolean(true));
0094:                bean.set("byteProperty", new Byte((byte) 121));
0095:                bean.set("doubleProperty", new Double(321.0));
0096:                bean.set("floatProperty", new Float((float) 123.0));
0097:                String dupProperty[] = { "Dup 0", "Dup 1", "Dup 2", "Dup 3",
0098:                        "Dup 4" };
0099:                bean.set("dupProperty", dupProperty);
0100:                int intArray[] = { 0, 10, 20, 30, 40 };
0101:                bean.set("intArray", intArray);
0102:                int intIndexed[] = { 0, 10, 20, 30, 40 };
0103:                bean.set("intIndexed", intIndexed);
0104:                bean.set("intProperty", new Integer(123));
0105:                List listIndexed = new ArrayList();
0106:                listIndexed.add("String 0");
0107:                listIndexed.add("String 1");
0108:                listIndexed.add("String 2");
0109:                listIndexed.add("String 3");
0110:                listIndexed.add("String 4");
0111:                bean.set("listIndexed", listIndexed);
0112:                bean.set("longProperty", new Long(321));
0113:                HashMap mapProperty = new HashMap();
0114:                mapProperty.put("First Key", "First Value");
0115:                mapProperty.put("Second Key", "Second Value");
0116:                bean.set("mapProperty", mapProperty);
0117:                HashMap mappedProperty = new HashMap();
0118:                mappedProperty.put("First Key", "First Value");
0119:                mappedProperty.put("Second Key", "Second Value");
0120:                bean.set("mappedProperty", mappedProperty);
0121:                HashMap mappedIntProperty = new HashMap();
0122:                mappedIntProperty.put("One", new Integer(1));
0123:                mappedIntProperty.put("Two", new Integer(2));
0124:                bean.set("mappedIntProperty", mappedIntProperty);
0125:                nested = new TestBean();
0126:                bean.set("nested", nested);
0127:                // Property "nullProperty" is not initialized, so it should return null
0128:                bean.set("shortProperty", new Short((short) 987));
0129:                String stringArray[] = { "String 0", "String 1", "String 2",
0130:                        "String 3", "String 4" };
0131:                bean.set("stringArray", stringArray);
0132:                String stringIndexed[] = { "String 0", "String 1", "String 2",
0133:                        "String 3", "String 4" };
0134:                bean.set("stringIndexed", stringIndexed);
0135:                bean.set("stringProperty", "This is a string");
0136:
0137:            }
0138:
0139:            /**
0140:             * Return the tests included in this test suite.
0141:             */
0142:            public static Test suite() {
0143:
0144:                return (new TestSuite(DynaBeanUtilsTestCase.class));
0145:
0146:            }
0147:
0148:            /**
0149:             * Tear down instance variables required by this test case.
0150:             */
0151:            public void tearDown() {
0152:
0153:                bean = null;
0154:                nested = null;
0155:
0156:            }
0157:
0158:            // ------------------------------------------------ Individual Test Methods
0159:
0160:            /**
0161:             * Test the cloneBean() method from a DynaBean.
0162:             */
0163:            public void testCloneDynaBean() {
0164:
0165:                // Set up an origin bean with customized properties
0166:                DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
0167:                DynaBean orig = null;
0168:                try {
0169:                    orig = dynaClass.newInstance();
0170:                } catch (Exception e) {
0171:                    fail("newInstance(): " + e);
0172:                }
0173:                orig.set("booleanProperty", Boolean.FALSE);
0174:                orig.set("byteProperty", new Byte((byte) 111));
0175:                orig.set("doubleProperty", new Double(333.33));
0176:                orig.set("dupProperty", new String[] { "New 0", "New 1",
0177:                        "New 2" });
0178:                orig.set("intArray", new int[] { 100, 200, 300 });
0179:                orig.set("intProperty", new Integer(333));
0180:                orig.set("longProperty", new Long(3333));
0181:                orig.set("shortProperty", new Short((short) 33));
0182:                orig.set("stringArray", new String[] { "New 0", "New 1" });
0183:                orig.set("stringProperty", "Custom string");
0184:
0185:                // Copy the origin bean to our destination test bean
0186:                DynaBean clonedBean = null;
0187:                try {
0188:                    clonedBean = (DynaBean) BeanUtils.cloneBean(orig);
0189:                } catch (Exception e) {
0190:                    fail("Threw exception: " + e);
0191:                }
0192:
0193:                // Validate the results for scalar properties
0194:                assertEquals("Cloned boolean property", false,
0195:                        ((Boolean) clonedBean.get("booleanProperty"))
0196:                                .booleanValue());
0197:                assertEquals("Cloned byte property", (byte) 111,
0198:                        ((Byte) clonedBean.get("byteProperty")).byteValue());
0199:                assertEquals("Cloned double property", 333.33,
0200:                        ((Double) clonedBean.get("doubleProperty"))
0201:                                .doubleValue(), 0.005);
0202:                assertEquals("Cloned int property", 333, ((Integer) clonedBean
0203:                        .get("intProperty")).intValue());
0204:                assertEquals("Cloned long property", 3333, ((Long) clonedBean
0205:                        .get("longProperty")).longValue());
0206:                assertEquals("Cloned short property", (short) 33,
0207:                        ((Short) clonedBean.get("shortProperty")).shortValue());
0208:                assertEquals("Cloned string property", "Custom string",
0209:                        (String) clonedBean.get("stringProperty"));
0210:
0211:                // Validate the results for array properties
0212:                String dupProperty[] = (String[]) clonedBean.get("dupProperty");
0213:                assertNotNull("dupProperty present", dupProperty);
0214:                assertEquals("dupProperty length", 3, dupProperty.length);
0215:                assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0216:                assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0217:                assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0218:                int intArray[] = (int[]) clonedBean.get("intArray");
0219:                assertNotNull("intArray present", intArray);
0220:                assertEquals("intArray length", 3, intArray.length);
0221:                assertEquals("intArray[0]", 100, intArray[0]);
0222:                assertEquals("intArray[1]", 200, intArray[1]);
0223:                assertEquals("intArray[2]", 300, intArray[2]);
0224:                String stringArray[] = (String[]) clonedBean.get("stringArray");
0225:                assertNotNull("stringArray present", stringArray);
0226:                assertEquals("stringArray length", 2, stringArray.length);
0227:                assertEquals("stringArray[0]", "New 0", stringArray[0]);
0228:                assertEquals("stringArray[1]", "New 1", stringArray[1]);
0229:
0230:            }
0231:
0232:            /**
0233:             * Test the copyProperties() method from a DynaBean.
0234:             */
0235:            public void testCopyPropertiesDynaBean() {
0236:
0237:                // Set up an origin bean with customized properties
0238:                DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
0239:                DynaBean orig = null;
0240:                try {
0241:                    orig = dynaClass.newInstance();
0242:                } catch (Exception e) {
0243:                    fail("newInstance(): " + e);
0244:                }
0245:                orig.set("booleanProperty", Boolean.FALSE);
0246:                orig.set("byteProperty", new Byte((byte) 111));
0247:                orig.set("doubleProperty", new Double(333.33));
0248:                orig.set("dupProperty", new String[] { "New 0", "New 1",
0249:                        "New 2" });
0250:                orig.set("intArray", new int[] { 100, 200, 300 });
0251:                orig.set("intProperty", new Integer(333));
0252:                orig.set("longProperty", new Long(3333));
0253:                orig.set("shortProperty", new Short((short) 33));
0254:                orig.set("stringArray", new String[] { "New 0", "New 1" });
0255:                orig.set("stringProperty", "Custom string");
0256:
0257:                // Copy the origin bean to our destination test bean
0258:                try {
0259:                    BeanUtils.copyProperties(bean, orig);
0260:                } catch (Exception e) {
0261:                    fail("Threw exception: " + e);
0262:                }
0263:
0264:                // Validate the results for scalar properties
0265:                assertEquals("Copied boolean property", false, ((Boolean) bean
0266:                        .get("booleanProperty")).booleanValue());
0267:                assertEquals("Copied byte property", (byte) 111, ((Byte) bean
0268:                        .get("byteProperty")).byteValue());
0269:                assertEquals("Copied double property", 333.33, ((Double) bean
0270:                        .get("doubleProperty")).doubleValue(), 0.005);
0271:                assertEquals("Copied int property", 333, ((Integer) bean
0272:                        .get("intProperty")).intValue());
0273:                assertEquals("Copied long property", 3333, ((Long) bean
0274:                        .get("longProperty")).longValue());
0275:                assertEquals("Copied short property", (short) 33, ((Short) bean
0276:                        .get("shortProperty")).shortValue());
0277:                assertEquals("Copied string property", "Custom string",
0278:                        (String) bean.get("stringProperty"));
0279:
0280:                // Validate the results for array properties
0281:                String dupProperty[] = (String[]) bean.get("dupProperty");
0282:                assertNotNull("dupProperty present", dupProperty);
0283:                assertEquals("dupProperty length", 3, dupProperty.length);
0284:                assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0285:                assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0286:                assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0287:                int intArray[] = (int[]) bean.get("intArray");
0288:                assertNotNull("intArray present", intArray);
0289:                assertEquals("intArray length", 3, intArray.length);
0290:                assertEquals("intArray[0]", 100, intArray[0]);
0291:                assertEquals("intArray[1]", 200, intArray[1]);
0292:                assertEquals("intArray[2]", 300, intArray[2]);
0293:                String stringArray[] = (String[]) bean.get("stringArray");
0294:                assertNotNull("stringArray present", stringArray);
0295:                assertEquals("stringArray length", 2, stringArray.length);
0296:                assertEquals("stringArray[0]", "New 0", stringArray[0]);
0297:                assertEquals("stringArray[1]", "New 1", stringArray[1]);
0298:
0299:            }
0300:
0301:            /**
0302:             * Test copyProperties() when the origin is a a <code>Map</code>.
0303:             */
0304:            public void testCopyPropertiesMap() {
0305:
0306:                Map map = new HashMap();
0307:                map.put("booleanProperty", "false");
0308:                map.put("byteProperty", "111");
0309:                map.put("doubleProperty", "333.0");
0310:                map.put("dupProperty",
0311:                        new String[] { "New 0", "New 1", "New 2" });
0312:                map.put("floatProperty", "222.0");
0313:                map.put("intArray", new String[] { "0", "100", "200" });
0314:                map.put("intProperty", "111");
0315:                map.put("longProperty", "444");
0316:                map.put("shortProperty", "555");
0317:                map.put("stringProperty", "New String Property");
0318:
0319:                try {
0320:                    BeanUtils.copyProperties(bean, map);
0321:                } catch (Throwable t) {
0322:                    fail("Threw " + t.toString());
0323:                }
0324:
0325:                // Scalar properties
0326:                assertEquals("booleanProperty", false, ((Boolean) bean
0327:                        .get("booleanProperty")).booleanValue());
0328:                assertEquals("byteProperty", (byte) 111, ((Byte) bean
0329:                        .get("byteProperty")).byteValue());
0330:                assertEquals("doubleProperty", 333.0, ((Double) bean
0331:                        .get("doubleProperty")).doubleValue(), 0.005);
0332:                assertEquals("floatProperty", (float) 222.0, ((Float) bean
0333:                        .get("floatProperty")).floatValue(), (float) 0.005);
0334:                assertEquals("intProperty", 111, ((Integer) bean
0335:                        .get("intProperty")).intValue());
0336:                assertEquals("longProperty", 444, ((Long) bean
0337:                        .get("longProperty")).longValue());
0338:                assertEquals("shortProperty", (short) 555, ((Short) bean
0339:                        .get("shortProperty")).shortValue());
0340:                assertEquals("stringProperty", "New String Property",
0341:                        (String) bean.get("stringProperty"));
0342:
0343:                // Indexed Properties
0344:                String dupProperty[] = (String[]) bean.get("dupProperty");
0345:                assertNotNull("dupProperty present", dupProperty);
0346:                assertEquals("dupProperty length", 3, dupProperty.length);
0347:                assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0348:                assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0349:                assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0350:                int intArray[] = (int[]) bean.get("intArray");
0351:                assertNotNull("intArray present", intArray);
0352:                assertEquals("intArray length", 3, intArray.length);
0353:                assertEquals("intArray[0]", 0, intArray[0]);
0354:                assertEquals("intArray[1]", 100, intArray[1]);
0355:                assertEquals("intArray[2]", 200, intArray[2]);
0356:
0357:            }
0358:
0359:            /**
0360:             * Test the copyProperties() method from a standard JavaBean.
0361:             */
0362:            public void testCopyPropertiesStandard() {
0363:
0364:                // Set up an origin bean with customized properties
0365:                TestBean orig = new TestBean();
0366:                orig.setBooleanProperty(false);
0367:                orig.setByteProperty((byte) 111);
0368:                orig.setDoubleProperty(333.33);
0369:                orig.setDupProperty(new String[] { "New 0", "New 1", "New 2" });
0370:                orig.setIntArray(new int[] { 100, 200, 300 });
0371:                orig.setIntProperty(333);
0372:                orig.setLongProperty(3333);
0373:                orig.setShortProperty((short) 33);
0374:                orig.setStringArray(new String[] { "New 0", "New 1" });
0375:                orig.setStringProperty("Custom string");
0376:
0377:                // Copy the origin bean to our destination test bean
0378:                try {
0379:                    BeanUtils.copyProperties(bean, orig);
0380:                } catch (Exception e) {
0381:                    fail("Threw exception: " + e);
0382:                }
0383:
0384:                // Validate the results for scalar properties
0385:                assertEquals("Copied boolean property", false, ((Boolean) bean
0386:                        .get("booleanProperty")).booleanValue());
0387:                assertEquals("Copied byte property", (byte) 111, ((Byte) bean
0388:                        .get("byteProperty")).byteValue());
0389:                assertEquals("Copied double property", 333.33, ((Double) bean
0390:                        .get("doubleProperty")).doubleValue(), 0.005);
0391:                assertEquals("Copied int property", 333, ((Integer) bean
0392:                        .get("intProperty")).intValue());
0393:                assertEquals("Copied long property", 3333, ((Long) bean
0394:                        .get("longProperty")).longValue());
0395:                assertEquals("Copied short property", (short) 33, ((Short) bean
0396:                        .get("shortProperty")).shortValue());
0397:                assertEquals("Copied string property", "Custom string",
0398:                        (String) bean.get("stringProperty"));
0399:
0400:                // Validate the results for array properties
0401:                String dupProperty[] = (String[]) bean.get("dupProperty");
0402:                assertNotNull("dupProperty present", dupProperty);
0403:                assertEquals("dupProperty length", 3, dupProperty.length);
0404:                assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
0405:                assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
0406:                assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
0407:                int intArray[] = (int[]) bean.get("intArray");
0408:                assertNotNull("intArray present", intArray);
0409:                assertEquals("intArray length", 3, intArray.length);
0410:                assertEquals("intArray[0]", 100, intArray[0]);
0411:                assertEquals("intArray[1]", 200, intArray[1]);
0412:                assertEquals("intArray[2]", 300, intArray[2]);
0413:                String stringArray[] = (String[]) bean.get("stringArray");
0414:                assertNotNull("stringArray present", stringArray);
0415:                assertEquals("stringArray length", 2, stringArray.length);
0416:                assertEquals("stringArray[0]", "New 0", stringArray[0]);
0417:                assertEquals("stringArray[1]", "New 1", stringArray[1]);
0418:
0419:            }
0420:
0421:            /**
0422:             * Test the describe() method.
0423:             */
0424:            public void testDescribe() {
0425:
0426:                Map map = null;
0427:                try {
0428:                    map = PropertyUtils.describe(bean);
0429:                } catch (Exception e) {
0430:                    fail("Threw exception " + e);
0431:                }
0432:
0433:                // Verify existence of all the properties that should be present
0434:                for (int i = 0; i < describes.length; i++) {
0435:                    assertTrue("Property '" + describes[i] + "' is present",
0436:                            map.containsKey(describes[i]));
0437:                }
0438:                assertTrue("Property 'writeOnlyProperty' is not present", !map
0439:                        .containsKey("writeOnlyProperty"));
0440:
0441:                // Verify the values of scalar properties
0442:                assertEquals("Value of 'booleanProperty'", Boolean.TRUE, map
0443:                        .get("booleanProperty"));
0444:                assertEquals("Value of 'byteProperty'", new Byte((byte) 121),
0445:                        map.get("byteProperty"));
0446:                assertEquals("Value of 'doubleProperty'", new Double(321.0),
0447:                        map.get("doubleProperty"));
0448:                assertEquals("Value of 'floatProperty'", new Float(
0449:                        (float) 123.0), map.get("floatProperty"));
0450:                assertEquals("Value of 'intProperty'", new Integer(123), map
0451:                        .get("intProperty"));
0452:                assertEquals("Value of 'longProperty'", new Long(321), map
0453:                        .get("longProperty"));
0454:                assertEquals("Value of 'shortProperty'",
0455:                        new Short((short) 987), map.get("shortProperty"));
0456:                assertEquals("Value of 'stringProperty'", "This is a string",
0457:                        (String) map.get("stringProperty"));
0458:
0459:            }
0460:
0461:            /**
0462:             * Test populate() method on array properties as a whole.
0463:             */
0464:            public void testPopulateArrayProperties() {
0465:
0466:                try {
0467:
0468:                    HashMap map = new HashMap();
0469:                    //            int intArray[] = new int[] { 123, 456, 789 };
0470:                    String intArrayIn[] = new String[] { "123", "456", "789" };
0471:                    map.put("intArray", intArrayIn);
0472:                    String stringArray[] = new String[] { "New String 0",
0473:                            "New String 1" };
0474:                    map.put("stringArray", stringArray);
0475:
0476:                    BeanUtils.populate(bean, map);
0477:
0478:                    int intArray[] = (int[]) bean.get("intArray");
0479:                    assertNotNull("intArray is present", intArray);
0480:                    assertEquals("intArray length", 3, intArray.length);
0481:                    assertEquals("intArray[0]", 123, intArray[0]);
0482:                    assertEquals("intArray[1]", 456, intArray[1]);
0483:                    assertEquals("intArray[2]", 789, intArray[2]);
0484:                    stringArray = (String[]) bean.get("stringArray");
0485:                    assertNotNull("stringArray is present", stringArray);
0486:                    assertEquals("stringArray length", 2, stringArray.length);
0487:                    assertEquals("stringArray[0]", "New String 0",
0488:                            stringArray[0]);
0489:                    assertEquals("stringArray[1]", "New String 1",
0490:                            stringArray[1]);
0491:
0492:                } catch (IllegalAccessException e) {
0493:                    fail("IllegalAccessException");
0494:                } catch (InvocationTargetException e) {
0495:                    fail("InvocationTargetException");
0496:                }
0497:
0498:            }
0499:
0500:            /**
0501:             *  tests the string and int arrays of TestBean
0502:             */
0503:            public void testGetArrayProperty() {
0504:                try {
0505:                    String arr[] = BeanUtils.getArrayProperty(bean,
0506:                            "stringArray");
0507:                    String comp[] = (String[]) bean.get("stringArray");
0508:
0509:                    assertTrue("String array length = " + comp.length,
0510:                            (comp.length == arr.length));
0511:
0512:                    arr = BeanUtils.getArrayProperty(bean, "intArray");
0513:                    int iarr[] = (int[]) bean.get("intArray");
0514:
0515:                    assertTrue("String array length = " + iarr.length,
0516:                            (iarr.length == arr.length));
0517:                } catch (IllegalAccessException e) {
0518:                    fail("IllegalAccessException");
0519:                } catch (InvocationTargetException e) {
0520:                    fail("InvocationTargetException");
0521:                } catch (NoSuchMethodException e) {
0522:                    fail("NoSuchMethodException");
0523:                }
0524:
0525:            }
0526:
0527:            /**
0528:             *  tests getting an indexed property
0529:             */
0530:            public void testGetIndexedProperty1() {
0531:                try {
0532:                    String val = BeanUtils.getIndexedProperty(bean,
0533:                            "intIndexed[3]");
0534:                    String comp = String.valueOf(bean.get("intIndexed", 3));
0535:                    assertTrue("intIndexed[3] == " + comp, val.equals(comp));
0536:
0537:                    val = BeanUtils
0538:                            .getIndexedProperty(bean, "stringIndexed[3]");
0539:                    comp = (String) bean.get("stringIndexed", 3);
0540:                    assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
0541:                } catch (IllegalAccessException e) {
0542:                    fail("IllegalAccessException");
0543:                } catch (InvocationTargetException e) {
0544:                    fail("InvocationTargetException");
0545:                } catch (NoSuchMethodException e) {
0546:                    fail("NoSuchMethodException");
0547:                }
0548:            }
0549:
0550:            /**
0551:             *  tests getting an indexed property
0552:             */
0553:            public void testGetIndexedProperty2() {
0554:                try {
0555:                    String val = BeanUtils.getIndexedProperty(bean,
0556:                            "intIndexed", 3);
0557:                    String comp = String.valueOf(bean.get("intIndexed", 3));
0558:
0559:                    assertTrue("intIndexed,3 == " + comp, val.equals(comp));
0560:
0561:                    val = BeanUtils
0562:                            .getIndexedProperty(bean, "stringIndexed", 3);
0563:                    comp = (String) bean.get("stringIndexed", 3);
0564:
0565:                    assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
0566:
0567:                } catch (IllegalAccessException e) {
0568:                    fail("IllegalAccessException");
0569:                } catch (InvocationTargetException e) {
0570:                    fail("InvocationTargetException");
0571:                } catch (NoSuchMethodException e) {
0572:                    fail("NoSuchMethodException");
0573:                }
0574:            }
0575:
0576:            /**
0577:             *  tests getting a nested property
0578:             */
0579:            public void testGetNestedProperty() {
0580:                try {
0581:                    String val = BeanUtils.getNestedProperty(bean,
0582:                            "nested.stringProperty");
0583:                    String comp = nested.getStringProperty();
0584:                    assertTrue("nested.StringProperty == " + comp, val
0585:                            .equals(comp));
0586:                } catch (IllegalAccessException e) {
0587:                    fail("IllegalAccessException");
0588:                } catch (InvocationTargetException e) {
0589:                    fail("InvocationTargetException");
0590:                } catch (NoSuchMethodException e) {
0591:                    fail("NoSuchMethodException");
0592:                }
0593:            }
0594:
0595:            /**
0596:             *  tests getting a 'whatever' property
0597:             */
0598:            public void testGetGeneralProperty() {
0599:                try {
0600:                    String val = BeanUtils.getProperty(bean,
0601:                            "nested.intIndexed[2]");
0602:                    String comp = String.valueOf(bean.get("intIndexed", 2));
0603:
0604:                    assertTrue("nested.intIndexed[2] == " + comp, val
0605:                            .equals(comp));
0606:                } catch (IllegalAccessException e) {
0607:                    fail("IllegalAccessException");
0608:                } catch (InvocationTargetException e) {
0609:                    fail("InvocationTargetException");
0610:                } catch (NoSuchMethodException e) {
0611:                    fail("NoSuchMethodException");
0612:                }
0613:            }
0614:
0615:            /**
0616:             *  tests getting a 'whatever' property
0617:             */
0618:            public void testGetSimpleProperty() {
0619:                try {
0620:                    String val = BeanUtils.getSimpleProperty(bean,
0621:                            "shortProperty");
0622:                    String comp = String.valueOf(bean.get("shortProperty"));
0623:
0624:                    assertTrue("shortProperty == " + comp, val.equals(comp));
0625:                } catch (IllegalAccessException e) {
0626:                    fail("IllegalAccessException");
0627:                } catch (InvocationTargetException e) {
0628:                    fail("InvocationTargetException");
0629:                } catch (NoSuchMethodException e) {
0630:                    fail("NoSuchMethodException");
0631:                }
0632:            }
0633:
0634:            /**
0635:             * Test populate() method on individual array elements.
0636:             */
0637:            public void testPopulateArrayElements() {
0638:
0639:                try {
0640:
0641:                    HashMap map = new HashMap();
0642:                    map.put("intIndexed[0]", "100");
0643:                    map.put("intIndexed[2]", "120");
0644:                    map.put("intIndexed[4]", "140");
0645:
0646:                    BeanUtils.populate(bean, map);
0647:                    Integer intIndexed0 = (Integer) bean.get("intIndexed", 0);
0648:                    assertEquals("intIndexed[0] is 100", 100, intIndexed0
0649:                            .intValue());
0650:                    Integer intIndexed1 = (Integer) bean.get("intIndexed", 1);
0651:                    assertEquals("intIndexed[1] is 10", 10, intIndexed1
0652:                            .intValue());
0653:                    Integer intIndexed2 = (Integer) bean.get("intIndexed", 2);
0654:                    assertEquals("intIndexed[2] is 120", 120, intIndexed2
0655:                            .intValue());
0656:                    Integer intIndexed3 = (Integer) bean.get("intIndexed", 3);
0657:                    assertEquals("intIndexed[3] is 30", 30, intIndexed3
0658:                            .intValue());
0659:                    Integer intIndexed4 = (Integer) bean.get("intIndexed", 4);
0660:                    assertEquals("intIndexed[4] is 140", 140, intIndexed4
0661:                            .intValue());
0662:
0663:                    map.clear();
0664:                    map.put("stringIndexed[1]", "New String 1");
0665:                    map.put("stringIndexed[3]", "New String 3");
0666:
0667:                    BeanUtils.populate(bean, map);
0668:
0669:                    assertEquals("stringIndexed[0] is \"String 0\"",
0670:                            "String 0", (String) bean.get("stringIndexed", 0));
0671:                    assertEquals("stringIndexed[1] is \"New String 1\"",
0672:                            "New String 1", (String) bean.get("stringIndexed",
0673:                                    1));
0674:                    assertEquals("stringIndexed[2] is \"String 2\"",
0675:                            "String 2", (String) bean.get("stringIndexed", 2));
0676:                    assertEquals("stringIndexed[3] is \"New String 3\"",
0677:                            "New String 3", (String) bean.get("stringIndexed",
0678:                                    3));
0679:                    assertEquals("stringIndexed[4] is \"String 4\"",
0680:                            "String 4", (String) bean.get("stringIndexed", 4));
0681:
0682:                } catch (IllegalAccessException e) {
0683:                    fail("IllegalAccessException");
0684:                } catch (InvocationTargetException e) {
0685:                    fail("InvocationTargetException");
0686:                }
0687:
0688:            }
0689:
0690:            /**
0691:             * Test populate() on mapped properties.
0692:             */
0693:            public void testPopulateMapped() {
0694:
0695:                try {
0696:
0697:                    HashMap map = new HashMap();
0698:                    map.put("mappedProperty(First Key)", "New First Value");
0699:                    map.put("mappedProperty(Third Key)", "New Third Value");
0700:
0701:                    BeanUtils.populate(bean, map);
0702:
0703:                    assertEquals("mappedProperty(First Key)",
0704:                            "New First Value", (String) bean.get(
0705:                                    "mappedProperty", "First Key"));
0706:                    assertEquals("mappedProperty(Second Key)", "Second Value",
0707:                            (String) bean.get("mappedProperty", "Second Key"));
0708:                    assertEquals("mappedProperty(Third Key)",
0709:                            "New Third Value", (String) bean.get(
0710:                                    "mappedProperty", "Third Key"));
0711:                    assertNull("mappedProperty(Fourth Key", bean.get(
0712:                            "mappedProperty", "Fourth Key"));
0713:
0714:                } catch (IllegalAccessException e) {
0715:                    fail("IllegalAccessException");
0716:                } catch (InvocationTargetException e) {
0717:                    fail("InvocationTargetException");
0718:                }
0719:
0720:            }
0721:
0722:            /**
0723:             * Test populate() method on nested properties.
0724:             */
0725:            public void testPopulateNested() {
0726:
0727:                try {
0728:
0729:                    HashMap map = new HashMap();
0730:                    map.put("nested.booleanProperty", "false");
0731:                    // booleanSecond is left at true
0732:                    map.put("nested.doubleProperty", "432.0");
0733:                    // floatProperty is left at 123.0
0734:                    map.put("nested.intProperty", "543");
0735:                    // longProperty is left at 321
0736:                    map.put("nested.shortProperty", "654");
0737:                    // stringProperty is left at "This is a string"
0738:
0739:                    BeanUtils.populate(bean, map);
0740:
0741:                    TestBean nested = (TestBean) bean.get("nested");
0742:                    assertTrue("booleanProperty is false", !nested
0743:                            .getBooleanProperty());
0744:                    assertTrue("booleanSecond is true", nested
0745:                            .isBooleanSecond());
0746:                    assertEquals("doubleProperty is 432.0", 432.0, nested
0747:                            .getDoubleProperty(), 0.005);
0748:                    assertEquals("floatProperty is 123.0", (float) 123.0,
0749:                            nested.getFloatProperty(), (float) 0.005);
0750:                    assertEquals("intProperty is 543", 543, nested
0751:                            .getIntProperty());
0752:                    assertEquals("longProperty is 321", 321, nested
0753:                            .getLongProperty());
0754:                    assertEquals("shortProperty is 654", (short) 654, nested
0755:                            .getShortProperty());
0756:                    assertEquals("stringProperty is \"This is a string\"",
0757:                            "This is a string", nested.getStringProperty());
0758:
0759:                } catch (IllegalAccessException e) {
0760:                    fail("IllegalAccessException");
0761:                } catch (InvocationTargetException e) {
0762:                    fail("InvocationTargetException");
0763:                }
0764:
0765:            }
0766:
0767:            /**
0768:             * Test populate() method on scalar properties.
0769:             */
0770:            public void testPopulateScalar() {
0771:
0772:                try {
0773:
0774:                    bean.set("nullProperty", "non-null value");
0775:
0776:                    HashMap map = new HashMap();
0777:                    map.put("booleanProperty", "false");
0778:                    // booleanSecond is left at true
0779:                    map.put("doubleProperty", "432.0");
0780:                    // floatProperty is left at 123.0
0781:                    map.put("intProperty", "543");
0782:                    // longProperty is left at 321
0783:                    map.put("nullProperty", null);
0784:                    map.put("shortProperty", "654");
0785:                    // stringProperty is left at "This is a string"
0786:
0787:                    BeanUtils.populate(bean, map);
0788:
0789:                    Boolean booleanProperty = (Boolean) bean
0790:                            .get("booleanProperty");
0791:                    assertTrue("booleanProperty is false", !booleanProperty
0792:                            .booleanValue());
0793:                    Boolean booleanSecond = (Boolean) bean.get("booleanSecond");
0794:                    assertTrue("booleanSecond is true", booleanSecond
0795:                            .booleanValue());
0796:                    Double doubleProperty = (Double) bean.get("doubleProperty");
0797:                    assertEquals("doubleProperty is 432.0", 432.0,
0798:                            doubleProperty.doubleValue(), 0.005);
0799:                    Float floatProperty = (Float) bean.get("floatProperty");
0800:                    assertEquals("floatProperty is 123.0", (float) 123.0,
0801:                            floatProperty.floatValue(), (float) 0.005);
0802:                    Integer intProperty = (Integer) bean.get("intProperty");
0803:                    assertEquals("intProperty is 543", 543, intProperty
0804:                            .intValue());
0805:                    Long longProperty = (Long) bean.get("longProperty");
0806:                    assertEquals("longProperty is 321", 321, longProperty
0807:                            .longValue());
0808:                    assertNull("nullProperty is null", bean.get("nullProperty"));
0809:                    Short shortProperty = (Short) bean.get("shortProperty");
0810:                    assertEquals("shortProperty is 654", (short) 654,
0811:                            shortProperty.shortValue());
0812:                    assertEquals("stringProperty is \"This is a string\"",
0813:                            "This is a string", (String) bean
0814:                                    .get("stringProperty"));
0815:
0816:                } catch (IllegalAccessException e) {
0817:                    fail("IllegalAccessException");
0818:                } catch (InvocationTargetException e) {
0819:                    fail("InvocationTargetException");
0820:                }
0821:
0822:            }
0823:
0824:            /**
0825:             * Test calling setProperty() with null property values.
0826:             */
0827:            public void testSetPropertyNullValues() throws Exception {
0828:
0829:                Object oldValue = null;
0830:                Object newValue = null;
0831:
0832:                // Scalar value into array
0833:                oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0834:                BeanUtils.setProperty(bean, "stringArray", (String) null);
0835:                newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0836:                assertNotNull("stringArray is not null", newValue);
0837:                assertTrue("stringArray of correct type",
0838:                        newValue instanceof  String[]);
0839:                assertEquals("stringArray length", 1,
0840:                        ((String[]) newValue).length);
0841:                PropertyUtils.setProperty(bean, "stringArray", oldValue);
0842:
0843:                // Indexed value into array
0844:                oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0845:                BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
0846:                newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
0847:                assertNotNull("stringArray is not null", newValue);
0848:                assertTrue("stringArray of correct type",
0849:                        newValue instanceof  String[]);
0850:                assertEquals("stringArray length", 5,
0851:                        ((String[]) newValue).length);
0852:                assertTrue("stringArray[2] is null",
0853:                        ((String[]) newValue)[2] == null);
0854:                PropertyUtils.setProperty(bean, "stringArray", oldValue);
0855:
0856:                // Value into scalar
0857:                BeanUtils.setProperty(bean, "stringProperty", null);
0858:                assertTrue("stringProperty is now null", BeanUtils.getProperty(
0859:                        bean, "stringProperty") == null);
0860:
0861:            }
0862:
0863:            /**
0864:             * Test converting to and from primitive wrapper types.
0865:             */
0866:            public void testSetPropertyOnPrimitiveWrappers() throws Exception {
0867:
0868:                BeanUtils.setProperty(bean, "intProperty", new Integer(1));
0869:                assertEquals(1, ((Integer) bean.get("intProperty")).intValue());
0870:                BeanUtils.setProperty(bean, "stringProperty", new Integer(1));
0871:                assertEquals(1, Integer.parseInt((String) bean
0872:                        .get("stringProperty")));
0873:
0874:            }
0875:
0876:            /**
0877:             * Test setting a null property value.
0878:             */
0879:            public void testSetPropertyNull() throws Exception {
0880:
0881:                bean.set("nullProperty", "non-null value");
0882:                BeanUtils.setProperty(bean, "nullProperty", null);
0883:                assertNull("nullProperty is null", bean.get("nullProperty"));
0884:
0885:            }
0886:
0887:            /**
0888:             * Test narrowing and widening conversions on byte.
0889:             */
0890:            public void testCopyPropertyByte() throws Exception {
0891:
0892:                BeanUtils.setProperty(bean, "byteProperty",
0893:                        new Byte((byte) 123));
0894:                assertEquals((byte) 123, ((Byte) bean.get("byteProperty"))
0895:                        .byteValue());
0896:                /*
0897:                 BeanUtils.setProperty(bean, "byteProperty", new Double((double) 123));
0898:                 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
0899:                 BeanUtils.setProperty(bean, "byteProperty", new Float((float) 123));
0900:                 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
0901:                 */
0902:                BeanUtils.setProperty(bean, "byteProperty", new Integer(123));
0903:                assertEquals((byte) 123, ((Byte) bean.get("byteProperty"))
0904:                        .byteValue());
0905:                BeanUtils.setProperty(bean, "byteProperty", new Long(123));
0906:                assertEquals((byte) 123, ((Byte) bean.get("byteProperty"))
0907:                        .byteValue());
0908:                BeanUtils.setProperty(bean, "byteProperty", new Short(
0909:                        (short) 123));
0910:                assertEquals((byte) 123, ((Byte) bean.get("byteProperty"))
0911:                        .byteValue());
0912:
0913:            }
0914:
0915:            /**
0916:             * Test narrowing and widening conversions on double.
0917:             */
0918:            public void testCopyPropertyDouble() throws Exception {
0919:
0920:                BeanUtils.setProperty(bean, "doubleProperty", new Byte(
0921:                        (byte) 123));
0922:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0923:                        .doubleValue(), 0.005);
0924:                BeanUtils.setProperty(bean, "doubleProperty", new Double(123));
0925:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0926:                        .doubleValue(), 0.005);
0927:                BeanUtils.setProperty(bean, "doubleProperty", new Float(123));
0928:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0929:                        .doubleValue(), 0.005);
0930:                BeanUtils.setProperty(bean, "doubleProperty", new Integer(123));
0931:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0932:                        .doubleValue(), 0.005);
0933:                BeanUtils.setProperty(bean, "doubleProperty", new Long(123));
0934:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0935:                        .doubleValue(), 0.005);
0936:                BeanUtils.setProperty(bean, "doubleProperty", new Short(
0937:                        (short) 123));
0938:                assertEquals(123, ((Double) bean.get("doubleProperty"))
0939:                        .doubleValue(), 0.005);
0940:
0941:            }
0942:
0943:            /**
0944:             * Test narrowing and widening conversions on float.
0945:             */
0946:            public void testCopyPropertyFloat() throws Exception {
0947:
0948:                BeanUtils.setProperty(bean, "floatProperty", new Byte(
0949:                        (byte) 123));
0950:                assertEquals(123, ((Float) bean.get("floatProperty"))
0951:                        .floatValue(), 0.005);
0952:                BeanUtils.setProperty(bean, "floatProperty", new Double(123));
0953:                assertEquals(123, ((Float) bean.get("floatProperty"))
0954:                        .floatValue(), 0.005);
0955:                BeanUtils.setProperty(bean, "floatProperty", new Float(123));
0956:                assertEquals(123, ((Float) bean.get("floatProperty"))
0957:                        .floatValue(), 0.005);
0958:                BeanUtils.setProperty(bean, "floatProperty", new Integer(123));
0959:                assertEquals(123, ((Float) bean.get("floatProperty"))
0960:                        .floatValue(), 0.005);
0961:                BeanUtils.setProperty(bean, "floatProperty", new Long(123));
0962:                assertEquals(123, ((Float) bean.get("floatProperty"))
0963:                        .floatValue(), 0.005);
0964:                BeanUtils.setProperty(bean, "floatProperty", new Short(
0965:                        (short) 123));
0966:                assertEquals(123, ((Float) bean.get("floatProperty"))
0967:                        .floatValue(), 0.005);
0968:
0969:            }
0970:
0971:            /**
0972:             * Test narrowing and widening conversions on int.
0973:             */
0974:            public void testCopyPropertyInteger() throws Exception {
0975:
0976:                BeanUtils.setProperty(bean, "longProperty",
0977:                        new Byte((byte) 123));
0978:                assertEquals(123, ((Integer) bean.get("intProperty"))
0979:                        .intValue());
0980:                /*
0981:                 BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
0982:                 assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
0983:                 BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
0984:                 assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
0985:                 */
0986:                BeanUtils.setProperty(bean, "longProperty", new Integer(123));
0987:                assertEquals(123, ((Integer) bean.get("intProperty"))
0988:                        .intValue());
0989:                BeanUtils.setProperty(bean, "longProperty", new Long(123));
0990:                assertEquals(123, ((Integer) bean.get("intProperty"))
0991:                        .intValue());
0992:                BeanUtils.setProperty(bean, "longProperty", new Short(
0993:                        (short) 123));
0994:                assertEquals(123, ((Integer) bean.get("intProperty"))
0995:                        .intValue());
0996:
0997:            }
0998:
0999:            /**
1000:             * Test narrowing and widening conversions on long.
1001:             */
1002:            public void testCopyPropertyLong() throws Exception {
1003:
1004:                BeanUtils.setProperty(bean, "longProperty",
1005:                        new Byte((byte) 123));
1006:                assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1007:                /*
1008:                 BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
1009:                 assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1010:                 BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
1011:                 assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1012:                 */
1013:                BeanUtils.setProperty(bean, "longProperty", new Integer(123));
1014:                assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1015:                BeanUtils.setProperty(bean, "longProperty", new Long(123));
1016:                assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1017:                BeanUtils.setProperty(bean, "longProperty", new Short(
1018:                        (short) 123));
1019:                assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1020:
1021:            }
1022:
1023:            /**
1024:             * Test copying a null property value.
1025:             */
1026:            public void testCopyPropertyNull() throws Exception {
1027:
1028:                bean.set("nullProperty", "non-null value");
1029:                BeanUtils.copyProperty(bean, "nullProperty", null);
1030:                assertNull("nullProperty is null", bean.get("nullProperty"));
1031:
1032:            }
1033:
1034:            /**
1035:             * Test narrowing and widening conversions on short.
1036:             */
1037:            public void testCopyPropertyShort() throws Exception {
1038:
1039:                BeanUtils.setProperty(bean, "shortProperty", new Byte(
1040:                        (byte) 123));
1041:                assertEquals((short) 123, ((Short) bean.get("shortProperty"))
1042:                        .shortValue());
1043:                /*
1044:                 BeanUtils.setProperty(bean, "shortProperty", new Double((double) 123));
1045:                 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1046:                 BeanUtils.setProperty(bean, "shortProperty", new Float((float) 123));
1047:                 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1048:                 */
1049:                BeanUtils.setProperty(bean, "shortProperty", new Integer(123));
1050:                assertEquals((short) 123, ((Short) bean.get("shortProperty"))
1051:                        .shortValue());
1052:                BeanUtils.setProperty(bean, "shortProperty", new Long(123));
1053:                assertEquals((short) 123, ((Short) bean.get("shortProperty"))
1054:                        .shortValue());
1055:                BeanUtils.setProperty(bean, "shortProperty", new Short(
1056:                        (short) 123));
1057:                assertEquals((short) 123, ((Short) bean.get("shortProperty"))
1058:                        .shortValue());
1059:
1060:            }
1061:
1062:            /**
1063:             * Test copying a property using a nested indexed array expression,
1064:             * with and without conversions.
1065:             */
1066:            public void testCopyPropertyNestedIndexedArray() throws Exception {
1067:
1068:                int origArray[] = { 0, 10, 20, 30, 40 };
1069:                int intArray[] = { 0, 0, 0 };
1070:                ((TestBean) bean.get("nested")).setIntArray(intArray);
1071:                int intChanged[] = { 0, 0, 0 };
1072:
1073:                // No conversion required
1074:                BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(
1075:                        1));
1076:                checkIntArray((int[]) bean.get("intArray"), origArray);
1077:                intChanged[1] = 1;
1078:                checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1079:                        intChanged);
1080:
1081:                // Widening conversion required
1082:                BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte(
1083:                        (byte) 2));
1084:                checkIntArray((int[]) bean.get("intArray"), origArray);
1085:                intChanged[1] = 2;
1086:                checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1087:                        intChanged);
1088:
1089:                // Narrowing conversion required
1090:                BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long(3));
1091:                checkIntArray((int[]) bean.get("intArray"), origArray);
1092:                intChanged[1] = 3;
1093:                checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1094:                        intChanged);
1095:
1096:                // String conversion required
1097:                BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
1098:                checkIntArray((int[]) bean.get("intArray"), origArray);
1099:                intChanged[1] = 4;
1100:                checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1101:                        intChanged);
1102:
1103:            }
1104:
1105:            /**
1106:             * Test copying a property using a nested mapped map property.
1107:             */
1108:            public void testCopyPropertyNestedMappedMap() throws Exception {
1109:
1110:                Map origMap = new HashMap();
1111:                origMap.put("First Key", "First Value");
1112:                origMap.put("Second Key", "Second Value");
1113:                Map changedMap = new HashMap();
1114:                changedMap.put("First Key", "First Value");
1115:                changedMap.put("Second Key", "Second Value");
1116:
1117:                // No conversion required
1118:                BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
1119:                        "New Second Value");
1120:                checkMap((Map) bean.get("mapProperty"), origMap);
1121:                changedMap.put("Second Key", "New Second Value");
1122:                checkMap(((TestBean) bean.get("nested")).getMapProperty(),
1123:                        changedMap);
1124:
1125:            }
1126:
1127:            /**
1128:             * Test copying a property using a nested simple expression, with and
1129:             * without conversions.
1130:             */
1131:            public void testCopyPropertyNestedSimple() throws Exception {
1132:
1133:                bean.set("intProperty", new Integer(0));
1134:                nested.setIntProperty(0);
1135:
1136:                // No conversion required
1137:                BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(
1138:                        1));
1139:                assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1140:                assertEquals(1, nested.getIntProperty());
1141:
1142:                // Widening conversion required
1143:                BeanUtils.copyProperty(bean, "nested.intProperty", new Byte(
1144:                        (byte) 2));
1145:                assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1146:                assertEquals(2, nested.getIntProperty());
1147:
1148:                // Narrowing conversion required
1149:                BeanUtils.copyProperty(bean, "nested.intProperty", new Long(3));
1150:                assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1151:                assertEquals(3, nested.getIntProperty());
1152:
1153:                // String conversion required
1154:                BeanUtils.copyProperty(bean, "nested.intProperty", "4");
1155:                assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1156:                assertEquals(4, nested.getIntProperty());
1157:
1158:            }
1159:
1160:            // ------------------------------------------------------ Protected Methods
1161:
1162:            // Ensure that the nested intArray matches the specified values
1163:            protected void checkIntArray(int actual[], int expected[]) {
1164:                assertNotNull("actual array not null", actual);
1165:                assertEquals("actual array length", expected.length,
1166:                        actual.length);
1167:                for (int i = 0; i < actual.length; i++) {
1168:                    assertEquals("actual array value[" + i + "]", expected[i],
1169:                            actual[i]);
1170:                }
1171:            }
1172:
1173:            // Ensure that the actual Map matches the expected Map
1174:            protected void checkMap(Map actual, Map expected) {
1175:                assertNotNull("actual map not null", actual);
1176:                assertEquals("actual map size", expected.size(), actual.size());
1177:                Iterator keys = expected.keySet().iterator();
1178:                while (keys.hasNext()) {
1179:                    Object key = keys.next();
1180:                    assertEquals("actual map value(" + key + ")", expected
1181:                            .get(key), actual.get(key));
1182:                }
1183:            }
1184:
1185:            /**
1186:             * Create and return a <code>DynaClass</code> instance for our test
1187:             * <code>DynaBean</code>.
1188:             */
1189:            protected static DynaClass createDynaClass() {
1190:
1191:                int intArray[] = new int[0];
1192:                String stringArray[] = new String[0];
1193:
1194:                DynaClass dynaClass = new BasicDynaClass(
1195:                        "TestDynaClass",
1196:                        null,
1197:                        new DynaProperty[] {
1198:                                new DynaProperty("booleanProperty",
1199:                                        Boolean.TYPE),
1200:                                new DynaProperty("booleanSecond", Boolean.TYPE),
1201:                                new DynaProperty("byteProperty", Byte.TYPE),
1202:                                new DynaProperty("doubleProperty", Double.TYPE),
1203:                                new DynaProperty("dupProperty", stringArray
1204:                                        .getClass()),
1205:                                new DynaProperty("floatProperty", Float.TYPE),
1206:                                new DynaProperty("intArray", intArray
1207:                                        .getClass()),
1208:                                new DynaProperty("intIndexed", intArray
1209:                                        .getClass()),
1210:                                new DynaProperty("intProperty", Integer.TYPE),
1211:                                new DynaProperty("listIndexed", List.class),
1212:                                new DynaProperty("longProperty", Long.TYPE),
1213:                                new DynaProperty("mapProperty", Map.class),
1214:                                new DynaProperty("mappedProperty", Map.class),
1215:                                new DynaProperty("mappedIntProperty", Map.class),
1216:                                new DynaProperty("nested", TestBean.class),
1217:                                new DynaProperty("nullProperty", String.class),
1218:                                new DynaProperty("shortProperty", Short.TYPE),
1219:                                new DynaProperty("stringArray", stringArray
1220:                                        .getClass()),
1221:                                new DynaProperty("stringIndexed", stringArray
1222:                                        .getClass()),
1223:                                new DynaProperty("stringProperty", String.class), });
1224:                return (dynaClass);
1225:
1226:            }
1227:
1228:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.