001: /*
002: * $Id: TestBigDecimalType.java,v 1.7 2005/08/24 00:38:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 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.io.IOException;
048: import java.math.BigDecimal;
049: import java.math.BigInteger;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.AxionException;
055: import org.axiondb.DataType;
056:
057: /**
058: * @version $Revision: 1.7 $ $Date: 2005/08/24 00:38:44 $
059: * @author Rodney Waldhoff
060: */
061: public class TestBigDecimalType extends BaseNumericDataTypeTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestBigDecimalType(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestBigDecimalType.class);
071: return suite;
072: }
073:
074: //--------------------------------------------------------------- Lifecycle
075:
076: private DataType type = null;
077:
078: public void setUp() throws Exception {
079: super .setUp();
080: type = new BigDecimalType(6, 2);
081: }
082:
083: public void tearDown() throws Exception {
084: super .tearDown();
085: type = null;
086: }
087:
088: //------------------------------------------------------------------- Super
089:
090: protected DataType getDataType() {
091: return type;
092: }
093:
094: //------------------------------------------------------------------- Tests
095:
096: public void testGetPrecision() throws Exception {
097: assertEquals(6, getDataType().getPrecision());
098: }
099:
100: public void testConvertNonNumericString() throws Exception {
101: expectExceptionWhileConvertingNonNumericString("22018");
102: }
103:
104: public void testAccepts() throws Exception {
105: assertTrue("Should accept Byte", type
106: .accepts(new Byte((byte) 3)));
107: assertTrue("Should accept Short", type.accepts(new Short(
108: (short) 3)));
109: assertTrue("Should accept Integer", type
110: .accepts(new Integer(3)));
111: assertTrue("Should accept Long", type.accepts(new Long(3L)));
112:
113: assertTrue("Should accept Double", type.accepts(new Double(
114: 3.14D)));
115: assertTrue("Should accept Float", type
116: .accepts(new Float(3.14F)));
117:
118: assertTrue("Should accept BigInteger", type
119: .accepts(new BigInteger("12345")));
120: assertTrue("Should accept BigDecimal", type
121: .accepts(new BigDecimal("12345.00")));
122:
123: assertTrue("Should accept integer String", type.accepts("3"));
124: assertTrue("Should accept non-integer String", type
125: .accepts("3.14"));
126: }
127:
128: public void testConvertLiterals() throws Exception {
129: assertEquals(new BigDecimal("17.00"), type.convert("17"));
130: assertEquals(new BigDecimal("17.00"), type.convert(" 17 "));
131: assertEquals(new BigDecimal("17.90"), type.convert("17.9"));
132: assertEquals(new BigDecimal("-17.90"), type
133: .convert(" -17.9 "));
134: assertEquals(new BigDecimal("17.99"), type
135: .convert(" 17.99 "));
136: assertEquals(new BigDecimal("17.99"), type
137: .convert("17.990 "));
138:
139: try {
140: type.convert("abcd");
141: fail("Expected AxionException");
142: } catch (AxionException expected) {
143: assertEquals(
144: "Expected AxionException (22018) - invalid character value for cast",
145: "22018", expected.getSQLState());
146: }
147:
148: try {
149: type.convert(new Float(Float.NaN));
150: fail("Expected AxionException");
151: } catch (AxionException expected) {
152: assertEquals(
153: "Expected AxionException (22003) - numeric value out of range",
154: "22003", expected.getSQLState());
155: }
156:
157: try {
158: type.convert(new Float(-Float.NaN));
159: fail("Expected AxionException");
160: } catch (AxionException expected) {
161: assertEquals(
162: "Expected AxionException (22003) - numeric value out of range",
163: "22003", expected.getSQLState());
164: }
165:
166: try {
167: type.convert(new Float(Float.POSITIVE_INFINITY));
168: fail("Expected AxionException");
169: } catch (AxionException expected) {
170: assertEquals(
171: "Expected AxionException (22003) - numeric value out of range",
172: "22003", expected.getSQLState());
173: }
174:
175: try {
176: type.convert(new Float(Float.NEGATIVE_INFINITY));
177: fail("Expected AxionException");
178: } catch (AxionException expected) {
179: assertEquals(
180: "Expected AxionException (22003) - numeric value out of range",
181: "22003", expected.getSQLState());
182: }
183: }
184:
185: public void testRounding() throws Exception {
186: assertEquals(new BigDecimal("17.99"), type.convert("17.9900"));
187: assertEquals(new BigDecimal("17.99"), type.convert("17.9910"));
188: assertEquals(new BigDecimal("17.99"), type.convert("17.9940"));
189: assertEquals(new BigDecimal("18.00"), type.convert("17.9950"));
190: assertEquals(new BigDecimal("18.00"), type.convert("17.9990"));
191: assertEquals(new BigDecimal("18.00"), type.convert("18.0000"));
192: }
193:
194: public void testNumericOverflow() throws Exception {
195: try {
196: type.convert("10000"); // numeric(5,0) - too large for numeric(6,2)
197: fail("Expected SQLException (22003) - numeric value out of range");
198: } catch (AxionException expected) {
199: assertEquals(
200: "Expected SQLException (22003) - numeric value out of range",
201: "22003", expected.getSQLState());
202: }
203: }
204:
205: public void testConvertIntegerTypes() throws Exception {
206: assertEquals(new BigDecimal("17.00"), type.convert(new Byte(
207: (byte) 17)));
208: assertEquals(new BigDecimal("17.00"), type.convert(new Short(
209: (short) 17)));
210: assertEquals(new BigDecimal("17.00"), type.convert(new Integer(
211: 17)));
212: assertEquals(new BigDecimal("17.00"), type
213: .convert(new Long(17)));
214: assertEquals(new BigDecimal("17.00"), type
215: .convert(new BigInteger("17")));
216: }
217:
218: public void testConvertDecimalTypes() throws Exception {
219: assertEquals(new BigDecimal("17.99"), type.convert(new Float(
220: 17.99f)));
221: assertEquals(new BigDecimal("17.90"), type.convert(new Float(
222: 17.9f)));
223: assertEquals(new BigDecimal("17.90"), type.convert(new Float(
224: 17.900f)));
225:
226: assertEquals(new BigDecimal("17.99"), type.convert(new Double(
227: 17.99d)));
228: assertEquals(new BigDecimal("17.90"), type.convert(new Double(
229: 17.9d)));
230: assertEquals(new BigDecimal("17.90"), type.convert(new Double(
231: 17.900d)));
232:
233: assertEquals(new BigDecimal("17.00"), type
234: .convert(new BigDecimal("17")));
235: assertEquals(new BigDecimal("17.90"), type
236: .convert(new BigDecimal("17.9")));
237: assertEquals(new BigDecimal("17.99"), type
238: .convert(new BigDecimal("17.99")));
239: assertEquals(new BigDecimal("17.99"), type
240: .convert(new BigDecimal("17.990")));
241: }
242:
243: public void testWriteReadNonNull() throws Exception {
244: BigDecimal orig = new BigDecimal("17.36");
245: ByteArrayOutputStream buf = new ByteArrayOutputStream();
246: type.write(orig, new DataOutputStream(buf));
247: Object read = type.read(new DataInputStream(
248: new ByteArrayInputStream(buf.toByteArray())));
249: assertEquals(orig, read);
250: }
251:
252: public void testToBigDecimal() throws Exception {
253: assertEquals(new BigDecimal("17.00"), type
254: .toBigDecimal(new Byte((byte) 17)));
255: assertEquals(new BigDecimal("17.00"), type
256: .toBigDecimal(new Short((short) 17)));
257: assertEquals(new BigDecimal("17.00"), type
258: .toBigDecimal(new Integer(17)));
259: assertEquals(new BigDecimal("17.00"), type
260: .toBigDecimal(new Long(17)));
261: assertEquals(new BigDecimal("17.00"), type
262: .toBigDecimal(new BigInteger("17")));
263:
264: assertEquals(new BigDecimal("17.99"), type
265: .toBigDecimal(new Float(17.99f)));
266: assertEquals(new BigDecimal("17.90"), type
267: .toBigDecimal(new Float(17.9f)));
268: assertEquals(new BigDecimal("17.90"), type
269: .toBigDecimal(new Float(17.900f)));
270:
271: assertEquals(new BigDecimal("17.99"), type
272: .toBigDecimal(new Double(17.99d)));
273: assertEquals(new BigDecimal("17.90"), type
274: .toBigDecimal(new Double(17.9d)));
275: assertEquals(new BigDecimal("17.90"), type
276: .toBigDecimal(new Double(17.900d)));
277:
278: assertEquals(new BigDecimal("17.00"), type
279: .toBigDecimal(new BigDecimal("17")));
280: assertEquals(new BigDecimal("17.90"), type
281: .toBigDecimal(new BigDecimal("17.9")));
282: assertEquals(new BigDecimal("17.99"), type
283: .toBigDecimal(new BigDecimal("17.99")));
284: assertEquals(new BigDecimal("17.99"), type
285: .toBigDecimal(new BigDecimal("17.990")));
286: }
287:
288: public void testWriteReadSeveral() throws Exception {
289: BigDecimal[] data = { new BigDecimal("1.30"),
290: new BigDecimal("-1.30"), null, new BigDecimal("0.00"),
291: null, null, new BigDecimal("17.00"), null };
292: ByteArrayOutputStream out = new ByteArrayOutputStream();
293: for (int i = 0; i < data.length; i++) {
294: type.write(data[i], new DataOutputStream(out));
295: }
296: DataInputStream in = new DataInputStream(
297: new ByteArrayInputStream(out.toByteArray()));
298: for (int i = 0; i < data.length; i++) {
299: Object read = type.read(in);
300: if (null == data[i]) {
301: assertNull(read);
302: } else {
303: assertEquals(data[i], read);
304: }
305: }
306: }
307:
308: public void testGetColumnDisplaySize() {
309: assertCorrectColumnDisplaySize(2, 2, new BigDecimalType(2, 2));
310: assertCorrectColumnDisplaySize(4, 0, new BigDecimalType(4, 0));
311: assertCorrectColumnDisplaySize(10, 3, new BigDecimalType(10, 3));
312:
313: BigDecimalType defType = new BigDecimalType();
314: assertCorrectColumnDisplaySize(defType.getPrecision(), defType
315: .getScale(), defType);
316: }
317:
318: public void testInvalidWriteFails() throws Exception {
319: ByteArrayOutputStream bos = new ByteArrayOutputStream();
320: try {
321: // value exceeds (precision,scale) of (6,2)
322: type.write(new BigDecimal("123456789"),
323: new DataOutputStream(bos));
324: fail("Expected IOException due to invalid write data");
325: } catch (IOException expected) {
326: // expected
327: }
328: }
329:
330: public void testInvalidConvertFails() {
331: try {
332: type.convert(new Object());
333: fail("Expected AxionException");
334: } catch (AxionException expected) {
335: // Expected.
336: }
337: }
338:
339: public void testPrecisionScaleOfConvertedValues()
340: throws AxionException {
341: BigDecimalType bdType;
342:
343: bdType = new BigDecimalType(new BigDecimal("0"));
344: assertEquals(1, bdType.getPrecision());
345: assertEquals(0, bdType.getScale());
346:
347: bdType = new BigDecimalType(new BigDecimal(".0"));
348: assertEquals(1, bdType.getPrecision());
349: assertEquals(1, bdType.getScale());
350:
351: bdType = new BigDecimalType(new BigDecimal("0.00"));
352: assertEquals(3, bdType.getPrecision());
353: assertEquals(2, bdType.getScale());
354:
355: bdType = new BigDecimalType(new BigDecimal("0.00300000"));
356: assertEquals(9, bdType.getPrecision());
357: assertEquals(8, bdType.getScale());
358:
359: bdType = new BigDecimalType(new BigDecimal(".11"));
360: assertEquals(2, bdType.getPrecision());
361: assertEquals(2, bdType.getScale());
362:
363: bdType = new BigDecimalType(new BigDecimal("-.11"));
364: assertEquals(2, bdType.getPrecision());
365: assertEquals(2, bdType.getScale());
366:
367: bdType = new BigDecimalType(new BigDecimal(".10"));
368: assertEquals(2, bdType.getPrecision());
369: assertEquals(2, bdType.getScale());
370:
371: bdType = new BigDecimalType(new BigDecimal("-.10"));
372: assertEquals(2, bdType.getPrecision());
373: assertEquals(2, bdType.getScale());
374:
375: bdType = new BigDecimalType(new BigDecimal(".1"));
376: assertEquals(1, bdType.getPrecision());
377: assertEquals(1, bdType.getScale());
378:
379: bdType = new BigDecimalType(new BigDecimal("-.1"));
380: assertEquals(1, bdType.getPrecision());
381: assertEquals(1, bdType.getScale());
382:
383: bdType = new BigDecimalType(new BigDecimal("4.510"));
384: assertEquals(4, bdType.getPrecision());
385: assertEquals(3, bdType.getScale());
386:
387: bdType = new BigDecimalType(new BigDecimal("123456789.0"));
388: assertEquals(10, bdType.getPrecision());
389: assertEquals(1, bdType.getScale());
390:
391: bdType = new BigDecimalType(new BigDecimal("-123456789.0"));
392: assertEquals(10, bdType.getPrecision());
393: assertEquals(1, bdType.getScale());
394:
395: bdType = new BigDecimalType(new BigDecimal("1234567890"));
396: assertEquals(10, bdType.getPrecision());
397: assertEquals(0, bdType.getScale());
398:
399: bdType = new BigDecimalType(new BigDecimal("0123456789"));
400: assertEquals(9, bdType.getPrecision());
401: assertEquals(0, bdType.getScale());
402: }
403:
404: private void assertCorrectColumnDisplaySize(final int precision,
405: final int scale, DataType bdType) {
406: // # significant digits + decimal point (if any, as indicated by scale) + sign
407: assertEquals(precision + (scale == 0 ? 0 : 1) + 1, bdType
408: .getColumnDisplaySize());
409: }
410: }
|