Source Code Cross Referenced for ConvertUtilsTestCase.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.sql.Date;
021:        import java.sql.Time;
022:        import java.sql.Timestamp;
023:        import java.text.DateFormat;
024:        import java.text.SimpleDateFormat;
025:        import java.util.Locale;
026:        import org.apache.commons.beanutils.converters.DateConverter;
027:        import junit.framework.TestCase;
028:        import junit.framework.Test;
029:        import junit.framework.TestSuite;
030:
031:        /**
032:         * <p>
033:         *  Test Case for the ConvertUtils class.
034:         * </p>
035:         *
036:         * @author Craig R. McClanahan
037:         * @version $Revision: 551047 $ $Date: 2007-06-27 06:34:15 +0100 (Wed, 27 Jun 2007) $
038:         */
039:
040:        public class ConvertUtilsTestCase extends TestCase {
041:
042:            // ---------------------------------------------------- Instance Variables
043:
044:            // ---------------------------------------------------------- Constructors
045:
046:            /**
047:             * Construct a new instance of this test case.
048:             *
049:             * @param name Name of the test case
050:             */
051:            public ConvertUtilsTestCase(String name) {
052:                super (name);
053:            }
054:
055:            // -------------------------------------------------- Overall Test Methods
056:
057:            /**
058:             * Set up instance variables required by this test case.
059:             */
060:            public void setUp() {
061:
062:                ConvertUtils.deregister();
063:
064:            }
065:
066:            /**
067:             * Return the tests included in this test suite.
068:             */
069:            public static Test suite() {
070:                return (new TestSuite(ConvertUtilsTestCase.class));
071:            }
072:
073:            /**
074:             * Tear down instance variables required by this test case.
075:             */
076:            public void tearDown() {
077:                // No action required
078:            }
079:
080:            // ------------------------------------------------ Individual Test Methods
081:
082:            /**
083:             * Negative String to primitive integer array tests.
084:             */
085:            public void testNegativeIntegerArray() {
086:
087:                Object value = null;
088:                int intArray[] = new int[0];
089:
090:                value = ConvertUtils
091:                        .convert((String) null, intArray.getClass());
092:                checkIntegerArray(value, intArray);
093:                value = ConvertUtils.convert("a", intArray.getClass());
094:                checkIntegerArray(value, intArray);
095:                value = ConvertUtils.convert("{ a }", intArray.getClass());
096:                checkIntegerArray(value, intArray);
097:                value = ConvertUtils.convert("1a3", intArray.getClass());
098:                checkIntegerArray(value, intArray);
099:                value = ConvertUtils.convert("{ 1a3 }", intArray.getClass());
100:                checkIntegerArray(value, intArray);
101:                value = ConvertUtils.convert("0,1a3", intArray.getClass());
102:                checkIntegerArray(value, intArray);
103:                value = ConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());
104:                checkIntegerArray(value, intArray);
105:
106:            }
107:
108:            /**
109:             * Negative scalar conversion tests.  These rely on the standard
110:             * default value conversions in ConvertUtils.
111:             */
112:            public void testNegativeScalar() {
113:
114:                Object value = null;
115:
116:                value = ConvertUtils.convert("foo", Boolean.TYPE);
117:                assertTrue(value instanceof  Boolean);
118:                assertEquals(((Boolean) value).booleanValue(), false);
119:
120:                value = ConvertUtils.convert("foo", Boolean.class);
121:                assertTrue(value instanceof  Boolean);
122:                assertEquals(((Boolean) value).booleanValue(), false);
123:
124:                value = ConvertUtils.convert("foo", Byte.TYPE);
125:                assertTrue(value instanceof  Byte);
126:                assertEquals(((Byte) value).byteValue(), (byte) 0);
127:
128:                value = ConvertUtils.convert("foo", Byte.class);
129:                assertTrue(value instanceof  Byte);
130:                assertEquals(((Byte) value).byteValue(), (byte) 0);
131:
132:                try {
133:                    value = ConvertUtils.convert(
134:                            "org.apache.commons.beanutils.Undefined",
135:                            Class.class);
136:                    fail("Should have thrown conversion exception");
137:                } catch (ConversionException e) {
138:                    // Expected result
139:                }
140:
141:                value = ConvertUtils.convert("foo", Double.TYPE);
142:                assertTrue(value instanceof  Double);
143:                assertEquals(((Double) value).doubleValue(), 0.0, 0.005);
144:
145:                value = ConvertUtils.convert("foo", Double.class);
146:                assertTrue(value instanceof  Double);
147:                assertEquals(((Double) value).doubleValue(), 0.0, 0.005);
148:
149:                value = ConvertUtils.convert("foo", Float.TYPE);
150:                assertTrue(value instanceof  Float);
151:                assertEquals(((Float) value).floatValue(), (float) 0.0,
152:                        (float) 0.005);
153:
154:                value = ConvertUtils.convert("foo", Float.class);
155:                assertTrue(value instanceof  Float);
156:                assertEquals(((Float) value).floatValue(), (float) 0.0,
157:                        (float) 0.005);
158:
159:                value = ConvertUtils.convert("foo", Integer.TYPE);
160:                assertTrue(value instanceof  Integer);
161:                assertEquals(((Integer) value).intValue(), 0);
162:
163:                value = ConvertUtils.convert("foo", Integer.class);
164:                assertTrue(value instanceof  Integer);
165:                assertEquals(((Integer) value).intValue(), 0);
166:
167:                value = ConvertUtils.convert("foo", Byte.TYPE);
168:                assertTrue(value instanceof  Byte);
169:                assertEquals(((Byte) value).byteValue(), (byte) 0);
170:
171:                value = ConvertUtils.convert("foo", Long.class);
172:                assertTrue(value instanceof  Long);
173:                assertEquals(((Long) value).longValue(), 0);
174:
175:                value = ConvertUtils.convert("foo", Short.TYPE);
176:                assertTrue(value instanceof  Short);
177:                assertEquals(((Short) value).shortValue(), (short) 0);
178:
179:                value = ConvertUtils.convert("foo", Short.class);
180:                assertTrue(value instanceof  Short);
181:                assertEquals(((Short) value).shortValue(), (short) 0);
182:
183:            }
184:
185:            /**
186:             * Negative String to String array tests.
187:             */
188:            public void testNegativeStringArray() {
189:
190:                Object value = null;
191:                String stringArray[] = new String[0];
192:
193:                value = ConvertUtils.convert((String) null, stringArray
194:                        .getClass());
195:                checkStringArray(value, stringArray);
196:
197:            }
198:
199:            /**
200:             * Test conversion of object to string for arrays.
201:             */
202:            public void testObjectToStringArray() {
203:
204:                int intArray0[] = new int[0];
205:                int intArray1[] = { 123 };
206:                int intArray2[] = { 123, 456 };
207:                String stringArray0[] = new String[0];
208:                String stringArray1[] = { "abc" };
209:                String stringArray2[] = { "abc", "def" };
210:
211:                assertEquals("intArray0", null, ConvertUtils.convert(intArray0));
212:                assertEquals("intArray1", "123", ConvertUtils
213:                        .convert(intArray1));
214:                assertEquals("intArray2", "123", ConvertUtils
215:                        .convert(intArray2));
216:
217:                assertEquals("stringArray0", null, ConvertUtils
218:                        .convert(stringArray0));
219:                assertEquals("stringArray1", "abc", ConvertUtils
220:                        .convert(stringArray1));
221:                assertEquals("stringArray2", "abc", ConvertUtils
222:                        .convert(stringArray2));
223:
224:            }
225:
226:            /**
227:             * Test conversion of object to string for scalars.
228:             */
229:            public void testObjectToStringScalar() {
230:
231:                assertEquals("Boolean->String", "false", ConvertUtils
232:                        .convert(Boolean.FALSE));
233:                assertEquals("Boolean->String", "true", ConvertUtils
234:                        .convert(Boolean.TRUE));
235:                assertEquals("Byte->String", "123", ConvertUtils
236:                        .convert(new Byte((byte) 123)));
237:                assertEquals("Character->String", "a", ConvertUtils
238:                        .convert(new Character('a')));
239:                assertEquals("Double->String", "123.0", ConvertUtils
240:                        .convert(new Double(123.0)));
241:                assertEquals("Float->String", "123.0", ConvertUtils
242:                        .convert(new Float((float) 123.0)));
243:                assertEquals("Integer->String", "123", ConvertUtils
244:                        .convert(new Integer(123)));
245:                assertEquals("Long->String", "123", ConvertUtils
246:                        .convert(new Long(123)));
247:                assertEquals("Short->String", "123", ConvertUtils
248:                        .convert(new Short((short) 123)));
249:                assertEquals("String->String", "abc", ConvertUtils
250:                        .convert("abc"));
251:                assertEquals("String->String null", null, ConvertUtils
252:                        .convert(null));
253:
254:            }
255:
256:            /**
257:             * Positive array conversion tests.
258:             */
259:            public void testPositiveArray() {
260:
261:                String values1[] = { "10", "20", "30" };
262:                Object value = ConvertUtils.convert(values1, Integer.TYPE);
263:                int shape[] = new int[0];
264:                assertEquals(shape.getClass(), value.getClass());
265:                int results1[] = (int[]) value;
266:                assertEquals(results1[0], 10);
267:                assertEquals(results1[1], 20);
268:                assertEquals(results1[2], 30);
269:
270:                String values2[] = { "100", "200", "300" };
271:                value = ConvertUtils.convert(values2, shape.getClass());
272:                assertEquals(shape.getClass(), value.getClass());
273:                int results2[] = (int[]) value;
274:                assertEquals(results2[0], 100);
275:                assertEquals(results2[1], 200);
276:                assertEquals(results2[2], 300);
277:
278:            }
279:
280:            /**
281:             * Positive String to primitive integer array tests.
282:             */
283:            public void testPositiveIntegerArray() {
284:
285:                Object value = null;
286:                int intArray[] = new int[0];
287:                int intArray1[] = new int[] { 0 };
288:                int intArray2[] = new int[] { 0, 10 };
289:
290:                value = ConvertUtils.convert("{  }", intArray.getClass());
291:                checkIntegerArray(value, intArray);
292:
293:                value = ConvertUtils.convert("0", intArray.getClass());
294:                checkIntegerArray(value, intArray1);
295:                value = ConvertUtils.convert(" 0 ", intArray.getClass());
296:                checkIntegerArray(value, intArray1);
297:                value = ConvertUtils.convert("{ 0 }", intArray.getClass());
298:                checkIntegerArray(value, intArray1);
299:
300:                value = ConvertUtils.convert("0,10", intArray.getClass());
301:                checkIntegerArray(value, intArray2);
302:                value = ConvertUtils.convert("0 10", intArray.getClass());
303:                checkIntegerArray(value, intArray2);
304:                value = ConvertUtils.convert("{0,10}", intArray.getClass());
305:                checkIntegerArray(value, intArray2);
306:                value = ConvertUtils.convert("{0 10}", intArray.getClass());
307:                checkIntegerArray(value, intArray2);
308:                value = ConvertUtils.convert("{ 0, 10 }", intArray.getClass());
309:                checkIntegerArray(value, intArray2);
310:                value = ConvertUtils.convert("{ 0 10 }", intArray.getClass());
311:                checkIntegerArray(value, intArray2);
312:
313:            }
314:
315:            /**
316:             * Positive scalar conversion tests.
317:             */
318:            public void testPositiveScalar() {
319:
320:                Object value = null;
321:
322:                value = ConvertUtils.convert("true", Boolean.TYPE);
323:                assertTrue(value instanceof  Boolean);
324:                assertEquals(((Boolean) value).booleanValue(), true);
325:
326:                value = ConvertUtils.convert("true", Boolean.class);
327:                assertTrue(value instanceof  Boolean);
328:                assertEquals(((Boolean) value).booleanValue(), true);
329:
330:                value = ConvertUtils.convert("yes", Boolean.TYPE);
331:                assertTrue(value instanceof  Boolean);
332:                assertEquals(((Boolean) value).booleanValue(), true);
333:
334:                value = ConvertUtils.convert("yes", Boolean.class);
335:                assertTrue(value instanceof  Boolean);
336:                assertEquals(((Boolean) value).booleanValue(), true);
337:
338:                value = ConvertUtils.convert("y", Boolean.TYPE);
339:                assertTrue(value instanceof  Boolean);
340:                assertEquals(((Boolean) value).booleanValue(), true);
341:
342:                value = ConvertUtils.convert("y", Boolean.class);
343:                assertTrue(value instanceof  Boolean);
344:                assertEquals(((Boolean) value).booleanValue(), true);
345:
346:                value = ConvertUtils.convert("on", Boolean.TYPE);
347:                assertTrue(value instanceof  Boolean);
348:                assertEquals(((Boolean) value).booleanValue(), true);
349:
350:                value = ConvertUtils.convert("on", Boolean.class);
351:                assertTrue(value instanceof  Boolean);
352:                assertEquals(((Boolean) value).booleanValue(), true);
353:
354:                value = ConvertUtils.convert("false", Boolean.TYPE);
355:                assertTrue(value instanceof  Boolean);
356:                assertEquals(((Boolean) value).booleanValue(), false);
357:
358:                value = ConvertUtils.convert("false", Boolean.class);
359:                assertTrue(value instanceof  Boolean);
360:                assertEquals(((Boolean) value).booleanValue(), false);
361:
362:                value = ConvertUtils.convert("no", Boolean.TYPE);
363:                assertTrue(value instanceof  Boolean);
364:                assertEquals(((Boolean) value).booleanValue(), false);
365:
366:                value = ConvertUtils.convert("no", Boolean.class);
367:                assertTrue(value instanceof  Boolean);
368:                assertEquals(((Boolean) value).booleanValue(), false);
369:
370:                value = ConvertUtils.convert("n", Boolean.TYPE);
371:                assertTrue(value instanceof  Boolean);
372:                assertEquals(((Boolean) value).booleanValue(), false);
373:
374:                value = ConvertUtils.convert("n", Boolean.class);
375:                assertTrue(value instanceof  Boolean);
376:                assertEquals(((Boolean) value).booleanValue(), false);
377:
378:                value = ConvertUtils.convert("off", Boolean.TYPE);
379:                assertTrue(value instanceof  Boolean);
380:                assertEquals(((Boolean) value).booleanValue(), false);
381:
382:                value = ConvertUtils.convert("off", Boolean.class);
383:                assertTrue(value instanceof  Boolean);
384:                assertEquals(((Boolean) value).booleanValue(), false);
385:
386:                value = ConvertUtils.convert("123", Byte.TYPE);
387:                assertTrue(value instanceof  Byte);
388:                assertEquals(((Byte) value).byteValue(), (byte) 123);
389:
390:                value = ConvertUtils.convert("123", Byte.class);
391:                assertTrue(value instanceof  Byte);
392:                assertEquals(((Byte) value).byteValue(), (byte) 123);
393:
394:                value = ConvertUtils.convert("a", Character.TYPE);
395:                assertTrue(value instanceof  Character);
396:                assertEquals(((Character) value).charValue(), 'a');
397:
398:                value = ConvertUtils.convert("a", Character.class);
399:                assertTrue(value instanceof  Character);
400:                assertEquals(((Character) value).charValue(), 'a');
401:
402:                value = ConvertUtils.convert("java.lang.String", Class.class);
403:                assertTrue(value instanceof  Class);
404:                assertEquals(String.class, value);
405:
406:                value = ConvertUtils.convert("123.456", Double.TYPE);
407:                assertTrue(value instanceof  Double);
408:                assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
409:
410:                value = ConvertUtils.convert("123.456", Double.class);
411:                assertTrue(value instanceof  Double);
412:                assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
413:
414:                value = ConvertUtils.convert("123.456", Float.TYPE);
415:                assertTrue(value instanceof  Float);
416:                assertEquals(((Float) value).floatValue(), (float) 123.456,
417:                        (float) 0.005);
418:
419:                value = ConvertUtils.convert("123.456", Float.class);
420:                assertTrue(value instanceof  Float);
421:                assertEquals(((Float) value).floatValue(), (float) 123.456,
422:                        (float) 0.005);
423:
424:                value = ConvertUtils.convert("123", Integer.TYPE);
425:                assertTrue(value instanceof  Integer);
426:                assertEquals(((Integer) value).intValue(), 123);
427:
428:                value = ConvertUtils.convert("123", Integer.class);
429:                assertTrue(value instanceof  Integer);
430:                assertEquals(((Integer) value).intValue(), 123);
431:
432:                value = ConvertUtils.convert("123", Long.TYPE);
433:                assertTrue(value instanceof  Long);
434:                assertEquals(((Long) value).longValue(), 123);
435:
436:                value = ConvertUtils.convert("123", Long.class);
437:                assertTrue(value instanceof  Long);
438:                assertEquals(((Long) value).longValue(), 123);
439:
440:                value = ConvertUtils.convert("123", Short.TYPE);
441:                assertTrue(value instanceof  Short);
442:                assertEquals(((Short) value).shortValue(), (short) 123);
443:
444:                value = ConvertUtils.convert("123", Short.class);
445:                assertTrue(value instanceof  Short);
446:                assertEquals(((Short) value).shortValue(), (short) 123);
447:
448:                String input = null;
449:
450:                input = "2002-03-17";
451:                value = ConvertUtils.convert(input, Date.class);
452:                assertTrue(value instanceof  Date);
453:                assertEquals(input, value.toString());
454:
455:                input = "20:30:40";
456:                value = ConvertUtils.convert(input, Time.class);
457:                assertTrue(value instanceof  Time);
458:                assertEquals(input, value.toString());
459:
460:                input = "2002-03-17 20:30:40.0";
461:                value = ConvertUtils.convert(input, Timestamp.class);
462:                assertTrue(value instanceof  Timestamp);
463:                assertEquals(input, value.toString());
464:
465:            }
466:
467:            /**
468:             * Positive String to String array tests.
469:             */
470:            public void testPositiveStringArray() {
471:
472:                Object value = null;
473:                String stringArray[] = new String[0];
474:                String stringArray1[] = new String[] { "abc" };
475:                String stringArray2[] = new String[] { "abc", "de,f" };
476:
477:                value = ConvertUtils.convert("", stringArray.getClass());
478:                checkStringArray(value, stringArray);
479:                value = ConvertUtils.convert(" ", stringArray.getClass());
480:                checkStringArray(value, stringArray);
481:                value = ConvertUtils.convert("{}", stringArray.getClass());
482:                checkStringArray(value, stringArray);
483:                value = ConvertUtils.convert("{  }", stringArray.getClass());
484:                checkStringArray(value, stringArray);
485:
486:                value = ConvertUtils.convert("abc", stringArray.getClass());
487:                checkStringArray(value, stringArray1);
488:                value = ConvertUtils.convert("{abc}", stringArray.getClass());
489:                checkStringArray(value, stringArray1);
490:                value = ConvertUtils.convert("\"abc\"", stringArray.getClass());
491:                checkStringArray(value, stringArray1);
492:                value = ConvertUtils.convert("{\"abc\"}", stringArray
493:                        .getClass());
494:                checkStringArray(value, stringArray1);
495:                value = ConvertUtils.convert("'abc'", stringArray.getClass());
496:                checkStringArray(value, stringArray1);
497:                value = ConvertUtils.convert("{'abc'}", stringArray.getClass());
498:                checkStringArray(value, stringArray1);
499:
500:                value = ConvertUtils.convert("abc 'de,f'", stringArray
501:                        .getClass());
502:                checkStringArray(value, stringArray2);
503:                value = ConvertUtils.convert("{abc, 'de,f'}", stringArray
504:                        .getClass());
505:                checkStringArray(value, stringArray2);
506:                value = ConvertUtils.convert("\"abc\",\"de,f\"", stringArray
507:                        .getClass());
508:                checkStringArray(value, stringArray2);
509:                value = ConvertUtils.convert("{\"abc\" 'de,f'}", stringArray
510:                        .getClass());
511:                checkStringArray(value, stringArray2);
512:                value = ConvertUtils.convert("'abc' 'de,f'", stringArray
513:                        .getClass());
514:                checkStringArray(value, stringArray2);
515:                value = ConvertUtils.convert("{'abc', \"de,f\"}", stringArray
516:                        .getClass());
517:                checkStringArray(value, stringArray2);
518:
519:            }
520:
521:            public void testSeparateConvertInstances() throws Exception {
522:                ConvertUtilsBean utilsOne = new ConvertUtilsBean();
523:                ConvertUtilsBean utilsTwo = new ConvertUtilsBean();
524:
525:                // make sure that the test work ok before anything's changed
526:                Object value = utilsOne.convert("true", Boolean.TYPE);
527:                assertTrue(value instanceof  Boolean);
528:                assertEquals("Standard conversion failed (1)",
529:                        ((Boolean) value).booleanValue(), true);
530:
531:                value = utilsTwo.convert("true", Boolean.TYPE);
532:                assertTrue(value instanceof  Boolean);
533:                assertEquals("Standard conversion failed (2)",
534:                        ((Boolean) value).booleanValue(), true);
535:
536:                // now register a test
537:
538:                utilsOne.register(new ThrowExceptionConverter(), Boolean.TYPE);
539:                try {
540:
541:                    utilsOne.convert("true", Boolean.TYPE);
542:                    fail("Register converter failed.");
543:
544:                } catch (PassTestException e) { /* This shows that the registration has worked */
545:                }
546:
547:                try {
548:                    // nothing should have changed
549:                    value = utilsTwo.convert("true", Boolean.TYPE);
550:                    assertTrue(value instanceof  Boolean);
551:                    assertEquals("Standard conversion failed (3)",
552:                            ((Boolean) value).booleanValue(), true);
553:
554:                } catch (PassTestException e) {
555:                    // This is a failure since utilsTwo should still have
556:                    // standard converters registered
557:                    fail("Registering a converter for an instance should not effect another instance.");
558:                }
559:
560:                // nothing we'll test deregister
561:                utilsOne.deregister();
562:                value = utilsOne.convert("true", Boolean.TYPE);
563:                assertTrue(value instanceof  Boolean);
564:                assertEquals("Instance deregister failed.", ((Boolean) value)
565:                        .booleanValue(), true);
566:
567:                value = utilsTwo.convert("true", Boolean.TYPE);
568:                assertTrue(value instanceof  Boolean);
569:                assertEquals("Standard conversion failed (4)",
570:                        ((Boolean) value).booleanValue(), true);
571:            }
572:
573:            public void testDeregisteringSingleConverter() throws Exception {
574:                // make sure that the test work ok before anything's changed
575:                Object value = ConvertUtils.convert("true", Boolean.TYPE);
576:                assertTrue(value instanceof  Boolean);
577:                assertEquals("Standard conversion failed (1)",
578:                        ((Boolean) value).booleanValue(), true);
579:
580:                // we'll test deregister
581:                ConvertUtils.deregister(Boolean.TYPE);
582:                assertNull("Converter should be null", ConvertUtils
583:                        .lookup(Boolean.TYPE));
584:
585:            }
586:
587:            public void testConvertToString() throws Exception {
588:                Converter dummyConverter = new Converter() {
589:                    public Object convert(Class type, Object value) {
590:                        return value;
591:                    }
592:                };
593:
594:                Converter fooConverter = new Converter() {
595:                    public Object convert(Class type, Object value) {
596:                        return "Foo-Converter";
597:                    }
598:                };
599:
600:                DateConverter dateConverter = new DateConverter();
601:                dateConverter.setLocale(Locale.US);
602:
603:                ConvertUtilsBean utils = new ConvertUtilsBean();
604:                utils.register(dateConverter, java.util.Date.class);
605:                utils.register(fooConverter, String.class);
606:
607:                // Convert using registerd DateConverter
608:                java.util.Date today = new java.util.Date();
609:                DateFormat fmt = new SimpleDateFormat("M/d/yy"); /* US Short Format */
610:                String expected = fmt.format(today);
611:                assertEquals("DateConverter M/d/yy", expected, utils.convert(
612:                        today, String.class));
613:
614:                // Date converter doesn't do String conversion - use String Converter
615:                utils.register(dummyConverter, java.util.Date.class);
616:                assertEquals("Date Converter doesn't do String conversion",
617:                        "Foo-Converter", utils.convert(today, String.class));
618:
619:                // No registered Date converter - use String Converter
620:                utils.deregister(java.util.Date.class);
621:                assertEquals("No registered Date converter", "Foo-Converter",
622:                        utils.convert(today, String.class));
623:
624:                // String Converter doesn't do Strings!!!
625:                utils.register(dummyConverter, String.class);
626:                assertEquals("String Converter doesn't do Strings!!!", today
627:                        .toString(), utils.convert(today, String.class));
628:
629:                // No registered Date or String converter - use Object's toString()
630:                utils.deregister(String.class);
631:                assertEquals("Object's toString()", today.toString(), utils
632:                        .convert(today, String.class));
633:
634:            }
635:
636:            // -------------------------------------------------------- Private Methods
637:
638:            private void checkIntegerArray(Object value, int intArray[]) {
639:
640:                assertNotNull("Returned value is not null", value);
641:                assertEquals("Returned value is int[]", intArray.getClass(),
642:                        value.getClass());
643:                int results[] = (int[]) value;
644:                assertEquals("Returned array length", intArray.length,
645:                        results.length);
646:                for (int i = 0; i < intArray.length; i++) {
647:                    assertEquals("Returned array value " + i, intArray[i],
648:                            results[i]);
649:                }
650:
651:            }
652:
653:            private void checkStringArray(Object value, String stringArray[]) {
654:
655:                assertNotNull("Returned value is not null", value);
656:                assertEquals("Returned value is String[]", stringArray
657:                        .getClass(), value.getClass());
658:                String results[] = (String[]) value;
659:                assertEquals("Returned array length", stringArray.length,
660:                        results.length);
661:                for (int i = 0; i < stringArray.length; i++) {
662:                    assertEquals("Returned array value " + i, stringArray[i],
663:                            results[i]);
664:                }
665:
666:            }
667:
668:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.