Source Code Cross Referenced for TestDateMidnight_Basics.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) 


0001:        /*
0002:         *  Copyright 2001-2006 Stephen Colebourne
0003:         *
0004:         *  Licensed under the Apache License, Version 2.0 (the "License");
0005:         *  you may not use this file except in compliance with the License.
0006:         *  You may obtain a copy of the License at
0007:         *
0008:         *      http://www.apache.org/licenses/LICENSE-2.0
0009:         *
0010:         *  Unless required by applicable law or agreed to in writing, software
0011:         *  distributed under the License is distributed on an "AS IS" BASIS,
0012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         *  See the License for the specific language governing permissions and
0014:         *  limitations under the License.
0015:         */
0016:        package org.joda.time;
0017:
0018:        import java.io.ByteArrayInputStream;
0019:        import java.io.ByteArrayOutputStream;
0020:        import java.io.ObjectInputStream;
0021:        import java.io.ObjectOutputStream;
0022:        import java.util.Calendar;
0023:        import java.util.Date;
0024:        import java.util.GregorianCalendar;
0025:        import java.util.Locale;
0026:        import java.util.TimeZone;
0027:
0028:        import junit.framework.TestCase;
0029:        import junit.framework.TestSuite;
0030:
0031:        import org.joda.time.base.AbstractInstant;
0032:        import org.joda.time.chrono.BuddhistChronology;
0033:        import org.joda.time.chrono.CopticChronology;
0034:        import org.joda.time.chrono.GJChronology;
0035:        import org.joda.time.chrono.GregorianChronology;
0036:        import org.joda.time.chrono.ISOChronology;
0037:        import org.joda.time.field.UnsupportedDateTimeField;
0038:        import org.joda.time.field.UnsupportedDurationField;
0039:        import org.joda.time.format.DateTimeFormat;
0040:        import org.joda.time.format.DateTimeFormatter;
0041:
0042:        /**
0043:         * This class is a Junit unit test for DateMidnight.
0044:         *
0045:         * @author Stephen Colebourne
0046:         */
0047:        public class TestDateMidnight_Basics extends TestCase {
0048:            // Test in 2002/03 as time zones are more well known
0049:            // (before the late 90's they were all over the place)
0050:
0051:            private static final DateTimeZone PARIS = DateTimeZone
0052:                    .forID("Europe/Paris");
0053:            private static final DateTimeZone LONDON = DateTimeZone
0054:                    .forID("Europe/London");
0055:            private static final DateTimeZone NEWYORK = DateTimeZone
0056:                    .forID("America/New_York");
0057:
0058:            // the default time zone is set to LONDON in setUp()
0059:            // we have to hard code LONDON here (instead of ISOChronology.getInstance() etc.)
0060:            // as TestAll sets up a different time zone for better all-round testing
0061:            private static final ISOChronology ISO_DEFAULT = ISOChronology
0062:                    .getInstance(LONDON);
0063:            private static final ISOChronology ISO_PARIS = ISOChronology
0064:                    .getInstance(PARIS);
0065:            private static final GJChronology GJ_DEFAULT = GJChronology
0066:                    .getInstance(LONDON);
0067:            private static final GregorianChronology GREGORIAN_DEFAULT = GregorianChronology
0068:                    .getInstance(LONDON);
0069:            private static final GregorianChronology GREGORIAN_PARIS = GregorianChronology
0070:                    .getInstance(PARIS);
0071:            private static final BuddhistChronology BUDDHIST_DEFAULT = BuddhistChronology
0072:                    .getInstance(LONDON);
0073:            private static final CopticChronology COPTIC_DEFAULT = CopticChronology
0074:                    .getInstance(LONDON);
0075:
0076:            long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0077:                    + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0078:                    + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0079:                    + 365 + 365 + 366 + 365;
0080:            long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0081:                    + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0082:                    + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0083:                    + 365 + 365 + 366 + 365 + 365;
0084:
0085:            // 2002-06-09
0086:            private long TEST_TIME_NOW_UTC = (y2002days + 31L + 28L + 31L + 30L
0087:                    + 31L + 9L - 1L)
0088:                    * DateTimeConstants.MILLIS_PER_DAY;
0089:            private long TEST_TIME_NOW_LONDON = TEST_TIME_NOW_UTC
0090:                    - DateTimeConstants.MILLIS_PER_HOUR;
0091:            //    private long TEST_TIME_NOW_PARIS =
0092:            //            TEST_TIME_NOW_UTC - 2*DateTimeConstants.MILLIS_PER_HOUR;
0093:
0094:            // 2002-04-05
0095:            private long TEST_TIME1_UTC = (y2002days + 31L + 28L + 31L + 5L - 1L)
0096:                    * DateTimeConstants.MILLIS_PER_DAY
0097:                    + 12L
0098:                    * DateTimeConstants.MILLIS_PER_HOUR
0099:                    + 24L
0100:                    * DateTimeConstants.MILLIS_PER_MINUTE;
0101:            private long TEST_TIME1_LONDON = (y2002days + 31L + 28L + 31L + 5L - 1L)
0102:                    * DateTimeConstants.MILLIS_PER_DAY
0103:                    - DateTimeConstants.MILLIS_PER_HOUR;
0104:            private long TEST_TIME1_PARIS = (y2002days + 31L + 28L + 31L + 5L - 1L)
0105:                    * DateTimeConstants.MILLIS_PER_DAY
0106:                    - 2
0107:                    * DateTimeConstants.MILLIS_PER_HOUR;
0108:
0109:            // 2003-05-06
0110:            private long TEST_TIME2_UTC = (y2003days + 31L + 28L + 31L + 30L
0111:                    + 6L - 1L)
0112:                    * DateTimeConstants.MILLIS_PER_DAY
0113:                    + 14L
0114:                    * DateTimeConstants.MILLIS_PER_HOUR
0115:                    + 28L
0116:                    * DateTimeConstants.MILLIS_PER_MINUTE;
0117:            private long TEST_TIME2_LONDON = (y2003days + 31L + 28L + 31L + 30L
0118:                    + 6L - 1L)
0119:                    * DateTimeConstants.MILLIS_PER_DAY
0120:                    - DateTimeConstants.MILLIS_PER_HOUR;
0121:            private long TEST_TIME2_PARIS = (y2003days + 31L + 28L + 31L + 30L
0122:                    + 6L - 1L)
0123:                    * DateTimeConstants.MILLIS_PER_DAY
0124:                    - 2
0125:                    * DateTimeConstants.MILLIS_PER_HOUR;
0126:
0127:            private DateTimeZone originalDateTimeZone = null;
0128:            private TimeZone originalTimeZone = null;
0129:            private Locale originalLocale = null;
0130:
0131:            public static void main(String[] args) {
0132:                junit.textui.TestRunner.run(suite());
0133:            }
0134:
0135:            public static TestSuite suite() {
0136:                return new TestSuite(TestDateMidnight_Basics.class);
0137:            }
0138:
0139:            public TestDateMidnight_Basics(String name) {
0140:                super (name);
0141:            }
0142:
0143:            protected void setUp() throws Exception {
0144:                DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC);
0145:                originalDateTimeZone = DateTimeZone.getDefault();
0146:                originalTimeZone = TimeZone.getDefault();
0147:                originalLocale = Locale.getDefault();
0148:                DateTimeZone.setDefault(LONDON);
0149:                TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
0150:                Locale.setDefault(Locale.UK);
0151:            }
0152:
0153:            protected void tearDown() throws Exception {
0154:                DateTimeUtils.setCurrentMillisSystem();
0155:                DateTimeZone.setDefault(originalDateTimeZone);
0156:                TimeZone.setDefault(originalTimeZone);
0157:                Locale.setDefault(originalLocale);
0158:                originalDateTimeZone = null;
0159:                originalTimeZone = null;
0160:                originalLocale = null;
0161:            }
0162:
0163:            //-----------------------------------------------------------------------
0164:            public void testTest() {
0165:                assertEquals("2002-06-09T00:00:00.000Z", new Instant(
0166:                        TEST_TIME_NOW_UTC).toString());
0167:                assertEquals("2002-04-05T12:24:00.000Z", new Instant(
0168:                        TEST_TIME1_UTC).toString());
0169:                assertEquals("2003-05-06T14:28:00.000Z", new Instant(
0170:                        TEST_TIME2_UTC).toString());
0171:            }
0172:
0173:            //-----------------------------------------------------------------------
0174:            public void testGet_DateTimeField() {
0175:                DateMidnight test = new DateMidnight();
0176:                assertEquals(1, test.get(ISO_DEFAULT.era()));
0177:                assertEquals(20, test.get(ISO_DEFAULT.centuryOfEra()));
0178:                assertEquals(2, test.get(ISO_DEFAULT.yearOfCentury()));
0179:                assertEquals(2002, test.get(ISO_DEFAULT.yearOfEra()));
0180:                assertEquals(2002, test.get(ISO_DEFAULT.year()));
0181:                assertEquals(6, test.get(ISO_DEFAULT.monthOfYear()));
0182:                assertEquals(9, test.get(ISO_DEFAULT.dayOfMonth()));
0183:                assertEquals(2002, test.get(ISO_DEFAULT.weekyear()));
0184:                assertEquals(23, test.get(ISO_DEFAULT.weekOfWeekyear()));
0185:                assertEquals(7, test.get(ISO_DEFAULT.dayOfWeek()));
0186:                assertEquals(160, test.get(ISO_DEFAULT.dayOfYear()));
0187:                assertEquals(0, test.get(ISO_DEFAULT.halfdayOfDay()));
0188:                assertEquals(0, test.get(ISO_DEFAULT.hourOfHalfday()));
0189:                assertEquals(24, test.get(ISO_DEFAULT.clockhourOfDay()));
0190:                assertEquals(12, test.get(ISO_DEFAULT.clockhourOfHalfday()));
0191:                assertEquals(0, test.get(ISO_DEFAULT.hourOfDay()));
0192:                assertEquals(0, test.get(ISO_DEFAULT.minuteOfHour()));
0193:                assertEquals(0, test.get(ISO_DEFAULT.minuteOfDay()));
0194:                assertEquals(0, test.get(ISO_DEFAULT.secondOfMinute()));
0195:                assertEquals(0, test.get(ISO_DEFAULT.secondOfDay()));
0196:                assertEquals(0, test.get(ISO_DEFAULT.millisOfSecond()));
0197:                assertEquals(0, test.get(ISO_DEFAULT.millisOfDay()));
0198:                try {
0199:                    test.get((DateTimeField) null);
0200:                    fail();
0201:                } catch (IllegalArgumentException ex) {
0202:                }
0203:            }
0204:
0205:            public void testGet_DateTimeFieldType() {
0206:                DateMidnight test = new DateMidnight();
0207:                assertEquals(1, test.get(DateTimeFieldType.era()));
0208:                assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
0209:                assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
0210:                assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
0211:                assertEquals(2002, test.get(DateTimeFieldType.year()));
0212:                assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
0213:                assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
0214:                assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
0215:                assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
0216:                assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
0217:                assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
0218:                assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
0219:                assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday()));
0220:                assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay()));
0221:                assertEquals(12, test.get(DateTimeFieldType
0222:                        .clockhourOfHalfday()));
0223:                assertEquals(0, test.get(DateTimeFieldType.hourOfDay()));
0224:                assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
0225:                assertEquals(0, test.get(DateTimeFieldType.minuteOfDay()));
0226:                assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
0227:                assertEquals(0, test.get(DateTimeFieldType.secondOfDay()));
0228:                assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
0229:                assertEquals(0, test.get(DateTimeFieldType.millisOfDay()));
0230:                try {
0231:                    test.get((DateTimeFieldType) null);
0232:                    fail();
0233:                } catch (IllegalArgumentException ex) {
0234:                }
0235:            }
0236:
0237:            //-----------------------------------------------------------------------
0238:            public void testGetters() {
0239:                DateMidnight test = new DateMidnight();
0240:
0241:                assertEquals(ISO_DEFAULT, test.getChronology());
0242:                assertEquals(LONDON, test.getZone());
0243:                assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
0244:
0245:                assertEquals(1, test.getEra());
0246:                assertEquals(20, test.getCenturyOfEra());
0247:                assertEquals(2, test.getYearOfCentury());
0248:                assertEquals(2002, test.getYearOfEra());
0249:                assertEquals(2002, test.getYear());
0250:                assertEquals(6, test.getMonthOfYear());
0251:                assertEquals(9, test.getDayOfMonth());
0252:                assertEquals(2002, test.getWeekyear());
0253:                assertEquals(23, test.getWeekOfWeekyear());
0254:                assertEquals(7, test.getDayOfWeek());
0255:                assertEquals(160, test.getDayOfYear());
0256:                assertEquals(0, test.getHourOfDay());
0257:                assertEquals(0, test.getMinuteOfHour());
0258:                assertEquals(0, test.getMinuteOfDay());
0259:                assertEquals(0, test.getSecondOfMinute());
0260:                assertEquals(0, test.getSecondOfDay());
0261:                assertEquals(0, test.getMillisOfSecond());
0262:                assertEquals(0, test.getMillisOfDay());
0263:            }
0264:
0265:            public void testWithers() {
0266:                DateMidnight test = new DateMidnight(1970, 6, 9, GJ_DEFAULT);
0267:                check(test.withYear(2000), 2000, 6, 9);
0268:                check(test.withMonthOfYear(2), 1970, 2, 9);
0269:                check(test.withDayOfMonth(2), 1970, 6, 2);
0270:                check(test.withDayOfYear(6), 1970, 1, 6);
0271:                check(test.withDayOfWeek(6), 1970, 6, 13);
0272:                check(test.withWeekOfWeekyear(6), 1970, 2, 3);
0273:                check(test.withWeekyear(1971), 1971, 6, 15);
0274:                check(test.withYearOfCentury(60), 1960, 6, 9);
0275:                check(test.withCenturyOfEra(21), 2070, 6, 9);
0276:                check(test.withYearOfEra(1066), 1066, 6, 9);
0277:                check(test.withEra(DateTimeConstants.BC), -1970, 6, 9);
0278:
0279:                try {
0280:                    test.withMonthOfYear(0);
0281:                    fail();
0282:                } catch (IllegalArgumentException ex) {
0283:                }
0284:                try {
0285:                    test.withMonthOfYear(13);
0286:                    fail();
0287:                } catch (IllegalArgumentException ex) {
0288:                }
0289:            }
0290:
0291:            //-----------------------------------------------------------------------
0292:            public void testEqualsHashCode() {
0293:                DateMidnight test1 = new DateMidnight(TEST_TIME1_UTC);
0294:                DateMidnight test2 = new DateMidnight(TEST_TIME1_UTC);
0295:                assertEquals(true, test1.equals(test2));
0296:                assertEquals(true, test2.equals(test1));
0297:                assertEquals(true, test1.equals(test1));
0298:                assertEquals(true, test2.equals(test2));
0299:                assertEquals(true, test1.hashCode() == test2.hashCode());
0300:                assertEquals(true, test1.hashCode() == test1.hashCode());
0301:                assertEquals(true, test2.hashCode() == test2.hashCode());
0302:
0303:                DateMidnight test3 = new DateMidnight(TEST_TIME2_UTC);
0304:                assertEquals(false, test1.equals(test3));
0305:                assertEquals(false, test2.equals(test3));
0306:                assertEquals(false, test3.equals(test1));
0307:                assertEquals(false, test3.equals(test2));
0308:                assertEquals(false, test1.hashCode() == test3.hashCode());
0309:                assertEquals(false, test2.hashCode() == test3.hashCode());
0310:
0311:                assertEquals(false, test1.equals("Hello"));
0312:                assertEquals(true, test1.equals(new MockInstant()));
0313:                assertEquals(false, test1.equals(new DateMidnight(
0314:                        TEST_TIME1_UTC, GREGORIAN_DEFAULT)));
0315:            }
0316:
0317:            class MockInstant extends AbstractInstant {
0318:                public String toString() {
0319:                    return null;
0320:                }
0321:
0322:                public long getMillis() {
0323:                    return TEST_TIME1_LONDON;
0324:                }
0325:
0326:                public Chronology getChronology() {
0327:                    return ISO_DEFAULT;
0328:                }
0329:            }
0330:
0331:            public void testCompareTo() {
0332:                DateMidnight test1 = new DateMidnight(TEST_TIME1_UTC);
0333:                DateMidnight test1a = new DateMidnight(TEST_TIME1_UTC);
0334:                assertEquals(0, test1.compareTo(test1a));
0335:                assertEquals(0, test1a.compareTo(test1));
0336:                assertEquals(0, test1.compareTo(test1));
0337:                assertEquals(0, test1a.compareTo(test1a));
0338:
0339:                DateMidnight test2 = new DateMidnight(TEST_TIME2_UTC);
0340:                assertEquals(-1, test1.compareTo(test2));
0341:                assertEquals(+1, test2.compareTo(test1));
0342:
0343:                DateMidnight test3 = new DateMidnight(TEST_TIME2_UTC,
0344:                        GREGORIAN_PARIS);
0345:                assertEquals(-1, test1.compareTo(test3));
0346:                assertEquals(+1, test3.compareTo(test1));
0347:                assertEquals(-1, test3.compareTo(test2)); // midnight paris before london
0348:
0349:                assertEquals(+1, test2.compareTo(new MockInstant()));
0350:                assertEquals(0, test1.compareTo(new MockInstant()));
0351:
0352:                try {
0353:                    test1.compareTo(null);
0354:                    fail();
0355:                } catch (NullPointerException ex) {
0356:                }
0357:                try {
0358:                    test1.compareTo(new Date());
0359:                    fail();
0360:                } catch (ClassCastException ex) {
0361:                }
0362:            }
0363:
0364:            public void testIsEqual() {
0365:                DateMidnight test1 = new DateMidnight(TEST_TIME1_UTC);
0366:                DateMidnight test1a = new DateMidnight(TEST_TIME1_UTC);
0367:                assertEquals(true, test1.isEqual(test1a));
0368:                assertEquals(true, test1a.isEqual(test1));
0369:                assertEquals(true, test1.isEqual(test1));
0370:                assertEquals(true, test1a.isEqual(test1a));
0371:
0372:                DateMidnight test2 = new DateMidnight(TEST_TIME2_UTC);
0373:                assertEquals(false, test1.isEqual(test2));
0374:                assertEquals(false, test2.isEqual(test1));
0375:
0376:                DateMidnight test3 = new DateMidnight(TEST_TIME2_UTC,
0377:                        GREGORIAN_PARIS);
0378:                assertEquals(false, test1.isEqual(test3));
0379:                assertEquals(false, test3.isEqual(test1));
0380:                assertEquals(false, test3.isEqual(test2)); // midnight paris before london
0381:
0382:                assertEquals(false, test2.isEqual(new MockInstant()));
0383:                assertEquals(true, test1.isEqual(new MockInstant()));
0384:
0385:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC
0386:                        + DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0387:                        .isEqual(null));
0388:                assertEquals(true, new DateMidnight(TEST_TIME_NOW_UTC,
0389:                        DateTimeZone.UTC).isEqual(null));
0390:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC
0391:                        - DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0392:                        .isEqual(null));
0393:
0394:                assertEquals(false, new DateMidnight(2004, 6, 9)
0395:                        .isEqual(new DateTime(2004, 6, 8, 23, 59, 59, 999)));
0396:                assertEquals(true, new DateMidnight(2004, 6, 9)
0397:                        .isEqual(new DateTime(2004, 6, 9, 0, 0, 0, 0)));
0398:                assertEquals(false, new DateMidnight(2004, 6, 9)
0399:                        .isEqual(new DateTime(2004, 6, 9, 0, 0, 0, 1)));
0400:            }
0401:
0402:            public void testIsBefore() {
0403:                DateMidnight test1 = new DateMidnight(TEST_TIME1_UTC);
0404:                DateMidnight test1a = new DateMidnight(TEST_TIME1_UTC);
0405:                assertEquals(false, test1.isBefore(test1a));
0406:                assertEquals(false, test1a.isBefore(test1));
0407:                assertEquals(false, test1.isBefore(test1));
0408:                assertEquals(false, test1a.isBefore(test1a));
0409:
0410:                DateMidnight test2 = new DateMidnight(TEST_TIME2_UTC);
0411:                assertEquals(true, test1.isBefore(test2));
0412:                assertEquals(false, test2.isBefore(test1));
0413:
0414:                DateMidnight test3 = new DateMidnight(TEST_TIME2_UTC,
0415:                        GREGORIAN_PARIS);
0416:                assertEquals(true, test1.isBefore(test3));
0417:                assertEquals(false, test3.isBefore(test1));
0418:                assertEquals(true, test3.isBefore(test2)); // midnight paris before london
0419:
0420:                assertEquals(false, test2.isBefore(new MockInstant()));
0421:                assertEquals(false, test1.isBefore(new MockInstant()));
0422:
0423:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC
0424:                        + DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0425:                        .isBefore(null));
0426:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC,
0427:                        DateTimeZone.UTC).isBefore(null));
0428:                assertEquals(true, new DateMidnight(TEST_TIME_NOW_UTC
0429:                        - DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0430:                        .isBefore(null));
0431:
0432:                assertEquals(false, new DateMidnight(2004, 6, 9)
0433:                        .isBefore(new DateTime(2004, 6, 8, 23, 59, 59, 999)));
0434:                assertEquals(false, new DateMidnight(2004, 6, 9)
0435:                        .isBefore(new DateTime(2004, 6, 9, 0, 0, 0, 0)));
0436:                assertEquals(true, new DateMidnight(2004, 6, 9)
0437:                        .isBefore(new DateTime(2004, 6, 9, 0, 0, 0, 1)));
0438:            }
0439:
0440:            public void testIsAfter() {
0441:                DateMidnight test1 = new DateMidnight(TEST_TIME1_UTC);
0442:                DateMidnight test1a = new DateMidnight(TEST_TIME1_UTC);
0443:                assertEquals(false, test1.isAfter(test1a));
0444:                assertEquals(false, test1a.isAfter(test1));
0445:                assertEquals(false, test1.isAfter(test1));
0446:                assertEquals(false, test1a.isAfter(test1a));
0447:
0448:                DateMidnight test2 = new DateMidnight(TEST_TIME2_UTC);
0449:                assertEquals(false, test1.isAfter(test2));
0450:                assertEquals(true, test2.isAfter(test1));
0451:
0452:                DateMidnight test3 = new DateMidnight(TEST_TIME2_UTC,
0453:                        GREGORIAN_PARIS);
0454:                assertEquals(false, test1.isAfter(test3));
0455:                assertEquals(true, test3.isAfter(test1));
0456:                assertEquals(false, test3.isAfter(test2)); // midnight paris before london
0457:
0458:                assertEquals(true, test2.isAfter(new MockInstant()));
0459:                assertEquals(false, test1.isAfter(new MockInstant()));
0460:
0461:                assertEquals(true, new DateMidnight(TEST_TIME_NOW_UTC
0462:                        + DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0463:                        .isAfter(null));
0464:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC,
0465:                        DateTimeZone.UTC).isAfter(null));
0466:                assertEquals(false, new DateMidnight(TEST_TIME_NOW_UTC
0467:                        - DateTimeConstants.MILLIS_PER_DAY, DateTimeZone.UTC)
0468:                        .isAfter(null));
0469:
0470:                assertEquals(true, new DateMidnight(2004, 6, 9)
0471:                        .isAfter(new DateTime(2004, 6, 8, 23, 59, 59, 999)));
0472:                assertEquals(false, new DateMidnight(2004, 6, 9)
0473:                        .isAfter(new DateTime(2004, 6, 9, 0, 0, 0, 0)));
0474:                assertEquals(false, new DateMidnight(2004, 6, 9)
0475:                        .isAfter(new DateTime(2004, 6, 9, 0, 0, 0, 1)));
0476:            }
0477:
0478:            //-----------------------------------------------------------------------
0479:            public void testSerialization() throws Exception {
0480:                DateMidnight test = new DateMidnight(TEST_TIME_NOW_UTC);
0481:
0482:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
0483:                ObjectOutputStream oos = new ObjectOutputStream(baos);
0484:                oos.writeObject(test);
0485:                byte[] bytes = baos.toByteArray();
0486:                oos.close();
0487:
0488:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0489:                ObjectInputStream ois = new ObjectInputStream(bais);
0490:                DateMidnight result = (DateMidnight) ois.readObject();
0491:                ois.close();
0492:
0493:                assertEquals(test, result);
0494:            }
0495:
0496:            //-----------------------------------------------------------------------
0497:            public void testToString() {
0498:                DateMidnight test = new DateMidnight(TEST_TIME_NOW_UTC);
0499:                assertEquals("2002-06-09T00:00:00.000+01:00", test.toString());
0500:
0501:                test = new DateMidnight(TEST_TIME_NOW_UTC, PARIS);
0502:                assertEquals("2002-06-09T00:00:00.000+02:00", test.toString());
0503:
0504:                test = new DateMidnight(TEST_TIME_NOW_UTC, NEWYORK);
0505:                assertEquals("2002-06-08T00:00:00.000-04:00", test.toString()); // the 8th
0506:            }
0507:
0508:            public void testToString_String() {
0509:                DateMidnight test = new DateMidnight(TEST_TIME_NOW_UTC);
0510:                assertEquals("2002 00", test.toString("yyyy HH"));
0511:                assertEquals("2002-06-09T00:00:00.000+01:00", test
0512:                        .toString((String) null));
0513:            }
0514:
0515:            public void testToString_String_String() {
0516:                DateMidnight test = new DateMidnight(TEST_TIME_NOW_UTC);
0517:                assertEquals("Sun 9/6", test
0518:                        .toString("EEE d/M", Locale.ENGLISH));
0519:                assertEquals("dim. 9/6", test
0520:                        .toString("EEE d/M", Locale.FRENCH));
0521:                assertEquals("2002-06-09T00:00:00.000+01:00", test.toString(
0522:                        null, Locale.ENGLISH));
0523:                assertEquals("Sun 9/6", test.toString("EEE d/M", null));
0524:                assertEquals("2002-06-09T00:00:00.000+01:00", test.toString(
0525:                        null, null));
0526:            }
0527:
0528:            public void testToString_DTFormatter() {
0529:                DateMidnight test = new DateMidnight(TEST_TIME_NOW_UTC);
0530:                assertEquals("2002 00", test.toString(DateTimeFormat
0531:                        .forPattern("yyyy HH")));
0532:                assertEquals("2002-06-09T00:00:00.000+01:00", test
0533:                        .toString((DateTimeFormatter) null));
0534:            }
0535:
0536:            //-----------------------------------------------------------------------
0537:            public void testToInstant() {
0538:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0539:                Instant result = test.toInstant();
0540:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0541:            }
0542:
0543:            public void testToDateTime() {
0544:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0545:                DateTime result = test.toDateTime();
0546:                assertEquals(test.getMillis(), result.getMillis());
0547:                assertEquals(TEST_TIME1_PARIS, result.getMillis());
0548:                assertEquals(PARIS, result.getZone());
0549:            }
0550:
0551:            public void testToDateTimeISO() {
0552:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0553:                DateTime result = test.toDateTimeISO();
0554:                assertSame(DateTime.class, result.getClass());
0555:                assertSame(ISOChronology.class, result.getChronology()
0556:                        .getClass());
0557:                assertEquals(test.getMillis(), result.getMillis());
0558:                assertEquals(ISO_PARIS, result.getChronology());
0559:            }
0560:
0561:            public void testToDateTime_DateTimeZone() {
0562:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0563:                DateTime result = test.toDateTime(LONDON);
0564:                assertEquals(test.getMillis(), result.getMillis());
0565:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0566:                assertEquals(LONDON, result.getZone());
0567:
0568:                test = new DateMidnight(TEST_TIME1_UTC);
0569:                result = test.toDateTime(PARIS);
0570:                assertEquals(test.getMillis(), result.getMillis());
0571:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0572:                assertEquals(PARIS, result.getZone());
0573:
0574:                test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0575:                result = test.toDateTime((DateTimeZone) null);
0576:                assertEquals(test.getMillis(), result.getMillis());
0577:                assertEquals(TEST_TIME1_PARIS, result.getMillis());
0578:                assertEquals(LONDON, result.getZone());
0579:
0580:                test = new DateMidnight(TEST_TIME1_UTC);
0581:                result = test.toDateTime((DateTimeZone) null);
0582:                assertEquals(test.getMillis(), result.getMillis());
0583:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0584:                assertEquals(LONDON, result.getZone());
0585:            }
0586:
0587:            public void testToDateTime_Chronology() {
0588:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0589:                DateTime result = test.toDateTime(ISO_DEFAULT);
0590:                assertEquals(test.getMillis(), result.getMillis());
0591:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0592:                assertEquals(LONDON, result.getZone());
0593:
0594:                test = new DateMidnight(TEST_TIME1_UTC);
0595:                result = test.toDateTime(GREGORIAN_PARIS);
0596:                assertEquals(test.getMillis(), result.getMillis());
0597:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0598:                assertEquals(GREGORIAN_PARIS, result.getChronology());
0599:
0600:                test = new DateMidnight(TEST_TIME1_UTC, GREGORIAN_PARIS);
0601:                result = test.toDateTime((Chronology) null);
0602:                assertEquals(test.getMillis(), result.getMillis());
0603:                assertEquals(TEST_TIME1_PARIS, result.getMillis());
0604:                assertEquals(ISO_DEFAULT, result.getChronology());
0605:
0606:                test = new DateMidnight(TEST_TIME1_UTC);
0607:                result = test.toDateTime((Chronology) null);
0608:                assertEquals(test.getMillis(), result.getMillis());
0609:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0610:                assertEquals(ISO_DEFAULT, result.getChronology());
0611:            }
0612:
0613:            public void testToMutableDateTime() {
0614:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0615:                MutableDateTime result = test.toMutableDateTime();
0616:                assertEquals(test.getMillis(), result.getMillis());
0617:                assertEquals(ISO_PARIS, result.getChronology());
0618:            }
0619:
0620:            public void testToMutableDateTimeISO() {
0621:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0622:                MutableDateTime result = test.toMutableDateTimeISO();
0623:                assertSame(MutableDateTime.class, result.getClass());
0624:                assertSame(ISOChronology.class, result.getChronology()
0625:                        .getClass());
0626:                assertEquals(test.getMillis(), result.getMillis());
0627:                assertEquals(ISO_PARIS, result.getChronology());
0628:            }
0629:
0630:            public void testToMutableDateTime_DateTimeZone() {
0631:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0632:                MutableDateTime result = test.toMutableDateTime(LONDON);
0633:                assertEquals(test.getMillis(), result.getMillis());
0634:                assertEquals(ISO_DEFAULT, result.getChronology());
0635:
0636:                test = new DateMidnight(TEST_TIME1_UTC);
0637:                result = test.toMutableDateTime(PARIS);
0638:                assertEquals(test.getMillis(), result.getMillis());
0639:                assertEquals(ISO_PARIS, result.getChronology());
0640:
0641:                test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0642:                result = test.toMutableDateTime((DateTimeZone) null);
0643:                assertEquals(test.getMillis(), result.getMillis());
0644:                assertEquals(ISO_DEFAULT, result.getChronology());
0645:
0646:                test = new DateMidnight(TEST_TIME1_UTC);
0647:                result = test.toMutableDateTime((DateTimeZone) null);
0648:                assertEquals(test.getMillis(), result.getMillis());
0649:                assertEquals(ISO_DEFAULT, result.getChronology());
0650:            }
0651:
0652:            public void testToMutableDateTime_Chronology() {
0653:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0654:                MutableDateTime result = test.toMutableDateTime(ISO_DEFAULT);
0655:                assertEquals(test.getMillis(), result.getMillis());
0656:                assertEquals(ISO_DEFAULT, result.getChronology());
0657:
0658:                test = new DateMidnight(TEST_TIME1_UTC);
0659:                result = test.toMutableDateTime(GREGORIAN_PARIS);
0660:                assertEquals(test.getMillis(), result.getMillis());
0661:                assertEquals(GREGORIAN_PARIS, result.getChronology());
0662:
0663:                test = new DateMidnight(TEST_TIME1_UTC, GREGORIAN_PARIS);
0664:                result = test.toMutableDateTime((Chronology) null);
0665:                assertEquals(test.getMillis(), result.getMillis());
0666:                assertEquals(ISO_DEFAULT, result.getChronology());
0667:
0668:                test = new DateMidnight(TEST_TIME1_UTC);
0669:                result = test.toMutableDateTime((Chronology) null);
0670:                assertEquals(test.getMillis(), result.getMillis());
0671:                assertEquals(ISO_DEFAULT, result.getChronology());
0672:            }
0673:
0674:            public void testToDate() {
0675:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0676:                Date result = test.toDate();
0677:                assertEquals(test.getMillis(), result.getTime());
0678:            }
0679:
0680:            public void testToCalendar_Locale() {
0681:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0682:                Calendar result = test.toCalendar(null);
0683:                assertEquals(test.getMillis(), result.getTime().getTime());
0684:                assertEquals(TimeZone.getTimeZone("Europe/London"), result
0685:                        .getTimeZone());
0686:
0687:                test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0688:                result = test.toCalendar(null);
0689:                assertEquals(test.getMillis(), result.getTime().getTime());
0690:                assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0691:                        .getTimeZone());
0692:
0693:                test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0694:                result = test.toCalendar(Locale.UK);
0695:                assertEquals(test.getMillis(), result.getTime().getTime());
0696:                assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0697:                        .getTimeZone());
0698:            }
0699:
0700:            public void testToGregorianCalendar() {
0701:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0702:                GregorianCalendar result = test.toGregorianCalendar();
0703:                assertEquals(test.getMillis(), result.getTime().getTime());
0704:                assertEquals(TimeZone.getTimeZone("Europe/London"), result
0705:                        .getTimeZone());
0706:
0707:                test = new DateMidnight(TEST_TIME1_UTC, PARIS);
0708:                result = test.toGregorianCalendar();
0709:                assertEquals(test.getMillis(), result.getTime().getTime());
0710:                assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0711:                        .getTimeZone());
0712:            }
0713:
0714:            //-----------------------------------------------------------------------
0715:            public void testToYearMonthDay() {
0716:                DateMidnight base = new DateMidnight(TEST_TIME1_UTC,
0717:                        COPTIC_DEFAULT);
0718:                YearMonthDay test = base.toYearMonthDay();
0719:                assertEquals(new YearMonthDay(TEST_TIME1_UTC, COPTIC_DEFAULT),
0720:                        test);
0721:            }
0722:
0723:            public void testToLocalDate() {
0724:                DateMidnight base = new DateMidnight(TEST_TIME1_UTC,
0725:                        COPTIC_DEFAULT);
0726:                LocalDate test = base.toLocalDate();
0727:                assertEquals(new LocalDate(TEST_TIME1_UTC, COPTIC_DEFAULT),
0728:                        test);
0729:            }
0730:
0731:            public void testToInterval() {
0732:                DateMidnight base = new DateMidnight(TEST_TIME1_UTC,
0733:                        COPTIC_DEFAULT);
0734:                Interval test = base.toInterval();
0735:                DateMidnight end = base.plus(Period.days(1));
0736:                assertEquals(new Interval(base, end), test);
0737:            }
0738:
0739:            //-----------------------------------------------------------------------
0740:            public void testWithMillis_long() {
0741:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0742:                DateMidnight result = test.withMillis(TEST_TIME2_UTC);
0743:                assertEquals(TEST_TIME2_LONDON, result.getMillis());
0744:                assertEquals(test.getChronology(), result.getChronology());
0745:
0746:                test = new DateMidnight(TEST_TIME1_UTC, GREGORIAN_PARIS);
0747:                result = test.withMillis(TEST_TIME2_UTC);
0748:                assertEquals(TEST_TIME2_PARIS, result.getMillis());
0749:                assertEquals(test.getChronology(), result.getChronology());
0750:
0751:                test = new DateMidnight(TEST_TIME1_UTC);
0752:                result = test.withMillis(TEST_TIME1_UTC);
0753:                assertSame(test, result);
0754:            }
0755:
0756:            public void testWithChronology_Chronology() {
0757:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0758:                DateMidnight result = test.withChronology(GREGORIAN_PARIS);
0759:                assertEquals(TEST_TIME1_LONDON, test.getMillis());
0760:                assertEquals(TEST_TIME1_PARIS, result.getMillis());
0761:                assertEquals(GREGORIAN_PARIS, result.getChronology());
0762:
0763:                test = new DateMidnight(TEST_TIME1_UTC, GREGORIAN_PARIS);
0764:                result = test.withChronology(null);
0765:                assertEquals(TEST_TIME1_PARIS, test.getMillis());
0766:                // midnight Paris is previous day in London
0767:                assertEquals(TEST_TIME1_LONDON
0768:                        - DateTimeConstants.MILLIS_PER_DAY, result.getMillis());
0769:                assertEquals(ISO_DEFAULT, result.getChronology());
0770:
0771:                test = new DateMidnight(TEST_TIME1_UTC);
0772:                result = test.withChronology(null);
0773:                assertEquals(test.getMillis(), result.getMillis());
0774:                assertEquals(ISO_DEFAULT, result.getChronology());
0775:
0776:                test = new DateMidnight(TEST_TIME1_UTC);
0777:                result = test.withChronology(ISO_DEFAULT);
0778:                assertSame(test, result);
0779:            }
0780:
0781:            public void testWithZoneRetainFields_DateTimeZone() {
0782:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
0783:                DateMidnight result = test.withZoneRetainFields(PARIS);
0784:                assertEquals(TEST_TIME1_LONDON, test.getMillis());
0785:                assertEquals(TEST_TIME1_PARIS, result.getMillis());
0786:                assertEquals(ISO_PARIS, result.getChronology());
0787:
0788:                test = new DateMidnight(TEST_TIME1_UTC, GREGORIAN_PARIS);
0789:                result = test.withZoneRetainFields(null);
0790:                assertEquals(TEST_TIME1_PARIS, test.getMillis());
0791:                assertEquals(TEST_TIME1_LONDON, result.getMillis());
0792:                assertEquals(GREGORIAN_DEFAULT, result.getChronology());
0793:
0794:                test = new DateMidnight(TEST_TIME1_UTC);
0795:                result = test.withZoneRetainFields(LONDON);
0796:                assertSame(test, result);
0797:
0798:                test = new DateMidnight(TEST_TIME1_UTC);
0799:                result = test.withZoneRetainFields(null);
0800:                assertSame(test, result);
0801:
0802:                test = new DateMidnight(TEST_TIME1_UTC,
0803:                        new MockNullZoneChronology());
0804:                result = test.withZoneRetainFields(LONDON);
0805:                assertSame(test, result);
0806:            }
0807:
0808:            //-----------------------------------------------------------------------
0809:            public void testWithFields_RPartial() {
0810:                DateMidnight test = new DateMidnight(2004, 5, 6);
0811:                DateMidnight result = test.withFields(new YearMonthDay(2003, 4,
0812:                        5));
0813:                DateMidnight expected = new DateMidnight(2003, 4, 5);
0814:                assertEquals(expected, result);
0815:
0816:                test = new DateMidnight(TEST_TIME1_UTC);
0817:                result = test.withFields(null);
0818:                assertSame(test, result);
0819:            }
0820:
0821:            //-----------------------------------------------------------------------
0822:            public void testWithField1() {
0823:                DateMidnight test = new DateMidnight(2004, 6, 9);
0824:                DateMidnight result = test.withField(DateTimeFieldType.year(),
0825:                        2006);
0826:
0827:                assertEquals(new DateMidnight(2004, 6, 9), test);
0828:                assertEquals(new DateMidnight(2006, 6, 9), result);
0829:            }
0830:
0831:            public void testWithField2() {
0832:                DateMidnight test = new DateMidnight(2004, 6, 9);
0833:                try {
0834:                    test.withField(null, 6);
0835:                    fail();
0836:                } catch (IllegalArgumentException ex) {
0837:                }
0838:            }
0839:
0840:            //-----------------------------------------------------------------------
0841:            public void testWithFieldAdded1() {
0842:                DateMidnight test = new DateMidnight(2004, 6, 9);
0843:                DateMidnight result = test.withFieldAdded(DurationFieldType
0844:                        .years(), 6);
0845:
0846:                assertEquals(new DateMidnight(2004, 6, 9), test);
0847:                assertEquals(new DateMidnight(2010, 6, 9), result);
0848:            }
0849:
0850:            public void testWithFieldAdded2() {
0851:                DateMidnight test = new DateMidnight(2004, 6, 9);
0852:                try {
0853:                    test.withFieldAdded(null, 0);
0854:                    fail();
0855:                } catch (IllegalArgumentException ex) {
0856:                }
0857:            }
0858:
0859:            public void testWithFieldAdded3() {
0860:                DateMidnight test = new DateMidnight(2004, 6, 9);
0861:                try {
0862:                    test.withFieldAdded(null, 6);
0863:                    fail();
0864:                } catch (IllegalArgumentException ex) {
0865:                }
0866:            }
0867:
0868:            public void testWithFieldAdded4() {
0869:                DateMidnight test = new DateMidnight(2004, 6, 9);
0870:                DateMidnight result = test.withFieldAdded(DurationFieldType
0871:                        .years(), 0);
0872:                assertSame(test, result);
0873:            }
0874:
0875:            //-----------------------------------------------------------------------
0876:            public void testWithDurationAdded_long_int() {
0877:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
0878:                        BUDDHIST_DEFAULT);
0879:                DateMidnight result = test.withDurationAdded(123456789L, 1);
0880:                DateMidnight expected = new DateMidnight(
0881:                        test.getMillis() + 123456789L, BUDDHIST_DEFAULT);
0882:                assertEquals(expected, result);
0883:
0884:                result = test.withDurationAdded(123456789L, 0);
0885:                assertSame(test, result);
0886:
0887:                result = test.withDurationAdded(123456789L, 2);
0888:                expected = new DateMidnight(test.getMillis()
0889:                        + (2L * 123456789L), BUDDHIST_DEFAULT);
0890:                assertEquals(expected, result);
0891:
0892:                result = test.withDurationAdded(123456789L, -3);
0893:                expected = new DateMidnight(test.getMillis()
0894:                        - (3L * 123456789L), BUDDHIST_DEFAULT);
0895:                assertEquals(expected, result);
0896:            }
0897:
0898:            //-----------------------------------------------------------------------
0899:            public void testWithDurationAdded_RD_int() {
0900:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
0901:                        BUDDHIST_DEFAULT);
0902:                DateMidnight result = test.withDurationAdded(new Duration(
0903:                        123456789L), 1);
0904:                DateMidnight expected = new DateMidnight(
0905:                        test.getMillis() + 123456789L, BUDDHIST_DEFAULT);
0906:                assertEquals(expected, result);
0907:
0908:                result = test.withDurationAdded(null, 1);
0909:                assertSame(test, result);
0910:
0911:                result = test.withDurationAdded(new Duration(123456789L), 0);
0912:                assertSame(test, result);
0913:
0914:                result = test.withDurationAdded(new Duration(123456789L), 2);
0915:                expected = new DateMidnight(test.getMillis()
0916:                        + (2L * 123456789L), BUDDHIST_DEFAULT);
0917:                assertEquals(expected, result);
0918:
0919:                result = test.withDurationAdded(new Duration(123456789L), -3);
0920:                expected = new DateMidnight(test.getMillis()
0921:                        - (3L * 123456789L), BUDDHIST_DEFAULT);
0922:                assertEquals(expected, result);
0923:            }
0924:
0925:            //-----------------------------------------------------------------------
0926:            public void testWithDurationAdded_RP_int() {
0927:                DateMidnight test = new DateMidnight(2002, 5, 3,
0928:                        BUDDHIST_DEFAULT);
0929:                DateMidnight result = test.withPeriodAdded(new Period(1, 2, 3,
0930:                        4, 5, 6, 7, 8), 1);
0931:                DateMidnight expected = new DateMidnight(2003, 7, 28,
0932:                        BUDDHIST_DEFAULT);
0933:                assertEquals(expected, result);
0934:
0935:                result = test.withPeriodAdded(null, 1);
0936:                assertSame(test, result);
0937:
0938:                result = test.withPeriodAdded(
0939:                        new Period(1, 2, 3, 4, 5, 6, 7, 8), 0);
0940:                assertSame(test, result);
0941:
0942:                result = test.withPeriodAdded(
0943:                        new Period(1, 2, 0, 4, 5, 6, 7, 8), 3);
0944:                expected = new DateMidnight(2005, 11, 15, BUDDHIST_DEFAULT);
0945:                assertEquals(expected, result);
0946:
0947:                result = test.withPeriodAdded(
0948:                        new Period(1, 2, 0, 1, 1, 2, 3, 4), -1);
0949:                expected = new DateMidnight(2001, 3, 1, BUDDHIST_DEFAULT);
0950:                assertEquals(expected, result);
0951:            }
0952:
0953:            //-----------------------------------------------------------------------    
0954:            public void testPlus_long() {
0955:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
0956:                        BUDDHIST_DEFAULT);
0957:                DateMidnight result = test.plus(123456789L);
0958:                DateMidnight expected = new DateMidnight(
0959:                        test.getMillis() + 123456789L, BUDDHIST_DEFAULT);
0960:                assertEquals(expected, result);
0961:            }
0962:
0963:            public void testPlus_RD() {
0964:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
0965:                        BUDDHIST_DEFAULT);
0966:                DateMidnight result = test.plus(new Duration(123456789L));
0967:                DateMidnight expected = new DateMidnight(
0968:                        test.getMillis() + 123456789L, BUDDHIST_DEFAULT);
0969:                assertEquals(expected, result);
0970:
0971:                result = test.plus((ReadableDuration) null);
0972:                assertSame(test, result);
0973:            }
0974:
0975:            public void testPlus_RP() {
0976:                DateMidnight test = new DateMidnight(2002, 5, 3,
0977:                        BUDDHIST_DEFAULT);
0978:                DateMidnight result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7,
0979:                        8));
0980:                DateMidnight expected = new DateMidnight(2003, 7, 28,
0981:                        BUDDHIST_DEFAULT);
0982:                assertEquals(expected, result);
0983:
0984:                result = test.plus((ReadablePeriod) null);
0985:                assertSame(test, result);
0986:            }
0987:
0988:            public void testPlusYears_int() {
0989:                DateMidnight test = new DateMidnight(2002, 5, 3,
0990:                        BUDDHIST_DEFAULT);
0991:                DateMidnight result = test.plusYears(1);
0992:                DateMidnight expected = new DateMidnight(2003, 5, 3,
0993:                        BUDDHIST_DEFAULT);
0994:                assertEquals(expected, result);
0995:
0996:                result = test.plusYears(0);
0997:                assertSame(test, result);
0998:            }
0999:
1000:            public void testPlusMonths_int() {
1001:                DateMidnight test = new DateMidnight(2002, 5, 3,
1002:                        BUDDHIST_DEFAULT);
1003:                DateMidnight result = test.plusMonths(1);
1004:                DateMidnight expected = new DateMidnight(2002, 6, 3,
1005:                        BUDDHIST_DEFAULT);
1006:                assertEquals(expected, result);
1007:
1008:                result = test.plusMonths(0);
1009:                assertSame(test, result);
1010:            }
1011:
1012:            public void testPlusWeeks_int() {
1013:                DateMidnight test = new DateMidnight(2002, 5, 3,
1014:                        BUDDHIST_DEFAULT);
1015:                DateMidnight result = test.plusWeeks(1);
1016:                DateMidnight expected = new DateMidnight(2002, 5, 10,
1017:                        BUDDHIST_DEFAULT);
1018:                assertEquals(expected, result);
1019:
1020:                result = test.plusWeeks(0);
1021:                assertSame(test, result);
1022:            }
1023:
1024:            public void testPlusDays_int() {
1025:                DateMidnight test = new DateMidnight(2002, 5, 3,
1026:                        BUDDHIST_DEFAULT);
1027:                DateMidnight result = test.plusDays(1);
1028:                DateMidnight expected = new DateMidnight(2002, 5, 4,
1029:                        BUDDHIST_DEFAULT);
1030:                assertEquals(expected, result);
1031:
1032:                result = test.plusDays(0);
1033:                assertSame(test, result);
1034:            }
1035:
1036:            //-----------------------------------------------------------------------    
1037:            public void testMinus_long() {
1038:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
1039:                        BUDDHIST_DEFAULT);
1040:                DateMidnight result = test.minus(123456789L);
1041:                DateMidnight expected = new DateMidnight(
1042:                        test.getMillis() - 123456789L, BUDDHIST_DEFAULT);
1043:                assertEquals(expected, result);
1044:            }
1045:
1046:            public void testMinus_RD() {
1047:                DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
1048:                        BUDDHIST_DEFAULT);
1049:                DateMidnight result = test.minus(new Duration(123456789L));
1050:                DateMidnight expected = new DateMidnight(
1051:                        test.getMillis() - 123456789L, BUDDHIST_DEFAULT);
1052:                assertEquals(expected, result);
1053:
1054:                result = test.minus((ReadableDuration) null);
1055:                assertSame(test, result);
1056:            }
1057:
1058:            public void testMinus_RP() {
1059:                DateMidnight test = new DateMidnight(2002, 5, 3,
1060:                        BUDDHIST_DEFAULT);
1061:                DateMidnight result = test.minus(new Period(1, 1, 1, 1, 1, 1,
1062:                        1, 1));
1063:                DateMidnight expected = new DateMidnight(2001, 3, 25,
1064:                        BUDDHIST_DEFAULT);
1065:                assertEquals(expected, result);
1066:
1067:                result = test.minus((ReadablePeriod) null);
1068:                assertSame(test, result);
1069:            }
1070:
1071:            public void testMinusYears_int() {
1072:                DateMidnight test = new DateMidnight(2002, 5, 3,
1073:                        BUDDHIST_DEFAULT);
1074:                DateMidnight result = test.minusYears(1);
1075:                DateMidnight expected = new DateMidnight(2001, 5, 3,
1076:                        BUDDHIST_DEFAULT);
1077:                assertEquals(expected, result);
1078:
1079:                result = test.minusYears(0);
1080:                assertSame(test, result);
1081:            }
1082:
1083:            public void testMinusMonths_int() {
1084:                DateMidnight test = new DateMidnight(2002, 5, 3,
1085:                        BUDDHIST_DEFAULT);
1086:                DateMidnight result = test.minusMonths(1);
1087:                DateMidnight expected = new DateMidnight(2002, 4, 3,
1088:                        BUDDHIST_DEFAULT);
1089:                assertEquals(expected, result);
1090:
1091:                result = test.minusMonths(0);
1092:                assertSame(test, result);
1093:            }
1094:
1095:            public void testMinusWeeks_int() {
1096:                DateMidnight test = new DateMidnight(2002, 5, 3,
1097:                        BUDDHIST_DEFAULT);
1098:                DateMidnight result = test.minusWeeks(1);
1099:                DateMidnight expected = new DateMidnight(2002, 4, 26,
1100:                        BUDDHIST_DEFAULT);
1101:                assertEquals(expected, result);
1102:
1103:                result = test.minusWeeks(0);
1104:                assertSame(test, result);
1105:            }
1106:
1107:            public void testMinusDays_int() {
1108:                DateMidnight test = new DateMidnight(2002, 5, 3,
1109:                        BUDDHIST_DEFAULT);
1110:                DateMidnight result = test.minusDays(1);
1111:                DateMidnight expected = new DateMidnight(2002, 5, 2,
1112:                        BUDDHIST_DEFAULT);
1113:                assertEquals(expected, result);
1114:
1115:                result = test.minusDays(0);
1116:                assertSame(test, result);
1117:            }
1118:
1119:            //-----------------------------------------------------------------------
1120:            public void testProperty() {
1121:                DateMidnight test = new DateMidnight();
1122:                assertEquals(test.year(), test.property(DateTimeFieldType
1123:                        .year()));
1124:                assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType
1125:                        .dayOfWeek()));
1126:                assertEquals(test.weekOfWeekyear(), test
1127:                        .property(DateTimeFieldType.weekOfWeekyear()));
1128:                assertEquals(test.property(DateTimeFieldType.millisOfSecond()),
1129:                        test.property(DateTimeFieldType.millisOfSecond()));
1130:                DateTimeFieldType bad = new DateTimeFieldType("bad") {
1131:                    public DurationFieldType getDurationType() {
1132:                        return DurationFieldType.weeks();
1133:                    }
1134:
1135:                    public DurationFieldType getRangeDurationType() {
1136:                        return null;
1137:                    }
1138:
1139:                    public DateTimeField getField(Chronology chronology) {
1140:                        return UnsupportedDateTimeField.getInstance(this ,
1141:                                UnsupportedDurationField
1142:                                        .getInstance(getDurationType()));
1143:                    }
1144:                };
1145:                try {
1146:                    test.property(bad);
1147:                    fail();
1148:                } catch (IllegalArgumentException ex) {
1149:                }
1150:                try {
1151:                    test.property(null);
1152:                    fail();
1153:                } catch (IllegalArgumentException ex) {
1154:                }
1155:            }
1156:
1157:            //-----------------------------------------------------------------------
1158:            private void check(DateMidnight test, int year, int month, int day) {
1159:                assertEquals(year, test.getYear());
1160:                assertEquals(month, test.getMonthOfYear());
1161:                assertEquals(day, test.getDayOfMonth());
1162:            }
1163:
1164:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.