001: /*
002: * $Id: TestTimestampType.java,v 1.20 2005/08/24 00:37:26 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.io.IOException;
048: import java.sql.Timestamp;
049: import java.text.DateFormat;
050: import java.util.Date;
051:
052: import junit.framework.Test;
053: import junit.framework.TestSuite;
054:
055: import org.axiondb.AxionException;
056: import org.axiondb.DataType;
057:
058: /**
059: * @version $Revision: 1.20 $ $Date: 2005/08/24 00:37:26 $
060: * @author Chuck Burdick
061: */
062: public class TestTimestampType extends BaseDataTypeTest {
063: private long now = System.currentTimeMillis();
064:
065: //------------------------------------------------------------ Conventional
066:
067: public TestTimestampType(String testName) {
068: super (testName);
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new TestSuite(TestTimestampType.class);
073: return suite;
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: private DataType type = null;
079:
080: public void setUp() throws Exception {
081: super .setUp();
082: TimestampType.setTimeZone("GMT");
083: type = new TimestampType();
084:
085: }
086:
087: public void tearDown() throws Exception {
088: super .tearDown();
089: type = null;
090: }
091:
092: //------------------------------------------------------------------- Super
093:
094: protected DataType getDataType() {
095: return type;
096: }
097:
098: //------------------------------------------------------------------- Tests
099:
100: public void testToTime() throws Exception {
101: assertNotNull(getDataType().toTime(new Date()));
102: }
103:
104: public void testToBigDecimal() throws Exception {
105: assertNotNull(getDataType().toBigDecimal(new Date()));
106: }
107:
108: public void testToString() throws Exception {
109: assertNotNull(getDataType().toString(new Date()));
110: }
111:
112: public void testGetPrecision() throws Exception {
113: assertEquals(29, getDataType().getPrecision());
114: assertEquals(10, getDataType().getPrecisionRadix());
115: }
116:
117: public void testGetColumnDisplaySize() throws Exception {
118: assertEquals(29, getDataType().getColumnDisplaySize());
119: }
120:
121: public void testAccepts() throws Exception {
122: assertTrue("Should accept Long", type.accepts(new Long(now)));
123: assertTrue("Should accept util Date", type
124: .accepts(new java.util.Date()));
125: assertTrue("Should accept sql Date", type
126: .accepts(new java.sql.Date(now)));
127: assertTrue("Should accept sql Time", type
128: .accepts(new java.sql.Time(now)));
129: assertTrue("Should accept Timestamp", type
130: .accepts(new Timestamp(now)));
131: assertTrue("Should accept String", type.accepts("2002-08-01"));
132: assertTrue("Should not accept Object", !type
133: .accepts(new Object()));
134: }
135:
136: public void testSupportsSuccessor() {
137: assertTrue(type.supportsSuccessor());
138: }
139:
140: public void testConvertObject() {
141: try {
142: type.convert(new Object());
143: fail("Expected AxionException");
144: } catch (AxionException e) {
145: // expected
146: }
147: try {
148: type.convert("this is not a date");
149: fail("Expected AxionException");
150: } catch (AxionException e) {
151: // expected
152: }
153: try {
154: type.toDate(new Object());
155: fail("Expected AxionException");
156: } catch (AxionException e) {
157: // expected
158: }
159: try {
160: type.toTimestamp(new Object());
161: fail("Expected AxionException");
162: } catch (AxionException e) {
163: // expected
164: }
165: }
166:
167: public void testConvertNumberTypes() throws AxionException {
168: assertEquals(new Timestamp(now), type.convert(new Long(now)));
169: }
170:
171: public void testConvertDateTypes() throws AxionException {
172: Timestamp expected = new Timestamp(now);
173: assertEquals(expected, type.convert(new java.util.Date(now)));
174: assertEquals(expected, type.convert(new java.sql.Date(now)));
175: assertEquals(expected, type.convert(new java.sql.Time(now)));
176:
177: expected.setNanos(123);
178: assertEquals(expected, type.convert(expected));
179: }
180:
181: public void testToDate() throws Exception {
182: Object result = type.toDate(new Timestamp(now));
183: assertNotNull("Should get object back", result);
184: assertTrue("Should get Date back",
185: result instanceof java.sql.Date);
186: }
187:
188: public void testToTimestamp() throws Exception {
189: Object result = type.toTimestamp(new Timestamp(now));
190: assertNotNull("Should get object back", result);
191: assertTrue("Should get Timestamp back",
192: result instanceof java.sql.Timestamp);
193: }
194:
195: public void testConvert24HourString() throws Exception {
196: // August 1, 2002 12:00 AM GMT
197: Timestamp expected = new Timestamp(32 * 365 * 24 * 60 * 60
198: * 1000L
199: + //year
200: 8 * 24 * 60 * 60 * 1000L
201: + // leap years '72,'76,'80,'84,'88,'92,'96,2000
202: (31 + 28 + 31 + 30 + 31 + 30 + 31) * 24 * 60 * 60
203: * 1000L + // August 1
204: 15 * (60 * 60 * 1000L) + // hours
205: 18 * (60 * 1000L) + // minutes
206: 29 * (1000L) // seconds
207: );
208: assertEquals(expected, type.convert("2002-08-01 15:18:29"));
209: }
210:
211: public void testConvertStringTypes() throws Exception {
212: // August 1, 2002 12:00 AM GMT
213: Timestamp expected = new Timestamp(32 * 365 * 24 * 60 * 60
214: * 1000L
215: + //year
216: 8 * 24 * 60 * 60 * 1000L
217: + // leap years '72,'76,'80,'84,'88,'92,'96,2000
218: (31 + 28 + 31 + 30 + 31 + 30 + 31) * 24 * 60 * 60
219: * 1000L + // August
220: 0 // time
221: );
222: assertEquals("Should accept", expected, type
223: .convert("2002-08-01"));
224:
225: // August 1, 2002 12:00 PM CDT
226: expected = new Timestamp(expected.getTime() + 12 * 60 * 60
227: * 1000L // time (daylight savings)
228: );
229: DateFormat fmt = DateFormat.getDateTimeInstance(
230: DateFormat.LONG, DateFormat.LONG, TimestampType.LOCALE);
231: fmt.setTimeZone(TimestampType.getTimeZone());
232: assertEquals("Make sure 'expected' is as expected",
233: "01 August 2002 12:00:00 GMT", fmt.format(expected));
234:
235: String toConvert = type.toString(expected);
236: assertEquals("Check format", "2002-08-01 12:00:00.000",
237: toConvert);
238:
239: // August 1, 2002 12:00:00.001 PM CDT
240: expected = new Timestamp(expected.getTime() + 1L // 1 millisecond
241: );
242: toConvert = type.toString(expected);
243: assertEquals("Check format", "2002-08-01 12:00:00.001",
244: toConvert);
245:
246: // August 1, 2002 12:00:00.050 PM CDT
247: expected = new Timestamp(expected.getTime() + 49L // 49 milliseconds
248: );
249: toConvert = type.toString(expected);
250: assertEquals("Check format", "2002-08-01 12:00:00.050",
251: toConvert);
252: assertEquals("Should be identical", expected, type
253: .convert("2002-08-01 12:00:00.050"));
254:
255: // August 1, 2002 12:00:00.050 PM CDT
256: expected = new Timestamp(expected.getTime() + 100L // 100 milliseconds
257: );
258: toConvert = type.toString(expected);
259: assertEquals("Check format", "2002-08-01 12:00:00.150",
260: toConvert);
261: assertEquals("Should be identical", expected, type
262: .convert("2002-08-01 12:00:00.150"));
263: }
264:
265: public void testTimestampNanoProps() {
266: Timestamp foo = new Timestamp(now);
267: int[] good = { 0, 1, 999, 1000, 999999, 1000000, 999999999 };
268: int[] bad = { -1, 1000000000 };
269:
270: for (int i = 0; i < good.length; i++) {
271: foo.setNanos(good[i]);
272: assertEquals("Should get nanos back", good[i], foo
273: .getNanos());
274: }
275:
276: for (int i = 0; i < bad.length; i++) {
277: try {
278: foo.setNanos(bad[i]);
279: fail("Should not be able to set nanos to " + bad[i]);
280: } catch (Exception e) {
281: // expected
282: }
283: }
284: }
285:
286: public void testCompare() throws Exception {
287: Object[] list = new Object[] {
288: new java.util.Date(Long.MIN_VALUE),
289: new java.util.Date(-1L),
290: new java.util.Date(0L),
291: new java.util.Date(1L),
292: java.sql.Date.valueOf("2003-11-10"),
293: java.sql.Date.valueOf("2003-11-11"),
294: java.sql.Timestamp.valueOf("2003-11-11 12:34:56.1"),
295: java.sql.Timestamp.valueOf("2003-11-11 12:34:56.2"),
296: java.sql.Timestamp
297: .valueOf("2003-11-11 12:34:56.200001"),
298: java.sql.Timestamp.valueOf("2003-11-11 12:34:57"),
299: java.sql.Date.valueOf("2003-11-12"),
300: new java.util.Date(Long.MAX_VALUE) };
301: compareEm(list);
302: }
303:
304: public void testWriteReadNonNull() throws Exception {
305: Timestamp orig = new Timestamp(now);
306: orig.setNanos(123);
307: ByteArrayOutputStream buf = new ByteArrayOutputStream();
308: type.write(orig, new DataOutputStream(buf));
309: Object read = type.read(new DataInputStream(
310: new ByteArrayInputStream(buf.toByteArray())));
311: assertEquals(orig, read);
312: }
313:
314: public void testWriteReadSeveral() throws Exception {
315: Timestamp[] data = {
316: new Timestamp(System.currentTimeMillis()),
317: // 06:34:12 am
318: new Timestamp(6 * 60 * 60 * 1000 + 34 * 60 * 1000 + 12
319: * 1000), null, new Timestamp(0L), null };
320: ByteArrayOutputStream out = new ByteArrayOutputStream();
321: for (int i = 0; i < data.length; i++) {
322: type.write(data[i], new DataOutputStream(out));
323: }
324: DataInputStream in = new DataInputStream(
325: new ByteArrayInputStream(out.toByteArray()));
326: for (int i = 0; i < data.length; i++) {
327: Object read = type.read(in);
328: if (null == data[i]) {
329: assertNull(read);
330: } else {
331: assertEquals(data[i], read);
332: }
333: }
334: }
335:
336: public void testInvalidWriteFails() throws Exception {
337: ByteArrayOutputStream out = new ByteArrayOutputStream();
338: try {
339: type.write(new Object(), new DataOutputStream(out));
340: } catch (IOException expected) {
341: // Expected.
342: }
343: }
344:
345: public void testSuccessor() {
346: Timestamp value = new Timestamp(System.currentTimeMillis());
347: Timestamp next = (Timestamp) type.successor(value);
348: assertTrue("Should be greater", value.before(next));
349: assertEquals("Should get next nanos", value.getNanos() + 1,
350: next.getNanos());
351:
352: value.setNanos(999999999);
353: next = (Timestamp) type.successor(value);
354: assertTrue("Should be greater", value.before(next));
355: assertEquals("Should get next nanos", 0, next.getNanos());
356: }
357:
358: public void testInvalidConversion() throws Exception {
359: try {
360: type.toBigDecimal(new Object());
361: fail("Expected AxionException");
362: } catch (AxionException e) {
363: // Expected.
364: }
365:
366: try {
367: type.toDate(new Object());
368: fail("Expected AxionException");
369: } catch (AxionException e) {
370: // Expected.
371: }
372:
373: try {
374: type.toString(new Object());
375: fail("Expected AxionException");
376: } catch (AxionException e) {
377: // Expected.
378: }
379:
380: try {
381: type.toTime(new Object());
382: fail("Expected AxionException");
383: } catch (AxionException e) {
384: // Expected.
385: }
386:
387: try {
388: type.toTimestamp(new Object());
389: fail("Expected AxionException");
390: } catch (AxionException e) {
391: // Expected.
392: }
393:
394: }
395: }
|