001: /*
002: * $Id: TestAnyType.java,v 1.12 2005/12/20 18:32:59 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.ByteArrayOutputStream;
044: import java.io.DataOutputStream;
045: import java.io.IOException;
046: import java.sql.Date;
047: import java.sql.Time;
048: import java.sql.Timestamp;
049:
050: import junit.framework.Test;
051: import junit.framework.TestCase;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.AxionException;
055: import org.axiondb.DataType;
056: import org.axiondb.DataTypeFactory;
057:
058: /**
059: * @version $Revision: 1.12 $ $Date: 2005/12/20 18:32:59 $
060: * @author Rodney Waldhoff
061: */
062: public class TestAnyType extends TestCase {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestAnyType(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(TestAnyType.class);
072: return suite;
073: }
074:
075: //--------------------------------------------------------------- Lifecycle
076:
077: private DataType type = null;
078:
079: public void setUp() throws Exception {
080: super .setUp();
081: type = new AnyType();
082: }
083:
084: public void tearDown() throws Exception {
085: super .tearDown();
086: type = null;
087: }
088:
089: //------------------------------------------------------------------- Tests
090:
091: public void testAccepts() throws Exception {
092: // AnyType accepts anything
093: assertTrue("Should accept null", type.accepts(null));
094: assertTrue("Should accept Byte", type
095: .accepts(new Byte((byte) 3)));
096: assertTrue("Should accept Short", type.accepts(new Short(
097: (short) 3)));
098: assertTrue("Should accept Integer", type
099: .accepts(new Integer(3)));
100: assertTrue("Should accept Long", type.accepts(new Long(3L)));
101: assertTrue("Should accept Double", type.accepts(new Double(
102: 3.14D)));
103: assertTrue("Should accept Float", type
104: .accepts(new Float(3.14F)));
105: assertTrue("Should accept Integer String", type.accepts("3"));
106: assertTrue("Should accept non-integer String", type
107: .accepts("The quick brown fox."));
108: assertTrue("Should Date", type.accepts(new Date(System
109: .currentTimeMillis())));
110: assertTrue("Should Timestamp", type.accepts(new Timestamp(
111: System.currentTimeMillis())));
112: assertTrue("Should Time", type.accepts(new Time(System
113: .currentTimeMillis())));
114: }
115:
116: public void testConvert() throws Exception {
117: // AnyType.convert just returns the given object
118: assertNull(type.convert(null));
119: assertEquals(new Byte((byte) 17), type.convert(new Byte(
120: (byte) 17)));
121: assertEquals(new Short((short) 17), type.convert(new Short(
122: (short) 17)));
123: assertEquals(new Integer(17), type.convert(new Integer(17)));
124: assertEquals(new Long(17), type.convert(new Long(17)));
125: assertEquals(new Float(17.99), type.convert(new Float(17.99)));
126: assertEquals(new Double(17.99), type.convert(new Double(17.99)));
127: assertEquals("The quick brown fox.", type
128: .convert("The quick brown fox."));
129: }
130:
131: public void testWrite() throws Exception {
132: ByteArrayOutputStream buf = new ByteArrayOutputStream();
133: try {
134: type.write("foo", new DataOutputStream(buf));
135: fail("Exepcted IOException.");
136: } catch (IOException e) {
137: // expected
138: }
139: }
140:
141: public void testRead() throws Exception {
142: try {
143: type.read(null);
144: fail("Exepcted IOException.");
145: } catch (IOException e) {
146: // expected
147: }
148: }
149:
150: public void testGetPreferredValueClassName() throws Exception {
151: assertEquals("java.lang.String", type
152: .getPreferredValueClassName());
153: }
154:
155: public void testGetJdbcType() throws Exception {
156: assertEquals(java.sql.Types.OTHER, type.getJdbcType());
157: }
158:
159: public void testToNumber() throws Exception {
160: assertNull(((AnyType) type).toNumber(null));
161: assertEquals(new Integer(3), ((AnyType) type)
162: .toNumber(new Integer(3)));
163: assertEquals(new Integer(3), ((AnyType) type).toNumber("3"));
164: }
165:
166: public void testSuccessor() throws Exception {
167: assertTrue(!type.supportsSuccessor());
168: try {
169: type.successor(new Integer(3));
170: fail("Exepcted UnsupportedOperationException.");
171: } catch (UnsupportedOperationException e) {
172: // expected
173: }
174: }
175:
176: public void testToBoolean() throws Exception {
177: try {
178: type.toBoolean(null);
179: fail("Expected AxionException");
180: } catch (AxionException e) {
181: // expected
182: }
183: try {
184: type.toBoolean(new Integer(17));
185: fail("Expected AxionException");
186: } catch (AxionException e) {
187: // expected
188: }
189: try {
190: type.toBoolean("foo");
191: fail("Expected AxionException");
192: } catch (AxionException e) {
193: // expected
194: }
195: assertEquals(false, type.toBoolean("false"));
196: assertEquals(true, type.toBoolean("true"));
197: assertEquals(true, type.toBoolean(Boolean.TRUE));
198: assertEquals(false, type.toBoolean(Boolean.FALSE));
199: }
200:
201: public void testToByte() throws Exception {
202: assertEquals((byte) 17, type.toByte(new Byte((byte) 17)));
203: assertEquals((byte) 17, type.toByte(new Short((short) 17)));
204: assertEquals((byte) 17, type.toByte(new Integer(17)));
205: assertEquals((byte) 17, type.toByte(new Long(17)));
206: assertEquals((byte) 17, type.toByte(new Float(17)));
207: assertEquals((byte) 17, type.toByte(new Double(17)));
208: assertEquals((byte) 17, type.toByte("17"));
209: try {
210: type.toByte(null);
211: fail("Expected AxionException");
212: } catch (AxionException e) {
213: // expected
214: }
215: try {
216: type.toByte("foo");
217: fail("Expected AxionException");
218: } catch (AxionException e) {
219: // expected
220: }
221: try {
222: type.toByte(Boolean.TRUE);
223: fail("Exepected AxionException");
224: } catch (AxionException e) {
225: // expected
226: }
227: }
228:
229: public void testToByteArray() throws Exception {
230: Byte b17 = new Byte((byte) 17);
231: assertEquals(new String(String.valueOf(b17).getBytes()),
232: new String(type.toByteArray(b17)));
233: Short s17 = new Short((short) 17);
234: assertEquals(new String(String.valueOf(s17).getBytes()),
235: new String(type.toByteArray(s17)));
236: Integer i17 = new Integer(17);
237: assertEquals(new String(String.valueOf(i17).getBytes()),
238: new String(type.toByteArray(i17)));
239: Long l17 = new Long(17);
240: assertEquals(new String(String.valueOf(l17).getBytes()),
241: new String(type.toByteArray(l17)));
242: Float f17 = new Float(17);
243: assertEquals(new String(String.valueOf(f17).getBytes()),
244: new String(type.toByteArray(f17)));
245: Double d17 = new Double(17);
246:
247: assertEquals(new String(String.valueOf(d17).getBytes()),
248: new String(type.toByteArray(d17)));
249: assertEquals(new String("17".getBytes()), new String(type
250: .toByteArray("17")));
251: assertEquals(new String("17".getBytes()), new String(type
252: .toByteArray("17".getBytes())));
253:
254: try {
255: type.toByteArray(null);
256: fail("Exepected AxionException");
257: } catch (AxionException e) {
258: // expected
259: }
260: }
261:
262: public void testToDouble() throws Exception {
263: assertEquals(17d, type.toDouble(new Byte((byte) 17)), 0d);
264: assertEquals(17d, type.toDouble(new Short((short) 17)), 0d);
265: assertEquals(17d, type.toDouble(new Integer(17)), 0d);
266: assertEquals(17d, type.toDouble(new Long(17)), 0d);
267: assertEquals(17.35d, type.toDouble(new Float(17.35f)), 0.001d);
268: assertEquals(17.35d, type.toDouble(new Double(17.35d)), 0.001d);
269: assertEquals(17.35d, type.toDouble("17.35"), 0d);
270: try {
271: type.toDouble(null);
272: fail("Expected AxionException");
273: } catch (AxionException e) {
274: // expected
275: }
276: try {
277: type.toDouble("foo");
278: fail("Expected AxionException");
279: } catch (AxionException e) {
280: // expected
281: }
282: try {
283: type.toDouble(Boolean.TRUE);
284: fail("Expected AxionException");
285: } catch (AxionException e) {
286: // expected
287: }
288: }
289:
290: public void testToFloat() throws Exception {
291: assertEquals(17f, type.toFloat(new Byte((byte) 17)), 0f);
292: assertEquals(17f, type.toFloat(new Short((short) 17)), 0f);
293: assertEquals(17f, type.toFloat(new Integer(17)), 0f);
294: assertEquals(17f, type.toFloat(new Long(17)), 0f);
295: assertEquals(17.35f, type.toFloat(new Float(17.35)), 0f);
296: assertEquals(17.35f, type.toFloat(new Double(17.35)), 0f);
297: assertEquals(17.35f, type.toFloat("17.35"), 0f);
298: try {
299: type.toFloat(null);
300: fail("Expected AxionException");
301: } catch (AxionException e) {
302: // expected
303: }
304: try {
305: type.toFloat("foo");
306: fail("Expected AxionException");
307: } catch (AxionException e) {
308: // expected
309: }
310: try {
311: type.toFloat(new Boolean(true));
312: fail("Expected AxionException");
313: } catch (AxionException e) {
314: // expected
315: }
316: }
317:
318: public void testToInt() throws Exception {
319: assertEquals(17, type.toInt(new Byte((byte) 17)));
320: assertEquals(17, type.toInt(new Short((short) 17)));
321: assertEquals(17, type.toInt(new Integer(17)));
322: assertEquals(17, type.toInt(new Long(17)));
323: assertEquals(17, type.toInt("17"));
324: assertEquals(17, type.toInt(new Float(17.35)));
325: assertEquals(17, type.toInt(new Double(17.35)));
326: try {
327: type.toInt(null);
328: fail("Expected AxionException");
329: } catch (AxionException e) {
330: // expected
331: }
332: try {
333: type.toInt("17.35");
334: fail("Expected AxionException");
335: } catch (AxionException e) {
336: // expected
337: }
338: try {
339: type.toInt("foo");
340: fail("Expected AxionException");
341: } catch (AxionException e) {
342: // expected
343: }
344: try {
345: type.toInt(new Boolean(true));
346: fail("Expected AxionException");
347: } catch (AxionException e) {
348: // expected
349: }
350: }
351:
352: public void testToLong() throws Exception {
353: assertEquals(17, type.toLong(new Byte((byte) 17)));
354: assertEquals(17, type.toLong(new Short((short) 17)));
355: assertEquals(17, type.toLong(new Integer(17)));
356: assertEquals(17, type.toLong(new Long(17)));
357: assertEquals(17, type.toLong("17"));
358: assertEquals(17, type.toLong(new Float(17.35)));
359: assertEquals(17, type.toLong(new Double(17.35)));
360: try {
361: type.toLong(null);
362: fail("Expected AxionException");
363: } catch (AxionException e) {
364: // expected
365: }
366: try {
367: type.toLong("17.35");
368: fail("Expected AxionException");
369: } catch (AxionException e) {
370: // expected
371: }
372: try {
373: type.toLong("foo");
374: fail("Expected AxionException");
375: } catch (AxionException e) {
376: // expected
377: }
378: try {
379: type.toLong(new Boolean(true));
380: fail("Expected AxionException");
381: } catch (AxionException e) {
382: // expected
383: }
384: long millis = System.currentTimeMillis();
385: assertEquals(millis, type.toLong(String.valueOf(millis)));
386: assertEquals(millis, type.toLong(new Date(millis)));
387: assertEquals(millis, type.toLong(new Time(millis)));
388: // JDBC 2/3 and/or JDK 1.3/1.4 seem to differ slightly in
389: // the Timestamp millisecond resolution (JDK 1.3 gives time to seconds,
390: // JDK 1.4 to milliseconds). Here we'll trim both sides to allow the
391: // test to pass in both environments
392: long found = type.toLong(new Timestamp(millis));
393: assertEquals(millis - (millis % 1000L), found - (found % 1000L));
394: }
395:
396: public void testToShort() throws Exception {
397: assertEquals(17, type.toShort(new Byte((byte) 17)));
398: assertEquals(17, type.toShort(new Short((short) 17)));
399: assertEquals(17, type.toShort(new Integer(17)));
400: assertEquals(17, type.toShort(new Long(17)));
401: assertEquals(17, type.toShort("17"));
402: assertEquals(17, type.toShort(new Float(17.35)));
403: assertEquals(17, type.toShort(new Double(17.35)));
404: try {
405: type.toShort(null);
406: fail("Expected AxionException");
407: } catch (AxionException e) {
408: // expected
409: }
410: try {
411: type.toShort("17.35");
412: fail("Expected AxionException");
413: } catch (AxionException e) {
414: // expected
415: }
416: try {
417: type.toShort("foo");
418: fail("Expected AxionException");
419: } catch (AxionException e) {
420: // expected
421: }
422: try {
423: type.toShort(new Boolean(true));
424: fail("Expected AxionException");
425: } catch (AxionException e) {
426: // expected
427: }
428: }
429:
430: public void testToString() throws Exception {
431: assertEquals("17", type.toString(new Byte((byte) 17)));
432: assertEquals("17", type.toString(new Short((short) 17)));
433: assertEquals("17", type.toString(new Integer(17)));
434: assertEquals("17", type.toString(new Long(17)));
435: assertEquals("17", type.toString("17"));
436: assertEquals("17.35", type.toString(new Float(17.35)));
437: assertEquals("17.35", type.toString(new Double(17.35)));
438: assertEquals("17.35", type.toString("17.35"));
439: assertNull(type.toString(null));
440: assertEquals("foo", type.toString("foo"));
441: assertEquals("true", type.toString(new Boolean(true)));
442: assertEquals("false", type.toString(new Boolean(false)));
443: }
444:
445: public void testToDate() throws Exception {
446: assertNull(type.toDate(null));
447: long millis = System.currentTimeMillis();
448: Date date = new Date(millis);
449: assertEquals(date, type.toDate(date));
450: assertEquals(date, type.toDate(new Long(millis)));
451: try {
452: type.toDate("foo");
453: fail("Expected AxionException");
454: } catch (AxionException e) {
455: // expected
456: }
457: try {
458: type.toDate(new Boolean(true));
459: fail("Expected AxionException");
460: } catch (AxionException e) {
461: // expected
462: }
463: }
464:
465: public void testToTime() throws Exception {
466: assertNull(type.toTime(null));
467: long millis = System.currentTimeMillis();
468: Time time = new Time(millis);
469: assertEquals(time, type.toTime(time));
470: assertEquals(time, type.toTime(new Long(millis)));
471: try {
472: type.toTime("foo");
473: fail("Expected AxionException");
474: } catch (AxionException e) {
475: // expected
476: }
477: try {
478: type.toTime(new Boolean(true));
479: fail("Expected AxionException");
480: } catch (AxionException e) {
481: // expected
482: }
483: }
484:
485: public void testToTimestamp() throws Exception {
486: assertNull(type.toTimestamp(null));
487: long millis = System.currentTimeMillis();
488: Timestamp time = new Timestamp(millis);
489: assertEquals(time, type.toTimestamp(time));
490: assertEquals(time, type.toTimestamp(new Long(millis)));
491: try {
492: type.toTimestamp("foo");
493: fail("Expected AxionException");
494: } catch (AxionException e) {
495: // expected
496: }
497: try {
498: type.toTimestamp(new Boolean(true));
499: fail("Expected AxionException");
500: } catch (AxionException e) {
501: // expected
502: }
503: }
504:
505: public void testToClob() throws Exception {
506: assertNull(type.toClob(null));
507: assertNotNull(type.toClob("foo"));
508: assertNotNull(type.toClob(new Integer(17)));
509: StringClob expected = new StringClob("xyzzy");
510: assertSame(expected, type.toClob(expected)); // wouldn't really have to be the same, but this is easier than a comparision for now
511: }
512:
513: public void testToBlob() throws Exception {
514: assertNull(type.toBlob(null));
515: try {
516: type.toBlob(new Integer(17));
517: fail("Expected AxionException");
518: } catch (AxionException e) {
519: // expected
520: }
521: try {
522: type.toBlob(new Boolean(true));
523: fail("Expected AxionException");
524: } catch (AxionException e) {
525: // expected
526: }
527: ByteArrayBlob expected = new ByteArrayBlob("xyzzy".getBytes());
528: assertSame(expected, type.toBlob(expected)); // wouldn't really have to be the same, but this is easier than a comparision for now
529: }
530:
531: public void testMakeNewInstance() throws Exception {
532: DataType made = ((DataTypeFactory) type).makeNewInstance();
533: assertNotNull(made);
534: assertTrue(made instanceof AnyType);
535: assertTrue(made != type);
536: assertTrue(made != ((DataTypeFactory) type).makeNewInstance());
537: }
538:
539: public void testGetColumnDisplaySize() {
540: assertEquals(0, type.getColumnDisplaySize());
541: }
542: }
|