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