001: /*
002: * $Id: TestUnsignedShortType.java,v 1.11 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.sql.Timestamp;
048:
049: import junit.framework.Test;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.DataType;
054:
055: /**
056: * @version $Revision: 1.11 $ $Date: 2005/05/10 00:15:20 $
057: * @author Rodney Waldhoff
058: * @author Jonathan Giron
059: */
060: public class TestUnsignedShortType extends BaseNumericDataTypeTest {
061:
062: //------------------------------------------------------------ Conventional
063:
064: public TestUnsignedShortType(String testName) {
065: super (testName);
066: }
067:
068: public static Test suite() {
069: TestSuite suite = new TestSuite(TestUnsignedShortType.class);
070: return suite;
071: }
072:
073: //--------------------------------------------------------------- Lifecycle
074:
075: private DataType _type = null;
076:
077: public void setUp() throws Exception {
078: super .setUp();
079: _type = new UnsignedShortType();
080: }
081:
082: public void tearDown() throws Exception {
083: super .tearDown();
084: _type = null;
085: }
086:
087: //------------------------------------------------------------------- Super
088:
089: protected DataType getDataType() {
090: return _type;
091: }
092:
093: //------------------------------------------------------------------- Tests
094:
095: public void testGetPrecision() throws Exception {
096: assertEquals(5, getDataType().getPrecision());
097: assertEquals(10, getDataType().getPrecisionRadix());
098: }
099:
100: public void testGetColumnDisplayType() throws Exception {
101: assertEquals(String.valueOf(UnsignedShortType.MAX_VALUE)
102: .length(), _type.getColumnDisplaySize());
103: }
104:
105: public void testConvertNonNumericString() throws Exception {
106: expectExceptionWhileConvertingNonNumericString("22018");
107: }
108:
109: public void testSuccessor() throws Exception {
110: Integer i = new Integer(4);
111: assertTrue(_type.supportsSuccessor());
112: assertEquals(new Integer(5), _type.successor(i));
113: assertEquals(new Integer(0xFFFF), _type.successor(new Integer(
114: 0xFFFF)));
115: }
116:
117: public void testAccepts() throws Exception {
118: assertTrue("Should accept Byte", _type.accepts(new Byte(
119: (byte) 3)));
120: assertTrue("Should accept Short", _type.accepts(new Short(
121: (short) 3)));
122: assertTrue("Should accept Integer", _type
123: .accepts(new Integer(3)));
124: assertTrue("Should accept Long", _type.accepts(new Long(3L)));
125:
126: assertTrue("Should accept Double", _type.accepts(new Double(
127: 3.14D)));
128: assertTrue("Should accept Float", _type
129: .accepts(new Float(3.14F)));
130:
131: assertTrue("Should accept integer String", _type.accepts("3"));
132: assertTrue("Should accept number String (will truncate)", _type
133: .accepts("3.14159"));
134: }
135:
136: public void testDoesNotAccept() throws Exception {
137: assertFalse("Should not accept non-number object", _type
138: .accepts(new Object()));
139: assertFalse(
140: "Should not accept integer > largest unsigned short",
141: _type.accepts(new Integer(
142: UnsignedShortType.MAX_VALUE + 1)));
143: assertFalse("Should not accept negative value", _type
144: .accepts(new Short((short) -1)));
145: assertFalse("Should not accept Timestamp", _type
146: .accepts(new Timestamp(0L)));
147: }
148:
149: public void testConvertIntegerTypes() throws Exception {
150: assertEquals(new Integer(17), _type
151: .convert(new Byte((byte) 17)));
152: assertEquals(new Integer(17), _type.convert(new Short(
153: (short) 17)));
154: assertEquals(new Integer(17), _type.convert(new Integer(17)));
155: assertEquals(new Integer(17), _type.convert(new Long(17)));
156: assertEquals(new Integer(UnsignedShortType.MAX_VALUE), _type
157: .convert(new Integer(UnsignedShortType.MAX_VALUE)));
158: }
159:
160: public void testConvertDecimalTypes() throws Exception {
161: assertEquals(new Integer(17), _type.convert(new Float(17.99)));
162: assertEquals(new Integer(17), _type.convert(new Double(17.99)));
163: }
164:
165: public void testConvertLiteralStrings() throws Exception {
166: assertEquals(new Integer(17), _type.convert("17"));
167: assertEquals(new Integer(17), _type.convert(" 17"));
168: assertEquals(new Integer(17), _type.convert("17 "));
169: assertEquals(new Integer(17), _type.convert("17.0 "));
170: assertEquals(new Integer(17), _type.convert(" 17.0 "));
171:
172: try {
173: _type.convert("-17");
174: fail("Expected AxionException (22003)");
175: } catch (AxionException expected) {
176: assertEquals("22003", expected.getSQLState());
177: }
178:
179: try {
180: _type.convert(String.valueOf((Short.MAX_VALUE + 1) * 2));
181: fail("Expected AxionException (22003)");
182: } catch (AxionException expected) {
183: assertEquals("22003", expected.getSQLState());
184: }
185:
186: try {
187: _type.convert("");
188: fail("Expected AxionException (22018)");
189: } catch (AxionException expected) {
190: assertEquals("22018", expected.getSQLState());
191: }
192: }
193:
194: public void testInvalidSuccessorFails() {
195: try {
196: _type
197: .successor(new Integer(
198: UnsignedShortType.MAX_VALUE + 1));
199: fail("Expected IllegalArgumentException");
200: } catch (IllegalArgumentException expected) {
201: // Expected.
202: }
203: }
204:
205: public void testWriteReadNonNull() throws Exception {
206: Integer orig = new Integer(17);
207:
208: ByteArrayOutputStream buf = new ByteArrayOutputStream();
209: _type.write(orig, new DataOutputStream(buf));
210:
211: Object read = _type.read(new DataInputStream(
212: new ByteArrayInputStream(buf.toByteArray())));
213: assertEquals(orig, read);
214: }
215:
216: public void testWriteReadSeveral() throws Exception {
217: Integer[] data = { new Integer(1), new Integer(0xFFFF), null,
218: new Integer(0), new Integer(0xFFFF), null,
219: new Integer(Short.MAX_VALUE), new Integer(17), null };
220:
221: ByteArrayOutputStream out = new ByteArrayOutputStream();
222: for (int i = 0; i < data.length; i++) {
223: _type.write(data[i], new DataOutputStream(out));
224: }
225: DataInputStream in = new DataInputStream(
226: new ByteArrayInputStream(out.toByteArray()));
227: for (int i = 0; i < data.length; i++) {
228: Object read = _type.read(in);
229: if (null == data[i]) {
230: assertNull(read);
231: } else {
232: assertEquals(data[i], read);
233: }
234: }
235: }
236:
237: public void testInvalidWriteFails() throws Exception {
238: assertInvalidWriteFails(new Integer(
239: UnsignedShortType.MAX_VALUE + 1));
240: assertInvalidWriteFails(new Integer(
241: UnsignedShortType.MIN_VALUE - 1));
242: assertInvalidWriteFails(new Object());
243: }
244: }
|