001: /*
002: * $Id: TestDoubleType.java,v 1.1 2005/05/02 22:32:03 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.DataInputStream;
046: import java.io.DataOutputStream;
047: import java.math.BigDecimal;
048: import java.sql.Timestamp;
049:
050: import junit.framework.Test;
051: import junit.framework.TestSuite;
052:
053: import org.axiondb.AxionException;
054: import org.axiondb.DataType;
055:
056: /**
057: * A {@link DataType} representing a double precision floating-point value.
058: *
059: * @version $Revision: 1.1 $ $Date: 2005/05/02 22:32:03 $
060: * @author Jonathan Giron
061: */
062: public class TestDoubleType extends BaseNumericDataTypeTest {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestDoubleType(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(TestDoubleType.class);
072: return suite;
073: }
074:
075: //--------------------------------------------------------------- Lifecycle
076:
077: private DataType _type = null;
078:
079: public void setUp() throws Exception {
080: super .setUp();
081: _type = new DoubleType();
082: }
083:
084: public void tearDown() throws Exception {
085: super .tearDown();
086: _type = null;
087: }
088:
089: //------------------------------------------------------------------- Super
090:
091: protected DataType getDataType() {
092: return _type;
093: }
094:
095: //------------------------------------------------------------------- Tests
096:
097: public void testSupportsSuccessor() throws Exception {
098: assertTrue(_type.supportsSuccessor());
099: }
100:
101: public void testGetPrecision() throws Exception {
102: assertEquals(22, getDataType().getPrecision());
103: }
104:
105: public void testConvertNonNumericString() throws Exception {
106: expectExceptionWhileConvertingNonNumericString("22018");
107: }
108:
109: public void testSuccessor() throws Exception {
110: assertTrue(_type.supportsSuccessor());
111:
112: Double d = new Double(-Double.MAX_VALUE);
113: Double returned = (Double) _type.successor(d);
114: assertTrue(returned.compareTo(d) == 1);
115:
116: d = new Double(-2.43235987e231);
117: returned = (Double) _type.successor(d);
118: assertTrue(returned.compareTo(d) == 1);
119:
120: d = new Double(-4.9875e-15);
121: returned = (Double) _type.successor(d);
122: assertTrue(returned.compareTo(d) == 1);
123:
124: d = new Double(-Double.MIN_VALUE);
125: returned = (Double) _type.successor(d);
126: assertEquals(new Double(-0.0), returned);
127:
128: d = returned;
129: returned = (Double) _type.successor(d);
130: assertEquals(new Double(0.0), returned);
131:
132: d = returned;
133: returned = (Double) _type.successor(d);
134: assertEquals(new Double(Double.MIN_VALUE), returned);
135:
136: d = new Double(8.583e-12);
137: returned = (Double) _type.successor(d);
138: assertTrue(returned.compareTo(d) == 1);
139:
140: d = new Double(15.0);
141: returned = (Double) _type.successor(d);
142: assertTrue(returned.compareTo(d) == 1);
143:
144: d = new Double(2.43235987e231);
145: returned = (Double) _type.successor(d);
146: assertTrue(returned.compareTo(d) == 1);
147:
148: d = new Double(Double.MAX_VALUE);
149: returned = (Double) _type.successor(d);
150: assertEquals(new Double(Double.MAX_VALUE), returned);
151: }
152:
153: public void testRangeChecks() throws Exception {
154: final String msg = "Expected AxionException (22003)";
155:
156: final BigDecimal exceedsMaxPos = new BigDecimal(
157: Double.MAX_VALUE).add(new BigDecimal("1E308"));
158: final BigDecimal exceedsMaxNeg = new BigDecimal(
159: -Double.MAX_VALUE).subtract(new BigDecimal("1E308"));
160:
161: try {
162: _type.convert(exceedsMaxPos);
163: fail(msg);
164: } catch (AxionException expected) {
165: assertEquals(msg, "22003", expected.getSQLState());
166: }
167:
168: try {
169: _type.convert(exceedsMaxNeg);
170: fail(msg);
171: } catch (AxionException expected) {
172: assertEquals(msg, "22003", expected.getSQLState());
173: }
174:
175: try {
176: _type.convert(exceedsMaxPos.toString());
177: fail(msg);
178: } catch (AxionException expected) {
179: assertEquals(msg, "22003", expected.getSQLState());
180: }
181:
182: try {
183: _type.convert(exceedsMaxNeg.toString());
184: fail(msg);
185: } catch (AxionException expected) {
186: assertEquals(msg, "22003", expected.getSQLState());
187: }
188: }
189:
190: public void testCompare() throws Exception {
191: compareEm(new Double[] { new Double(Double.NEGATIVE_INFINITY),
192: new Double(-1f * Double.MAX_VALUE), new Double(-1),
193: new Double(-1f * Double.MIN_VALUE), new Double(0),
194: new Double(Double.MIN_VALUE), new Double(1),
195: new Double(Double.MAX_VALUE),
196: new Double(Double.POSITIVE_INFINITY) });
197: }
198:
199: public void testAccepts() throws Exception {
200: assertTrue("Should accept Byte", _type.accepts(new Byte(
201: (byte) 3)));
202: assertTrue("Should accept Short", _type.accepts(new Short(
203: (short) 3)));
204: assertTrue("Should accept Integer", _type
205: .accepts(new Integer(3)));
206: assertTrue("Should accept Long", _type.accepts(new Long(3L)));
207:
208: assertTrue("Should accept Double", _type.accepts(new Double(
209: 3.14D)));
210: assertTrue("Should accept Double", _type.accepts(new Double(
211: 3.14F)));
212:
213: assertTrue("Should accept integer String", _type.accepts("3"));
214: assertTrue("Should accept non-integer String", _type
215: .accepts("3.14159"));
216:
217: assertFalse("Should not accept Object", _type
218: .accepts(new Object()));
219: assertFalse("Should not accept Timestamp", _type
220: .accepts(new Timestamp(0L)));
221: }
222:
223: public void testConvertIntegerTypes() throws Exception {
224: assertEquals(new Double(17.0), _type
225: .convert(new Byte((byte) 17)));
226: assertEquals(new Double(17.0), _type.convert(new Short(
227: (short) 17)));
228: assertEquals(new Double(17.0), _type.convert(new Integer(17)));
229: assertEquals(new Double(17.0), _type.convert(new Long(17)));
230: }
231:
232: public void testConvertDecimalTypes() throws Exception {
233: assertEquals(new Double(17.99), _type
234: .convert(new Double(17.99)));
235: assertEquals(new Double(17.99), _type
236: .convert(new Double(17.99)));
237: assertEquals(new Double(17.99), _type.convert("17.99"));
238: }
239:
240: public void testConvertLiterals() throws Exception {
241: assertEquals(new Double(17.0), _type.convert("17.0"));
242: assertEquals(new Double(-17.0), _type.convert("-17.0"));
243:
244: assertEquals(new Double(17.0), _type.convert(" 17"));
245: assertEquals(new Double(-17.0), _type.convert(" -17 "));
246:
247: assertEquals(new Double(17.0), _type.convert("17.0 "));
248: assertEquals(new Double(-17.0), _type.convert(" -17.0"));
249:
250: try {
251: _type.convert("- 150.0");
252: fail("Expected AxionException");
253: } catch (AxionException expected) {
254: assertEquals(
255: "Expected AxionException (22018) - invalid character value for cast",
256: "22018", expected.getSQLState());
257: }
258:
259: try {
260: _type.convert("abcd");
261: fail("Expected AxionException");
262: } catch (AxionException expected) {
263: assertEquals(
264: "Expected AxionException (22018) - invalid character value for cast",
265: "22018", expected.getSQLState());
266: }
267: }
268:
269: public void testWriteReadNonNull() throws Exception {
270: Double orig = new Double(17.3);
271:
272: ByteArrayOutputStream buf = new ByteArrayOutputStream();
273: _type.write(orig, new DataOutputStream(buf));
274:
275: Object read = _type.read(new DataInputStream(
276: new ByteArrayInputStream(buf.toByteArray())));
277: assertEquals(orig, read);
278: }
279:
280: public void testWriteReadSeveral() throws Exception {
281: Double[] data = { new Double(1.3), new Double(-1.3), null,
282: new Double(0.0), new Double(Double.MAX_VALUE), null,
283: new Double(Double.MIN_VALUE), new Double(17.0), null };
284:
285: ByteArrayOutputStream out = new ByteArrayOutputStream();
286: for (int i = 0; i < data.length; i++) {
287: _type.write(data[i], new DataOutputStream(out));
288: }
289:
290: DataInputStream in = new DataInputStream(
291: new ByteArrayInputStream(out.toByteArray()));
292: for (int i = 0; i < data.length; i++) {
293: Object read = _type.read(in);
294: if (null == data[i]) {
295: assertNull(read);
296: } else {
297: assertEquals(data[i], read);
298: }
299: }
300: }
301:
302: public void testInvalidWriteFails() throws Exception {
303: assertInvalidWriteFails(new Double(Double.POSITIVE_INFINITY));
304: assertInvalidWriteFails(new Double(Double.NEGATIVE_INFINITY));
305: assertInvalidWriteFails(new Double(Double.NaN));
306: }
307: }
|