KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > reflection > StringToObjectConverterTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

10 package org.nanocontainer.reflection;
11
12 import junit.framework.TestCase;
13
14 import java.io.File JavaDoc;
15
16 public class StringToObjectConverterTestCase extends TestCase {
17     private StringToObjectConverter converter = new StringToObjectConverter();
18
19     public void testConvertsToString() {
20         assertEquals("hello", converter.convertTo(String JavaDoc.class, "hello"));
21         assertEquals("", converter.convertTo(String JavaDoc.class, ""));
22     }
23
24     public void testConvertsToInts() {
25         assertEquals(new Integer JavaDoc(22), converter.convertTo(Integer JavaDoc.class, "22"));
26         assertEquals(new Integer JavaDoc(-9), converter.convertTo(Integer JavaDoc.class, "-9"));
27     }
28
29     public void testConvertsToLong() {
30         assertEquals(new Long JavaDoc(123456789012L), converter.convertTo(Long JavaDoc.class, "123456789012"));
31         assertEquals(new Long JavaDoc(-123456789012L), converter.convertTo(Long JavaDoc.class, "-123456789012"));
32         assertEquals(new Long JavaDoc(0), converter.convertTo(Long JavaDoc.class, "0"));
33     }
34
35     public void testConvertsToBooleanUsingBestGuess() {
36         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "t"));
37         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "true"));
38         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "T"));
39         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "TRUE"));
40         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "1"));
41         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "yes"));
42         assertEquals(Boolean.TRUE, converter.convertTo(Boolean JavaDoc.class, "Yo!"));
43
44         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "f"));
45         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "false"));
46         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "FALSE"));
47         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "0"));
48         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "no"));
49         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "nada!"));
50         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, ""));
51         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, "I'm a lumberjack and I'm okay"));
52     }
53
54     public void testCustomConversionsCanBeRegistered() {
55         converter.register(File JavaDoc.class, new Converter() {
56             public Object JavaDoc convert(String JavaDoc in) {
57                 return new File JavaDoc(in);
58             }
59         });
60         assertEquals("hello", converter.convertTo(String JavaDoc.class, "hello"));
61         assertEquals(new File JavaDoc("hello"), converter.convertTo(File JavaDoc.class, "hello"));
62     }
63
64     public void testNullsMapToDefaultValues() {
65         assertNull(converter.convertTo(String JavaDoc.class, null));
66         assertEquals(new Integer JavaDoc(0), converter.convertTo(Integer JavaDoc.class, null));
67         assertEquals(new Long JavaDoc(0), converter.convertTo(Long JavaDoc.class, null));
68         assertEquals(Boolean.FALSE, converter.convertTo(Boolean JavaDoc.class, null));
69     }
70
71     public void testExceptionThrownIfConverterNotRegistered() {
72         try {
73             converter.convertTo(File JavaDoc.class, "hello");
74             fail("Should have thrown exception");
75         } catch (InvalidConversionException e) {
76             // good
77
}
78     }
79
80     public void testDodgyFormatThrowExceptions() {
81         try {
82             converter.convertTo(Integer JavaDoc.class, "fooo");
83             fail("Should have thrown exception");
84         } catch (NumberFormatException JavaDoc e) {
85             // good
86
}
87     }
88
89 }
90
Popular Tags