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.StringToShortConverter;
15:
16: import com.ibm.icu.text.NumberFormat;
17:
18: /**
19: * @since 1.1
20: */
21: public class StringToShortConverterTest extends TestCase {
22: private NumberFormat numberFormat;
23: private StringToShortConverter converter;
24:
25: /*
26: * (non-Javadoc)
27: *
28: * @see junit.framework.TestCase#setUp()
29: */
30: protected void setUp() throws Exception {
31: super .setUp();
32: numberFormat = NumberFormat.getIntegerInstance();
33: converter = StringToShortConverter.toShort(numberFormat, false);
34: }
35:
36: public void testConvertsToShort() throws Exception {
37: Short value = new Short((short) 1);
38: Short result = (Short) converter.convert(numberFormat
39: .format(value));
40:
41: assertEquals(value, result);
42: }
43:
44: public void testConvertsToShortPrimitive() throws Exception {
45: converter = StringToShortConverter.toShort(numberFormat, true);
46: Short value = new Short((short) 1);
47: Short result = (Short) converter.convert(numberFormat
48: .format(value));
49: assertEquals(value, result);
50: }
51:
52: public void testFromTypeIsString() throws Exception {
53: assertEquals(String.class, converter.getFromType());
54: }
55:
56: public void testToTypeIsShort() throws Exception {
57: assertEquals(Short.class, converter.getToType());
58: }
59:
60: public void testToTypeIsShortPrimitive() throws Exception {
61: converter = StringToShortConverter.toShort(true);
62: assertEquals(Short.TYPE, converter.getToType());
63: }
64:
65: public void testReturnsNullBoxedTypeForEmptyString()
66: throws Exception {
67: assertNull(converter.convert(""));
68: }
69:
70: public void testThrowsIllegalArgumentExceptionIfAskedToConvertNonString()
71: throws Exception {
72: try {
73: converter.convert(new Integer(1));
74: fail("exception should have been thrown");
75: } catch (IllegalArgumentException e) {
76: }
77: }
78: }
|