Project Report: fawkez

Packagesummary org.jcoderz.commons.types

org.jcoderz.commons.types.DateTest

LineHitsNoteSource
1  /*
2   * $Id: DateTest.java 1011 2008-06-16 17:57:36Z amandel $
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   */
33  package org.jcoderz.commons.types;
34  
35  
36  import java.io.ByteArrayInputStream;
37  import java.io.ByteArrayOutputStream;
38  import java.io.IOException;
39  import java.io.ObjectInputStream;
40  import java.io.ObjectOutputStream;
41  import java.sql.Timestamp;
42  import java.text.ParseException;
43  import java.text.SimpleDateFormat;
44  import junit.framework.TestCase;
45  import org.jcoderz.commons.util.Constants;
46  
47  
48  
49  /**
50   * Test class to test the Date class.
51   *
52   */
53100(1)public 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     {
66100       final java.sql.Date sqlDate = new java.sql.Date(SOME_TIME_IN_MILLIES);
67100       final Date date = new Date(SOME_TIME_IN_MILLIES);
68100       assertEquals("Value should not change when created via sql date.",
69              date, Date.fromSqlDate(sqlDate));
70100    }
71  
72     /** Successful test for "fromSqlTimestamp". */
73     public void testFromSqlTimestamp ()
74     {
75100       final Timestamp sqlTimestamp = new Timestamp(SOME_TIME_IN_MILLIES);
76100       final Date date = new Date(sqlTimestamp.getTime());
77100       assertEquals("Value should not change when created via sql timestamp.",
78              date, Date.fromSqlTimestamp(sqlTimestamp));
79100    }
80  
81     /** Successful test for "now". */
82     public void testNow ()
83     {
84100       final Date currentDate = new Date(System.currentTimeMillis());
85100       final long diff = Date.now().getTime() - currentDate.getTime();
8675       assertTrue("Now is to far from Date.now()", diff < MAX_DIFF_FOR_NOW);
87100    }
88  
89     /** Successful test for "nowPlus". */
90     public void testNowPlus ()
91     {
92100       final Date currentDate
93              = new Date(System.currentTimeMillis() + SOME_TIME_IN_MILLIES_2);
94100       final long diff
95              = Date.nowPlus(SOME_TIME_IN_MILLIES_2).getTime()
96                 - currentDate.getTime();
9775       assertTrue("NowPlus is to far from Date.now() + ...",
98              diff < MAX_DIFF_FOR_NOW);
99100    }
100  
101     /**
102      * Tests the method {@link Date#plus(long)}.
103      */
104     public void testPlus ()
105     {
106100       final long time = System.currentTimeMillis();
107100       final Date date = new Date(time);
108100       final Date plusDate = new Date(time + SOME_TIME_IN_MILLIES);
109100(2)      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)));
112100    }
113  
114     /**
115      * Tests the method {@link Date#minus(long)}.
116      */
117     public void testMinus ()
118     {
119100       final long time = System.currentTimeMillis();
120100       final Date date = new Date(time);
121100       final Date minusDate = new Date(time - SOME_TIME_IN_MILLIES);
122100(3)      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)));
125100    }
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
135100       final Date currentDate = new Date(System.currentTimeMillis());
136100       final String pattern = "dd.MM.yyyy";
137100       final String date = currentDate.toString(pattern);
138100       assertEquals("Empty string should produce null result.",
139              null, Date.fromString("", pattern));
140  
141        // if date string is not empty
142100       final SimpleDateFormat dateFormat
143              = new SimpleDateFormat(pattern, Constants.SYSTEM_LOCALE);
144100       dateFormat.setTimeZone(Date.TIME_ZONE);
145100       final Date expected = Date.fromUtilDate(dateFormat.parse(date));
146100       assertEquals("Valid date not parsed correctly.", expected,
147              Date.fromString(date, pattern));
148  
149100       final String time = "2004-09-04T10:04:22.000Z";
150100       final Date timeDate = Date.fromString(time);
151100       assertEquals("should be the same string representation", time,
152              timeDate.toString());
153  
154100       assertNull("Should be null for null argument.", Date.fromString(null));
155100    }
156  
157     /** Successful test for "toString". */
158     public void testToString ()
159     {
160100       assertEquals("For day 0 string representation should be fix.",
161              "1970-01-01T00:00:00.000Z", Date.OLD_DATE.toString());
162100    }
163  
164     /** Successful test for "toString". */
165     public void testToStringWithMillies ()
166     {
167100       assertEquals("For day 0 string representation should be fix.",
168              "1970-01-01T00:00:00.001Z", new Date(1L).toString());
169100    }
170  
171     /** Successful test for "toString". */
172     public void testDateString ()
173     {
174100       assertEquals("String representation should be same for default pattern.",
175              "1970-01-01Z", Date.OLD_DATE.toDateString());
176100    }
177  
178     /** Successful test for "toUtilDate". */
179     public void testToUtilDate ()
180     {
181100       final long time = System.currentTimeMillis();
182100       final Date date = new Date(time);
183  
184100       assertEquals("Util Date differs from Date.",
185              new java.util.Date(time).getTime(), date.toUtilDate().getTime());
186100    }
187  
188    /** Successful test for "toSqlDate". */
189     public void testToSqlDate ()
190     {
191100       final long time = System.currentTimeMillis();
192100       final Date date = new Date(time);
193  
194100       assertEquals("Sql Date differs from Date.",
195              new java.sql.Date(time).getTime(), date.toSqlDate().getTime());
196100    }
197  
198    /** Successful test for "toSqlTimestamp". */
199     public void testToSqlTimestamp ()
200     {
201100       final long time = System.currentTimeMillis();
202100       final Date date = new Date(time);
203  
204100       assertEquals("Sql timestamp differs from Date.",
205              time, date.toSqlTimestamp().getTime());
206100    }
207  
208    /** Successful test for "equals". */
209     public void testEquals ()
210     {
211100       final long time = System.currentTimeMillis();
212100       final Date date = new Date(time);
213100       Date date2 = new Date(time);
214100       final Date date3 = new Date(SOME_TIME_IN_MILLIES);
215100       assertEquals("Date equals created wrong result.",
216              true, date.equals(date2));
217100       assertEquals("Date equals created wrong result.",
218              false, date.equals(date3));
219100       date2 = new Date (SOME_TIME_IN_MILLIES - 1);
220100       assertEquals("Date equals created wrong result.",
221              false, date.equals(date2));
222100    }
223  
224    /** Successful test for "compareTo". */
225     public void testCompareTo ()
226     {
227100       final long time = System.currentTimeMillis();
228100       final Date date = new Date(time);
229100       Date date2 = new Date(time);
230100       assertEquals("Date comparison created wrong result.",
231              0, date.compareTo(date2));
232  
233100       final Date date3 = new Date(time - SOME_TIME_IN_MILLIES_2);
234100       assertEquals("Date comparison created wrong result.",
235              1, date.compareTo(date3));
236  
237100       date2 = new Date (time + SOME_TIME_IN_MILLIES_2);
238100       assertEquals("Date comparison created wrong result.",
239              -1, date.compareTo(date2));
240100    }
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     {
250100       final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
251100       final ObjectOutputStream objOut = new ObjectOutputStream(bOut);
252        ByteArrayInputStream bIn;
253        ObjectInputStream objIn;
254  
255100       final Date date = Date.now();
256  
257100       objOut.writeObject(date);
258100       objOut.flush();
259100       bIn = new ByteArrayInputStream(bOut.toByteArray());
260100       objIn = new ObjectInputStream(bIn);
261100       final Date dateRead = (Date) objIn.readObject();
262  
263100       assertEquals("Value changed during serialization.", date, dateRead);
264100    }
265  
266     /** Tests the sql timestamp handling. */
267     public void testSqlTimestamp ()
268     {
269100       final Date refDate = Date.now();
270100       final Timestamp test = new Timestamp(refDate.getTime());
271  
272100       assertEquals("Refdate changed in timestamp representation.",
273              refDate.getTime(), test.getTime());
274  
275100       final Date testDate = Date.fromSqlTimestamp(test);
276  
277100       assertEquals("Timestamp Value changed within Date type conversion.",
278              test, testDate.toSqlTimestamp());
279100    }
280  
281     /** Tests the sql timestamp handling. */
282     public void testSqlTimestampWithNanos ()
283     {
284100       final Date refDate = Date.now();
285100       final Timestamp test = new Timestamp(refDate.getTime());
286  
287100       test.setNanos(test.getNanos() + 1);
288  
289100       assertEquals("Refdate changed in timestamp representation.",
290              refDate.getTime(), test.getTime());
291  
292100       final Date testDate = Date.fromSqlTimestamp(test);
293100       test.setNanos(test.getNanos() - 1);
294  
295100       assertEquals("Timestamp Value changed within Date type conversion.",
296              test, testDate.toSqlTimestamp());
297100    }
298  
299     /** Tests the {@link Date#getDaysSinceEpoch()} method. */
300     public void testGetDaysSinceEpoch ()
301     {
302100       final int days = Date.getDaysSinceEpoch();
30375       assertTrue("Result must be positive but was " + days, days > 0);
304100    }
305  
306     /** Tests the {@link Date#getDaysSinceEpoch(Date)} method. */
307     public void testGetDaysSinceEpochDate ()
308     {
309100       final int days = Date.getDaysSinceEpoch(Date.now());
31075       assertTrue("Result must be positive but was " + days, days > 0);
311100       assertEquals("No days passed.", 0, Date.getDaysSinceEpoch(new Date(0)));
312100    }
313  
314     /**
315      * Method to test for {@link Date#hashCode()}.
316      */
317     public void testHashCode ()
318     {
319100       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());
322100    }
323  
324     /**
325      * Tests the method {@link Date#elapsedMillis()}.
326      */
327     public void testElapsed ()
328     {
329100       final Date currentDate = new Date(System.currentTimeMillis());
330100       final long diff = currentDate.elapsedMillis();
33175       assertTrue("Diff is to far from Date.now()", diff < MAX_DIFF_FOR_NOW);
33275       assertTrue("Diff is negative.", diff >= 0);
333100    }
334  
335     /**
336      * Tests the method {@link Date#elapsedMillis(Date)}.
337      */
338     public void testElapsedDate ()
339     {
340100       final Date currentDate = new Date(System.currentTimeMillis());
341100       final long diff = currentDate.elapsedMillis(
342              new Date(currentDate.getTime() + SOME_TIME_IN_MILLIES_2));
343100       assertEquals("Diff is wrong", SOME_TIME_IN_MILLIES_2, diff);
344100    }
345  
346     /** Tests the after method. */
347     public void testAfter ()
348     {
349100       final long time = System.currentTimeMillis();
350100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
351100       final Date date2 = new Date(time);
352100       final Date date3 = new Date(time);
353100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
354100       assertTrue("After comparison result unexpected (this < other)",
355              date2.after(date1));
356100       assertFalse("After comparison result unexpected (this == other)",
357              date2.after(date3));
358100       assertFalse("After comparison result unexpected (this > other)",
359              date2.after(date4));
360100    }
361  
362     /** Tests the after or equal method. */
363     public void testAfterOrEqual ()
364     {
365100       final long time = System.currentTimeMillis();
366100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
367100       final Date date2 = new Date(time);
368100       final Date date3 = new Date(time);
369100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
370100       assertTrue("AfterOrEqual comparison result unexpected (this < other)",
371              date2.afterOrEqual(date1));
372100       assertTrue("AfterOrEqual comparison result unexpected (this == other)",
373              date2.afterOrEqual(date3));
374100       assertFalse("AfterOrEqual comparison result unexpected (this > other)",
375              date2.afterOrEqual(date4));
376100    }
377  
378     /** Tests the before method. */
379     public void testBefore ()
380     {
381100       final long time = System.currentTimeMillis();
382100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
383100       final Date date2 = new Date(time);
384100       final Date date3 = new Date(time);
385100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
386100       assertFalse("Before comparison result unexpected (this < other)",
387              date2.before(date1));
388100       assertFalse("Before comparison result unexpected (this == other)",
389              date2.before(date3));
390100       assertTrue("Before comparison result unexpected (this > other)",
391              date2.before(date4));
392100    }
393  
394     /** Tests the after method. */
395     public void testBeforeOrEqual ()
396     {
397100       final long time = System.currentTimeMillis();
398100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
399100       final Date date2 = new Date(time);
400100       final Date date3 = new Date(time);
401100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
402100       assertFalse("BeforeOrEqual comparison result unexpected (this < other)",
403              date2.beforeOrEqual(date1));
404100       assertTrue("BeforeOrEqual comparison result unexpected (this == other)",
405              date2.beforeOrEqual(date3));
406100       assertTrue("BeforeOrEqual comparison result unexpected (this > other)",
407              date2.beforeOrEqual(date4));
408100    }
409  
410     /** Tests the earliest method. */
411     public void testEarliest ()
412     {
413100       final long time = System.currentTimeMillis();
414100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
415100       final Date date2 = new Date(time);
416100       final Date date3 = new Date(time);
417100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
418100       assertEquals("Earliest result unexpected (a < b)", date1,
419              Date.earliest(date1, date2));
420100       assertEquals("Earliest result unexpected (a == b)", date2,
421              Date.earliest(date2, date3));
422100       assertEquals("Earliest result unexpected (a > b)", date2,
423              Date.earliest(date4, date2));
424100    }
425  
426     /** Tests the latest method. */
427     public void testLatest ()
428     {
429100       final long time = System.currentTimeMillis();
430100       final Date date1 = new Date(time - SOME_TIME_IN_MILLIES_2);
431100       final Date date2 = new Date(time);
432100       final Date date3 = new Date(time);
433100       final Date date4 = new Date(time + SOME_TIME_IN_MILLIES_2);
434100       assertEquals("Latest result unexpected (a < b)", date2,
435              Date.latest(date1, date2));
436100       assertEquals("Latest result unexpected (a == b)", date2,
437              Date.latest(date2, date3));
438100       assertEquals("Latest result unexpected (a > b)", date4,
439              Date.latest(date2, date4));
440100    }
441  }

Findings in this File

c (1) 53 : 0 Type Javadoc comment is missing an @author tag.
c (2) 109 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
c (3) 122 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))