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: package org.apache.wicket.util.convert.converters;
018:
019: import java.util.Calendar;
020: import java.util.Date;
021: import java.util.Locale;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.wicket.IConverterLocator;
026: import org.apache.wicket.util.convert.ConversionException;
027: import org.apache.wicket.util.convert.ConverterLocator;
028:
029: /**
030: * Tests for the base converters.
031: *
032: * @author Eelco Hillenius
033: */
034: public final class ConvertersTest extends TestCase {
035: /** Dutch locale for localized testing. */
036: private static final Locale DUTCH_LOCALE = new Locale("nl", "NL");
037:
038: /**
039: * Construct.
040: */
041: public ConvertersTest() {
042: super ();
043: }
044:
045: /**
046: * Construct.
047: *
048: * @param name
049: */
050: public ConvertersTest(String name) {
051: super (name);
052: }
053:
054: /**
055: * Test generalized conversion
056: */
057: public void testConversion() {
058: final IConverterLocator converter = new ConverterLocator();
059: assertEquals("7", converter.getConverter(Integer.class)
060: .convertToString(new Integer(7), Locale.US));
061: assertEquals("7.1", converter.getConverter(Double.class)
062: .convertToString(new Double(7.1), Locale.US));
063: assertEquals("7,1", converter.getConverter(Double.class)
064: .convertToString(new Double(7.1), DUTCH_LOCALE));
065:
066: Calendar cal = Calendar.getInstance(DUTCH_LOCALE);
067: cal.clear();
068: cal.set(2002, Calendar.OCTOBER, 24);
069: Date date = cal.getTime();
070:
071: assertEquals(date, converter.getConverter(Date.class)
072: .convertToObject("24-10-02", DUTCH_LOCALE));
073: assertEquals("24-10-02", converter.getConverter(Date.class)
074: .convertToString(date, DUTCH_LOCALE));
075:
076: // empty strings should return null, NOT throw NPEs
077: assertNull(converter.getConverter(Integer.class)
078: .convertToObject("", Locale.US));
079: assertNull(converter.getConverter(Byte.class).convertToObject(
080: "", Locale.US));
081: assertNull(converter.getConverter(Character.class)
082: .convertToObject("", Locale.US));
083: assertNull(converter.getConverter(Float.class).convertToObject(
084: "", Locale.US));
085: assertNull(converter.getConverter(Long.class).convertToObject(
086: "", Locale.US));
087: assertNull(converter.getConverter(Short.class).convertToObject(
088: "", Locale.US));
089: assertNull(converter.getConverter(Date.class).convertToObject(
090: "", Locale.US));
091: assertNull(converter.getConverter(Double.class)
092: .convertToObject("", Locale.US));
093: assertEquals(Boolean.FALSE, converter.getConverter(
094: Boolean.class).convertToObject("", Locale.US));
095: assertNotNull(converter.getConverter(String.class)
096: .convertToObject("", Locale.US));
097: }
098:
099: /**
100: * Test boolean conversions.
101: */
102: public void testBooleanConversions() {
103: BooleanConverter booleanConverter = new BooleanConverter();
104: assertEquals("true", booleanConverter.convertToString(
105: Boolean.TRUE, Locale.getDefault()));
106: assertEquals("false", booleanConverter.convertToString(
107: Boolean.FALSE, Locale.getDefault()));
108: assertEquals(Boolean.TRUE, booleanConverter.convertToObject(
109: "true", Locale.getDefault()));
110: assertEquals(Boolean.FALSE, booleanConverter.convertToObject(
111: "false", Locale.getDefault()));
112: try {
113: booleanConverter.convertToObject("whatever", Locale
114: .getDefault());
115: fail("Conversion should have thrown an exception");
116: } catch (ConversionException e) {
117: // this is correct
118: }
119: }
120:
121: /**
122: * Test byte conversions.
123: */
124: public void testByteConversions() {
125: ByteConverter converter = new ByteConverter();
126: assertEquals(new Byte((byte) 10), converter.convertToObject(
127: "10", Locale.US));
128: assertEquals("10", converter.convertToString(
129: new Byte((byte) 10), Locale.US));
130: try {
131: converter.convertToObject("whatever", Locale.US);
132: fail("Conversion should have thrown an exception");
133: } catch (ConversionException e) {
134: // This is correct
135: }
136: try {
137: converter.convertToObject("10whatever", Locale.US);
138: fail("Conversion should have thrown an exception");
139: } catch (ConversionException e) {
140: // This is correct
141: }
142: try {
143: converter.convertToObject("256", Locale.US);
144: fail("Conversion should have thrown an exception");
145: } catch (ConversionException e) {
146: // This is correct
147: }
148: }
149:
150: /**
151: * Test double conversions.
152: */
153: public void testDoubleConversions() {
154: DoubleConverter converter = new DoubleConverter();
155: assertEquals(new Double(1.1), converter.convertToObject("1.1",
156: Locale.US));
157: assertEquals("1.1", converter.convertToString(new Double(1.1),
158: Locale.US));
159: try {
160: converter.convertToObject("whatever", Locale.US);
161: fail("Conversion should have thrown an exception");
162: } catch (ConversionException e) {
163: // this is correct
164: }
165: try {
166: converter.convertToObject("1.1whatever", Locale.US);
167: fail("Conversion should have thrown an exception");
168: } catch (ConversionException e) {
169: // this is correct
170: }
171: }
172:
173: /**
174: * Test float conversions.
175: */
176: public void testFloatConversions() {
177: FloatConverter converter = new FloatConverter();
178: assertEquals(new Float(1.1), converter.convertToObject("1.1",
179: Locale.US));
180: assertEquals("1.1", converter.convertToString(new Float(1.1),
181: Locale.US));
182: try {
183: converter.convertToObject("whatever", Locale.US);
184: fail("Conversion should have thrown an exception");
185: } catch (ConversionException e) {
186: // this is correct
187: }
188: try {
189: converter.convertToObject("1.1whatever", Locale.US);
190: fail("Conversion should have thrown an exception");
191: } catch (ConversionException e) {
192: // this is correct
193: }
194: }
195:
196: /**
197: * Test integer conversions.
198: */
199: public void testIntegerConversions() {
200: IntegerConverter converter = new IntegerConverter();
201: assertEquals(new Integer(10), converter.convertToObject("10",
202: Locale.US));
203: assertEquals("10", converter.convertToString(new Integer(10),
204: Locale.US));
205: try {
206: converter.convertToObject("whatever", Locale.US);
207: fail("Conversion should have thrown an exception");
208: } catch (ConversionException e) {
209: // This is correct
210: }
211: try {
212: converter.convertToObject("10whatever", Locale.US);
213: fail("Conversion should have thrown an exception");
214: } catch (ConversionException e) {
215: // This is correct
216: }
217: try {
218: converter.convertToObject(""
219: + ((long) Integer.MAX_VALUE + 1), Locale.US);
220: fail("Conversion should have thrown an exception");
221: } catch (ConversionException e) {
222: // This is correct
223: }
224: }
225:
226: /**
227: * Test long conversions.
228: */
229: public void testLongConversions() {
230: LongConverter converter = new LongConverter();
231: assertEquals(new Long(10), converter.convertToObject("10",
232: Locale.US));
233: assertEquals("10", converter.convertToString(new Long(10),
234: Locale.US));
235: try {
236: converter.convertToObject("whatever", Locale.US);
237: fail("Conversion should have thrown an exception");
238: } catch (ConversionException e) {
239: // This is correct
240: }
241: try {
242: converter.convertToObject("10whatever", Locale.US);
243: fail("Conversion should have thrown an exception");
244: } catch (ConversionException e) {
245: // This is correct
246: }
247: try {
248: converter.convertToObject("" + Long.MAX_VALUE + "0",
249: Locale.US);
250: fail("Conversion should have thrown an exception");
251: } catch (ConversionException e) {
252: // This is correct
253: }
254: }
255:
256: /**
257: * Test short conversions
258: */
259: public void testShortConversions() {
260: ShortConverter converter = new ShortConverter();
261: assertEquals(new Short((short) 10), converter.convertToObject(
262: "10", Locale.US));
263: assertEquals("10", converter.convertToString(new Short(
264: (short) 10), Locale.US));
265: try {
266: converter.convertToObject("whatever", Locale.US);
267: fail("Conversion should have thrown an exception");
268: } catch (ConversionException e) {
269: // This is correct
270: }
271: try {
272: converter.convertToObject("10whatever", Locale.US);
273: fail("Conversion should have thrown an exception");
274: } catch (ConversionException e) {
275: // This is correct
276: }
277: try {
278: converter.convertToObject("" + (Short.MAX_VALUE + 1),
279: Locale.US);
280: fail("Conversion should have thrown an exception");
281: } catch (ConversionException e) {
282: // This is correct
283: }
284: }
285:
286: /**
287: * Test date locale conversions.
288: */
289: public void testDateConverter() {
290: DateConverter converter = new DateConverter();
291:
292: Calendar cal = Calendar.getInstance(DUTCH_LOCALE);
293: cal.clear();
294: cal.set(2002, Calendar.OCTOBER, 24);
295: Date date = cal.getTime();
296:
297: assertEquals("24-10-02", converter.convertToString(date,
298: DUTCH_LOCALE));
299: assertEquals(date, converter.convertToObject("24-10-02",
300: DUTCH_LOCALE));
301:
302: assertEquals("10/24/02", converter.convertToString(date,
303: Locale.US));
304: assertEquals(date, converter.convertToObject("10/24/02",
305: Locale.US));
306:
307: try {
308: converter.convertToObject("whatever", Locale.US);
309: fail("Conversion should have thrown an exception");
310: } catch (ConversionException e) {
311: // this is correct
312: }
313: try {
314: converter.convertToObject("10/24/02whatever", Locale.US);
315: fail("Conversion should have thrown an exception");
316: } catch (ConversionException e) {
317: // this is correct
318: }
319: }
320: }
|