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