001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestConvert.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import com.uwyn.rife.tools.exceptions.ConversionException;
011: import junit.framework.TestCase;
012:
013: public class TestConvert extends TestCase {
014: public TestConvert(String name) {
015: super (name);
016: }
017:
018: public void testToType() throws ConversionException {
019: assertEquals(true, Convert.toType(1, boolean.class));
020: assertEquals(false, Convert.toType(0, boolean.class));
021: assertEquals(true, Convert.toType("1", boolean.class));
022: assertEquals(false, Convert.toType("0", boolean.class));
023: assertEquals(true, Convert.toType("t", boolean.class));
024:
025: assertEquals((char) 23, Convert.toType(23L, char.class));
026: assertEquals((char) 89, Convert.toType(89.7d, char.class));
027: assertEquals('I', Convert.toType('I', char.class));
028: assertEquals('p', Convert
029: .toType(new Character('p'), char.class));
030: assertEquals('A', Convert.toType("A", char.class));
031:
032: assertEquals((byte) 23, Convert.toType(23L, byte.class));
033: assertEquals((byte) 89, Convert.toType(89.7d, byte.class));
034: assertEquals((byte) 12, Convert.toType("12", byte.class));
035:
036: assertEquals((short) 23, Convert.toType(23L, short.class));
037: assertEquals((short) 89, Convert.toType(89.7d, short.class));
038: assertEquals((short) 1247, Convert.toType("1247", short.class));
039:
040: assertEquals(23, Convert.toType(23L, int.class));
041: assertEquals(89, Convert.toType(89.7d, int.class));
042: assertEquals(239, Convert.toType("239", int.class));
043:
044: assertEquals(23L, Convert.toType(23, long.class));
045: assertEquals(890L, Convert.toType(890.7d, long.class));
046: assertEquals(86980L, Convert.toType("86980", long.class));
047:
048: assertEquals(23f, Convert.toType(23, float.class));
049: assertEquals(23.9f, Convert.toType(23.9f, float.class));
050: assertEquals(890.7f, Convert.toType(890.7d, float.class));
051: assertEquals(869.8f, Convert.toType("869.8", float.class));
052:
053: assertEquals(23d, Convert.toType(23, double.class));
054: assertEquals(23.9d, (Double) Convert
055: .toType(23.9f, double.class), 0.01d);
056: assertEquals(869232.98792d, (Double) Convert.toType(
057: 869232.98792d, double.class), 0.000001d);
058: assertEquals(2862324.2487d, (Double) Convert.toType(
059: "2862324.2487", double.class), 0.00001d);
060:
061: assertNull(Convert.toType(null, Boolean.class));
062: assertNull(Convert.toType(null, Character.class));
063: assertNull(Convert.toType(null, Byte.class));
064: assertNull(Convert.toType(null, Short.class));
065: assertNull(Convert.toType(null, Integer.class));
066: assertNull(Convert.toType(null, Long.class));
067: assertNull(Convert.toType(null, Float.class));
068: assertNull(Convert.toType(null, Double.class));
069: assertNull(Convert.toType(null, String.class));
070:
071: assertEquals(true, Convert.toType(1, Boolean.class));
072: assertEquals(false, Convert.toType(0, Boolean.class));
073: assertEquals(true, Convert.toType("1", Boolean.class));
074: assertEquals(false, Convert.toType("0", Boolean.class));
075: assertEquals(true, Convert.toType("t", Boolean.class));
076:
077: assertEquals((char) 23, Convert.toType(23L, Character.class));
078: assertEquals((char) 89, Convert.toType(89.7d, Character.class));
079: assertEquals('I', Convert.toType('I', char.class));
080: assertEquals('p', Convert.toType(new Character('p'),
081: Character.class));
082: assertEquals('A', Convert.toType("A", Character.class));
083:
084: assertEquals((byte) 23, Convert.toType(23L, Byte.class));
085: assertEquals((byte) 89, Convert.toType(89.7d, Byte.class));
086: assertEquals((byte) 12, Convert.toType("12", Byte.class));
087:
088: assertEquals((short) 23, Convert.toType(23L, Short.class));
089: assertEquals((short) 89, Convert.toType(89.7d, Short.class));
090: assertEquals((short) 1247, Convert.toType("1247", Short.class));
091:
092: assertEquals(23, Convert.toType(23L, Integer.class));
093: assertEquals(89, Convert.toType(89.7d, Integer.class));
094: assertEquals(239, Convert.toType("239", Integer.class));
095:
096: assertEquals(23L, Convert.toType(23, Long.class));
097: assertEquals(890L, Convert.toType(890.7d, Long.class));
098: assertEquals(86980L, Convert.toType("86980", Long.class));
099:
100: assertEquals(23f, Convert.toType(23, Float.class));
101: assertEquals(23.9f, Convert.toType(23.9f, Float.class));
102: assertEquals(890.7f, Convert.toType(890.7d, Float.class));
103: assertEquals(869.8f, Convert.toType("869.8", Float.class));
104:
105: assertEquals(23d, Convert.toType(23, Double.class));
106: assertEquals(23.9d, (Double) Convert
107: .toType(23.9f, Double.class), 0.01d);
108: assertEquals(869232.98792d, (Double) Convert.toType(
109: 869232.98792d, Double.class), 0.000001d);
110: assertEquals(2862324.2487d, (Double) Convert.toType(
111: "2862324.2487", Double.class), 0.00001d);
112:
113: assertEquals("1234", Convert.toType(1234, String.class));
114:
115: assertEquals("IUOJKO".toString(), Convert.toType(
116: new StringBuffer("IUOJKO"), CharSequence.class)
117: .toString());
118: }
119:
120: public void testToString() {
121: assertNull(Convert.toString(null));
122: assertEquals("1234", Convert.toString(1234));
123: }
124:
125: public void testToBooleanErrors() {
126: try {
127: Convert.toBoolean(null);
128: fail("exception not thrown");
129: } catch (ConversionException e) {
130: assertNull(e.getFrom());
131: assertSame(boolean.class, e.getTo());
132: }
133:
134: Integer integer = new Integer(4);
135: try {
136: Convert.toBoolean(integer);
137: fail("exception not thrown");
138: } catch (ConversionException e) {
139: assertSame(integer, e.getFrom());
140: assertSame(boolean.class, e.getTo());
141: }
142:
143: Object object = new Object();
144: try {
145: Convert.toBoolean(object);
146: fail("exception not thrown");
147: } catch (ConversionException e) {
148: assertSame(object, e.getFrom());
149: assertSame(boolean.class, e.getTo());
150: }
151: }
152:
153: public void testToBoolean() throws ConversionException {
154: assertEquals(true, Convert.toBoolean(1));
155: assertEquals(false, Convert.toBoolean(0));
156: assertEquals(true, Convert.toBoolean("1"));
157: assertEquals(false, Convert.toBoolean("0"));
158: assertEquals(true, Convert.toBoolean("t"));
159: }
160:
161: public void testToBooleanDefaults() {
162: assertEquals(true, Convert.toBoolean(null, true));
163: assertEquals(false, Convert.toBoolean(4, false));
164: assertEquals(true, Convert.toBoolean(new Object(), true));
165: }
166:
167: public void testToCharErrors() {
168: try {
169: Convert.toChar(null);
170: fail("exception not thrown");
171: } catch (ConversionException e) {
172: assertNull(e.getFrom());
173: assertSame(char.class, e.getTo());
174: }
175:
176: Object object = new Object();
177: try {
178: Convert.toChar(object);
179: fail("exception not thrown");
180: } catch (ConversionException e) {
181: assertSame(object, e.getFrom());
182: assertSame(char.class, e.getTo());
183: }
184:
185: String string = "dfsdf";
186: try {
187: Convert.toChar(string);
188: fail("exception not thrown");
189: } catch (ConversionException e) {
190: assertSame(string, e.getFrom());
191: assertSame(char.class, e.getTo());
192: }
193: }
194:
195: public void testToChar() throws ConversionException {
196: assertEquals((char) 23, Convert.toChar(23L));
197: assertEquals((char) 89, Convert.toChar(89.7d));
198: assertEquals('I', Convert.toChar('I'));
199: assertEquals('p', Convert.toChar(new Character('p')));
200: assertEquals('A', Convert.toChar("A"));
201: }
202:
203: public void testToCharDefaults() {
204: assertEquals('c', Convert.toChar(null, 'c'));
205: assertEquals('f', Convert.toChar(new Object(), 'f'));
206: assertEquals('W', Convert.toChar("kjoiji", 'W'));
207: }
208:
209: public void testToByteErrors() {
210: try {
211: Convert.toByte(null);
212: fail("exception not thrown");
213: } catch (ConversionException e) {
214: assertNull(e.getFrom());
215: assertSame(byte.class, e.getTo());
216: }
217:
218: Object object = new Object();
219: try {
220: Convert.toByte(object);
221: fail("exception not thrown");
222: } catch (ConversionException e) {
223: assertSame(object, e.getFrom());
224: assertSame(byte.class, e.getTo());
225: }
226:
227: String string = "dfsdf";
228: try {
229: Convert.toByte(string);
230: fail("exception not thrown");
231: } catch (ConversionException e) {
232: assertSame(string, e.getFrom());
233: assertSame(byte.class, e.getTo());
234: assertTrue(e.getCause() instanceof NumberFormatException);
235: }
236: }
237:
238: public void testToByte() throws ConversionException {
239: assertEquals((byte) 23, Convert.toByte(23L));
240: assertEquals((byte) 89, Convert.toByte(89.7d));
241: assertEquals((byte) 12, Convert.toByte("12"));
242: }
243:
244: public void testToByteDefaults() {
245: assertEquals((byte) 87, Convert.toByte(null, (byte) 87));
246: assertEquals((byte) 3, Convert.toByte(new Object(), (byte) 3));
247: assertEquals((byte) 79, Convert.toByte("kjoiji", (byte) 79));
248: }
249:
250: public void testToShortErrors() {
251: try {
252: Convert.toShort(null);
253: fail("exception not thrown");
254: } catch (ConversionException e) {
255: assertNull(e.getFrom());
256: assertSame(short.class, e.getTo());
257: }
258:
259: Object object = new Object();
260: try {
261: Convert.toShort(object);
262: fail("exception not thrown");
263: } catch (ConversionException e) {
264: assertSame(object, e.getFrom());
265: assertSame(short.class, e.getTo());
266: }
267:
268: String string = "dfsdf";
269: try {
270: Convert.toShort(string);
271: fail("exception not thrown");
272: } catch (ConversionException e) {
273: assertSame(string, e.getFrom());
274: assertSame(short.class, e.getTo());
275: assertTrue(e.getCause() instanceof NumberFormatException);
276: }
277: }
278:
279: public void testToShort() throws ConversionException {
280: assertEquals((short) 23, Convert.toShort(23L));
281: assertEquals((short) 89, Convert.toShort(89.7d));
282: assertEquals((short) 1247, Convert.toShort("1247"));
283: }
284:
285: public void testToShortDefaults() {
286: assertEquals((short) 87, Convert.toShort(null, (short) 87));
287: assertEquals((short) 3, Convert
288: .toShort(new Object(), (short) 3));
289: assertEquals((short) 79, Convert.toShort("kjoiji", (short) 79));
290: }
291:
292: public void testToIntErrors() {
293: try {
294: Convert.toInt(null);
295: fail("exception not thrown");
296: } catch (ConversionException e) {
297: assertNull(e.getFrom());
298: assertSame(int.class, e.getTo());
299: }
300:
301: Object object = new Object();
302: try {
303: Convert.toInt(object);
304: fail("exception not thrown");
305: } catch (ConversionException e) {
306: assertSame(object, e.getFrom());
307: assertSame(int.class, e.getTo());
308: }
309:
310: String string = "dfsdf";
311: try {
312: Convert.toInt(string);
313: fail("exception not thrown");
314: } catch (ConversionException e) {
315: assertSame(string, e.getFrom());
316: assertSame(int.class, e.getTo());
317: assertTrue(e.getCause() instanceof NumberFormatException);
318: }
319: }
320:
321: public void testToInt() throws ConversionException {
322: assertEquals(23, Convert.toInt(23L));
323: assertEquals(89, Convert.toInt(89.7d));
324: assertEquals(239, Convert.toInt("239"));
325: }
326:
327: public void testToIntDefaults() {
328: assertEquals(87, Convert.toInt(null, 87));
329: assertEquals(3, Convert.toInt(new Object(), 3));
330: assertEquals(79, Convert.toInt("kjoiji", 79));
331: }
332:
333: public void testToLongErrors() {
334: try {
335: Convert.toLong(null);
336: fail("exception not thrown");
337: } catch (ConversionException e) {
338: assertNull(e.getFrom());
339: assertSame(long.class, e.getTo());
340: }
341:
342: Object object = new Object();
343: try {
344: Convert.toLong(object);
345: fail("exception not thrown");
346: } catch (ConversionException e) {
347: assertSame(object, e.getFrom());
348: assertSame(long.class, e.getTo());
349: }
350:
351: String string = "dfsdf";
352: try {
353: Convert.toLong(string);
354: fail("exception not thrown");
355: } catch (ConversionException e) {
356: assertSame(string, e.getFrom());
357: assertSame(long.class, e.getTo());
358: assertTrue(e.getCause() instanceof NumberFormatException);
359: }
360: }
361:
362: public void testToLong() throws ConversionException {
363: assertEquals(23L, Convert.toLong(23));
364: assertEquals(890L, Convert.toLong(890.7d));
365: assertEquals(86980L, Convert.toLong("86980"));
366: }
367:
368: public void testToLongDefaults() {
369: assertEquals(878069L, Convert.toLong(null, 878069L));
370: assertEquals(3L, Convert.toLong(new Object(), 3L));
371: assertEquals(24879L, Convert.toLong("dfjhoij", 24879L));
372: }
373:
374: public void testToFloatErrors() {
375: try {
376: Convert.toFloat(null);
377: fail("exception not thrown");
378: } catch (ConversionException e) {
379: assertNull(e.getFrom());
380: assertSame(float.class, e.getTo());
381: }
382:
383: Object object = new Object();
384: try {
385: Convert.toFloat(object);
386: fail("exception not thrown");
387: } catch (ConversionException e) {
388: assertSame(object, e.getFrom());
389: assertSame(float.class, e.getTo());
390: }
391:
392: String string = "dfsdf";
393: try {
394: Convert.toFloat(string);
395: fail("exception not thrown");
396: } catch (ConversionException e) {
397: assertSame(string, e.getFrom());
398: assertSame(float.class, e.getTo());
399: assertTrue(e.getCause() instanceof NumberFormatException);
400: }
401: }
402:
403: public void testToFloat() throws ConversionException {
404: assertEquals(23f, Convert.toFloat(23));
405: assertEquals(23.9f, Convert.toFloat(23.9f));
406: assertEquals(890.7f, Convert.toFloat(890.7d));
407: assertEquals(869.8f, Convert.toFloat("869.8"));
408: }
409:
410: public void testToFloatDefaults() {
411: assertEquals(89.76f, Convert.toFloat(null, 89.76f));
412: assertEquals(3.98f, Convert.toFloat(new Object(), 3.98f));
413: assertEquals(28.43f, Convert.toFloat("dfjhoij", 28.43f));
414: }
415:
416: public void testToDoubleErrors() {
417: try {
418: Convert.toDouble(null);
419: fail("exception not thrown");
420: } catch (ConversionException e) {
421: assertNull(e.getFrom());
422: assertSame(double.class, e.getTo());
423: }
424:
425: Object object = new Object();
426: try {
427: Convert.toDouble(object);
428: fail("exception not thrown");
429: } catch (ConversionException e) {
430: assertSame(object, e.getFrom());
431: assertSame(double.class, e.getTo());
432: }
433:
434: String string = "dfsdf";
435: try {
436: Convert.toDouble(string);
437: fail("exception not thrown");
438: } catch (ConversionException e) {
439: assertSame(string, e.getFrom());
440: assertSame(double.class, e.getTo());
441: assertTrue(e.getCause() instanceof NumberFormatException);
442: }
443: }
444:
445: public void testToDouble() throws ConversionException {
446: assertEquals(23d, Convert.toDouble(23));
447: assertEquals(23.9d, Convert.toDouble(23.9f), 0.01d);
448: assertEquals(869232.98792d, Convert.toDouble(869232.98792d),
449: 0.000001d);
450: assertEquals(2862324.2487d, Convert.toDouble("2862324.2487"),
451: 0.00001d);
452: }
453:
454: public void testToDoubleDefaults() {
455: assertEquals(89.76d, Convert.toDouble(null, 89.76d), 0.01d);
456: assertEquals(869232.98792d, Convert.toDouble(new Object(),
457: 869232.98792d), 0.000001d);
458: assertEquals(2248682.24242d, Convert.toDouble("dfjhoij",
459: 2248682.24242d), 0.000001d);
460: }
461: }
|