001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.core.tests.databinding.conversion;
011:
012: import java.math.BigInteger;
013:
014: import junit.framework.TestCase;
015:
016: import org.eclipse.core.databinding.conversion.NumberToStringConverter;
017:
018: import com.ibm.icu.text.NumberFormat;
019:
020: /**
021: * @since 1.1
022: */
023: public class NumberToStringConverterTest extends TestCase {
024: private NumberFormat numberFormat;
025: private NumberFormat integerFormat;
026:
027: /*
028: * (non-Javadoc)
029: *
030: * @see junit.framework.TestCase#setUp()
031: */
032: protected void setUp() throws Exception {
033: super .setUp();
034:
035: numberFormat = NumberFormat.getInstance();
036: integerFormat = NumberFormat.getIntegerInstance();
037: }
038:
039: public void testFromTypes() throws Exception {
040: assertEquals("Integer.class", Integer.class,
041: NumberToStringConverter.fromInteger(false)
042: .getFromType());
043: assertEquals("Integer.TYPE", Integer.TYPE,
044: NumberToStringConverter.fromInteger(true).getFromType());
045: assertEquals("Double.class", Double.class,
046: NumberToStringConverter.fromDouble(false).getFromType());
047: assertEquals("Double.TYPE", Double.TYPE,
048: NumberToStringConverter.fromDouble(true).getFromType());
049: assertEquals("Long.class", Long.class, NumberToStringConverter
050: .fromLong(false).getFromType());
051: assertEquals("Long.TYPE", Long.TYPE, NumberToStringConverter
052: .fromLong(true).getFromType());
053: assertEquals("Float.class", Float.class,
054: NumberToStringConverter.fromFloat(false).getFromType());
055: assertEquals("Float.TYPE", Float.TYPE, NumberToStringConverter
056: .fromFloat(true).getFromType());
057: assertEquals("BigInteger.class", BigInteger.class,
058: NumberToStringConverter.fromBigInteger().getFromType());
059: }
060:
061: public void testToTypeIsStringClass() throws Exception {
062: assertEquals(String.class, NumberToStringConverter.fromInteger(
063: false).getToType());
064: }
065:
066: public void testConvertIntegerToString() throws Exception {
067: Integer input = new Integer(1000);
068: String expected = integerFormat.format(input.longValue());
069:
070: NumberToStringConverter converter = NumberToStringConverter
071: .fromInteger(false);
072: String result = (String) converter.convert(input);
073: assertEquals(expected, result);
074: }
075:
076: public void testConvertDoubleToString() throws Exception {
077: Double input = new Double(1000.1d);
078: String expected = numberFormat.format(input.doubleValue());
079:
080: NumberToStringConverter converter = NumberToStringConverter
081: .fromDouble(false);
082: String result = (String) converter.convert(input);
083: assertEquals(expected, result);
084: }
085:
086: public void testConvertFloatToString() throws Exception {
087: Float input = new Float(1000.1f);
088: String expected = numberFormat.format(input.floatValue());
089:
090: NumberToStringConverter converter = NumberToStringConverter
091: .fromFloat(false);
092: String result = (String) converter.convert(input);
093: assertEquals(expected, result);
094: }
095:
096: public void testConvertLongToString() throws Exception {
097: Long input = new Long(1000l);
098: String expected = integerFormat.format(input.longValue());
099:
100: NumberToStringConverter converter = NumberToStringConverter
101: .fromLong(false);
102: String result = (String) converter.convert(input);
103: assertEquals(expected, result);
104: }
105:
106: public void testConvertBigIntegerToString() throws Exception {
107: BigInteger input = BigInteger.valueOf(1000);
108: String expected = integerFormat.format(input);
109:
110: NumberToStringConverter converter = NumberToStringConverter
111: .fromBigInteger();
112: String result = (String) converter.convert(input);
113: assertEquals(expected, result);
114: }
115:
116: public void testNullSourceConvertsToEmptyString() throws Exception {
117: NumberToStringConverter converter = NumberToStringConverter
118: .fromInteger(false);
119: assertEquals("", converter.convert(null));
120: }
121: }
|