Source Code Cross Referenced for TestDateTimeComparator.java in  » Development » Joda-Time » org » joda » time » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Joda Time » org.joda.time 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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