KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > coerce > TestValueConverter


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

15 package org.apache.tapestry.coerce;
16
17 import java.util.Collections JavaDoc;
18 import java.util.Date JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.easymock.MockControl;
23
24 /**
25  * Tests for {@link org.apache.tapestry.coerce.ValueConverterImpl}.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class TestValueConverter extends HiveMindTestCase
31 {
32
33     public void testAlreadyCorrectType()
34     {
35         ValueConverter v = new ValueConverterImpl();
36
37         Object JavaDoc input = new Integer JavaDoc(7);
38
39         assertSame(input, v.coerceValue(input, Integer JavaDoc.class));
40         assertSame(input, v.coerceValue(input, Number JavaDoc.class));
41     }
42
43     /**
44      * Check that primitive types are converted to equivalent wrapper classes for the assignable
45      * check. Ok, maybe we should check all the primitive types.
46      */

47
48     public void testAlreadyCorrectPrimitiveType()
49     {
50         ValueConverter v = new ValueConverterImpl();
51
52         Object JavaDoc input = new Integer JavaDoc(9);
53
54         assertSame(input, v.coerceValue(input, int.class));
55     }
56
57     public void testNoConverter()
58     {
59         ValueConverter v = new ValueConverterImpl();
60
61         try
62         {
63             v.coerceValue("FRED", Date JavaDoc.class);
64             unreachable();
65         }
66         catch (ApplicationRuntimeException ex)
67         {
68             assertEquals(CoerceMessages.noConverter(Date JavaDoc.class), ex.getMessage());
69         }
70     }
71
72     /**
73      * Verify that the message generated for a primtive type identifies the wrapper class.
74      */

75
76     public void testNoConverterPrimitive()
77     {
78         ValueConverter v = new ValueConverterImpl();
79
80         try
81         {
82             v.coerceValue(new Object JavaDoc(), int.class);
83             unreachable();
84         }
85         catch (ApplicationRuntimeException ex)
86         {
87             assertEquals(CoerceMessages.noConverter(Integer JavaDoc.class), ex.getMessage());
88         }
89     }
90
91     public void testConverterFound()
92     {
93         MockControl tcc = newControl(TypeConverter.class);
94         TypeConverter tc = (TypeConverter) tcc.getMock();
95
96         Object JavaDoc input = new Integer JavaDoc(7);
97         Object JavaDoc output = "SEVEN";
98
99         tc.convertValue(input);
100         tcc.setReturnValue(output);
101
102         replayControls();
103
104         ValueConverterImpl v = new ValueConverterImpl();
105
106         TypeConverterContribution contrib = new TypeConverterContribution();
107
108         contrib.setSubjectClass(String JavaDoc.class);
109         contrib.setConverter(tc);
110
111         v.setContributions(Collections.singletonList(contrib));
112
113         v.initializeService();
114
115         assertSame(output, v.coerceValue(input, String JavaDoc.class));
116
117         verifyControls();
118     }
119
120     public void testConverterFoundForNullValue()
121     {
122         MockControl tcc = newControl(TypeConverter.class);
123         TypeConverter tc = (TypeConverter) tcc.getMock();
124
125         Object JavaDoc output = "NULL";
126
127         tc.convertValue(null);
128         tcc.setReturnValue(output);
129
130         replayControls();
131
132         ValueConverterImpl v = new ValueConverterImpl();
133
134         TypeConverterContribution contrib = new TypeConverterContribution();
135
136         contrib.setSubjectClass(String JavaDoc.class);
137         contrib.setConverter(tc);
138
139         v.setContributions(Collections.singletonList(contrib));
140
141         v.initializeService();
142
143         assertSame(output, v.coerceValue(null, String JavaDoc.class));
144
145         verifyControls();
146     }
147
148     public void testNoConverterFoundForNullValue()
149     {
150         ValueConverterImpl v = new ValueConverterImpl();
151
152         assertNull(v.coerceValue(null, Date JavaDoc.class));
153     }
154
155     public void testStringToNumberPrimitive()
156     {
157         ValueConverterImpl v = new ValueConverterImpl();
158
159         Object JavaDoc result = v.coerceValue("123", int.class);
160
161         assertEquals(new Integer JavaDoc(123), result);
162     }
163
164     public void testStringToNumberWrapper()
165     {
166         ValueConverterImpl v = new ValueConverterImpl();
167
168         Object JavaDoc result = v.coerceValue("47347437", Long JavaDoc.class);
169
170         assertEquals(new Long JavaDoc(47347437), result);
171     }
172
173     public void testInvalidStringToNumber()
174     {
175         ValueConverterImpl v = new ValueConverterImpl();
176
177         try
178         {
179             v.coerceValue("fred12345", double.class);
180             unreachable();
181         }
182         catch (ApplicationRuntimeException ex)
183         {
184             assertExceptionSubstring(
185                     ex,
186                     "Unable to convert 'fred12345' to an instance of java.lang.Double");
187         }
188
189     }
190
191     public void testStringToNonNumericPrimitive()
192     {
193         MockControl tcc = newControl(TypeConverter.class);
194         TypeConverter tc = (TypeConverter) tcc.getMock();
195
196         Object JavaDoc input = "false";
197         Object JavaDoc output = Boolean.FALSE;
198
199         tc.convertValue(input);
200         tcc.setReturnValue(output);
201
202         replayControls();
203
204         ValueConverterImpl v = new ValueConverterImpl();
205
206         TypeConverterContribution contrib = new TypeConverterContribution();
207
208         contrib.setSubjectClass(Boolean JavaDoc.class);
209         contrib.setConverter(tc);
210
211         v.setContributions(Collections.singletonList(contrib));
212
213         v.initializeService();
214
215         assertSame(output, v.coerceValue(input, Boolean JavaDoc.class));
216
217         verifyControls();
218     }
219
220     public void testNumberToNumber()
221     {
222         ValueConverterImpl v = new ValueConverterImpl();
223
224         Object JavaDoc result = v.coerceValue(new Integer JavaDoc(123), long.class);
225
226         assertEquals(new Long JavaDoc(123), result);
227     }
228 }
Popular Tags