001: /*
002: * $Id: TestBigIntType.java,v 1.1 2005/11/03 01:26:56 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: * @version $Revision: 1.1 $ $Date: 2005/11/03 01:26:56 $
058: * @author Rodney Waldhoff
059: * @author Chuck Burdick
060: * @author Jonathan Giron
061: */
062: public class TestBigIntType extends BaseNumericDataTypeTest {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestBigIntType(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(TestBigIntType.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 BigIntType();
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 testGetPrecision() throws Exception {
098: assertEquals(19, getDataType().getPrecision());
099: assertEquals(10, getDataType().getPrecisionRadix());
100: }
101:
102: public void testGetColumnDisplaySize() {
103: assertEquals(String.valueOf(Long.MIN_VALUE).length(),
104: getDataType().getColumnDisplaySize());
105: }
106:
107: public void testConvertNonNumericString() throws Exception {
108: expectExceptionWhileConvertingNonNumericString("22018");
109: }
110:
111: public void testSuccessor() throws Exception {
112: Long val = new Long(4);
113: assertTrue(_type.supportsSuccessor());
114: assertEquals(new Long(5), _type.successor(val));
115: assertEquals(new Long(Long.MAX_VALUE), _type
116: .successor(new Long(Long.MAX_VALUE)));
117: }
118:
119: public void testRangeChecks() throws Exception {
120: final String msg = "Expected AxionException (22003)";
121:
122: final BigDecimal exceedsMax = new BigDecimal(Long.MAX_VALUE)
123: .add(new BigDecimal("1"));
124: final BigDecimal exceedsMin = new BigDecimal(Long.MIN_VALUE)
125: .subtract(new BigDecimal("1"));
126:
127: try {
128: _type.convert(exceedsMax);
129: fail(msg);
130: } catch (AxionException expected) {
131: assertEquals(msg, "22003", expected.getSQLState());
132: }
133:
134: try {
135: _type.convert(exceedsMin);
136: fail(msg);
137: } catch (AxionException expected) {
138: assertEquals(msg, "22003", expected.getSQLState());
139: }
140:
141: try {
142: _type.convert(exceedsMax.toString());
143: fail(msg);
144: } catch (AxionException expected) {
145: assertEquals(msg, "22003", expected.getSQLState());
146: }
147:
148: try {
149: _type.convert(exceedsMin.toString());
150: fail(msg);
151: } catch (AxionException expected) {
152: assertEquals(msg, "22003", expected.getSQLState());
153: }
154: }
155:
156: public void testCompare() throws Exception {
157: compareEm(new Long[] { new Long(Long.MIN_VALUE),
158: new Long((Long.MIN_VALUE + 1)), new Long(-1),
159: new Long(0), new Long(1),
160: new Long((Long.MAX_VALUE - 1)),
161: new Long(Long.MAX_VALUE) });
162: }
163:
164: public void testAccepts() throws Exception {
165: assertTrue("Should accept Byte", _type.accepts(new Byte(
166: (byte) 3)));
167: assertTrue("Should accept Short", _type.accepts(new Short(
168: (short) 3)));
169: assertTrue("Should accept Integer", _type
170: .accepts(new Integer(3)));
171: assertTrue("Should accept Long", _type.accepts(new Long(3L)));
172:
173: assertTrue("Should accept Double", _type.accepts(new Double(
174: 3.14D)));
175: assertTrue("Should accept Float", _type
176: .accepts(new Float(3.14F)));
177:
178: assertTrue("Should accept really long int", _type
179: .accepts(new Long(System.currentTimeMillis())));
180:
181: assertTrue("Should accept integer String", _type.accepts("3"));
182: assertTrue("Should accept Number String", _type
183: .accepts("3.14159"));
184:
185: assertTrue("Should accept Timestamp", _type
186: .accepts(new Timestamp(0L)));
187:
188: assertFalse("Should not accept Object", _type
189: .accepts(new Object()));
190: }
191:
192: public void testConvertIntegerTypes() throws Exception {
193: assertEquals(new Long(17), _type.convert(new Byte((byte) 17)));
194: assertEquals(new Long(17), _type.convert(new Short((short) 17)));
195: assertEquals(new Long(17), _type.convert(new Integer(17)));
196: assertEquals(new Long(17), _type.convert(new Long(17)));
197: }
198:
199: public void testConvertDecimalTypes() throws Exception {
200: assertEquals(new Long(17), _type.convert(new Float(17.99)));
201: assertEquals(new Long(17), _type.convert(new Double(17.99)));
202: }
203:
204: public void testConvertLiterals() throws Exception {
205: final Long seventeen = new Long(17L);
206: final Long negSeventeen = new Long(-17L);
207:
208: assertEquals(seventeen, _type.convert("17"));
209: assertEquals(negSeventeen, _type.convert("-17"));
210:
211: assertEquals(seventeen, _type.convert(" 17"));
212: assertEquals(negSeventeen, _type.convert(" -17 "));
213:
214: assertEquals(seventeen, _type.convert("17 "));
215: assertEquals(negSeventeen, _type.convert(" -17"));
216:
217: assertEquals("Should convert and truncate literal", new Long(
218: 17L), _type.convert("17.986"));
219:
220: try {
221: _type.convert("- 150");
222: fail("Expected AxionException");
223: } catch (AxionException expected) {
224: assertEquals(
225: "Expected AxionException (22018) - invalid character value for cast",
226: "22018", expected.getSQLState());
227: }
228:
229: try {
230: _type.convert("abcd");
231: fail("Expected AxionException");
232: } catch (AxionException expected) {
233: assertEquals(
234: "Expected AxionException (22018) - invalid character value for cast",
235: "22018", expected.getSQLState());
236: }
237: }
238:
239: public void testWriteReadNonNull() throws Exception {
240: Long orig = new Long(17);
241: ByteArrayOutputStream buf = new ByteArrayOutputStream();
242: _type.write(orig, new DataOutputStream(buf));
243: Object read = _type.read(new DataInputStream(
244: new ByteArrayInputStream(buf.toByteArray())));
245: assertEquals(orig, read);
246: }
247:
248: public void testWriteReadSeveral() throws Exception {
249: Long[] data = { new Long(1), new Long(-1), null, new Long(0),
250: new Long(Long.MAX_VALUE), null,
251: new Long(Long.MIN_VALUE), new Long(17), null };
252: ByteArrayOutputStream out = new ByteArrayOutputStream();
253: for (int i = 0; i < data.length; i++) {
254: _type.write(data[i], new DataOutputStream(out));
255: }
256: DataInputStream in = new DataInputStream(
257: new ByteArrayInputStream(out.toByteArray()));
258: for (int i = 0; i < data.length; i++) {
259: Object read = _type.read(in);
260: if (null == data[i]) {
261: assertNull(read);
262: } else {
263: assertEquals(data[i], read);
264: }
265: }
266: }
267:
268: public void testInvalidWriteFails() throws Exception {
269: assertInvalidWriteFails(new BigDecimal(Long.MAX_VALUE)
270: .add(new BigDecimal(1)));
271: assertInvalidWriteFails(new Object());
272: }
273: }
|