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.StringToNumberParser;
15: import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
16:
17: import com.ibm.icu.text.NumberFormat;
18:
19: /**
20: * @since 1.1
21: */
22: public class StringToNumberParserTest extends TestCase {
23: private NumberFormat integerFormat;
24:
25: /*
26: * (non-Javadoc)
27: *
28: * @see junit.framework.TestCase#setUp()
29: */
30: protected void setUp() throws Exception {
31: super .setUp();
32:
33: integerFormat = NumberFormat.getIntegerInstance();
34: }
35:
36: public void testParseNonStringThrowsIllegalArgumentException()
37: throws Exception {
38: try {
39: StringToNumberParser.parse(new Integer(0), integerFormat,
40: false);
41: fail("exception should have been thrown");
42: } catch (IllegalArgumentException e) {
43: }
44: }
45:
46: public void testEmptyStringReturnsNullIfNotPrimitive()
47: throws Exception {
48: ParseResult result = StringToNumberParser.parse("",
49: integerFormat, false);
50: assertNull(result.getNumber());
51: }
52:
53: public void testReturnsParsePositionWhenValueCannotBeParsed()
54: throws Exception {
55: ParseResult result = StringToNumberParser.parse("adsf",
56: integerFormat, false);
57: assertNotNull(result.getPosition());
58: assertNull(result.getNumber());
59: }
60:
61: public void testReturnsNumberWhenSuccessfullyParsed()
62: throws Exception {
63: Integer number = new Integer(5);
64: ParseResult result = StringToNumberParser.parse(integerFormat
65: .format(number.longValue()), integerFormat, false);
66: assertNull(result.getPosition());
67: assertEquals(number.intValue(), result.getNumber().intValue());
68: }
69: }
|