001: /*
002: * $Id: TestByteType.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 TestByteType extends BaseNumericDataTypeTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestByteType(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestByteType.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 ByteType();
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(3, getDataType().getPrecision());
098: assertEquals(10, getDataType().getPrecisionRadix());
099: }
100:
101: public void testGetColumnDisplaySize() {
102: assertEquals(String.valueOf(Byte.MIN_VALUE).length(), _type
103: .getColumnDisplaySize());
104: }
105:
106: public void testConvertNonNumericString() throws Exception {
107: expectExceptionWhileConvertingNonNumericString("22018");
108: }
109:
110: public void testSuccessor() throws Exception {
111: Byte val = new Byte((byte) 4);
112: assertTrue(_type.supportsSuccessor());
113: assertEquals(new Byte((byte) 5), _type.successor(val));
114: assertEquals(new Byte(Byte.MAX_VALUE), _type
115: .successor(new Byte(Byte.MAX_VALUE)));
116: }
117:
118: public void testRangeChecks() throws Exception {
119: final String msg = "Expected AxionException (22003)";
120:
121: final int exceedsMax = Byte.MAX_VALUE + 1;
122: final int exceedsMin = Byte.MIN_VALUE - 1;
123:
124: try {
125: _type.convert(new Integer(exceedsMax));
126: fail(msg);
127: } catch (AxionException expected) {
128: assertEquals(msg, "22003", expected.getSQLState());
129: }
130:
131: try {
132: _type.convert(new Integer(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 Byte[] { new Byte(Byte.MIN_VALUE),
169: new Byte((byte) (Byte.MIN_VALUE + 1)),
170: new Byte((byte) -1), new Byte((byte) 0),
171: new Byte((byte) 1),
172: new Byte((byte) (Byte.MAX_VALUE - 1)),
173: new Byte(Byte.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 (will truncate)", _type
192: .accepts("3.14159"));
193:
194: assertFalse("Should not accept non-number object", _type
195: .accepts(new Object()));
196: assertFalse("Should not accept non-number object", _type
197: .accepts(new Timestamp(0L)));
198: }
199:
200: public void testConvertIntegerTypes() throws Exception {
201: assertEquals(new Byte((byte) 17), _type.convert(new Byte(
202: (byte) 17)));
203: assertEquals(new Byte((byte) 17), _type.convert(new Short(
204: (short) 17)));
205: assertEquals(new Byte((byte) 17), _type
206: .convert(new Integer(17)));
207: assertEquals(new Byte((byte) 17), _type.convert(new Long(17)));
208: }
209:
210: public void testConvertDecimalTypes() throws Exception {
211: assertEquals(new Byte((byte) 17), _type
212: .convert(new Float(17.99)));
213: assertEquals(new Byte((byte) 17), _type.convert(new Double(
214: 17.99)));
215: }
216:
217: public void testWriteReadNonNull() throws Exception {
218: Byte orig = new Byte((byte) 17);
219:
220: ByteArrayOutputStream buf = new ByteArrayOutputStream();
221: _type.write(orig, new DataOutputStream(buf));
222:
223: Object read = _type.read(new DataInputStream(
224: new ByteArrayInputStream(buf.toByteArray())));
225: assertEquals(orig, read);
226: }
227:
228: public void testWriteReadSeveral() throws Exception {
229: Byte[] data = { new Byte((byte) 1), new Byte((byte) -1), null,
230: new Byte((byte) 0), new Byte(Byte.MAX_VALUE), null,
231: new Byte(Byte.MIN_VALUE), new Byte((byte) 17), null };
232:
233: ByteArrayOutputStream out = new ByteArrayOutputStream();
234: for (int i = 0; i < data.length; i++) {
235: _type.write(data[i], new DataOutputStream(out));
236: }
237:
238: DataInputStream in = new DataInputStream(
239: new ByteArrayInputStream(out.toByteArray()));
240: for (int i = 0; i < data.length; i++) {
241: Object read = _type.read(in);
242: if (null == data[i]) {
243: assertNull(read);
244: } else {
245: assertEquals(data[i], read);
246: }
247: }
248: }
249:
250: public void testInvalidWriteFails() throws Exception {
251: assertInvalidWriteFails(new Integer(Byte.MAX_VALUE + 1));
252: assertInvalidWriteFails(new Integer(Byte.MIN_VALUE - 1));
253: assertInvalidWriteFails(new Object());
254: }
255: }
|