Source Code Cross Referenced for BasicDynaBeanTestCase.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) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils;
019:
020:        import java.io.ByteArrayInputStream;
021:        import java.io.ByteArrayOutputStream;
022:        import java.io.ObjectInputStream;
023:        import java.io.ObjectOutputStream;
024:
025:        import java.util.ArrayList;
026:        import java.util.HashMap;
027:        import java.util.List;
028:        import java.util.Map;
029:
030:        import junit.framework.TestCase;
031:        import junit.framework.Test;
032:        import junit.framework.TestSuite;
033:
034:        /**
035:         * <p>Test Case for the <code>BasicDynaBean</code> implementation class.
036:         * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
037:         * because the two classes provide similar levels of functionality.</p>
038:         *
039:         * @author Craig R. McClanahan
040:         * @version $Revision: 469743 $ $Date: 2006-11-01 01:27:40 +0000 (Wed, 01 Nov 2006) $
041:         */
042:
043:        public class BasicDynaBeanTestCase extends TestCase {
044:
045:            // ---------------------------------------------------- Instance Variables
046:
047:            /**
048:             * The basic test bean for each test.
049:             */
050:            protected DynaBean bean = null;
051:
052:            /**
053:             * The set of property names we expect to have returned when calling
054:             * <code>getDynaProperties()</code>.  You should update this list
055:             * when new properties are added to TestBean.
056:             */
057:            protected final static String[] properties = { "booleanProperty",
058:                    "booleanSecond", "doubleProperty", "floatProperty",
059:                    "intArray", "intIndexed", "intProperty", "listIndexed",
060:                    "longProperty", "mappedProperty", "mappedIntProperty",
061:                    "nullProperty", "shortProperty", "stringArray",
062:                    "stringIndexed", "stringProperty", };
063:
064:            // ---------------------------------------------------------- Constructors
065:
066:            /**
067:             * Construct a new instance of this test case.
068:             *
069:             * @param name Name of the test case
070:             */
071:            public BasicDynaBeanTestCase(String name) {
072:
073:                super (name);
074:
075:            }
076:
077:            // -------------------------------------------------- Overall Test Methods
078:
079:            /**
080:             * Set up instance variables required by this test case.
081:             */
082:            public void setUp() throws Exception {
083:
084:                // Instantiate a new DynaBean instance
085:                DynaClass dynaClass = createDynaClass();
086:                bean = dynaClass.newInstance();
087:
088:                // Initialize the DynaBean's property values (like TestBean)
089:                bean.set("booleanProperty", new Boolean(true));
090:                bean.set("booleanSecond", new Boolean(true));
091:                bean.set("doubleProperty", new Double(321.0));
092:                bean.set("floatProperty", new Float((float) 123.0));
093:                int intArray[] = { 0, 10, 20, 30, 40 };
094:                bean.set("intArray", intArray);
095:                int intIndexed[] = { 0, 10, 20, 30, 40 };
096:                bean.set("intIndexed", intIndexed);
097:                bean.set("intProperty", new Integer(123));
098:                List listIndexed = new ArrayList();
099:                listIndexed.add("String 0");
100:                listIndexed.add("String 1");
101:                listIndexed.add("String 2");
102:                listIndexed.add("String 3");
103:                listIndexed.add("String 4");
104:                bean.set("listIndexed", listIndexed);
105:                bean.set("longProperty", new Long(321));
106:                HashMap mappedProperty = new HashMap();
107:                mappedProperty.put("First Key", "First Value");
108:                mappedProperty.put("Second Key", "Second Value");
109:                bean.set("mappedProperty", mappedProperty);
110:                HashMap mappedIntProperty = new HashMap();
111:                mappedIntProperty.put("One", new Integer(1));
112:                mappedIntProperty.put("Two", new Integer(2));
113:                bean.set("mappedIntProperty", mappedIntProperty);
114:                // Property "nullProperty" is not initialized, so it should return null
115:                bean.set("shortProperty", new Short((short) 987));
116:                String stringArray[] = { "String 0", "String 1", "String 2",
117:                        "String 3", "String 4" };
118:                bean.set("stringArray", stringArray);
119:                String stringIndexed[] = { "String 0", "String 1", "String 2",
120:                        "String 3", "String 4" };
121:                bean.set("stringIndexed", stringIndexed);
122:                bean.set("stringProperty", "This is a string");
123:
124:            }
125:
126:            /**
127:             * Return the tests included in this test suite.
128:             */
129:            public static Test suite() {
130:
131:                return (new TestSuite(BasicDynaBeanTestCase.class));
132:
133:            }
134:
135:            /**
136:             * Tear down instance variables required by this test case.
137:             */
138:            public void tearDown() {
139:
140:                bean = null;
141:
142:            }
143:
144:            // ------------------------------------------------ Individual Test Methods
145:
146:            /**
147:             * Corner cases on getDynaProperty invalid arguments.
148:             */
149:            public void testGetDescriptorArguments() {
150:
151:                try {
152:                    DynaProperty descriptor = bean.getDynaClass()
153:                            .getDynaProperty("unknown");
154:                    assertNull("Unknown property descriptor should be null",
155:                            descriptor);
156:                } catch (Throwable t) {
157:                    fail("Threw " + t + " instead of returning null");
158:                }
159:
160:                try {
161:                    bean.getDynaClass().getDynaProperty(null);
162:                    fail("Should throw IllegalArgumentException");
163:                } catch (IllegalArgumentException e) {
164:                    // Expected response
165:                } catch (Throwable t) {
166:                    fail("Threw " + t + " instead of IllegalArgumentException");
167:                }
168:
169:            }
170:
171:            /**
172:             * Positive getDynaProperty on property <code>booleanProperty</code>.
173:             */
174:            public void testGetDescriptorBoolean() {
175:
176:                testGetDescriptorBase("booleanProperty", Boolean.TYPE);
177:
178:            }
179:
180:            /**
181:             * Positive getDynaProperty on property <code>doubleProperty</code>.
182:             */
183:            public void testGetDescriptorDouble() {
184:
185:                testGetDescriptorBase("doubleProperty", Double.TYPE);
186:
187:            }
188:
189:            /**
190:             * Positive getDynaProperty on property <code>floatProperty</code>.
191:             */
192:            public void testGetDescriptorFloat() {
193:
194:                testGetDescriptorBase("floatProperty", Float.TYPE);
195:
196:            }
197:
198:            /**
199:             * Positive getDynaProperty on property <code>intProperty</code>.
200:             */
201:            public void testGetDescriptorInt() {
202:
203:                testGetDescriptorBase("intProperty", Integer.TYPE);
204:
205:            }
206:
207:            /**
208:             * Positive getDynaProperty on property <code>longProperty</code>.
209:             */
210:            public void testGetDescriptorLong() {
211:
212:                testGetDescriptorBase("longProperty", Long.TYPE);
213:
214:            }
215:
216:            /**
217:             * Positive getDynaProperty on property <code>booleanSecond</code>
218:             * that uses an "is" method as the getter.
219:             */
220:            public void testGetDescriptorSecond() {
221:
222:                testGetDescriptorBase("booleanSecond", Boolean.TYPE);
223:
224:            }
225:
226:            /**
227:             * Positive getDynaProperty on property <code>shortProperty</code>.
228:             */
229:            public void testGetDescriptorShort() {
230:
231:                testGetDescriptorBase("shortProperty", Short.TYPE);
232:
233:            }
234:
235:            /**
236:             * Positive getDynaProperty on property <code>stringProperty</code>.
237:             */
238:            public void testGetDescriptorString() {
239:
240:                testGetDescriptorBase("stringProperty", String.class);
241:
242:            }
243:
244:            /**
245:             * Positive test for getDynaPropertys().  Each property name
246:             * listed in <code>properties</code> should be returned exactly once.
247:             */
248:            public void testGetDescriptors() {
249:
250:                DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
251:                assertNotNull("Got descriptors", pd);
252:                int count[] = new int[properties.length];
253:                for (int i = 0; i < pd.length; i++) {
254:                    String name = pd[i].getName();
255:                    for (int j = 0; j < properties.length; j++) {
256:                        if (name.equals(properties[j]))
257:                            count[j]++;
258:                    }
259:                }
260:                for (int j = 0; j < properties.length; j++) {
261:                    if (count[j] < 0)
262:                        fail("Missing property " + properties[j]);
263:                    else if (count[j] > 1)
264:                        fail("Duplicate property " + properties[j]);
265:                }
266:
267:            }
268:
269:            /**
270:             * Corner cases on getIndexedProperty invalid arguments.
271:             */
272:            public void testGetIndexedArguments() {
273:
274:                try {
275:                    bean.get("intArray", -1);
276:                    fail("Should throw IndexOutOfBoundsException");
277:                } catch (IndexOutOfBoundsException e) {
278:                    // Expected response
279:                } catch (Throwable t) {
280:                    fail("Threw " + t + " instead of IndexOutOfBoundsException");
281:                }
282:
283:            }
284:
285:            /**
286:             * Positive and negative tests on getIndexedProperty valid arguments.
287:             */
288:            public void testGetIndexedValues() {
289:
290:                Object value = null;
291:
292:                for (int i = 0; i < 5; i++) {
293:
294:                    try {
295:                        value = bean.get("intArray", i);
296:                        assertNotNull("intArray returned value " + i, value);
297:                        assertTrue("intArray returned Integer " + i,
298:                                value instanceof  Integer);
299:                        assertEquals("intArray returned correct " + i, i * 10,
300:                                ((Integer) value).intValue());
301:                    } catch (Throwable t) {
302:                        fail("intArray " + i + " threw " + t);
303:                    }
304:
305:                    try {
306:                        value = bean.get("intIndexed", i);
307:                        assertNotNull("intIndexed returned value " + i, value);
308:                        assertTrue("intIndexed returned Integer " + i,
309:                                value instanceof  Integer);
310:                        assertEquals("intIndexed returned correct " + i,
311:                                i * 10, ((Integer) value).intValue());
312:                    } catch (Throwable t) {
313:                        fail("intIndexed " + i + " threw " + t);
314:                    }
315:
316:                    try {
317:                        value = bean.get("listIndexed", i);
318:                        assertNotNull("listIndexed returned value " + i, value);
319:                        assertTrue("list returned String " + i,
320:                                value instanceof  String);
321:                        assertEquals("listIndexed returned correct " + i,
322:                                "String " + i, (String) value);
323:                    } catch (Throwable t) {
324:                        fail("listIndexed " + i + " threw " + t);
325:                    }
326:
327:                    try {
328:                        value = bean.get("stringArray", i);
329:                        assertNotNull("stringArray returned value " + i, value);
330:                        assertTrue("stringArray returned String " + i,
331:                                value instanceof  String);
332:                        assertEquals("stringArray returned correct " + i,
333:                                "String " + i, (String) value);
334:                    } catch (Throwable t) {
335:                        fail("stringArray " + i + " threw " + t);
336:                    }
337:
338:                    try {
339:                        value = bean.get("stringIndexed", i);
340:                        assertNotNull("stringIndexed returned value " + i,
341:                                value);
342:                        assertTrue("stringIndexed returned String " + i,
343:                                value instanceof  String);
344:                        assertEquals("stringIndexed returned correct " + i,
345:                                "String " + i, (String) value);
346:                    } catch (Throwable t) {
347:                        fail("stringIndexed " + i + " threw " + t);
348:                    }
349:
350:                }
351:
352:            }
353:
354:            /**
355:             * Corner cases on getMappedProperty invalid arguments.
356:             */
357:            public void testGetMappedArguments() {
358:
359:                try {
360:                    Object value = bean.get("mappedProperty", "unknown");
361:                    assertNull("Should not return a value", value);
362:                } catch (Throwable t) {
363:                    fail("Threw " + t + " instead of returning null");
364:                }
365:
366:            }
367:
368:            /**
369:             * Positive and negative tests on getMappedProperty valid arguments.
370:             */
371:            public void testGetMappedValues() {
372:
373:                Object value = null;
374:
375:                try {
376:                    value = bean.get("mappedProperty", "First Key");
377:                    assertEquals("Can find first value", "First Value", value);
378:                } catch (Throwable t) {
379:                    fail("Finding first value threw " + t);
380:                }
381:
382:                try {
383:                    value = bean.get("mappedProperty", "Second Key");
384:                    assertEquals("Can find second value", "Second Value", value);
385:                } catch (Throwable t) {
386:                    fail("Finding second value threw " + t);
387:                }
388:
389:                try {
390:                    value = bean.get("mappedProperty", "Third Key");
391:                    assertNull("Can not find third value", value);
392:                } catch (Throwable t) {
393:                    fail("Finding third value threw " + t);
394:                }
395:
396:            }
397:
398:            /**
399:             * Corner cases on getSimpleProperty invalid arguments.
400:             */
401:            public void testGetSimpleArguments() {
402:
403:                try {
404:                    bean.get(null);
405:                    fail("Should throw IllegalArgumentException");
406:                } catch (IllegalArgumentException e) {
407:                    // Expected response
408:                } catch (Throwable t) {
409:                    fail("Threw " + t + " instead of IllegalArgumentException");
410:                }
411:
412:            }
413:
414:            /**
415:             * Test getSimpleProperty on a boolean property.
416:             */
417:            public void testGetSimpleBoolean() {
418:
419:                try {
420:                    Object value = bean.get("booleanProperty");
421:                    assertNotNull("Got a value", value);
422:                    assertTrue("Got correct type", (value instanceof  Boolean));
423:                    assertTrue("Got correct value", ((Boolean) value)
424:                            .booleanValue() == true);
425:                } catch (Throwable e) {
426:                    fail("Exception: " + e);
427:                }
428:
429:            }
430:
431:            /**
432:             * Test getSimpleProperty on a double property.
433:             */
434:            public void testGetSimpleDouble() {
435:
436:                try {
437:                    Object value = bean.get("doubleProperty");
438:                    assertNotNull("Got a value", value);
439:                    assertTrue("Got correct type", (value instanceof  Double));
440:                    assertEquals("Got correct value", ((Double) value)
441:                            .doubleValue(), 321.0, 0.005);
442:                } catch (Throwable t) {
443:                    fail("Exception: " + t);
444:                }
445:
446:            }
447:
448:            /**
449:             * Test getSimpleProperty on a float property.
450:             */
451:            public void testGetSimpleFloat() {
452:
453:                try {
454:                    Object value = bean.get("floatProperty");
455:                    assertNotNull("Got a value", value);
456:                    assertTrue("Got correct type", (value instanceof  Float));
457:                    assertEquals("Got correct value", ((Float) value)
458:                            .floatValue(), (float) 123.0, (float) 0.005);
459:                } catch (Throwable t) {
460:                    fail("Exception: " + t);
461:                }
462:
463:            }
464:
465:            /**
466:             * Test getSimpleProperty on a int property.
467:             */
468:            public void testGetSimpleInt() {
469:
470:                try {
471:                    Object value = bean.get("intProperty");
472:                    assertNotNull("Got a value", value);
473:                    assertTrue("Got correct type", (value instanceof  Integer));
474:                    assertEquals("Got correct value", ((Integer) value)
475:                            .intValue(), 123);
476:                } catch (Throwable t) {
477:                    fail("Exception: " + t);
478:                }
479:
480:            }
481:
482:            /**
483:             * Test getSimpleProperty on a long property.
484:             */
485:            public void testGetSimpleLong() {
486:
487:                try {
488:                    Object value = bean.get("longProperty");
489:                    assertNotNull("Got a value", value);
490:                    assertTrue("Got correct type", (value instanceof  Long));
491:                    assertEquals("Got correct value", ((Long) value)
492:                            .longValue(), 321);
493:                } catch (Throwable t) {
494:                    fail("Exception: " + t);
495:                }
496:
497:            }
498:
499:            /**
500:             * Test getSimpleProperty on a short property.
501:             */
502:            public void testGetSimpleShort() {
503:
504:                try {
505:                    Object value = bean.get("shortProperty");
506:                    assertNotNull("Got a value", value);
507:                    assertTrue("Got correct type", (value instanceof  Short));
508:                    assertEquals("Got correct value", ((Short) value)
509:                            .shortValue(), (short) 987);
510:                } catch (Throwable t) {
511:                    fail("Exception: " + t);
512:                }
513:
514:            }
515:
516:            /**
517:             * Test getSimpleProperty on a String property.
518:             */
519:            public void testGetSimpleString() {
520:
521:                try {
522:                    Object value = bean.get("stringProperty");
523:                    assertNotNull("Got a value", value);
524:                    assertTrue("Got correct type", (value instanceof  String));
525:                    assertEquals("Got correct value", (String) value,
526:                            "This is a string");
527:                } catch (Throwable t) {
528:                    fail("Exception: " + t);
529:                }
530:
531:            }
532:
533:            /**
534:             * Test <code>contains()</code> method for mapped properties.
535:             */
536:            public void testMappedContains() {
537:
538:                try {
539:                    assertTrue("Can see first key", bean.contains(
540:                            "mappedProperty", "First Key"));
541:                } catch (Throwable t) {
542:                    fail("Exception: " + t);
543:                }
544:
545:                try {
546:                    assertTrue("Can not see unknown key", !bean.contains(
547:                            "mappedProperty", "Unknown Key"));
548:                } catch (Throwable t) {
549:                    fail("Exception: " + t);
550:                }
551:
552:            }
553:
554:            /**
555:             * Test <code>remove()</code> method for mapped properties.
556:             */
557:            public void testMappedRemove() {
558:
559:                try {
560:                    assertTrue("Can see first key", bean.contains(
561:                            "mappedProperty", "First Key"));
562:                    bean.remove("mappedProperty", "First Key");
563:                    assertTrue("Can not see first key", !bean.contains(
564:                            "mappedProperty", "First Key"));
565:                } catch (Throwable t) {
566:                    fail("Exception: " + t);
567:                }
568:
569:                try {
570:                    assertTrue("Can not see unknown key", !bean.contains(
571:                            "mappedProperty", "Unknown Key"));
572:                    bean.remove("mappedProperty", "Unknown Key");
573:                    assertTrue("Can not see unknown key", !bean.contains(
574:                            "mappedProperty", "Unknown Key"));
575:                } catch (Throwable t) {
576:                    fail("Exception: " + t);
577:                }
578:
579:            }
580:
581:            /**
582:             * Test serialization and deserialization.
583:             */
584:            public void testSerialization() {
585:
586:                // Serialize the test bean
587:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
588:                try {
589:                    ObjectOutputStream oos = new ObjectOutputStream(baos);
590:                    oos.writeObject(bean);
591:                    oos.flush();
592:                    oos.close();
593:                } catch (Exception e) {
594:                    fail("Exception during serialization: " + e);
595:                }
596:
597:                // Deserialize the test bean
598:                try {
599:                    bean = null;
600:                    ByteArrayInputStream bais = new ByteArrayInputStream(baos
601:                            .toByteArray());
602:                    ObjectInputStream ois = new ObjectInputStream(bais);
603:                    bean = (DynaBean) ois.readObject();
604:                    bais.close();
605:                } catch (Exception e) {
606:                    fail("Exception during deserialization: " + e);
607:                }
608:
609:                // Confirm property values
610:                testGetDescriptorArguments();
611:                testGetDescriptorBoolean();
612:                testGetDescriptorDouble();
613:                testGetDescriptorFloat();
614:                testGetDescriptorInt();
615:                testGetDescriptorLong();
616:                testGetDescriptorSecond();
617:                testGetDescriptorShort();
618:                testGetDescriptorString();
619:                testGetDescriptors();
620:                testGetIndexedArguments();
621:                testGetIndexedValues();
622:                testGetMappedArguments();
623:                testGetMappedValues();
624:                testGetSimpleArguments();
625:                testGetSimpleBoolean();
626:                testGetSimpleDouble();
627:                testGetSimpleFloat();
628:                testGetSimpleInt();
629:                testGetSimpleLong();
630:                testGetSimpleShort();
631:                testGetSimpleString();
632:                testMappedContains();
633:                testMappedRemove();
634:
635:                // Ensure that we can create a new instance of the same DynaClass
636:                try {
637:                    bean = bean.getDynaClass().newInstance();
638:                } catch (Exception e) {
639:                    fail("Exception creating new instance: " + e);
640:                }
641:                testGetDescriptorArguments();
642:                testGetDescriptorBoolean();
643:                testGetDescriptorDouble();
644:                testGetDescriptorFloat();
645:                testGetDescriptorInt();
646:                testGetDescriptorLong();
647:                testGetDescriptorSecond();
648:                testGetDescriptorShort();
649:                testGetDescriptorString();
650:                testGetDescriptors();
651:
652:            }
653:
654:            /**
655:             * Corner cases on setIndexedProperty invalid arguments.
656:             */
657:            public void testSetIndexedArguments() {
658:
659:                try {
660:                    bean.set("intArray", -1, new Integer(0));
661:                    fail("Should throw IndexOutOfBoundsException");
662:                } catch (IndexOutOfBoundsException e) {
663:                    // Expected response
664:                } catch (Throwable t) {
665:                    fail("Threw " + t + " instead of IndexOutOfBoundsException");
666:                }
667:
668:            }
669:
670:            /**
671:             * Positive and negative tests on setIndexedProperty valid arguments.
672:             */
673:            public void testSetIndexedValues() {
674:
675:                Object value = null;
676:
677:                try {
678:                    bean.set("intArray", 0, new Integer(1));
679:                    value = (Integer) bean.get("intArray", 0);
680:                    assertNotNull("Returned new value 0", value);
681:                    assertTrue("Returned Integer new value 0",
682:                            value instanceof  Integer);
683:                    assertEquals("Returned correct new value 0", 1,
684:                            ((Integer) value).intValue());
685:                } catch (Throwable t) {
686:                    fail("Threw " + t);
687:                }
688:
689:                try {
690:                    bean.set("intIndexed", 1, new Integer(11));
691:                    value = (Integer) bean.get("intIndexed", 1);
692:                    assertNotNull("Returned new value 1", value);
693:                    assertTrue("Returned Integer new value 1",
694:                            value instanceof  Integer);
695:                    assertEquals("Returned correct new value 1", 11,
696:                            ((Integer) value).intValue());
697:                } catch (Throwable t) {
698:                    fail("Threw " + t);
699:                }
700:
701:                try {
702:                    bean.set("listIndexed", 2, "New Value 2");
703:                    value = (String) bean.get("listIndexed", 2);
704:                    assertNotNull("Returned new value 2", value);
705:                    assertTrue("Returned String new value 2",
706:                            value instanceof  String);
707:                    assertEquals("Returned correct new value 2", "New Value 2",
708:                            (String) value);
709:                } catch (Throwable t) {
710:                    fail("Threw " + t);
711:                }
712:
713:                try {
714:                    bean.set("stringArray", 3, "New Value 3");
715:                    value = (String) bean.get("stringArray", 3);
716:                    assertNotNull("Returned new value 3", value);
717:                    assertTrue("Returned String new value 3",
718:                            value instanceof  String);
719:                    assertEquals("Returned correct new value 3", "New Value 3",
720:                            (String) value);
721:                } catch (Throwable t) {
722:                    fail("Threw " + t);
723:                }
724:
725:                try {
726:                    bean.set("stringIndexed", 4, "New Value 4");
727:                    value = (String) bean.get("stringIndexed", 4);
728:                    assertNotNull("Returned new value 4", value);
729:                    assertTrue("Returned String new value 4",
730:                            value instanceof  String);
731:                    assertEquals("Returned correct new value 4", "New Value 4",
732:                            (String) value);
733:                } catch (Throwable t) {
734:                    fail("Threw " + t);
735:                }
736:
737:            }
738:
739:            /**
740:             * Positive and negative tests on setMappedProperty valid arguments.
741:             */
742:            public void testSetMappedValues() {
743:
744:                try {
745:                    bean.set("mappedProperty", "First Key", "New First Value");
746:                    assertEquals("Can replace old value", "New First Value",
747:                            (String) bean.get("mappedProperty", "First Key"));
748:                } catch (Throwable t) {
749:                    fail("Finding fourth value threw " + t);
750:                }
751:
752:                try {
753:                    bean.set("mappedProperty", "Fourth Key", "Fourth Value");
754:                    assertEquals("Can set new value", "Fourth Value",
755:                            (String) bean.get("mappedProperty", "Fourth Key"));
756:                } catch (Throwable t) {
757:                    fail("Finding fourth value threw " + t);
758:                }
759:
760:            }
761:
762:            /**
763:             * Test setSimpleProperty on a boolean property.
764:             */
765:            public void testSetSimpleBoolean() {
766:
767:                try {
768:                    boolean oldValue = ((Boolean) bean.get("booleanProperty"))
769:                            .booleanValue();
770:                    boolean newValue = !oldValue;
771:                    bean.set("booleanProperty", new Boolean(newValue));
772:                    assertTrue("Matched new value", newValue == ((Boolean) bean
773:                            .get("booleanProperty")).booleanValue());
774:                } catch (Throwable e) {
775:                    fail("Exception: " + e);
776:                }
777:
778:            }
779:
780:            /**
781:             * Test setSimpleProperty on a double property.
782:             */
783:            public void testSetSimpleDouble() {
784:
785:                try {
786:                    double oldValue = ((Double) bean.get("doubleProperty"))
787:                            .doubleValue();
788:                    double newValue = oldValue + 1.0;
789:                    bean.set("doubleProperty", new Double(newValue));
790:                    assertEquals("Matched new value", newValue, ((Double) bean
791:                            .get("doubleProperty")).doubleValue(), 0.005);
792:                } catch (Throwable e) {
793:                    fail("Exception: " + e);
794:                }
795:
796:            }
797:
798:            /**
799:             * Test setSimpleProperty on a float property.
800:             */
801:            public void testSetSimpleFloat() {
802:
803:                try {
804:                    float oldValue = ((Float) bean.get("floatProperty"))
805:                            .floatValue();
806:                    float newValue = oldValue + (float) 1.0;
807:                    bean.set("floatProperty", new Float(newValue));
808:                    assertEquals("Matched new value", newValue, ((Float) bean
809:                            .get("floatProperty")).floatValue(), (float) 0.005);
810:                } catch (Throwable e) {
811:                    fail("Exception: " + e);
812:                }
813:
814:            }
815:
816:            /**
817:             * Test setSimpleProperty on a int property.
818:             */
819:            public void testSetSimpleInt() {
820:
821:                try {
822:                    int oldValue = ((Integer) bean.get("intProperty"))
823:                            .intValue();
824:                    int newValue = oldValue + 1;
825:                    bean.set("intProperty", new Integer(newValue));
826:                    assertEquals("Matched new value", newValue, ((Integer) bean
827:                            .get("intProperty")).intValue());
828:                } catch (Throwable e) {
829:                    fail("Exception: " + e);
830:                }
831:
832:            }
833:
834:            /**
835:             * Test setSimpleProperty on a long property.
836:             */
837:            public void testSetSimpleLong() {
838:
839:                try {
840:                    long oldValue = ((Long) bean.get("longProperty"))
841:                            .longValue();
842:                    long newValue = oldValue + 1;
843:                    bean.set("longProperty", new Long(newValue));
844:                    assertEquals("Matched new value", newValue, ((Long) bean
845:                            .get("longProperty")).longValue());
846:                } catch (Throwable e) {
847:                    fail("Exception: " + e);
848:                }
849:
850:            }
851:
852:            /**
853:             * Test setSimpleProperty on a short property.
854:             */
855:            public void testSetSimpleShort() {
856:
857:                try {
858:                    short oldValue = ((Short) bean.get("shortProperty"))
859:                            .shortValue();
860:                    short newValue = (short) (oldValue + 1);
861:                    bean.set("shortProperty", new Short(newValue));
862:                    assertEquals("Matched new value", newValue, ((Short) bean
863:                            .get("shortProperty")).shortValue());
864:                } catch (Throwable e) {
865:                    fail("Exception: " + e);
866:                }
867:
868:            }
869:
870:            /**
871:             * Test setSimpleProperty on a String property.
872:             */
873:            public void testSetSimpleString() {
874:
875:                try {
876:                    String oldValue = (String) bean.get("stringProperty");
877:                    String newValue = oldValue + " Extra Value";
878:                    bean.set("stringProperty", newValue);
879:                    assertEquals("Matched new value", newValue, (String) bean
880:                            .get("stringProperty"));
881:                } catch (Throwable e) {
882:                    fail("Exception: " + e);
883:                }
884:
885:            }
886:
887:            // ------------------------------------------------------ Protected Methods
888:
889:            /**
890:             * Create and return a <code>DynaClass</code> instance for our test
891:             * <code>DynaBean</code>.
892:             */
893:            protected DynaClass createDynaClass() {
894:
895:                int intArray[] = new int[0];
896:                String stringArray[] = new String[0];
897:
898:                DynaClass dynaClass = new BasicDynaClass(
899:                        "TestDynaClass",
900:                        null,
901:                        new DynaProperty[] {
902:                                new DynaProperty("booleanProperty",
903:                                        Boolean.TYPE),
904:                                new DynaProperty("booleanSecond", Boolean.TYPE),
905:                                new DynaProperty("doubleProperty", Double.TYPE),
906:                                new DynaProperty("floatProperty", Float.TYPE),
907:                                new DynaProperty("intArray", intArray
908:                                        .getClass()),
909:                                new DynaProperty("intIndexed", intArray
910:                                        .getClass()),
911:                                new DynaProperty("intProperty", Integer.TYPE),
912:                                new DynaProperty("listIndexed", List.class),
913:                                new DynaProperty("longProperty", Long.TYPE),
914:                                new DynaProperty("mappedProperty", Map.class),
915:                                new DynaProperty("mappedIntProperty", Map.class),
916:                                new DynaProperty("nullProperty", String.class),
917:                                new DynaProperty("shortProperty", Short.TYPE),
918:                                new DynaProperty("stringArray", stringArray
919:                                        .getClass()),
920:                                new DynaProperty("stringIndexed", stringArray
921:                                        .getClass()),
922:                                new DynaProperty("stringProperty", String.class), });
923:                return (dynaClass);
924:
925:            }
926:
927:            /**
928:             * Base for testGetDescriptorXxxxx() series of tests.
929:             *
930:             * @param name Name of the property to be retrieved
931:             * @param type Expected class type of this property
932:             */
933:            protected void testGetDescriptorBase(String name, Class type) {
934:
935:                try {
936:                    DynaProperty descriptor = bean.getDynaClass()
937:                            .getDynaProperty(name);
938:                    assertNotNull("Got descriptor", descriptor);
939:                    assertEquals("Got correct type", type, descriptor.getType());
940:                } catch (Throwable t) {
941:                    fail("Threw an exception: " + t);
942:                }
943:
944:            }
945:
946:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.