KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > strategy > TestObjectStringConverters


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.betwixt.strategy;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.beanutils.ConvertUtils;
24 import org.apache.commons.beanutils.Converter;
25 import org.apache.commons.betwixt.expression.Context;
26
27 /**
28  * Test harness for ObjectStringConverter implementations
29  *
30  * @author <a HREF="mailto:rdonkin at apache.org">Robert Burrell Donkin</a>
31  * @version $Id: TestObjectStringConverters.java,v 1.6 2004/02/28 13:38:36 yoavs Exp $
32  */

33 public class TestObjectStringConverters extends TestCase
34 {
35
36     protected Context dummyContext = new Context();
37
38     public static Test suite() {
39         return new TestSuite(TestObjectStringConverters.class);
40     }
41     
42     public TestObjectStringConverters(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public void testBaseConverter() {
47         Object JavaDoc test = new Object JavaDoc () {
48             public String JavaDoc toString() {
49                 return "funciporcini";
50             }
51         };
52         
53         ObjectStringConverter converter = new ObjectStringConverter();
54         String JavaDoc stringFromObject = converter.objectToString( null, Object JavaDoc.class, "raspberry", dummyContext );
55         assertEquals("Null should return empty string", "", stringFromObject);
56         stringFromObject = converter.objectToString( test, Object JavaDoc.class, "raspberry", dummyContext );
57         assertEquals("Object should return toString", "funciporcini", stringFromObject);
58         
59         Object JavaDoc objectFromString = converter.stringToObject( "Mungo Jerry", Object JavaDoc.class, "strawberry", dummyContext );
60         assertEquals("String should return itself", "Mungo Jerry", objectFromString);
61     }
62     
63     
64     public void testConvertUtilsConverter() throws Exception JavaDoc {
65         ObjectStringConverter converter = new ConvertUtilsObjectStringConverter();
66         commonTestForConvertUtilsConverters( converter );
67     }
68     
69     private void commonTestForConvertUtilsConverters(ObjectStringConverter objectStringConverter) {
70         Converter converter = new Converter() {
71             public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
72                 if ( type == SecurityManager JavaDoc.class) {
73                     return "Life, The Universe And Everything";
74                 }
75                 return "The answer is " + value.toString();
76             }
77         };
78         
79         Long JavaDoc test = new Long JavaDoc(42);
80         
81         ConvertUtils.register( converter, Object JavaDoc.class );
82         ConvertUtils.register( converter, String JavaDoc.class );
83         ConvertUtils.register( converter, SecurityManager JavaDoc.class );
84         
85         String JavaDoc stringFromObject = objectStringConverter.objectToString( null, Object JavaDoc.class, "gooseberry", dummyContext );
86         assertEquals("Null should return empty string", "", stringFromObject);
87         stringFromObject = objectStringConverter.objectToString( test, Object JavaDoc.class, "logonberry", dummyContext );
88         assertEquals("Normal object conversion (1)", "The answer is 42", stringFromObject);
89
90         
91         Object JavaDoc objectFromString = objectStringConverter.stringToObject(
92                         "Forty Two", Object JavaDoc.class, "damsen", dummyContext );
93         assertEquals("Normal object conversion (2)", "The answer is Forty Two", objectFromString);
94         objectFromString = objectStringConverter.stringToObject(
95                         "Trillian", SecurityManager JavaDoc.class, "cranberry", dummyContext );
96         assertEquals("Special object conversion", "Life, The Universe And Everything", objectFromString);
97         
98         ConvertUtils.deregister();
99     }
100     
101     public void testDefaultOSConverter() {
102         ObjectStringConverter converter = new DefaultObjectStringConverter();
103         commonTestForConvertUtilsConverters( converter );
104     }
105     
106     public void testDefaultOSConverterDates() {
107         
108     
109         Converter converter = new Converter() {
110             public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
111                 return "Arthur Dent";
112             }
113         };
114         
115         ConvertUtils.register( converter, java.sql.Date JavaDoc.class );
116         
117         converter = new Converter() {
118             public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
119                 return "Ford Prefect";
120             }
121         };
122         
123         ConvertUtils.register( converter, String JavaDoc.class );
124         
125         converter = new Converter() {
126             public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
127                 return "Marvin";
128             }
129         };
130         
131         ConvertUtils.register( converter, java.util.Date JavaDoc.class );
132     
133         java.util.Date JavaDoc utilNow = new java.util.Date JavaDoc();
134         String JavaDoc nowAsString = utilNow.toString();
135         java.sql.Date JavaDoc sqlNow = new java.sql.Date JavaDoc(System.currentTimeMillis());
136         ObjectStringConverter objectStringConverter = new DefaultObjectStringConverter();
137         
138         String JavaDoc stringFromObject = objectStringConverter.objectToString(
139                                         utilNow, java.util.Date JavaDoc.class, "blackcurrent", dummyContext );
140         assertEquals( "String output same as java.util.Date.toString() (1)", utilNow.toString(), stringFromObject );
141
142         stringFromObject = objectStringConverter.objectToString(
143                                         sqlNow, java.util.Date JavaDoc.class, "redcurrent", dummyContext );
144         assertEquals( "String output same as java.util.Date.toString() (2)", utilNow.toString(), stringFromObject );
145         
146         stringFromObject = objectStringConverter.objectToString(
147                                         utilNow, java.sql.Date JavaDoc.class, "whitecurrent", dummyContext );
148         assertEquals( "Should use converter (2)", "Ford Prefect", stringFromObject );
149         
150         Object JavaDoc objectFromString = objectStringConverter.stringToObject(
151                                         nowAsString, java.sql.Date JavaDoc.class, "blackberry", dummyContext );
152         assertEquals( "Should use converter (3)", "Ford Prefect", stringFromObject );
153         objectFromString = objectStringConverter.stringToObject(
154                                         nowAsString, java.util.Date JavaDoc.class, "tayberry", dummyContext );
155         assertTrue( "Date should be returned", objectFromString instanceof java.util.Date JavaDoc);
156         assertEquals( "Date returned should be the same", nowAsString, objectFromString.toString());
157         
158         ConvertUtils.deregister();
159     }
160 }
161
162
163
Popular Tags