root/trunk/test/java/org/jcoderz/commons/types/DateTest.java

Revision 1011, 15.8 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.types;
34
35
36import java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.IOException;
39import java.io.ObjectInputStream;
40import java.io.ObjectOutputStream;
41import java.sql.Timestamp;
42import java.text.ParseException;
43import java.text.SimpleDateFormat;
44import junit.framework.TestCase;
45import org.jcoderz.commons.util.Constants;
46
47
48
49/**
50 * Test class to test the Date class.
51 *
52 */
53public class DateTest
54      extends TestCase
55{
56   /** Test value. */
57   static final long SOME_TIME_IN_MILLIES = 12345000001L;
58   /** Allowed difference for date now. */
59   static final long MAX_DIFF_FOR_NOW = 500L;
60   /** An other test value. */
61   static final long SOME_TIME_IN_MILLIES_2 = 123456L;
62
63   /** Successful test for "fromSqlDate". */
64   public void testFromSqlDate ()
65   {
66      final java.sql.Date sqlDate = new java.sql.Date(SOME_TIME_IN_MILLIES);
67      final Date date = new Date(SOME_TIME_IN_MILLIES);
68      assertEquals("Value should not change when created via sql date.",
69            date, Date.fromSqlDate(sqlDate));
70   }
71
72   /** Successful test for "fromSqlTimestamp". */
73   public void testFromSqlTimestamp ()
74   {
75      final Timestamp sqlTimestamp = new Timestamp(SOME_TIME_IN_MILLIES);
76      final Date date = new Date(sqlTimestamp.getTime());
77      assertEquals("Value should not change when created via sql timestamp.",
78            date, Date.fromSqlTimestamp(sqlTimestamp));
79   }
80
81   /** Successful test for "now". */
82   public void testNow ()
83   {
84      final Date currentDate = new Date(System.currentTimeMillis());
85      final long diff = Date.now().getTime() - currentDate.getTime();
86      assertTrue("Now is to far from Date.now()", diff < MAX_DIFF_FOR_NOW);
87   }
88
89   /** Successful test for "nowPlus". */
90   public void testNowPlus ()
91   {
92      final Date currentDate
93            = new Date(System.currentTimeMillis() + SOME_TIME_IN_MILLIES_2);
94      final long diff
95            = Date.nowPlus(SOME_TIME_IN_MILLIES_2).getTime()
96               - currentDate.getTime();
97      assertTrue("NowPlus is to far from Date.now() + ...",
98            diff < MAX_DIFF_FOR_NOW);
99   }
100
101   /**
102    * Tests the method {@link Date#plus(long)}.
103    */
104   public void testPlus ()
105   {
106      final long time = System.currentTimeMillis();
107      final Date date = new Date(time);
108      final Date plusDate = new Date(time + SOME_TIME_IN_MILLIES);
109      assertTrue("plusDate " + plusDate + " is not equals to date.plus() "
110            + date.plus(SOME_TIME_IN_MILLIES),
111            plusDate.equals(date.plus(SOME_TIME_IN_MILLIES)));
112   }
113
114   /**
115    * Tests the method {@link Date#minus(long)}.
116    */
117   public void testMinus ()
118   {
119      final long time = System.currentTimeMillis();
120      final Date date = new Date(time);
121      final Date minusDate = new Date(time - SOME_TIME_IN_MILLIES);
122      assertTrue("minusDate " + minusDate + " is not equals to date.minus() "
123            + date.plus(SOME_TIME_IN_MILLIES),
124            minusDate.equals(date.minus(SOME_TIME_IN_MILLIES)));
125   }
126
127   /**
128    * Successful test for "fromString (String date, String pattern)".
129    * @throws ParseException if the test case fails.
130    */
131   public void testFromString ()
132         throws ParseException
133   {
134      // if date string is empty then null expected
135      final Date currentDate = new Date(System.currentTimeMillis());
136      final String pattern = "dd.MM.yyyy";
137      final String date = currentDate.toString(pattern);
138      assertEquals("Empty string should produce null result.",
139            null, Date.fromString("", pattern));
140
141      // if date string is not empty
142      final SimpleDateFormat dateFormat
143            = new SimpleDateFormat(pattern, Constants.SYSTEM_LOCALE);
144      dateFormat.setTimeZone(Date.TIME_ZONE);
145      final Date expected = Date.fromUtilDate(dateFormat.parse(date));
146      assertEquals("Valid date not parsed correctly.", expected,
147            Date.fromString(date, pattern));
148
149      final String time = "2004-09-04T10:04:22.000Z";
150      final Date timeDate = Date.fromString(time);
151      assertEquals("should be the same string representation", time,
152            timeDate.toString());
153
154      assertNull("Should be null for null argument.", Date.fromString(null));
155   }
156
157   /** Successful test for "toString". */
158   public void testToString ()
159   {
160      assertEquals("For day 0 string representation should be fix.",
161            "1970-01-01T00:00:00.000Z", Date.OLD_DATE.toString());
162   }
163
164   /** Successful test for "toString". */
165   public void testToStringWithMillies ()
166   {
167      assertEquals("For day 0 string representation should be fix.",
168            "1970-01-01T00:00:00.001Z", new Date(1L).toString());
169   }
170
171   /** Successful test for "toString". */
172   public void testDateString ()
173   {
174      assertEquals("String representation should be same for default pattern.",
175            "1970-01-01Z", Date.OLD_DATE.toDateString());
176   }
177
178   /** Successful test for "toUtilDate". */
179   public void testToUtilDate ()
180   {
181      final long time = System.currentTimeMillis();
182      final Date date = new Date(time);
183
184      assertEquals("Util Date differs from Date.",
185            new java.util.Date(time).getTime(), date.toUtilDate().getTime());
186   }
187
188  /** Successful test for "toSqlDate". */
189   public void testToSqlDate ()
190   {
191      final long time = System.currentTimeMillis();
192      final Date date = new Date(time);
193
194      assertEquals("Sql Date differs from Date.",
195            new java.sql.Date(time).getTime(), date.toSqlDate().getTime());
196   }
197
198  /** Successful test for "toSqlTimestamp". */
199   public void testToSqlTimestamp ()
200   {
201      final long time = System.currentTimeMillis();
202      final Date date = new Date(time);
203
204      assertEquals("Sql timestamp differs from Date.",
205            time, date.toSqlTimestamp().getTime());
206   }
207
208  /** Successful test for "equals". */
209   public void testEquals ()
210   {
211      final long time = System.currentTimeMillis();
212      final Date date = new Date(time);
213      Date date2 = new Date(time);
214      final Date date3 = new Date(SOME_TIME_IN_MILLIES);
215      assertEquals("Date equals created wrong result.",
216            true, date.equals(date2));
217      assertEquals("Date equals created wrong result.",
218            false, date.equals(date3));
219      date2 = new Date (SOME_TIME_IN_MILLIES - 1);
220      assertEquals("Date equals created wrong result.",
221            false, date.equals(date2));
222   }
223
224  /** Successful test for "compareTo". */
225   public void testCompareTo ()
226   {
227      final long time = System.currentTimeMillis();
228      final Date date = new Date(time);
229      Date date2 = new Date(time);
230      assertEquals("Date comparison created wrong result.",
231            0, date.compareTo(date2));
232
233      final Date date3 = new Date(time - SOME_TIME_IN_MILLIES_2);
234      assertEquals("Date comparison created wrong result.",
235            1, date.compareTo(date3));
236
237      date2 = new Date (time + SOME_TIME_IN_MILLIES_2);
238      assertEquals("Date comparison created wrong result.",
239            -1, date.compareTo(date2));
240   }
241
242   /**
243    * Simple test method to check basic serialization.
244    * @throws ClassNotFoundException in case of an test case error
245    * @throws IOException in case of an test case error
246    */
247   public void testSerialize ()
248         throws IOException, ClassNotFoundException
249   {
250      final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
251      final ObjectOutputStream objOut = new ObjectOutputStream(bOut);
252      ByteArrayInputStream bIn;
253      ObjectInputStream objIn;
254
255      final Date date = Date.now();
256
257      objOut.writeObject(date);
258      objOut.flush();
259      bIn = new ByteArrayInputStream(bOut.toByteArray());
260      objIn = new ObjectInputStream(bIn);
261      final Date dateRead = (Date) objIn.readObject();
262
263      assertEquals("Value changed during serialization.", date, dateRead);
264   }
265
266   /** Tests the sql timestamp handling. */
267   public void testSqlTimestamp ()
268   {
269      final Date refDate = Date.now();
270      final Timestamp test = new Timestamp(refDate.getTime());
271
272      assertEquals("Refdate changed in timestamp representation.",
273            refDate.getTime(), test.getTime());
274
275      final Date testDate = Date.fromSqlTimestamp(test);
276
277      assertEquals("Timestamp Value changed within Date type conversion.",
278            test, testDate.toSqlTimestamp());
279   }
280
281   /** Tests the sql timestamp handling. */
282   public void testSqlTimestampWithNanos ()
283   {
284      final Date refDate = Date.now();
285      final Timestamp test = new Timestamp(refDate.getTime());
286
287      test.setNanos(test.getNanos() + 1);
288
289      assertEquals("Refdate changed in timestamp representation.",
290            refDate.getTime(), test.getTime());
291
292      final Date testDate = Date.fromSqlTimestamp(test);
293      test.setNanos(test.getNanos() - 1);
294
295      assertEquals("Timestamp Value changed within Date type conversion.",
296            test, testDate.toSqlTimestamp());
297   }
298
299   /** Tests the {@link Date#getDaysSinceEpoch()} method. */
300   public void testGetDaysSinceEpoch ()
301   {
302      final int days = Date.getDaysSinceEpoch();
303      assertTrue("Result must be positive but was " + days, days > 0);
304   }
305
306   /** Tests the {@link Date#getDaysSinceEpoch(Date)} method. */
307   public void testGetDaysSinceEpochDate ()
308   {
309      final int days = Date.getDaysSinceEpoch(Date.now());
310      assertTrue("Result must be positive but was " + days, days > 0);
311      assertEquals("No days passed.", 0, Date.getDaysSinceEpoch(new Date(0)));
312   }
313
314   /**
315    * Method to test for {@link Date#hashCode()}.
316    */
317   public void testHashCode ()
318   {
319      assertEquals("two dates with the vaue should have the same "
320            + "hashCode", new Date(SOME_TIME_IN_MILLIES).hashCode(),
321            new Date(SOME_TIME_IN_MILLIES).hashCode());
322   }
323
324   /**
325    * Tests the method {@link Date#elapsedMillis()}.
326    */
327   public void testElapsed ()
328   {
329      final Date currentDate = new Date(System.currentTimeMillis());
330      final long diff = currentDate.elapsedMillis();
331      assertTrue("Diff is to far from Date.now()", diff < MAX_DIFF_FOR_NOW);
332      assertTrue("Diff is negative.", diff >= 0);
333   }
334
335   /**
336    * Tests the method {@link Date#elapsedMillis(Date)}.
337    */
338   public void testElapsedDate ()
339   {
340      final Date currentDate = new Date(System.currentTimeMillis());
341      final long diff = currentDate.elapsedMillis(
342            new Date(currentDate.getTime() + SOME_TIME_IN_MILLIES_2));
343      assertEquals("Diff is wrong", SOME_TIME_IN_MILLIES_2, diff);
344   }
345
346   /** Tests the after method. */
347   public void testAfter ()
348   {
349      final long time = System.currentTimeMillis();
350      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
351      final Date date2 = new Date(time);
352      final Date date3 = new Date(time);
353      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
354      assertTrue("After comparison result unexpected (this < other)",
355            date2.after(date1));
356      assertFalse("After comparison result unexpected (this == other)",
357            date2.after(date3));
358      assertFalse("After comparison result unexpected (this > other)",
359            date2.after(date4));
360   }
361
362   /** Tests the after or equal method. */
363   public void testAfterOrEqual ()
364   {
365      final long time = System.currentTimeMillis();
366      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
367      final Date date2 = new Date(time);
368      final Date date3 = new Date(time);
369      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
370      assertTrue("AfterOrEqual comparison result unexpected (this < other)",
371            date2.afterOrEqual(date1));
372      assertTrue("AfterOrEqual comparison result unexpected (this == other)",
373            date2.afterOrEqual(date3));
374      assertFalse("AfterOrEqual comparison result unexpected (this > other)",
375            date2.afterOrEqual(date4));
376   }
377
378   /** Tests the before method. */
379   public void testBefore ()
380   {
381      final long time = System.currentTimeMillis();
382      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
383      final Date date2 = new Date(time);
384      final Date date3 = new Date(time);
385      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
386      assertFalse("Before comparison result unexpected (this < other)",
387            date2.before(date1));
388      assertFalse("Before comparison result unexpected (this == other)",
389            date2.before(date3));
390      assertTrue("Before comparison result unexpected (this > other)",
391            date2.before(date4));
392   }
393
394   /** Tests the after method. */
395   public void testBeforeOrEqual ()
396   {
397      final long time = System.currentTimeMillis();
398      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
399      final Date date2 = new Date(time);
400      final Date date3 = new Date(time);
401      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
402      assertFalse("BeforeOrEqual comparison result unexpected (this < other)",
403            date2.beforeOrEqual(date1));
404      assertTrue("BeforeOrEqual comparison result unexpected (this == other)",
405            date2.beforeOrEqual(date3));
406      assertTrue("BeforeOrEqual comparison result unexpected (this > other)",
407            date2.beforeOrEqual(date4));
408   }
409
410   /** Tests the earliest method. */
411   public void testEarliest ()
412   {
413      final long time = System.currentTimeMillis();
414      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
415      final Date date2 = new Date(time);
416      final Date date3 = new Date(time);
417      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
418      assertEquals("Earliest result unexpected (a < b)", date1,
419            Date.earliest(date1, date2));
420      assertEquals("Earliest result unexpected (a == b)", date2,
421            Date.earliest(date2, date3));
422      assertEquals("Earliest result unexpected (a > b)", date2,
423            Date.earliest(date4, date2));
424   }
425
426   /** Tests the latest method. */
427   public void testLatest ()
428   {
429      final long time = System.currentTimeMillis();
430      final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
431      final Date date2 = new Date(time);
432      final Date date3 = new Date(time);
433      final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
434      assertEquals("Latest result unexpected (a < b)", date2,
435            Date.latest(date1, date2));
436      assertEquals("Latest result unexpected (a == b)", date2,
437            Date.latest(date2, date3));
438      assertEquals("Latest result unexpected (a > b)", date4,
439            Date.latest(date2, date4));
440   }
441}
Note: See TracBrowser for help on using the browser.