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