001 /*
002 * Copyright 2001-2005 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time;
017
018 import java.io.ByteArrayInputStream;
019 import java.io.ByteArrayOutputStream;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.lang.reflect.Modifier;
023 import java.util.ArrayList;
024 import java.util.Calendar;
025 import java.util.Collections;
026 import java.util.Comparator;
027 import java.util.Date;
028 import java.util.List;
029
030 import junit.framework.TestCase;
031 import junit.framework.TestSuite;
032
033 import org.joda.time.chrono.ISOChronology;
034 /**
035 * This class is a Junit unit test for the
036 * org.joda.time.DateTimeComparator class.
037 *
038 * @author Guy Allard
039 */
040 public class TestDateTimeComparator extends TestCase {
041
042 private static final Chronology ISO = ISOChronology.getInstance();
043
044 public static void main(String[] args) {
045 junit.textui.TestRunner.run(suite());
046 }
047
048 public static TestSuite suite() {
049 return new TestSuite(TestDateTimeComparator.class);
050 }
051
052 public TestDateTimeComparator(String name) {
053 super(name);
054 }
055
056 /**
057 * A reference to a DateTime object.
058 */
059 DateTime aDateTime = null;
060 /**
061 * A reference to a DateTime object.
062 */
063 DateTime bDateTime = null;
064 /**
065 * A reference to a DateTimeComparator object
066 * (a Comparator) for millis of seconds.
067 */
068 Comparator cMillis = null;
069 /**
070 * A reference to a DateTimeComparator object
071 * (a Comparator) for seconds.
072 */
073 Comparator cSecond = null;
074 /**
075 * A reference to a DateTimeComparator object
076 * (a Comparator) for minutes.
077 */
078 Comparator cMinute = null;
079 /**
080 * A reference to a DateTimeComparator object
081 * (a Comparator) for hours.
082 */
083 Comparator cHour = null;
084 /**
085 * A reference to a DateTimeComparator object
086 * (a Comparator) for day of the week.
087 */
088 Comparator cDayOfWeek = null;
089 /**
090 * A reference to a DateTimeComparator object
091 * (a Comparator) for day of the month.
092 */
093 Comparator cDayOfMonth = null;
094 /**
095 * A reference to a DateTimeComparator object
096 * (a Comparator) for day of the year.
097 */
098 Comparator cDayOfYear = null;
099 /**
100 * A reference to a DateTimeComparator object
101 * (a Comparator) for week of the weekyear.
102 */
103 Comparator cWeekOfWeekyear = null;
104 /**
105 * A reference to a DateTimeComparator object
106 * (a Comparator) for year given a week of the year.
107 */
108 Comparator cWeekyear = null;
109 /**
110 * A reference to a DateTimeComparator object
111 * (a Comparator) for months.
112 */
113 Comparator cMonth = null;
114 /**
115 * A reference to a DateTimeComparator object
116 * (a Comparator) for year.
117 */
118 Comparator cYear = null;
119 /**
120 * A reference to a DateTimeComparator object
121 * (a Comparator) for the date portion of an
122 * object.
123 */
124 Comparator cDate = null;
125 /**
126 * A reference to a DateTimeComparator object
127 * (a Comparator) for the time portion of an
128 * object.
129 */
130 Comparator cTime = null;
131 /**
132 * Junit <code>setUp()</code> method.
133 */
134 public void setUp() /* throws Exception */ {
135 Chronology chrono = ISOChronology.getInstanceUTC();
136
137 // super.setUp();
138 // Obtain comparator's
139 cMillis = DateTimeComparator.getInstance(null, DateTimeFieldType.secondOfMinute());
140 cSecond = DateTimeComparator.getInstance(DateTimeFieldType.secondOfMinute(), DateTimeFieldType.minuteOfHour());
141 cMinute = DateTimeComparator.getInstance(DateTimeFieldType.minuteOfHour(), DateTimeFieldType.hourOfDay());
142 cHour = DateTimeComparator.getInstance(DateTimeFieldType.hourOfDay(), DateTimeFieldType.dayOfYear());
143 cDayOfWeek = DateTimeComparator.getInstance(DateTimeFieldType.dayOfWeek(), DateTimeFieldType.weekOfWeekyear());
144 cDayOfMonth = DateTimeComparator.getInstance(DateTimeFieldType.dayOfMonth(), DateTimeFieldType.monthOfYear());
145 cDayOfYear = DateTimeComparator.getInstance(DateTimeFieldType.dayOfYear(), DateTimeFieldType.year());
146 cWeekOfWeekyear = DateTimeComparator.getInstance(DateTimeFieldType.weekOfWeekyear(), DateTimeFieldType.weekyear());
147 cWeekyear = DateTimeComparator.getInstance(DateTimeFieldType.weekyear());
148 cMonth = DateTimeComparator.getInstance(DateTimeFieldType.monthOfYear(), DateTimeFieldType.year());
149 cYear = DateTimeComparator.getInstance(DateTimeFieldType.year());
150 cDate = DateTimeComparator.getDateOnlyInstance();
151 cTime = DateTimeComparator.getTimeOnlyInstance();
152 }
153
154 /**
155 * Junit <code>tearDown()</code> method.
156 */
157 protected void tearDown() /* throws Exception */ {
158 // super.tearDown();
159 aDateTime = null;
160 bDateTime = null;
161 //
162 cMillis = null;
163 cSecond = null;
164 cMinute = null;
165 cHour = null;
166 cDayOfWeek = null;
167 cDayOfMonth = null;
168 cDayOfYear = null;
169 cWeekOfWeekyear = null;
170 cWeekyear = null;
171 cMonth = null;
172 cYear = null;
173 cDate = null;
174 cTime = null;
175 }
176
177 //-----------------------------------------------------------------------
178 public void testClass() {
179 assertEquals(true, Modifier.isPublic(DateTimeComparator.class.getModifiers()));
180 assertEquals(false, Modifier.isFinal(DateTimeComparator.class.getModifiers()));
181 assertEquals(1, DateTimeComparator.class.getDeclaredConstructors().length);
182 assertEquals(true, Modifier.isProtected(DateTimeComparator.class.getDeclaredConstructors()[0].getModifiers()));
183 }
184
185 //-----------------------------------------------------------------------
186 public void testStaticGetInstance() {
187 DateTimeComparator c = DateTimeComparator.getInstance();
188 assertEquals(null, c.getLowerLimit());
189 assertEquals(null, c.getUpperLimit());
190 assertEquals("DateTimeComparator[]", c.toString());
191 }
192 public void testStaticGetDateOnlyInstance() {
193 DateTimeComparator c = DateTimeComparator.getDateOnlyInstance();
194 assertEquals(DateTimeFieldType.dayOfYear(), c.getLowerLimit());
195 assertEquals(null, c.getUpperLimit());
196 assertEquals("DateTimeComparator[dayOfYear-]", c.toString());
197
198 assertSame(DateTimeComparator.getDateOnlyInstance(), DateTimeComparator.getDateOnlyInstance());
199 }
200 public void testStaticGetTimeOnlyInstance() {
201 DateTimeComparator c = DateTimeComparator.getTimeOnlyInstance();
202 assertEquals(null, c.getLowerLimit());
203 assertEquals(DateTimeFieldType.dayOfYear(), c.getUpperLimit());
204 assertEquals("DateTimeComparator[-dayOfYear]", c.toString());
205
206 assertSame(DateTimeComparator.getTimeOnlyInstance(), DateTimeComparator.getTimeOnlyInstance());
207 }
208 public void testStaticGetInstanceLower() {
209 DateTimeComparator c = DateTimeComparator.getInstance(DateTimeFieldType.hourOfDay());
210 assertEquals(DateTimeFieldType.hourOfDay(), c.getLowerLimit());
211 assertEquals(null, c.getUpperLimit());
212 assertEquals("DateTimeComparator[hourOfDay-]", c.toString());
213
214 c = DateTimeComparator.getInstance(null);
215 assertSame(DateTimeComparator.getInstance(), c);
216 }
217 public void testStaticGetInstanceLowerUpper() {
218 DateTimeComparator c = DateTimeComparator.getInstance(DateTimeFieldType.hourOfDay(), DateTimeFieldType.dayOfYear());
219 assertEquals(DateTimeFieldType.hourOfDay(), c.getLowerLimit());
220 assertEquals(DateTimeFieldType.dayOfYear(), c.getUpperLimit());
221 assertEquals("DateTimeComparator[hourOfDay-dayOfYear]", c.toString());
222
223 c = DateTimeComparator.getInstance(DateTimeFieldType.hourOfDay(), DateTimeFieldType.hourOfDay());
224 assertEquals(DateTimeFieldType.hourOfDay(), c.getLowerLimit());
225 assertEquals(DateTimeFieldType.hourOfDay(), c.getUpperLimit());
226 assertEquals("DateTimeComparator[hourOfDay]", c.toString());
227
228 c = DateTimeComparator.getInstance(null, null);
229 assertSame(DateTimeComparator.getInstance(), c);
230
231 c = DateTimeComparator.getInstance(DateTimeFieldType.dayOfYear(), null);
232 assertSame(DateTimeComparator.getDateOnlyInstance(), c);
233
234 c = DateTimeComparator.getInstance(null, DateTimeFieldType.dayOfYear());
235 assertSame(DateTimeComparator.getTimeOnlyInstance(), c);
236 }
237
238 //-----------------------------------------------------------------------
239 public void testEqualsHashCode() {
240 DateTimeComparator c1 = DateTimeComparator.getInstance();
241 assertEquals(true, c1.equals(c1));
242 assertEquals(false, c1.equals(null));
243 assertEquals(true, c1.hashCode() == c1.hashCode());
244
245 DateTimeComparator c2 = DateTimeComparator.getTimeOnlyInstance();
246 assertEquals(true, c2.equals(c2));
247 assertEquals(false, c2.equals(c1));
248 assertEquals(false, c1.equals(c2));
249 assertEquals(false, c2.equals(null));
250 assertEquals(false, c1.hashCode() == c2.hashCode());
251
252 DateTimeComparator c3 = DateTimeComparator.getTimeOnlyInstance();
253 assertEquals(true, c3.equals(c3));
254 assertEquals(false, c3.equals(c1));
255 assertEquals(true, c3.equals(c2));
256 assertEquals(false, c1.equals(c3));
257 assertEquals(true, c2.equals(c3));
258 assertEquals(false, c1.hashCode() == c3.hashCode());
259 assertEquals(true, c2.hashCode() == c3.hashCode());
260
261 DateTimeComparator c4 = DateTimeComparator.getDateOnlyInstance();
262 assertEquals(false, c4.hashCode() == c3.hashCode());
263 }
264
265 //-----------------------------------------------------------------------
266 public void testSerialization1() throws Exception {
267 DateTimeField f = ISO.dayOfYear();
268 f.toString();
269 DateTimeComparator c = DateTimeComparator.getInstance(DateTimeFieldType.hourOfDay(), DateTimeFieldType.dayOfYear());
270
271 ByteArrayOutputStream baos = new ByteArrayOutputStream();
272 ObjectOutputStream oos = new ObjectOutputStream(baos);
273 oos.writeObject(c);
274 byte[] bytes = baos.toByteArray();
275 oos.close();
276
277 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
278 ObjectInputStream ois = new ObjectInputStream(bais);
279 DateTimeComparator result = (DateTimeComparator) ois.readObject();
280 ois.close();
281
282 assertEquals(c, result);
283 }
284
285 //-----------------------------------------------------------------------
286 public void testSerialization2() throws Exception {
287 DateTimeComparator c = DateTimeComparator.getInstance();
288
289 ByteArrayOutputStream baos = new ByteArrayOutputStream();
290 ObjectOutputStream oos = new ObjectOutputStream(baos);
291 oos.writeObject(c);
292 byte[] bytes = baos.toByteArray();
293 oos.close();
294
295 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
296 ObjectInputStream ois = new ObjectInputStream(bais);
297 DateTimeComparator result = (DateTimeComparator) ois.readObject();
298 ois.close();
299
300 assertSame(c, result);
301 }
302
303 //-----------------------------------------------------------------------
304 /**
305 * Test all basic comparator operation with DateTime objects.
306 */
307 public void testBasicComps1() {
308 aDateTime = new DateTime( System.currentTimeMillis(), DateTimeZone.UTC );
309 bDateTime = new DateTime( aDateTime.getMillis(), DateTimeZone.UTC );
310 assertEquals( "getMillis", aDateTime.getMillis(),
311 bDateTime.getMillis() );
312 assertEquals( "MILLIS", 0, cMillis.compare( aDateTime, bDateTime ) );
313 assertEquals( "SECOND", 0, cSecond.compare( aDateTime, bDateTime ) );
314 assertEquals( "MINUTE", 0, cMinute.compare( aDateTime, bDateTime ) );
315 assertEquals( "HOUR", 0, cHour.compare( aDateTime, bDateTime ) );
316 assertEquals( "DOW", 0, cDayOfWeek.compare( aDateTime, bDateTime ) );
317 assertEquals( "DOM", 0, cDayOfMonth.compare( aDateTime, bDateTime ) );
318 assertEquals( "DOY", 0, cDayOfYear.compare( aDateTime, bDateTime ) );
319 assertEquals( "WOW", 0, cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
320 assertEquals( "WY", 0, cWeekyear.compare( aDateTime, bDateTime ) );
321 assertEquals( "MONTH", 0, cMonth.compare( aDateTime, bDateTime ) );
322 assertEquals( "YEAR", 0, cYear.compare( aDateTime, bDateTime ) );
323 assertEquals( "DATE", 0, cDate.compare( aDateTime, bDateTime ) );
324 assertEquals( "TIME", 0, cTime.compare( aDateTime, bDateTime ) );
325 } // end of testBasicComps
326
327
328 /**
329 * Test all basic comparator operation with ReadableInstant objects.
330 */
331 public void testBasicComps2() {
332 ReadableInstant aDateTime = new DateTime( System.currentTimeMillis(), DateTimeZone.UTC );
333 ReadableInstant bDateTime = new DateTime( aDateTime.getMillis(), DateTimeZone.UTC );
334 assertEquals( "getMillis", aDateTime.getMillis(),
335 bDateTime.getMillis() );
336 assertEquals( "MILLIS", 0, cMillis.compare( aDateTime, bDateTime ) );
337 assertEquals( "SECOND", 0, cSecond.compare( aDateTime, bDateTime ) );
338 assertEquals( "MINUTE", 0, cMinute.compare( aDateTime, bDateTime ) );
339 assertEquals( "HOUR", 0, cHour.compare( aDateTime, bDateTime ) );
340 assertEquals( "DOW", 0, cDayOfWeek.compare( aDateTime, bDateTime ) );
341 assertEquals( "DOM", 0, cDayOfMonth.compare( aDateTime, bDateTime ) );
342 assertEquals( "DOY", 0, cDayOfYear.compare( aDateTime, bDateTime ) );
343 assertEquals( "WOW", 0, cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
344 assertEquals( "WY", 0, cWeekyear.compare( aDateTime, bDateTime ) );
345 assertEquals( "MONTH", 0, cMonth.compare( aDateTime, bDateTime ) );
346 assertEquals( "YEAR", 0, cYear.compare( aDateTime, bDateTime ) );
347 assertEquals( "DATE", 0, cDate.compare( aDateTime, bDateTime ) );
348 assertEquals( "TIME", 0, cTime.compare( aDateTime, bDateTime ) );
349 } // end of testBasicComps
350
351 /**
352 * Test all basic comparator operation with java Date objects.
353 */
354 public void testBasicComps3() {
355 Date aDateTime
356 = new Date( System.currentTimeMillis() );
357 Date bDateTime
358 = new Date( aDateTime.getTime() );
359 assertEquals( "MILLIS", 0, cMillis.compare( aDateTime, bDateTime ) );
360 assertEquals( "SECOND", 0, cSecond.compare( aDateTime, bDateTime ) );
361 assertEquals( "MINUTE", 0, cMinute.compare( aDateTime, bDateTime ) );
362 assertEquals( "HOUR", 0, cHour.compare( aDateTime, bDateTime ) );
363 assertEquals( "DOW", 0, cDayOfWeek.compare( aDateTime, bDateTime ) );
364 assertEquals( "DOM", 0, cDayOfMonth.compare( aDateTime, bDateTime ) );
365 assertEquals( "DOY", 0, cDayOfYear.compare( aDateTime, bDateTime ) );
366 assertEquals( "WOW", 0, cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
367 assertEquals( "WY", 0, cWeekyear.compare( aDateTime, bDateTime ) );
368 assertEquals( "MONTH", 0, cMonth.compare( aDateTime, bDateTime ) );
369 assertEquals( "YEAR", 0, cYear.compare( aDateTime, bDateTime ) );
370 assertEquals( "DATE", 0, cDate.compare( aDateTime, bDateTime ) );
371 assertEquals( "TIME", 0, cTime.compare( aDateTime, bDateTime ) );
372 } // end of testBasicComps
373
374 /**
375 * Test all basic comparator operation with Long objects.
376 */
377 public void testBasicComps4() {
378 Long aDateTime
379 = new Long( System.currentTimeMillis() );
380 Long bDateTime
381 = new Long( aDateTime.longValue() );
382 assertEquals( "MILLIS", 0, cMillis.compare( aDateTime, bDateTime ) );
383 assertEquals( "SECOND", 0, cSecond.compare( aDateTime, bDateTime ) );
384 assertEquals( "MINUTE", 0, cMinute.compare( aDateTime, bDateTime ) );
385 assertEquals( "HOUR", 0, cHour.compare( aDateTime, bDateTime ) );
386 assertEquals( "DOW", 0, cDayOfWeek.compare( aDateTime, bDateTime ) );
387 assertEquals( "DOM", 0, cDayOfMonth.compare( aDateTime, bDateTime ) );
388 assertEquals( "DOY", 0, cDayOfYear.compare( aDateTime, bDateTime ) );
389 assertEquals( "WOW", 0, cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
390 assertEquals( "WY", 0, cWeekyear.compare( aDateTime, bDateTime ) );
391 assertEquals( "MONTH", 0, cMonth.compare( aDateTime, bDateTime ) );
392 assertEquals( "YEAR", 0, cYear.compare( aDateTime, bDateTime ) );
393 assertEquals( "DATE", 0, cDate.compare( aDateTime, bDateTime ) );
394 assertEquals( "TIME", 0, cTime.compare( aDateTime, bDateTime ) );
395 } // end of testBasicComps
396
397 /**
398 * Test all basic comparator operation with Calendar objects.
399 */
400 public void testBasicComps5() {
401 Calendar aDateTime
402 = Calendar.getInstance(); // right now
403 Calendar bDateTime = aDateTime;
404 assertEquals( "MILLIS", 0, cMillis.compare( aDateTime, bDateTime ) );
405 assertEquals( "SECOND", 0, cSecond.compare( aDateTime, bDateTime ) );
406 assertEquals( "MINUTE", 0, cMinute.compare( aDateTime, bDateTime ) );
407 assertEquals( "HOUR", 0, cHour.compare( aDateTime, bDateTime ) );
408 assertEquals( "DOW", 0, cDayOfWeek.compare( aDateTime, bDateTime ) );
409 assertEquals( "DOM", 0, cDayOfMonth.compare( aDateTime, bDateTime ) );
410 assertEquals( "DOY", 0, cDayOfYear.compare( aDateTime, bDateTime ) );
411 assertEquals( "WOW", 0, cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
412 assertEquals( "WY", 0, cWeekyear.compare( aDateTime, bDateTime ) );
413 assertEquals( "MONTH", 0, cMonth.compare( aDateTime, bDateTime ) );
414 assertEquals( "YEAR", 0, cYear.compare( aDateTime, bDateTime ) );
415 assertEquals( "DATE", 0, cDate.compare( aDateTime, bDateTime ) );
416 assertEquals( "TIME", 0, cTime.compare( aDateTime, bDateTime ) );
417 } // end of testBasicComps
418
419
420 /**
421 * Test unequal comparisons with millis of second comparators.
422 */
423 public void testMillis() {
424 aDateTime = new DateTime( System.currentTimeMillis(), DateTimeZone.UTC );
425 bDateTime = new DateTime( aDateTime.getMillis() + 1, DateTimeZone.UTC );
426 assertEquals( "MillisM1", -1, cMillis.compare( aDateTime, bDateTime ) );
427 assertEquals( "MillisP1", 1, cMillis.compare( bDateTime, aDateTime ) );
428 } // end of testMillis
429
430 /**
431 * Test unequal comparisons with second comparators.
432 */
433 public void testSecond() {
434 aDateTime = getADate( "1969-12-31T23:59:58" );
435 bDateTime = getADate( "1969-12-31T23:50:59" );
436 assertEquals( "SecondM1a", -1, cSecond.compare( aDateTime, bDateTime ) );
437 assertEquals( "SecondP1a", 1, cSecond.compare( bDateTime, aDateTime ) );
438 aDateTime = getADate( "1970-01-01T00:00:00" );
439 bDateTime = getADate( "1970-01-01T00:00:01" );
440 assertEquals( "SecondM1b", -1, cSecond.compare( aDateTime, bDateTime ) );
441 assertEquals( "SecondP1b", 1, cSecond.compare( bDateTime, aDateTime ) );
442 } // end of testSecond
443
444 /**
445 * Test unequal comparisons with minute comparators.
446 */
447 public void testMinute() {
448 aDateTime = getADate( "1969-12-31T23:58:00" );
449 bDateTime = getADate( "1969-12-31T23:59:00" );
450 assertEquals( "MinuteM1a", -1, cMinute.compare( aDateTime, bDateTime ) );
451 assertEquals( "MinuteP1a", 1, cMinute.compare( bDateTime, aDateTime ) );
452 aDateTime = getADate( "1970-01-01T00:00:00" );
453 bDateTime = getADate( "1970-01-01T00:01:00" );
454 assertEquals( "MinuteM1b", -1, cMinute.compare( aDateTime, bDateTime ) );
455 assertEquals( "MinuteP1b", 1, cMinute.compare( bDateTime, aDateTime ) );
456 } // end of testMinute
457
458 /**
459 * Test unequal comparisons with hour comparators.
460 */
461 public void testHour() {
462 aDateTime = getADate( "1969-12-31T22:00:00" );
463 bDateTime = getADate( "1969-12-31T23:00:00" );
464 assertEquals( "HourM1a", -1, cHour.compare( aDateTime, bDateTime ) );
465 assertEquals( "HourP1a", 1, cHour.compare( bDateTime, aDateTime ) );
466 aDateTime = getADate( "1970-01-01T00:00:00" );
467 bDateTime = getADate( "1970-01-01T01:00:00" );
468 assertEquals( "HourM1b", -1, cHour.compare( aDateTime, bDateTime ) );
469 assertEquals( "HourP1b", 1, cHour.compare( bDateTime, aDateTime ) );
470 aDateTime = getADate( "1969-12-31T23:59:59" );
471 bDateTime = getADate( "1970-01-01T00:00:00" );
472 assertEquals( "HourP1c", 1, cHour.compare( aDateTime, bDateTime ) );
473 assertEquals( "HourM1c", -1, cHour.compare( bDateTime, aDateTime ) );
474 } // end of testHour
475
476 /**
477 * Test unequal comparisons with day of week comparators.
478 */
479 public void testDOW() {
480 /*
481 * Dates chosen when I wrote the code, so I know what day of
482 * the week it is.
483 */
484 aDateTime = getADate( "2002-04-12T00:00:00" );
485 bDateTime = getADate( "2002-04-13T00:00:00" );
486 assertEquals( "DOWM1a", -1, cDayOfWeek.compare( aDateTime, bDateTime ) );
487 assertEquals( "DOWP1a", 1, cDayOfWeek.compare( bDateTime, aDateTime ) );
488 } // end of testDOW
489
490 /**
491 * Test unequal comparisons with day of month comparators.
492 */
493 public void testDOM() {
494 aDateTime = getADate( "2002-04-12T00:00:00" );
495 bDateTime = getADate( "2002-04-13T00:00:00" );
496 assertEquals( "DOMM1a", -1, cDayOfMonth.compare( aDateTime, bDateTime ) );
497 assertEquals( "DOMP1a", 1, cDayOfMonth.compare( bDateTime, aDateTime ) );
498 aDateTime = getADate( "2000-12-01T00:00:00" );
499 bDateTime = getADate( "1814-04-30T00:00:00" );
500 assertEquals( "DOMM1b", -1, cDayOfMonth.compare( aDateTime, bDateTime ) );
501 assertEquals( "DOMP1b", 1, cDayOfMonth.compare( bDateTime, aDateTime ) );
502 } // end of testDOM
503
504 /**
505 * Test unequal comparisons with day of year comparators.
506 */
507 public void testDOY() {
508 aDateTime = getADate( "2002-04-12T00:00:00" );
509 bDateTime = getADate( "2002-04-13T00:00:00" );
510 assertEquals( "DOYM1a", -1, cDayOfYear.compare( aDateTime, bDateTime ) );
511 assertEquals( "DOYP1a", 1, cDayOfYear.compare( bDateTime, aDateTime ) );
512 aDateTime = getADate( "2000-02-29T00:00:00" );
513 bDateTime = getADate( "1814-11-30T00:00:00" );
514 assertEquals( "DOYM1b", -1, cDayOfYear.compare( aDateTime, bDateTime ) );
515 assertEquals( "DOYP1b", 1, cDayOfYear.compare( bDateTime, aDateTime ) );
516 } // end of testDOY
517
518 /**
519 * Test unequal comparisons with week of weekyear comparators.
520 */
521 public void testWOW() {
522 // 1st week of year contains Jan 04.
523 aDateTime = getADate( "2000-01-04T00:00:00" );
524 bDateTime = getADate( "2000-01-11T00:00:00" );
525 assertEquals( "WOWM1a", -1,
526 cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
527 assertEquals( "WOWP1a", 1,
528 cWeekOfWeekyear.compare( bDateTime, aDateTime ) );
529 aDateTime = getADate( "2000-01-04T00:00:00" );
530 bDateTime = getADate( "1999-12-31T00:00:00" );
531 assertEquals( "WOWM1b", -1,
532 cWeekOfWeekyear.compare( aDateTime, bDateTime ) );
533 assertEquals( "WOWP1b", 1,
534 cWeekOfWeekyear.compare( bDateTime, aDateTime ) );
535 } // end of testMillis
536
537 /**
538 * Test unequal comparisons with year given the week comparators.
539 */
540 public void testWOYY() {
541 // How do I test the end conditions of this?
542 // Don't understand ......
543 aDateTime = getADate( "1998-12-31T23:59:59" );
544 bDateTime = getADate( "1999-01-01T00:00:00" );
545 assertEquals( "YOYYZ", 0, cWeekyear.compare( aDateTime, bDateTime ) );
546 bDateTime = getADate( "1999-01-04T00:00:00" );
547 assertEquals( "YOYYM1", -1, cWeekyear.compare( aDateTime, bDateTime ) );
548 assertEquals( "YOYYP1", 1, cWeekyear.compare( bDateTime, aDateTime ) );
549 } // end of testWOYY
550
551 /**
552 * Test unequal comparisons with month comparators.
553 */
554 public void testMonth() {
555 aDateTime = getADate( "2002-04-30T00:00:00" );
556 bDateTime = getADate( "2002-05-01T00:00:00" );
557 assertEquals( "MONTHM1a", -1, cMonth.compare( aDateTime, bDateTime ) );
558 assertEquals( "MONTHP1a", 1, cMonth.compare( bDateTime, aDateTime ) );
559 aDateTime = getADate( "1900-01-01T00:00:00" );
560 bDateTime = getADate( "1899-12-31T00:00:00" );
561 assertEquals( "MONTHM1b", -1, cMonth.compare( aDateTime, bDateTime ) );
562 assertEquals( "MONTHP1b", 1, cMonth.compare( bDateTime, aDateTime ) );
563 } // end of testMonth
564
565 /**
566 * Test unequal comparisons with year comparators.
567 */
568 public void testYear() {
569 aDateTime = getADate( "2000-01-01T00:00:00" );
570 bDateTime = getADate( "2001-01-01T00:00:00" );
571 assertEquals( "YEARM1a", -1, cYear.compare( aDateTime, bDateTime ) );
572 assertEquals( "YEARP1a", 1, cYear.compare( bDateTime, aDateTime ) );
573 aDateTime = getADate( "1968-12-31T23:59:59" );
574 bDateTime = getADate( "1970-01-01T00:00:00" );
575 assertEquals( "YEARM1b", -1, cYear.compare( aDateTime, bDateTime ) );
576 assertEquals( "YEARP1b", 1, cYear.compare( bDateTime, aDateTime ) );
577 aDateTime = getADate( "1969-12-31T23:59:59" );
578 bDateTime = getADate( "1970-01-01T00:00:00" );
579 assertEquals( "YEARM1c", -1, cYear.compare( aDateTime, bDateTime ) );
580 assertEquals( "YEARP1c", 1, cYear.compare( bDateTime, aDateTime ) );
581 } // end of testYear
582
583 /*
584 * 'List' processing tests follow.
585 */
586
587 /**
588 * Test sorting with full default comparator.
589 */
590 public void testListBasic() {
591 String[] dtStrs = {
592 "1999-02-01T00:00:00",
593 "1998-01-20T00:00:00"
594 };
595 //
596 List sl = loadAList( dtStrs );
597 boolean isSorted1 = isListSorted( sl );
598 Collections.sort( sl );
599 boolean isSorted2 = isListSorted( sl );
600 assertEquals("ListBasic", !isSorted1, isSorted2);
601 } // end of testListBasic
602
603 /**
604 * Test sorting with millis of second comparator.
605 */
606 public void testListMillis() {
607 //
608 List sl = new ArrayList();
609 long base = 12345L * 1000L;
610 sl.add( new DateTime( base + 999L, DateTimeZone.UTC ) );
611 sl.add( new DateTime( base + 222L, DateTimeZone.UTC ) );
612 sl.add( new DateTime( base + 456L, DateTimeZone.UTC ) );
613 sl.add( new DateTime( base + 888L, DateTimeZone.UTC ) );
614 sl.add( new DateTime( base + 123L, DateTimeZone.UTC ) );
615 sl.add( new DateTime( base + 000L, DateTimeZone.UTC ) );
616 //
617 boolean isSorted1 = isListSorted( sl );
618 Collections.sort( sl, cMillis );
619 boolean isSorted2 = isListSorted( sl );
620 assertEquals("ListLillis", !isSorted1, isSorted2);
621 } // end of testListSecond
622
623
624 /**
625 * Test sorting with second comparator.
626 */
627 public void testListSecond() {
628 String[] dtStrs = {
629 "1999-02-01T00:00:10",
630 "1999-02-01T00:00:30",
631 "1999-02-01T00:00:25",
632 "1999-02-01T00:00:18",
633 "1999-02-01T00:00:01",
634 "1999-02-01T00:00:59",
635 "1999-02-01T00:00:22"
636 };
637 //
638 List sl = loadAList( dtStrs );
639 boolean isSorted1 = isListSorted( sl );
640 Collections.sort( sl, cSecond );
641 boolean isSorted2 = isListSorted( sl );
642 assertEquals("ListSecond", !isSorted1, isSorted2);
643 } // end of testListSecond
644
645 /**
646 * Test sorting with minute comparator.
647 */
648 public void testListMinute() {
649 String[] dtStrs = {
650 "1999-02-01T00:10:00",
651 "1999-02-01T00:30:00",
652 "1999-02-01T00:25:00",
653 "1999-02-01T00:18:00",
654 "1999-02-01T00:01:00",
655 "1999-02-01T00:59:00",
656 "1999-02-01T00:22:00"
657 };
658 //
659 List sl = loadAList( dtStrs );
660 boolean isSorted1 = isListSorted( sl );
661 Collections.sort( sl, cMinute );
662 boolean isSorted2 = isListSorted( sl );
663 assertEquals("ListMinute", !isSorted1, isSorted2);
664 } // end of testListMinute
665
666 /**
667 * Test sorting with hour comparator.
668 */
669 public void testListHour() {
670 String[] dtStrs = {
671 "1999-02-01T10:00:00",
672 "1999-02-01T23:00:00",
673 "1999-02-01T01:00:00",
674 "1999-02-01T15:00:00",
675 "1999-02-01T05:00:00",
676 "1999-02-01T20:00:00",
677 "1999-02-01T17:00:00"
678 };
679 //
680 List sl = loadAList( dtStrs );
681 boolean isSorted1 = isListSorted( sl );
682 Collections.sort( sl, cHour );
683 boolean isSorted2 = isListSorted( sl );
684 assertEquals("ListHour", !isSorted1, isSorted2);
685 } // end of testListHour
686
687
688 /**
689 * Test sorting with day of week comparator.
690 */
691 public void testListDOW() {
692 String[] dtStrs = {
693 /* 2002-04-15 = Monday */
694 "2002-04-21T10:00:00",
695 "2002-04-16T10:00:00",
696 "2002-04-15T10:00:00",
697 "2002-04-17T10:00:00",
698 "2002-04-19T10:00:00",
699 "2002-04-18T10:00:00",
700 "2002-04-20T10:00:00"
701 };
702 //
703 List sl = loadAList( dtStrs );
704 boolean isSorted1 = isListSorted( sl );
705 Collections.sort( sl, cDayOfWeek );
706 boolean isSorted2 = isListSorted( sl );
707 assertEquals("ListDOW", !isSorted1, isSorted2);
708 } // end of testListDOW
709
710 /**
711 * Test sorting with day of month comparator.
712 */
713 public void testListDOM() {
714 String[] dtStrs = {
715 /* 2002-04-14 = Sunday */
716 "2002-04-20T10:00:00",
717 "2002-04-16T10:00:00",
718 "2002-04-15T10:00:00",
719 "2002-04-17T10:00:00",
720 "2002-04-19T10:00:00",
721 "2002-04-18T10:00:00",
722 "2002-04-14T10:00:00"
723 };
724 //
725 List sl = loadAList( dtStrs );
726 boolean isSorted1 = isListSorted( sl );
727 Collections.sort( sl, cDayOfMonth );
728 boolean isSorted2 = isListSorted( sl );
729 assertEquals("ListDOM", !isSorted1, isSorted2);
730 } // end of testListDOM
731
732 /**
733 * Test sorting with day of year comparator.
734 */
735 public void testListDOY() {
736 String[] dtStrs = {
737 "2002-04-20T10:00:00",
738 "2002-01-16T10:00:00",
739 "2002-12-31T10:00:00",
740 "2002-09-14T10:00:00",
741 "2002-09-19T10:00:00",
742 "2002-02-14T10:00:00",
743 "2002-10-30T10:00:00"
744 };
745 //
746 List sl = loadAList( dtStrs );
747 boolean isSorted1 = isListSorted( sl );
748 Collections.sort( sl, cDayOfYear );
749 boolean isSorted2 = isListSorted( sl );
750 assertEquals("ListDOY", !isSorted1, isSorted2);
751 } // end of testListDOY
752
753 /**
754 * Test sorting with week of weekyear comparator.
755 */
756 public void testListWOW() {
757 String[] dtStrs = {
758 "2002-04-01T10:00:00",
759 "2002-01-01T10:00:00",
760 "2002-12-01T10:00:00",
761 "2002-09-01T10:00:00",
762 "2002-09-01T10:00:00",
763 "2002-02-01T10:00:00",
764 "2002-10-01T10:00:00"
765 };
766 //
767 List sl = loadAList( dtStrs );
768 boolean isSorted1 = isListSorted( sl );
769 Collections.sort( sl, cWeekOfWeekyear );
770 boolean isSorted2 = isListSorted( sl );
771 assertEquals("ListWOW", !isSorted1, isSorted2);
772 } // end of testListWOW
773
774 /**
775 * Test sorting with year (given week) comparator.
776 */
777 public void testListYOYY() {
778 // ?? How to catch end conditions ??
779 String[] dtStrs = {
780 "2010-04-01T10:00:00",
781 "2002-01-01T10:00:00"
782 };
783 //
784 List sl = loadAList( dtStrs );
785 boolean isSorted1 = isListSorted( sl );
786 Collections.sort( sl, cWeekyear );
787 boolean isSorted2 = isListSorted( sl );
788 assertEquals("ListYOYY", !isSorted1, isSorted2);
789 } // end of testListYOYY
790
791
792 /**
793 * Test sorting with month comparator.
794 */
795 public void testListMonth() {
796 String[] dtStrs = {
797 "2002-04-01T10:00:00",
798 "2002-01-01T10:00:00",
799 "2002-12-01T10:00:00",
800 "2002-09-01T10:00:00",
801 "2002-09-01T10:00:00",
802 "2002-02-01T10:00:00",
803 "2002-10-01T10:00:00"
804 };
805 //
806 List sl = loadAList( dtStrs );
807 boolean isSorted1 = isListSorted( sl );
808 Collections.sort( sl, cMonth );
809 boolean isSorted2 = isListSorted( sl );
810 assertEquals("ListMonth", !isSorted1, isSorted2);
811 } // end of testListMonth
812
813 /**
814 * Test sorting with year comparator.
815 */
816 public void testListYear() {
817 String[] dtStrs = {
818 "1999-02-01T00:00:00",
819 "1998-02-01T00:00:00",
820 "2525-02-01T00:00:00",
821 "1776-02-01T00:00:00",
822 "1863-02-01T00:00:00",
823 "1066-02-01T00:00:00",
824 "2100-02-01T00:00:00"
825 };
826 //
827 List sl = loadAList( dtStrs );
828 boolean isSorted1 = isListSorted( sl );
829 Collections.sort( sl, cYear );
830 boolean isSorted2 = isListSorted( sl );
831 assertEquals("ListYear", !isSorted1, isSorted2);
832 } // end of testListYear
833
834 /**
835 * Test sorting with date only comparator.
836 */
837 public void testListDate() {
838 String[] dtStrs = {
839 "1999-02-01T00:00:00",
840 "1998-10-03T00:00:00",
841 "2525-05-20T00:00:00",
842 "1776-12-25T00:00:00",
843 "1863-01-31T00:00:00",
844 "1066-09-22T00:00:00",
845 "2100-07-04T00:00:00"
846 };
847 //
848 List sl = loadAList( dtStrs );
849 boolean isSorted1 = isListSorted( sl );
850 Collections.sort( sl, cDate );
851 boolean isSorted2 = isListSorted( sl );
852 assertEquals("ListDate", !isSorted1, isSorted2);
853 } // end of testListDate
854
855 /**
856 * Test sorting with time only comparator.
857 */
858 public void testListTime() {
859 String[] dtStrs = {
860 "1999-02-01T01:02:05",
861 "1999-02-01T22:22:22",
862 "1999-02-01T05:30:45",
863 "1999-02-01T09:17:59",
864 "1999-02-01T09:17:58",
865 "1999-02-01T15:30:00",
866 "1999-02-01T17:00:44"
867 };
868 //
869 List sl = loadAList( dtStrs );
870 boolean isSorted1 = isListSorted( sl );
871 Collections.sort( sl, cTime );
872 boolean isSorted2 = isListSorted( sl );
873 assertEquals("ListTime", !isSorted1, isSorted2);
874 } // end of testListTime
875
876
877 /**
878 * Test comparator operation with null object(s).
879 */
880 public void testNullDT() {
881 // null means now
882 aDateTime = getADate("2000-01-01T00:00:00");
883 assertTrue(cYear.compare(null, aDateTime) > 0);
884 assertTrue(cYear.compare(aDateTime, null) < 0);
885 }
886
887 /**
888 * Test comparator operation with an invalid object type.
889 */
890 public void testInvalidObj() {
891 aDateTime = getADate("2000-01-01T00:00:00");
892 try {
893 cYear.compare("FreeBird", aDateTime);
894 fail("Invalid object failed");
895 } catch (IllegalArgumentException cce) {}
896 }
897
898 // private convenience methods
899 //-----------------------------------------------------------------------
900 /**
901 * Creates a date to test with.
902 */
903 private DateTime getADate(String s) {
904 DateTime retDT = null;
905 try {
906 retDT = new DateTime(s, DateTimeZone.UTC);
907 } catch (IllegalArgumentException pe) {
908 pe.printStackTrace();
909 }
910 return retDT;
911 }
912
913 /**
914 * Load a string array.
915 */
916 private List loadAList(String[] someStrs) {
917 List newList = new ArrayList();
918 try {
919 for (int i = 0; i < someStrs.length; ++i) {
920 newList.add(new DateTime(someStrs[i], DateTimeZone.UTC));
921 } // end of the for
922 } catch (IllegalArgumentException pe) {
923 pe.printStackTrace();
924 }
925 return newList;
926 }
927
928 /**
929 * Check if the list is sorted.
930 */
931 private boolean isListSorted(List tl) {
932 // tl must be populated with DateTime objects.
933 DateTime lhDT = (DateTime)tl.get(0);
934 DateTime rhDT = null;
935 Long lhVal = new Long( lhDT.getMillis() );
936 Long rhVal = null;
937 for (int i = 1; i < tl.size(); ++i) {
938 rhDT = (DateTime)tl.get(i);
939 rhVal = new Long( rhDT.getMillis() );
940 if ( lhVal.compareTo( rhVal) > 0 ) return false;
941 //
942 lhVal = rhVal; // swap for next iteration
943 lhDT = rhDT; // swap for next iteration
944 }
945 return true;
946 }
947
948 }