01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.conversion;
11:
12: import junit.framework.TestCase;
13:
14: import org.eclipse.core.internal.databinding.conversion.IntegerToStringConverter;
15:
16: import com.ibm.icu.text.NumberFormat;
17:
18: /**
19: * @since 1.1
20: */
21: public class IntegerToStringConverterTest extends TestCase {
22: private NumberFormat integerFormat;
23:
24: /*
25: * (non-Javadoc)
26: *
27: * @see junit.framework.TestCase#setUp()
28: */
29: protected void setUp() throws Exception {
30: super .setUp();
31:
32: integerFormat = NumberFormat.getIntegerInstance();
33: }
34:
35: public void testFromTypeShort() throws Exception {
36: assertEquals(Short.class, IntegerToStringConverter.fromShort(
37: false).getFromType());
38: assertEquals(Short.TYPE, IntegerToStringConverter.fromShort(
39: true).getFromType());
40: assertEquals(Byte.class, IntegerToStringConverter.fromByte(
41: false).getFromType());
42: assertEquals(Byte.TYPE, IntegerToStringConverter.fromByte(true)
43: .getFromType());
44: }
45:
46: public void testToTypeIsStringClass() throws Exception {
47: assertEquals(String.class, IntegerToStringConverter.fromShort(
48: false).getToType());
49: }
50:
51: public void testConvertShortToString() throws Exception {
52: Short value = new Short((short) 1);
53: String expected = integerFormat.format(value);
54:
55: IntegerToStringConverter converter = IntegerToStringConverter
56: .fromShort(integerFormat, false);
57: String result = (String) converter.convert(value);
58: assertEquals(expected, result);
59: }
60:
61: public void testConvertByteToString() throws Exception {
62: Byte value = new Byte((byte) 1);
63: String expected = integerFormat.format(value);
64:
65: IntegerToStringConverter converter = IntegerToStringConverter
66: .fromByte(integerFormat, false);
67: String result = (String) converter.convert(value);
68: assertEquals(expected, result);
69: }
70:
71: public void testNullSourceConvertsToEmptyString() throws Exception {
72: IntegerToStringConverter converter = IntegerToStringConverter
73: .fromByte(false);
74: assertEquals("", converter.convert(null));
75: }
76:
77: public void testIllegalArgumentExceptionIfSourceIsNotExpectedType()
78: throws Exception {
79: IntegerToStringConverter converter = IntegerToStringConverter
80: .fromByte(false);
81: try {
82: converter.convert(new Integer(1));
83: fail("exception should have been thrown");
84: } catch (IllegalArgumentException e) {
85: }
86: }
87: }
|