001: /*
002: * $Id: TestUnsignedByteType.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 TestUnsignedByteType extends BaseNumericDataTypeTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestUnsignedByteType(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestUnsignedByteType.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 UnsignedByteType();
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 testGetColumnDisplayType() throws Exception {
102: assertEquals(String.valueOf(UnsignedByteType.MAX_VALUE)
103: .length(), _type.getColumnDisplaySize());
104: }
105:
106: public void testConvertNonNumericString() throws Exception {
107: expectExceptionWhileConvertingNonNumericString("22018");
108: }
109:
110: public void testSuccessor() throws Exception {
111: Short i = new Short((short) 4);
112: assertTrue(_type.supportsSuccessor());
113: assertEquals(new Short((short) 5), _type.successor(i));
114: assertEquals(new Short(UnsignedByteType.MAX_VALUE), _type
115: .successor(new Short(UnsignedByteType.MAX_VALUE)));
116: }
117:
118: public void testRangeChecks() throws Exception {
119: final String msg = "Expected AxionException (22003)";
120:
121: final short exceedsMax = (short) (UnsignedByteType.MAX_VALUE + 1);
122: final short exceedsMin = (short) (UnsignedByteType.MIN_VALUE - 1);
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 byte",
191: _type.accepts(new Short(
192: (short) (UnsignedByteType.MAX_VALUE + 1))));
193: assertFalse("Should not accept negative value", _type
194: .accepts(new Short((short) -1)));
195: assertFalse("Should not accept Timestamp", _type
196: .accepts(new Timestamp(0L)));
197: }
198:
199: public void testConvertIntegerTypes() throws Exception {
200: assertEquals(new Short((short) 17), _type.convert(new Byte(
201: (byte) 17)));
202: assertEquals(new Short((short) 17), _type.convert(new Short(
203: (short) 17)));
204: assertEquals(new Short((short) 17), _type.convert(new Integer(
205: 17)));
206: assertEquals(new Short((short) 17), _type.convert(new Long(17)));
207: assertEquals(new Short(UnsignedByteType.MAX_VALUE), _type
208: .convert(new Short(UnsignedByteType.MAX_VALUE)));
209: }
210:
211: public void testConvertDecimalTypes() throws Exception {
212: assertEquals(new Short((short) 17), _type.convert(new Float(
213: 17.99)));
214: assertEquals(new Short((short) 17), _type.convert(new Double(
215: 17.99)));
216: }
217:
218: public void testConvertLiteralStrings() throws Exception {
219: assertEquals(new Short((short) 17), _type.convert("17"));
220: assertEquals(new Short((short) 17), _type.convert(" 17"));
221: assertEquals(new Short((short) 17), _type.convert("17 "));
222: assertEquals(new Short((short) 17), _type.convert("17.0 "));
223: assertEquals(new Short((short) 17), _type.convert(" 17.0 "));
224:
225: try {
226: _type.convert("-17");
227: fail("Expected AxionException (22003)");
228: } catch (AxionException expected) {
229: assertEquals("22003", expected.getSQLState());
230: }
231:
232: try {
233: _type.convert(String.valueOf((Byte.MAX_VALUE + 1) * 2));
234: fail("Expected AxionException (22003)");
235: } catch (AxionException expected) {
236: assertEquals("22003", expected.getSQLState());
237: }
238:
239: try {
240: _type.convert("");
241: fail("Expected AxionException (22018)");
242: } catch (AxionException expected) {
243: assertEquals("22018", expected.getSQLState());
244: }
245: }
246:
247: public void testInvalidSuccessorFails() {
248: try {
249: _type.successor(new Short(
250: (short) (UnsignedByteType.MAX_VALUE + (short) 1)));
251: fail("Expected IllegalArgumentException");
252: } catch (IllegalArgumentException expected) {
253: // Expected.
254: }
255: }
256:
257: public void testWriteReadNonNull() throws Exception {
258: Short orig = new Short((short) 17);
259:
260: ByteArrayOutputStream buf = new ByteArrayOutputStream();
261: _type.write(orig, new DataOutputStream(buf));
262:
263: Object read = _type.read(new DataInputStream(
264: new ByteArrayInputStream(buf.toByteArray())));
265: assertEquals(orig, read);
266: }
267:
268: public void testWriteReadSeveral() throws Exception {
269: Short[] data = { new Short((short) 1), new Short((short) 128),
270: null, new Short((short) 0), new Short((short) 255),
271: null, new Short((short) 129), new Short((short) 17),
272: null };
273:
274: ByteArrayOutputStream out = new ByteArrayOutputStream();
275: for (int i = 0; i < data.length; i++) {
276: _type.write(data[i], new DataOutputStream(out));
277: }
278:
279: DataInputStream in = new DataInputStream(
280: new ByteArrayInputStream(out.toByteArray()));
281: for (int i = 0; i < data.length; i++) {
282: Object read = _type.read(in);
283: if (null == data[i]) {
284: assertNull(read);
285: } else {
286: assertEquals(data[i], read);
287: }
288: }
289: }
290:
291: public void testInvalidWriteFails() throws Exception {
292: assertInvalidWriteFails(new Short(
293: (short) (UnsignedByteType.MAX_VALUE + 1)));
294: assertInvalidWriteFails(new Short(
295: (short) (UnsignedByteType.MIN_VALUE - 1)));
296: assertInvalidWriteFails(new Object());
297: }
298: }
|