001: /*
002: * $Id: TestUnsignedIntegerType.java,v 1.10 2005/05/10 00:15:20 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.10 $ $Date: 2005/05/10 00:15:20 $
058: * @author Rodney Waldhoff
059: * @author Jonathan Giron
060: */
061: public class TestUnsignedIntegerType extends BaseNumericDataTypeTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestUnsignedIntegerType(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestUnsignedIntegerType.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 UnsignedIntegerType();
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 testGetColumnDisplayType() throws Exception {
102: assertEquals(String.valueOf((Integer.MAX_VALUE + 1L) * 2L)
103: .length(), _type.getColumnDisplaySize());
104: }
105:
106: public void testConvertNonNumericString() throws Exception {
107: expectExceptionWhileConvertingNonNumericString("22018");
108: }
109:
110: public void testSuccessor() throws Exception {
111: Long i = new Long(4);
112: assertTrue(_type.supportsSuccessor());
113: assertEquals(new Long(5), _type.successor(i));
114: assertEquals(new Long(UnsignedIntegerType.MAX_VALUE), _type
115: .successor(new Long(UnsignedIntegerType.MAX_VALUE)));
116: }
117:
118: public void testRangeChecks() throws Exception {
119: final String msg = "Expected AxionException (22003)";
120:
121: final long exceedsMax = UnsignedIntegerType.MAX_VALUE + 1L;
122: final long exceedsMin = UnsignedIntegerType.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 testAccepts() throws Exception {
168: assertTrue("Should accept Byte", _type.accepts(new Byte(
169: (byte) 3)));
170: assertTrue("Should accept Short", _type.accepts(new Short(
171: (short) 3)));
172: assertTrue("Should accept Integer", _type
173: .accepts(new Integer(3)));
174: assertTrue("Should accept Long", _type.accepts(new Long(3L)));
175:
176: assertTrue("Should accept Double", _type.accepts(new Double(
177: 3.14D)));
178: assertTrue("Should accept Float", _type
179: .accepts(new Float(3.14F)));
180:
181: assertTrue("Should accept integer String", _type.accepts("3"));
182: assertTrue("Should accept Number String (will truncate)", _type
183: .accepts("3.14159"));
184: }
185:
186: public void testDoesNotAccept() throws Exception {
187: assertFalse("Should not accept non-number object", _type
188: .accepts(new Object()));
189: assertFalse(
190: "Should not accept integer > largest unsigned integer",
191: _type.accepts(new Long(
192: UnsignedIntegerType.MAX_VALUE + 1L)));
193: assertFalse("Should not accept negative value", _type
194: .accepts(new Integer(-1)));
195: assertFalse("Should not accept Timestamp", _type
196: .accepts(new Timestamp(0L)));
197: }
198:
199: public void testConvertIntegerTypes() throws Exception {
200: assertEquals(new Long(17), _type.convert(new Byte((byte) 17)));
201: assertEquals(new Long(17), _type.convert(new Short((short) 17)));
202: assertEquals(new Long(17), _type.convert(new Integer(17)));
203: assertEquals(new Long(17), _type.convert(new Long(17)));
204: assertEquals(new Long(UnsignedIntegerType.MAX_VALUE), _type
205: .convert(new Long(UnsignedIntegerType.MAX_VALUE)));
206: }
207:
208: public void testConvertDecimalTypes() throws Exception {
209: assertEquals(new Long(17), _type.convert(new Float(17.99)));
210: assertEquals(new Long(17), _type.convert(new Double(17.99)));
211: }
212:
213: public void testConvertLiteralStrings() throws Exception {
214: assertEquals(new Long(17), _type.convert("17"));
215: assertEquals(new Long(17), _type.convert(" 17"));
216: assertEquals(new Long(17), _type.convert("17 "));
217: assertEquals(new Long(17), _type.convert("17.0 "));
218: assertEquals(new Long(17), _type.convert(" 17.0 "));
219:
220: try {
221: _type.convert("-17");
222: fail("Expected AxionException (22003)");
223: } catch (AxionException expected) {
224: assertEquals("22003", expected.getSQLState());
225: }
226:
227: try {
228: _type
229: .convert(String
230: .valueOf((Integer.MAX_VALUE + 1L) * 2L));
231: fail("Expected AxionException (22003)");
232: } catch (AxionException expected) {
233: assertEquals("22003", expected.getSQLState());
234: }
235:
236: try {
237: _type.convert("");
238: fail("Expected AxionException (22018)");
239: } catch (AxionException expected) {
240: assertEquals("22018", expected.getSQLState());
241: }
242: }
243:
244: public void testInvalidSuccessorFails() {
245: try {
246: _type
247: .successor(new Long(
248: UnsignedIntegerType.MAX_VALUE + 1L));
249: fail("Expected IllegalArgumentException");
250: } catch (IllegalArgumentException expected) {
251: // Expected.
252: }
253: }
254:
255: public void testWriteReadNonNull() throws Exception {
256: Long orig = new Long(17);
257:
258: ByteArrayOutputStream buf = new ByteArrayOutputStream();
259: _type.write(orig, new DataOutputStream(buf));
260:
261: Object read = _type.read(new DataInputStream(
262: new ByteArrayInputStream(buf.toByteArray())));
263: assertEquals(orig, read);
264: }
265:
266: public void testWriteReadSeveral() throws Exception {
267: Long[] data = { new Long(1L), new Long(Integer.MAX_VALUE),
268: null, new Long(0L), new Long(0xFFFFFFFFL), null,
269: new Long(Integer.MAX_VALUE + 1L), new Long(17L), null };
270:
271: ByteArrayOutputStream out = new ByteArrayOutputStream();
272: for (int i = 0; i < data.length; i++) {
273: _type.write(data[i], new DataOutputStream(out));
274: }
275:
276: DataInputStream in = new DataInputStream(
277: new ByteArrayInputStream(out.toByteArray()));
278: for (int i = 0; i < data.length; i++) {
279: Object read = _type.read(in);
280: if (null == data[i]) {
281: assertNull(read);
282: } else {
283: assertEquals(data[i], read);
284: }
285: }
286: }
287:
288: public void testInvalidWriteFails() throws Exception {
289: assertInvalidWriteFails(new Long(
290: UnsignedIntegerType.MAX_VALUE + 1));
291: assertInvalidWriteFails(new Long(
292: UnsignedIntegerType.MIN_VALUE - 1));
293: assertInvalidWriteFails(new Object());
294: }
295: }
|