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.internal.databinding.conversion;
011:
012: import java.math.BigDecimal;
013: import java.math.BigInteger;
014:
015: import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * @since 1.1
021: */
022: public abstract class StringToNumberParserTestHarness extends TestCase {
023:
024: protected abstract Number getValidMax();
025:
026: protected abstract Number getValidMin();
027:
028: protected abstract boolean assertValid(Number number);
029:
030: public void testRanges() throws Exception {
031: Number min = getValidMin();
032: Number max = getValidMax();
033:
034: double minDouble = min.doubleValue();
035: double maxDouble = max.doubleValue();
036:
037: //test bytes
038: assertTrue("valid byte", assertValid(new Byte((byte) 1)));
039: assertTrue("valid byte min", assertValid(new Byte(
040: Byte.MIN_VALUE)));
041: assertTrue("valid byte max", assertValid(new Byte(
042: Byte.MAX_VALUE)));
043:
044: // test shorts
045: assertTrue("valid short", assertValid(new Short((short) 1)));
046: boolean result = assertValid(new Short(Short.MIN_VALUE));
047: if (minDouble > Short.MIN_VALUE) {
048: assertFalse("invalid short min", result);
049: } else {
050: assertTrue("valid short min", result);
051: }
052:
053: result = assertValid(new Short(Short.MAX_VALUE));
054: if (maxDouble < Short.MAX_VALUE) {
055: assertFalse("invalid short max", result);
056: } else {
057: assertTrue("valid short max", result);
058: }
059:
060: // test integers
061: assertTrue("valid Integer", assertValid(new Integer(1)));
062:
063: result = assertValid(new Integer(Integer.MIN_VALUE));
064: if (minDouble > Integer.MIN_VALUE) {
065: assertFalse("invalid Integer min", result);
066: } else {
067: assertTrue("valid integer min", result);
068: }
069:
070: result = assertValid(new Integer(Integer.MAX_VALUE));
071: if (maxDouble < Integer.MAX_VALUE) {
072: assertFalse("valid Integer max", result);
073: } else {
074: assertTrue("valid integer max", result);
075: }
076:
077: // test longs
078: assertTrue("valid long", assertValid(new Long(1)));
079: result = assertValid(new Long(Long.MIN_VALUE));
080: if (minDouble > Long.MIN_VALUE) {
081: assertFalse("invalid long min", result);
082: } else {
083: assertTrue("valid long min", result);
084: }
085:
086: result = assertValid(new Long(Long.MAX_VALUE));
087: if (maxDouble < Long.MAX_VALUE) {
088: assertFalse("invalid long max", result);
089: } else {
090: assertTrue("valid long max", result);
091: }
092:
093: // test floats
094: assertTrue("valid float", assertValid(new Float(1)));
095: result = assertValid(new Float(-Float.MAX_VALUE));
096: if (minDouble > -Float.MAX_VALUE) {
097: assertFalse("invalid float min", result);
098: } else {
099: assertTrue("valid float min", result);
100: }
101:
102: result = assertValid(new Float(Float.MAX_VALUE));
103: if (maxDouble < Float.MAX_VALUE) {
104: assertFalse("invalid float max", result);
105: } else {
106: assertTrue("valid float max", result);
107: }
108:
109: assertFalse("invalid negative float infinity",
110: assertValid(new Float(Float.NEGATIVE_INFINITY)));
111: assertFalse("invalid positive float infinity",
112: assertValid(new Float(Float.POSITIVE_INFINITY)));
113: assertFalse("invalid float NaN", assertValid(new Float(
114: Float.NaN)));
115:
116: // test doubles
117: assertTrue("valid double", assertValid(new Double(1)));
118: result = assertValid(new Double(-Double.MAX_VALUE));
119: if (minDouble > -Double.MAX_VALUE) {
120: assertFalse("invalid double min", result);
121: } else {
122: assertTrue("valid double min", result);
123: }
124:
125: result = assertValid(new Double(Double.MAX_VALUE));
126: if (maxDouble < Double.MAX_VALUE) {
127: assertFalse("invalid float max", result);
128: } else {
129: assertTrue("valid float max", result);
130: }
131:
132: assertFalse("invalid negative double infinity",
133: assertValid(new Double(Double.NEGATIVE_INFINITY)));
134: assertFalse("invalid positive double infinity",
135: assertValid(new Double(Double.POSITIVE_INFINITY)));
136: assertFalse("invalid double NaN", assertValid(new Double(
137: Double.NaN)));
138:
139: // test BigIntegers
140: assertTrue("valid BigInteger", assertValid(BigInteger
141: .valueOf(1)));
142: BigDecimal bigDecimalMin = new BigDecimal(min.doubleValue());
143: bigDecimalMin = bigDecimalMin.subtract(new BigDecimal(1));
144: assertFalse("invalid BigInteger min", assertValid(bigDecimalMin
145: .toBigInteger()));
146:
147: BigDecimal bigDecimalMax = new BigDecimal(max.doubleValue());
148: bigDecimalMax = bigDecimalMax.add(new BigDecimal(1));
149: assertFalse("invalid BigInteger max", assertValid(bigDecimalMax
150: .toBigInteger()));
151:
152: // test BigDecimals
153: assertTrue("valid BigDecimal", assertValid(new BigDecimal(1)));
154: assertFalse("invalid BigDecimal min",
155: assertValid(bigDecimalMin));
156: assertFalse("invalid BigDecimal max",
157: assertValid(bigDecimalMax));
158:
159: /**
160: * The ICU4J plugin's NumberFormat will return it's own BigDecimal
161: * implementation, com.ibm.icu.math.BigDecimal. The issue this causes is
162: * that we can't reference this class as it's not part of the
163: * replacement plugin. So in order to ensure that we handle Number's
164: * that are not part of the JDK stub a number implemenation and ensure
165: * that the double representation of this number is used.
166: *
167: * @throws Exception
168: */
169: class MyNumber extends Number {
170: double value;
171: int count;
172:
173: MyNumber(double value) {
174: this .value = value;
175: }
176:
177: private static final long serialVersionUID = 1L;
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see java.lang.Number#doubleValue()
183: */
184: public double doubleValue() {
185: count++;
186: return value;
187: }
188:
189: /*
190: * (non-Javadoc)
191: *
192: * @see java.lang.Number#floatValue()
193: */
194: public float floatValue() {
195: return 0;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see java.lang.Number#intValue()
202: */
203: public int intValue() {
204: return 0;
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see java.lang.Number#longValue()
211: */
212: public long longValue() {
213: return 0;
214: }
215: }
216:
217: MyNumber number = new MyNumber(1);
218: assertEquals(0, number.count);
219: assertTrue(StringToNumberParser.inIntegerRange(number));
220: assertTrue("double value retrieved", number.count > 0);
221: }
222: }
|