0001: /*
0002: * Copyright 2001-2007 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.Arrays;
0023: import java.util.Date;
0024: import java.util.Locale;
0025:
0026: import junit.framework.TestCase;
0027: import junit.framework.TestSuite;
0028:
0029: import org.joda.time.chrono.BuddhistChronology;
0030: import org.joda.time.chrono.CopticChronology;
0031: import org.joda.time.chrono.GJChronology;
0032: import org.joda.time.chrono.GregorianChronology;
0033: import org.joda.time.chrono.ISOChronology;
0034: import org.joda.time.format.DateTimeFormat;
0035: import org.joda.time.format.DateTimeFormatter;
0036:
0037: /**
0038: * This class is a Junit unit test for LocalDate.
0039: *
0040: * @author Stephen Colebourne
0041: */
0042: public class TestLocalDateTime_Basics extends TestCase {
0043:
0044: private static final DateTimeZone PARIS = DateTimeZone
0045: .forID("Europe/Paris");
0046: private static final DateTimeZone LONDON = DateTimeZone
0047: .forID("Europe/London");
0048: private static final DateTimeZone TOKYO = DateTimeZone
0049: .forID("Asia/Tokyo");
0050: private static final int OFFSET = 1;
0051: private static final GJChronology GJ_UTC = GJChronology
0052: .getInstanceUTC();
0053: private static final Chronology COPTIC_PARIS = CopticChronology
0054: .getInstance(PARIS);
0055: private static final Chronology COPTIC_LONDON = CopticChronology
0056: .getInstance(LONDON);
0057: private static final Chronology COPTIC_TOKYO = CopticChronology
0058: .getInstance(TOKYO);
0059: private static final Chronology COPTIC_UTC = CopticChronology
0060: .getInstanceUTC();
0061: private static final Chronology ISO_PARIS = ISOChronology
0062: .getInstance(PARIS);
0063: private static final Chronology ISO_LONDON = ISOChronology
0064: .getInstance(LONDON);
0065: private static final Chronology ISO_TOKYO = ISOChronology
0066: .getInstance(TOKYO);
0067: private static final Chronology ISO_UTC = ISOChronology
0068: .getInstanceUTC();
0069: private static final Chronology GREGORIAN_UTC = GregorianChronology
0070: .getInstanceUTC();
0071: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
0072: .getInstance(PARIS);
0073: private static final Chronology BUDDHIST_LONDON = BuddhistChronology
0074: .getInstance(LONDON);
0075: private static final Chronology BUDDHIST_TOKYO = BuddhistChronology
0076: .getInstance(TOKYO);
0077: private static final Chronology BUDDHIST_UTC = BuddhistChronology
0078: .getInstanceUTC();
0079:
0080: // private long TEST_TIME1 =
0081: // (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
0082: // + 12L * DateTimeConstants.MILLIS_PER_HOUR
0083: // + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
0084: //
0085: // private long TEST_TIME2 =
0086: // (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
0087: // + 14L * DateTimeConstants.MILLIS_PER_HOUR
0088: // + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
0089:
0090: private int MILLIS_OF_DAY_UTC = (int) (10L
0091: * DateTimeConstants.MILLIS_PER_HOUR + 20L
0092: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
0093: * DateTimeConstants.MILLIS_PER_SECOND + 40L);
0094:
0095: private long TEST_TIME_NOW_UTC = (31L + 28L + 31L + 30L + 31L + 9L - 1L)
0096: * DateTimeConstants.MILLIS_PER_DAY + MILLIS_OF_DAY_UTC;
0097:
0098: private DateTimeZone zone = null;
0099:
0100: public static void main(String[] args) {
0101: junit.textui.TestRunner.run(suite());
0102: }
0103:
0104: public static TestSuite suite() {
0105: return new TestSuite(TestLocalDateTime_Basics.class);
0106: }
0107:
0108: public TestLocalDateTime_Basics(String name) {
0109: super (name);
0110: }
0111:
0112: protected void setUp() throws Exception {
0113: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC);
0114: zone = DateTimeZone.getDefault();
0115: DateTimeZone.setDefault(LONDON);
0116: }
0117:
0118: protected void tearDown() throws Exception {
0119: DateTimeUtils.setCurrentMillisSystem();
0120: DateTimeZone.setDefault(zone);
0121: zone = null;
0122: }
0123:
0124: //-----------------------------------------------------------------------
0125: public void testGet_DateTimeFieldType() {
0126: LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30,
0127: 40);
0128: assertEquals(1970, test.get(DateTimeFieldType.year()));
0129: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
0130: assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
0131: assertEquals(2, test.get(DateTimeFieldType.dayOfWeek()));
0132: assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
0133: assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear()));
0134: assertEquals(1970, test.get(DateTimeFieldType.weekyear()));
0135: assertEquals(10, test.get(DateTimeFieldType.hourOfDay()));
0136: assertEquals(20, test.get(DateTimeFieldType.minuteOfHour()));
0137: assertEquals(30, test.get(DateTimeFieldType.secondOfMinute()));
0138: assertEquals(40, test.get(DateTimeFieldType.millisOfSecond()));
0139: assertEquals(MILLIS_OF_DAY_UTC / 60000, test
0140: .get(DateTimeFieldType.minuteOfDay()));
0141: assertEquals(MILLIS_OF_DAY_UTC / 1000, test
0142: .get(DateTimeFieldType.secondOfDay()));
0143: assertEquals(MILLIS_OF_DAY_UTC, test.get(DateTimeFieldType
0144: .millisOfDay()));
0145: assertEquals(10, test.get(DateTimeFieldType.hourOfHalfday()));
0146: assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType
0147: .halfdayOfDay()));
0148:
0149: test = new LocalDateTime(1970, 6, 9, 12, 30);
0150: assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday()));
0151: assertEquals(12, test.get(DateTimeFieldType
0152: .clockhourOfHalfday()));
0153: assertEquals(12, test.get(DateTimeFieldType.clockhourOfDay()));
0154: assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType
0155: .halfdayOfDay()));
0156: test = new LocalDateTime(1970, 6, 9, 14, 30);
0157: assertEquals(2, test.get(DateTimeFieldType.hourOfHalfday()));
0158: assertEquals(2, test
0159: .get(DateTimeFieldType.clockhourOfHalfday()));
0160: assertEquals(14, test.get(DateTimeFieldType.clockhourOfDay()));
0161: assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType
0162: .halfdayOfDay()));
0163: test = new LocalDateTime(1970, 6, 9, 0, 30);
0164: assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday()));
0165: assertEquals(12, test.get(DateTimeFieldType
0166: .clockhourOfHalfday()));
0167: assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay()));
0168: assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType
0169: .halfdayOfDay()));
0170: try {
0171: test.get(null);
0172: fail();
0173: } catch (IllegalArgumentException ex) {
0174: }
0175: }
0176:
0177: public void testSize() {
0178: LocalDateTime test = new LocalDateTime();
0179: assertEquals(4, test.size());
0180: }
0181:
0182: public void testGetFieldType_int() {
0183: LocalDateTime test = new LocalDateTime(COPTIC_PARIS);
0184: assertSame(DateTimeFieldType.year(), test.getFieldType(0));
0185: assertSame(DateTimeFieldType.monthOfYear(), test
0186: .getFieldType(1));
0187: assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2));
0188: assertSame(DateTimeFieldType.millisOfDay(), test
0189: .getFieldType(3));
0190: try {
0191: test.getFieldType(-1);
0192: } catch (IndexOutOfBoundsException ex) {
0193: }
0194: try {
0195: test.getFieldType(3);
0196: } catch (IndexOutOfBoundsException ex) {
0197: }
0198: }
0199:
0200: public void testGetFieldTypes() {
0201: LocalDateTime test = new LocalDateTime(COPTIC_PARIS);
0202: DateTimeFieldType[] fields = test.getFieldTypes();
0203: assertSame(DateTimeFieldType.year(), fields[0]);
0204: assertSame(DateTimeFieldType.monthOfYear(), fields[1]);
0205: assertSame(DateTimeFieldType.dayOfMonth(), fields[2]);
0206: assertSame(DateTimeFieldType.millisOfDay(), fields[3]);
0207: assertNotSame(test.getFieldTypes(), test.getFieldTypes());
0208: }
0209:
0210: public void testGetField_int() {
0211: LocalDateTime test = new LocalDateTime(COPTIC_PARIS);
0212: assertSame(COPTIC_UTC.year(), test.getField(0));
0213: assertSame(COPTIC_UTC.monthOfYear(), test.getField(1));
0214: assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2));
0215: assertSame(COPTIC_UTC.millisOfDay(), test.getField(3));
0216: try {
0217: test.getField(-1);
0218: } catch (IndexOutOfBoundsException ex) {
0219: }
0220: try {
0221: test.getField(3);
0222: } catch (IndexOutOfBoundsException ex) {
0223: }
0224: }
0225:
0226: public void testGetFields() {
0227: LocalDateTime test = new LocalDateTime(COPTIC_PARIS);
0228: DateTimeField[] fields = test.getFields();
0229: assertSame(COPTIC_UTC.year(), fields[0]);
0230: assertSame(COPTIC_UTC.monthOfYear(), fields[1]);
0231: assertSame(COPTIC_UTC.dayOfMonth(), fields[2]);
0232: assertSame(COPTIC_UTC.millisOfDay(), fields[3]);
0233: assertNotSame(test.getFields(), test.getFields());
0234: }
0235:
0236: public void testGetValue_int() {
0237: LocalDateTime test = new LocalDateTime(ISO_UTC);
0238: assertEquals(1970, test.getValue(0));
0239: assertEquals(6, test.getValue(1));
0240: assertEquals(9, test.getValue(2));
0241: assertEquals(MILLIS_OF_DAY_UTC, test.getValue(3));
0242: try {
0243: test.getValue(-1);
0244: } catch (IndexOutOfBoundsException ex) {
0245: }
0246: try {
0247: test.getValue(3);
0248: } catch (IndexOutOfBoundsException ex) {
0249: }
0250: }
0251:
0252: public void testGetValues() {
0253: LocalDateTime test = new LocalDateTime(ISO_UTC);
0254: int[] values = test.getValues();
0255: assertEquals(1970, values[0]);
0256: assertEquals(6, values[1]);
0257: assertEquals(9, values[2]);
0258: assertEquals(MILLIS_OF_DAY_UTC, values[3]);
0259: assertNotSame(test.getValues(), test.getValues());
0260: }
0261:
0262: public void testIsSupported_DateTimeFieldType() {
0263: LocalDateTime test = new LocalDateTime();
0264: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
0265: assertEquals(true, test.isSupported(DateTimeFieldType
0266: .monthOfYear()));
0267: assertEquals(true, test.isSupported(DateTimeFieldType
0268: .dayOfMonth()));
0269: assertEquals(true, test.isSupported(DateTimeFieldType
0270: .dayOfWeek()));
0271: assertEquals(true, test.isSupported(DateTimeFieldType
0272: .dayOfYear()));
0273: assertEquals(true, test.isSupported(DateTimeFieldType
0274: .weekOfWeekyear()));
0275: assertEquals(true, test.isSupported(DateTimeFieldType
0276: .weekyear()));
0277: assertEquals(true, test.isSupported(DateTimeFieldType
0278: .yearOfCentury()));
0279: assertEquals(true, test.isSupported(DateTimeFieldType
0280: .yearOfEra()));
0281: assertEquals(true, test.isSupported(DateTimeFieldType
0282: .centuryOfEra()));
0283: assertEquals(true, test.isSupported(DateTimeFieldType
0284: .weekyearOfCentury()));
0285: assertEquals(true, test.isSupported(DateTimeFieldType.era()));
0286:
0287: assertEquals(true, test.isSupported(DateTimeFieldType
0288: .hourOfDay()));
0289: assertEquals(true, test.isSupported(DateTimeFieldType
0290: .minuteOfHour()));
0291: assertEquals(true, test.isSupported(DateTimeFieldType
0292: .secondOfMinute()));
0293: assertEquals(true, test.isSupported(DateTimeFieldType
0294: .millisOfSecond()));
0295: assertEquals(true, test.isSupported(DateTimeFieldType
0296: .minuteOfDay()));
0297: assertEquals(true, test.isSupported(DateTimeFieldType
0298: .secondOfDay()));
0299: assertEquals(true, test.isSupported(DateTimeFieldType
0300: .millisOfDay()));
0301: assertEquals(true, test.isSupported(DateTimeFieldType
0302: .hourOfHalfday()));
0303: assertEquals(true, test.isSupported(DateTimeFieldType
0304: .halfdayOfDay()));
0305: assertEquals(true, test.isSupported(DateTimeFieldType
0306: .clockhourOfHalfday()));
0307: assertEquals(true, test.isSupported(DateTimeFieldType
0308: .clockhourOfDay()));
0309:
0310: assertEquals(false, test.isSupported((DateTimeFieldType) null));
0311: }
0312:
0313: public void testIsSupported_DurationFieldType() {
0314: LocalDateTime test = new LocalDateTime();
0315: assertEquals(false, test.isSupported(DurationFieldType.eras()));
0316: assertEquals(true, test.isSupported(DurationFieldType
0317: .centuries()));
0318: assertEquals(true, test.isSupported(DurationFieldType.years()));
0319: assertEquals(true, test.isSupported(DurationFieldType.months()));
0320: assertEquals(true, test.isSupported(DurationFieldType
0321: .weekyears()));
0322: assertEquals(true, test.isSupported(DurationFieldType.weeks()));
0323: assertEquals(true, test.isSupported(DurationFieldType.days()));
0324:
0325: assertEquals(true, test.isSupported(DurationFieldType.hours()));
0326: assertEquals(true, test
0327: .isSupported(DurationFieldType.minutes()));
0328: assertEquals(true, test
0329: .isSupported(DurationFieldType.seconds()));
0330: assertEquals(true, test.isSupported(DurationFieldType.millis()));
0331: assertEquals(true, test.isSupported(DurationFieldType
0332: .halfdays()));
0333:
0334: assertEquals(false, test.isSupported((DurationFieldType) null));
0335: }
0336:
0337: public void testEqualsHashCode() {
0338: LocalDateTime test1 = new LocalDateTime(1970, 6, 9, 10, 20, 30,
0339: 40, COPTIC_PARIS);
0340: LocalDateTime test2 = new LocalDateTime(1970, 6, 9, 10, 20, 30,
0341: 40, COPTIC_PARIS);
0342: assertEquals(true, test1.equals(test2));
0343: assertEquals(true, test2.equals(test1));
0344: assertEquals(true, test1.equals(test1));
0345: assertEquals(true, test2.equals(test2));
0346: assertEquals(true, test1.hashCode() == test2.hashCode());
0347: assertEquals(true, test1.hashCode() == test1.hashCode());
0348: assertEquals(true, test2.hashCode() == test2.hashCode());
0349:
0350: LocalDateTime test3 = new LocalDateTime(1971, 6, 9, 10, 20, 30,
0351: 40);
0352: assertEquals(false, test1.equals(test3));
0353: assertEquals(false, test2.equals(test3));
0354: assertEquals(false, test3.equals(test1));
0355: assertEquals(false, test3.equals(test2));
0356: assertEquals(false, test1.hashCode() == test3.hashCode());
0357: assertEquals(false, test2.hashCode() == test3.hashCode());
0358:
0359: assertEquals(false, test1.equals("Hello"));
0360: assertEquals(true, test1.equals(new MockInstant()));
0361: Partial partial = new Partial(new DateTimeFieldType[] {
0362: DateTimeFieldType.year(),
0363: DateTimeFieldType.monthOfYear(),
0364: DateTimeFieldType.dayOfMonth(),
0365: DateTimeFieldType.millisOfDay() }, new int[] { 1970, 6,
0366: 9, MILLIS_OF_DAY_UTC }, COPTIC_PARIS);
0367: assertEquals(true, test1.equals(partial));
0368: assertEquals(true, test1.hashCode() == partial.hashCode());
0369: assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
0370: }
0371:
0372: class MockInstant extends MockPartial {
0373: public Chronology getChronology() {
0374: return COPTIC_UTC;
0375: }
0376:
0377: public DateTimeField[] getFields() {
0378: return new DateTimeField[] { COPTIC_UTC.year(),
0379: COPTIC_UTC.monthOfYear(), COPTIC_UTC.dayOfMonth(),
0380: COPTIC_UTC.millisOfDay(), };
0381: }
0382:
0383: public int[] getValues() {
0384: return new int[] { 1970, 6, 9, MILLIS_OF_DAY_UTC };
0385: }
0386: }
0387:
0388: //-----------------------------------------------------------------------
0389: public void testCompareTo() {
0390: LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30,
0391: 40);
0392: LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20,
0393: 30, 40);
0394: assertEquals(0, test1.compareTo(test1a));
0395: assertEquals(0, test1a.compareTo(test1));
0396: assertEquals(0, test1.compareTo(test1));
0397: assertEquals(0, test1a.compareTo(test1a));
0398:
0399: LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0400: 40);
0401: assertEquals(-1, test1.compareTo(test2));
0402: assertEquals(+1, test2.compareTo(test1));
0403:
0404: LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0405: 40, GREGORIAN_UTC);
0406: assertEquals(-1, test1.compareTo(test3));
0407: assertEquals(+1, test3.compareTo(test1));
0408: assertEquals(0, test3.compareTo(test2));
0409:
0410: DateTimeFieldType[] types = new DateTimeFieldType[] {
0411: DateTimeFieldType.year(),
0412: DateTimeFieldType.monthOfYear(),
0413: DateTimeFieldType.dayOfMonth(),
0414: DateTimeFieldType.millisOfDay(), };
0415: int[] values = new int[] { 2005, 6, 2, MILLIS_OF_DAY_UTC };
0416: Partial p = new Partial(types, values);
0417: assertEquals(0, test1.compareTo(p));
0418: try {
0419: test1.compareTo(null);
0420: fail();
0421: } catch (NullPointerException ex) {
0422: }
0423: try {
0424: test1.compareTo(new Date());
0425: fail();
0426: } catch (ClassCastException ex) {
0427: }
0428: try {
0429: test1.compareTo(new YearMonthDay());
0430: fail();
0431: } catch (ClassCastException ex) {
0432: }
0433: try {
0434: test1.compareTo(new TimeOfDay());
0435: fail();
0436: } catch (ClassCastException ex) {
0437: }
0438: Partial partial = new Partial().with(
0439: DateTimeFieldType.centuryOfEra(), 1).with(
0440: DateTimeFieldType.halfdayOfDay(), 0).with(
0441: DateTimeFieldType.dayOfMonth(), 9);
0442: try {
0443: new LocalDateTime(1970, 6, 9, 10, 20, 30, 40)
0444: .compareTo(partial);
0445: fail();
0446: } catch (ClassCastException ex) {
0447: }
0448: }
0449:
0450: //-----------------------------------------------------------------------
0451: public void testIsEqual_LocalDateTime() {
0452: LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30,
0453: 40);
0454: LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20,
0455: 30, 40);
0456: assertEquals(true, test1.isEqual(test1a));
0457: assertEquals(true, test1a.isEqual(test1));
0458: assertEquals(true, test1.isEqual(test1));
0459: assertEquals(true, test1a.isEqual(test1a));
0460:
0461: LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0462: 40);
0463: assertEquals(false, test1.isEqual(test2));
0464: assertEquals(false, test2.isEqual(test1));
0465:
0466: LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0467: 40, GREGORIAN_UTC);
0468: assertEquals(false, test1.isEqual(test3));
0469: assertEquals(false, test3.isEqual(test1));
0470: assertEquals(true, test3.isEqual(test2));
0471:
0472: try {
0473: new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isEqual(null);
0474: fail();
0475: } catch (IllegalArgumentException ex) {
0476: }
0477: }
0478:
0479: //-----------------------------------------------------------------------
0480: public void testIsBefore_LocalDateTime() {
0481: LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30,
0482: 40);
0483: LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20,
0484: 30, 40);
0485: assertEquals(false, test1.isBefore(test1a));
0486: assertEquals(false, test1a.isBefore(test1));
0487: assertEquals(false, test1.isBefore(test1));
0488: assertEquals(false, test1a.isBefore(test1a));
0489:
0490: LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0491: 40);
0492: assertEquals(true, test1.isBefore(test2));
0493: assertEquals(false, test2.isBefore(test1));
0494:
0495: LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0496: 40, GREGORIAN_UTC);
0497: assertEquals(true, test1.isBefore(test3));
0498: assertEquals(false, test3.isBefore(test1));
0499: assertEquals(false, test3.isBefore(test2));
0500:
0501: try {
0502: new LocalDateTime(2005, 7, 2, 10, 20, 30, 40)
0503: .isBefore(null);
0504: fail();
0505: } catch (IllegalArgumentException ex) {
0506: }
0507: }
0508:
0509: //-----------------------------------------------------------------------
0510: public void testIsAfter_LocalDateTime() {
0511: LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30,
0512: 40);
0513: LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20,
0514: 30, 40);
0515: assertEquals(false, test1.isAfter(test1a));
0516: assertEquals(false, test1a.isAfter(test1));
0517: assertEquals(false, test1.isAfter(test1));
0518: assertEquals(false, test1a.isAfter(test1a));
0519:
0520: LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0521: 40);
0522: assertEquals(false, test1.isAfter(test2));
0523: assertEquals(true, test2.isAfter(test1));
0524:
0525: LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30,
0526: 40, GREGORIAN_UTC);
0527: assertEquals(false, test1.isAfter(test3));
0528: assertEquals(true, test3.isAfter(test1));
0529: assertEquals(false, test3.isAfter(test2));
0530:
0531: try {
0532: new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isAfter(null);
0533: fail();
0534: } catch (IllegalArgumentException ex) {
0535: }
0536: }
0537:
0538: //-----------------------------------------------------------------------
0539: public void testWithDate() {
0540: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0541: 40);
0542: LocalDateTime result = test.withDate(2006, 2, 1);
0543:
0544: check(test, 2004, 6, 9, 10, 20, 30, 40);
0545: check(result, 2006, 2, 1, 10, 20, 30, 40);
0546: }
0547:
0548: //-----------------------------------------------------------------------
0549: public void testWithTime() {
0550: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0551: 40);
0552: LocalDateTime result = test.withTime(9, 8, 7, 6);
0553:
0554: check(test, 2004, 6, 9, 10, 20, 30, 40);
0555: check(result, 2004, 6, 9, 9, 8, 7, 6);
0556: }
0557:
0558: //-----------------------------------------------------------------------
0559: public void testWithField_DateTimeFieldType_int_1() {
0560: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0561: 40);
0562: LocalDateTime result = test.withField(DateTimeFieldType.year(),
0563: 2006);
0564:
0565: assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40),
0566: test);
0567: assertEquals(new LocalDateTime(2006, 6, 9, 10, 20, 30, 40),
0568: result);
0569: }
0570:
0571: public void testWithField_DateTimeFieldType_int_2() {
0572: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0573: 40);
0574: try {
0575: test.withField(null, 6);
0576: fail();
0577: } catch (IllegalArgumentException ex) {
0578: }
0579: }
0580:
0581: public void testWithField_DateTimeFieldType_int_3() {
0582: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0583: 40);
0584: LocalDateTime result = test.withField(DateTimeFieldType.year(),
0585: 2004);
0586: assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40),
0587: test);
0588: assertSame(test, result);
0589: }
0590:
0591: //-----------------------------------------------------------------------
0592: public void testWithFieldAdded_DurationFieldType_int_1() {
0593: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0594: 40);
0595: LocalDateTime result = test.withFieldAdded(DurationFieldType
0596: .years(), 6);
0597:
0598: assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40),
0599: test);
0600: assertEquals(new LocalDateTime(2010, 6, 9, 10, 20, 30, 40),
0601: result);
0602: }
0603:
0604: public void testWithFieldAdded_DurationFieldType_int_2() {
0605: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0606: 40);
0607: try {
0608: test.withFieldAdded(null, 0);
0609: fail();
0610: } catch (IllegalArgumentException ex) {
0611: }
0612: }
0613:
0614: public void testWithFieldAdded_DurationFieldType_int_3() {
0615: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0616: 40);
0617: try {
0618: test.withFieldAdded(null, 6);
0619: fail();
0620: } catch (IllegalArgumentException ex) {
0621: }
0622: }
0623:
0624: public void testWithFieldAdded_DurationFieldType_int_4() {
0625: LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30,
0626: 40);
0627: LocalDateTime result = test.withFieldAdded(DurationFieldType
0628: .years(), 0);
0629: assertSame(test, result);
0630: }
0631:
0632: //-----------------------------------------------------------------------
0633: public void testPlus_RP() {
0634: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0635: 40, BUDDHIST_LONDON);
0636: LocalDateTime result = test.plus(new Period(1, 2, 3, 4, 29, 6,
0637: 7, 8));
0638: LocalDateTime expected = new LocalDateTime(2003, 7, 29, 15, 26,
0639: 37, 48, BUDDHIST_LONDON);
0640: assertEquals(expected, result);
0641:
0642: result = test.plus((ReadablePeriod) null);
0643: assertSame(test, result);
0644: }
0645:
0646: public void testPlusYears_int() {
0647: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0648: 40, BUDDHIST_LONDON);
0649: LocalDateTime result = test.plusYears(1);
0650: LocalDateTime expected = new LocalDateTime(2003, 5, 3, 10, 20,
0651: 30, 40, BUDDHIST_LONDON);
0652: assertEquals(expected, result);
0653:
0654: result = test.plusYears(0);
0655: assertSame(test, result);
0656: }
0657:
0658: public void testPlusMonths_int() {
0659: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0660: 40, BUDDHIST_LONDON);
0661: LocalDateTime result = test.plusMonths(1);
0662: LocalDateTime expected = new LocalDateTime(2002, 6, 3, 10, 20,
0663: 30, 40, BUDDHIST_LONDON);
0664: assertEquals(expected, result);
0665:
0666: result = test.plusMonths(0);
0667: assertSame(test, result);
0668: }
0669:
0670: public void testPlusWeeks_int() {
0671: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0672: 40, BUDDHIST_LONDON);
0673: LocalDateTime result = test.plusWeeks(1);
0674: LocalDateTime expected = new LocalDateTime(2002, 5, 10, 10, 20,
0675: 30, 40, BUDDHIST_LONDON);
0676: assertEquals(expected, result);
0677:
0678: result = test.plusWeeks(0);
0679: assertSame(test, result);
0680: }
0681:
0682: public void testPlusDays_int() {
0683: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0684: 40, BUDDHIST_LONDON);
0685: LocalDateTime result = test.plusDays(1);
0686: LocalDateTime expected = new LocalDateTime(2002, 5, 4, 10, 20,
0687: 30, 40, BUDDHIST_LONDON);
0688: assertEquals(expected, result);
0689:
0690: result = test.plusDays(0);
0691: assertSame(test, result);
0692: }
0693:
0694: public void testPlusHours_int() {
0695: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0696: 40, BUDDHIST_LONDON);
0697: LocalDateTime result = test.plusHours(1);
0698: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 11, 20,
0699: 30, 40, BUDDHIST_LONDON);
0700: assertEquals(expected, result);
0701:
0702: result = test.plusHours(0);
0703: assertSame(test, result);
0704: }
0705:
0706: public void testPlusMinutes_int() {
0707: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0708: 40, BUDDHIST_LONDON);
0709: LocalDateTime result = test.plusMinutes(1);
0710: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 21,
0711: 30, 40, BUDDHIST_LONDON);
0712: assertEquals(expected, result);
0713:
0714: result = test.plusMinutes(0);
0715: assertSame(test, result);
0716: }
0717:
0718: public void testPlusSeconds_int() {
0719: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0720: 40, BUDDHIST_LONDON);
0721: LocalDateTime result = test.plusSeconds(1);
0722: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20,
0723: 31, 40, BUDDHIST_LONDON);
0724: assertEquals(expected, result);
0725:
0726: result = test.plusSeconds(0);
0727: assertSame(test, result);
0728: }
0729:
0730: public void testPlusMillis_int() {
0731: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0732: 40, BUDDHIST_LONDON);
0733: LocalDateTime result = test.plusMillis(1);
0734: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20,
0735: 30, 41, BUDDHIST_LONDON);
0736: assertEquals(expected, result);
0737:
0738: result = test.plusMillis(0);
0739: assertSame(test, result);
0740: }
0741:
0742: //-----------------------------------------------------------------------
0743: public void testMinus_RP() {
0744: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0745: 40, BUDDHIST_LONDON);
0746: LocalDateTime result = test.minus(new Period(1, 1, 1, 1, 1, 1,
0747: 1, 1));
0748:
0749: LocalDateTime expected = new LocalDateTime(2001, 3, 26, 9, 19,
0750: 29, 39, BUDDHIST_LONDON);
0751: assertEquals(expected, result);
0752:
0753: result = test.minus((ReadablePeriod) null);
0754: assertSame(test, result);
0755: }
0756:
0757: public void testMinusYears_int() {
0758: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0759: 40, BUDDHIST_LONDON);
0760: LocalDateTime result = test.minusYears(1);
0761: LocalDateTime expected = new LocalDateTime(2001, 5, 3, 10, 20,
0762: 30, 40, BUDDHIST_LONDON);
0763: assertEquals(expected, result);
0764:
0765: result = test.minusYears(0);
0766: assertSame(test, result);
0767: }
0768:
0769: public void testMinusMonths_int() {
0770: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0771: 40, BUDDHIST_LONDON);
0772: LocalDateTime result = test.minusMonths(1);
0773: LocalDateTime expected = new LocalDateTime(2002, 4, 3, 10, 20,
0774: 30, 40, BUDDHIST_LONDON);
0775: assertEquals(expected, result);
0776:
0777: result = test.minusMonths(0);
0778: assertSame(test, result);
0779: }
0780:
0781: public void testMinusWeeks_int() {
0782: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0783: 40, BUDDHIST_LONDON);
0784: LocalDateTime result = test.minusWeeks(1);
0785: LocalDateTime expected = new LocalDateTime(2002, 4, 26, 10, 20,
0786: 30, 40, BUDDHIST_LONDON);
0787: assertEquals(expected, result);
0788:
0789: result = test.minusWeeks(0);
0790: assertSame(test, result);
0791: }
0792:
0793: public void testMinusDays_int() {
0794: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0795: 40, BUDDHIST_LONDON);
0796: LocalDateTime result = test.minusDays(1);
0797: LocalDateTime expected = new LocalDateTime(2002, 5, 2, 10, 20,
0798: 30, 40, BUDDHIST_LONDON);
0799: assertEquals(expected, result);
0800:
0801: result = test.minusDays(0);
0802: assertSame(test, result);
0803: }
0804:
0805: public void testMinusHours_int() {
0806: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0807: 40, BUDDHIST_LONDON);
0808: LocalDateTime result = test.minusHours(1);
0809: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 9, 20,
0810: 30, 40, BUDDHIST_LONDON);
0811: assertEquals(expected, result);
0812:
0813: result = test.minusHours(0);
0814: assertSame(test, result);
0815: }
0816:
0817: public void testMinusMinutes_int() {
0818: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0819: 40, BUDDHIST_LONDON);
0820: LocalDateTime result = test.minusMinutes(1);
0821: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 19,
0822: 30, 40, BUDDHIST_LONDON);
0823: assertEquals(expected, result);
0824:
0825: result = test.minusMinutes(0);
0826: assertSame(test, result);
0827: }
0828:
0829: public void testMinusSeconds_int() {
0830: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0831: 40, BUDDHIST_LONDON);
0832: LocalDateTime result = test.minusSeconds(1);
0833: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20,
0834: 29, 40, BUDDHIST_LONDON);
0835: assertEquals(expected, result);
0836:
0837: result = test.minusSeconds(0);
0838: assertSame(test, result);
0839: }
0840:
0841: public void testMinusMillis_int() {
0842: LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30,
0843: 40, BUDDHIST_LONDON);
0844: LocalDateTime result = test.minusMillis(1);
0845: LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20,
0846: 30, 39, BUDDHIST_LONDON);
0847: assertEquals(expected, result);
0848:
0849: result = test.minusMillis(0);
0850: assertSame(test, result);
0851: }
0852:
0853: //-----------------------------------------------------------------------
0854: public void testGetters() {
0855: LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30,
0856: 40, GJ_UTC);
0857: assertEquals(1970, test.getYear());
0858: assertEquals(6, test.getMonthOfYear());
0859: assertEquals(9, test.getDayOfMonth());
0860: assertEquals(160, test.getDayOfYear());
0861: assertEquals(2, test.getDayOfWeek());
0862: assertEquals(24, test.getWeekOfWeekyear());
0863: assertEquals(1970, test.getWeekyear());
0864: assertEquals(70, test.getYearOfCentury());
0865: assertEquals(20, test.getCenturyOfEra());
0866: assertEquals(1970, test.getYearOfEra());
0867: assertEquals(DateTimeConstants.AD, test.getEra());
0868: assertEquals(10, test.getHourOfDay());
0869: assertEquals(20, test.getMinuteOfHour());
0870: assertEquals(30, test.getSecondOfMinute());
0871: assertEquals(40, test.getMillisOfSecond());
0872: assertEquals(MILLIS_OF_DAY_UTC, test.getMillisOfDay());
0873: }
0874:
0875: //-----------------------------------------------------------------------
0876: public void testWithers() {
0877: LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30,
0878: 40, GJ_UTC);
0879: check(test.withYear(2000), 2000, 6, 9, 10, 20, 30, 40);
0880: check(test.withMonthOfYear(2), 1970, 2, 9, 10, 20, 30, 40);
0881: check(test.withDayOfMonth(2), 1970, 6, 2, 10, 20, 30, 40);
0882: check(test.withDayOfYear(6), 1970, 1, 6, 10, 20, 30, 40);
0883: check(test.withDayOfWeek(6), 1970, 6, 13, 10, 20, 30, 40);
0884: check(test.withWeekOfWeekyear(6), 1970, 2, 3, 10, 20, 30, 40);
0885: check(test.withWeekyear(1971), 1971, 6, 15, 10, 20, 30, 40);
0886: check(test.withYearOfCentury(60), 1960, 6, 9, 10, 20, 30, 40);
0887: check(test.withCenturyOfEra(21), 2070, 6, 9, 10, 20, 30, 40);
0888: check(test.withYearOfEra(1066), 1066, 6, 9, 10, 20, 30, 40);
0889: check(test.withEra(DateTimeConstants.BC), -1970, 6, 9, 10, 20,
0890: 30, 40);
0891: check(test.withHourOfDay(6), 1970, 6, 9, 6, 20, 30, 40);
0892: check(test.withMinuteOfHour(6), 1970, 6, 9, 10, 6, 30, 40);
0893: check(test.withSecondOfMinute(6), 1970, 6, 9, 10, 20, 6, 40);
0894: check(test.withMillisOfSecond(6), 1970, 6, 9, 10, 20, 30, 6);
0895: check(test.withMillisOfDay(61234), 1970, 6, 9, 0, 1, 1, 234);
0896: try {
0897: test.withMonthOfYear(0);
0898: fail();
0899: } catch (IllegalArgumentException ex) {
0900: }
0901: try {
0902: test.withMonthOfYear(13);
0903: fail();
0904: } catch (IllegalArgumentException ex) {
0905: }
0906: }
0907:
0908: //-----------------------------------------------------------------------
0909: public void testToDateTime() {
0910: LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9,
0911: COPTIC_PARIS); // PARIS irrelevant
0912:
0913: DateTime test = base.toDateTime();
0914: check(base, 2005, 6, 9, 6, 7, 8, 9);
0915: DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9,
0916: COPTIC_LONDON);
0917: assertEquals(expected, test);
0918: }
0919:
0920: //-----------------------------------------------------------------------
0921: public void testToDateTime_Zone() {
0922: LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9,
0923: COPTIC_PARIS); // PARIS irrelevant
0924:
0925: DateTime test = base.toDateTime(TOKYO);
0926: check(base, 2005, 6, 9, 6, 7, 8, 9);
0927: DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9,
0928: COPTIC_TOKYO);
0929: assertEquals(expected, test);
0930: }
0931:
0932: public void testToDateTime_nullZone() {
0933: LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9,
0934: COPTIC_PARIS); // PARIS irrelevant
0935:
0936: DateTime test = base.toDateTime((DateTimeZone) null);
0937: check(base, 2005, 6, 9, 6, 7, 8, 9);
0938: DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9,
0939: COPTIC_LONDON);
0940: assertEquals(expected, test);
0941: }
0942:
0943: //-----------------------------------------------------------------------
0944: public void testToLocalDate() {
0945: LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9,
0946: COPTIC_PARIS); // PARIS irrelevant
0947: LocalDate expected = new LocalDate(2005, 6, 9, COPTIC_LONDON);
0948: assertEquals(expected, base.toLocalDate());
0949: }
0950:
0951: public void testToLocalTime() {
0952: LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9,
0953: COPTIC_PARIS); // PARIS irrelevant
0954: LocalTime expected = new LocalTime(6, 7, 8, 9, COPTIC_LONDON);
0955: assertEquals(expected, base.toLocalTime());
0956: }
0957:
0958: //-----------------------------------------------------------------------
0959: public void testToDateTime_RI() {
0960: LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30,
0961: 40, COPTIC_PARIS);
0962: DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7,
0963: BUDDHIST_TOKYO);
0964:
0965: DateTime test = base.toDateTime(dt);
0966: check(base, 2005, 6, 9, 10, 20, 30, 40);
0967: DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40,
0968: BUDDHIST_TOKYO);
0969: assertEquals(expected, test);
0970: }
0971:
0972: public void testToDateTime_nullRI() {
0973: LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30,
0974: 40, COPTIC_PARIS);
0975:
0976: DateTime test = base.toDateTime((ReadableInstant) null);
0977: check(base, 2005, 6, 9, 10, 20, 30, 40);
0978: DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40,
0979: ISO_LONDON);
0980: assertEquals(expected, test);
0981: }
0982:
0983: //-----------------------------------------------------------------------
0984: public void testProperty() {
0985: LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30,
0986: 40, GJ_UTC);
0987: assertEquals(test.year(), test.property(DateTimeFieldType
0988: .year()));
0989: assertEquals(test.monthOfYear(), test
0990: .property(DateTimeFieldType.monthOfYear()));
0991: assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType
0992: .dayOfMonth()));
0993: assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType
0994: .dayOfWeek()));
0995: assertEquals(test.dayOfYear(), test.property(DateTimeFieldType
0996: .dayOfYear()));
0997: assertEquals(test.weekOfWeekyear(), test
0998: .property(DateTimeFieldType.weekOfWeekyear()));
0999: assertEquals(test.weekyear(), test.property(DateTimeFieldType
1000: .weekyear()));
1001: assertEquals(test.yearOfCentury(), test
1002: .property(DateTimeFieldType.yearOfCentury()));
1003: assertEquals(test.yearOfEra(), test.property(DateTimeFieldType
1004: .yearOfEra()));
1005: assertEquals(test.centuryOfEra(), test
1006: .property(DateTimeFieldType.centuryOfEra()));
1007: assertEquals(test.era(), test.property(DateTimeFieldType.era()));
1008: assertEquals(test.hourOfDay(), test.property(DateTimeFieldType
1009: .hourOfDay()));
1010: assertEquals(test.minuteOfHour(), test
1011: .property(DateTimeFieldType.minuteOfHour()));
1012: assertEquals(test.secondOfMinute(), test
1013: .property(DateTimeFieldType.secondOfMinute()));
1014: assertEquals(test.millisOfSecond(), test
1015: .property(DateTimeFieldType.millisOfSecond()));
1016: assertEquals(test.millisOfDay(), test
1017: .property(DateTimeFieldType.millisOfDay()));
1018:
1019: try {
1020: test.property(null);
1021: fail();
1022: } catch (IllegalArgumentException ex) {
1023: }
1024: assertEquals(test, test.property(
1025: DateTimeFieldType.minuteOfDay()).getLocalDateTime());
1026: }
1027:
1028: //-----------------------------------------------------------------------
1029: public void testSerialization() throws Exception {
1030: LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30,
1031: 40, COPTIC_PARIS);
1032:
1033: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1034: ObjectOutputStream oos = new ObjectOutputStream(baos);
1035: oos.writeObject(test);
1036: byte[] bytes = baos.toByteArray();
1037: oos.close();
1038:
1039: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
1040: ObjectInputStream ois = new ObjectInputStream(bais);
1041: LocalDateTime result = (LocalDateTime) ois.readObject();
1042: ois.close();
1043:
1044: assertEquals(test, result);
1045: assertTrue(Arrays.equals(test.getValues(), result.getValues()));
1046: assertTrue(Arrays.equals(test.getFields(), result.getFields()));
1047: assertEquals(test.getChronology(), result.getChronology());
1048: }
1049:
1050: //-----------------------------------------------------------------------
1051: public void testToString() {
1052: LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30,
1053: 40);
1054: assertEquals("2002-06-09T10:20:30.040", test.toString());
1055: }
1056:
1057: //-----------------------------------------------------------------------
1058: public void testToString_String() {
1059: LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30,
1060: 40);
1061: assertEquals("2002 10", test.toString("yyyy HH"));
1062: assertEquals("2002-06-09T10:20:30.040", test
1063: .toString((String) null));
1064: }
1065:
1066: //-----------------------------------------------------------------------
1067: public void testToString_String_Locale() {
1068: LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30,
1069: 40);
1070: assertEquals("Tue 9/6", test
1071: .toString("EEE d/M", Locale.ENGLISH));
1072: assertEquals("mar. 9/6", test
1073: .toString("EEE d/M", Locale.FRENCH));
1074: assertEquals("1970-06-09T10:20:30.040", test.toString(null,
1075: Locale.ENGLISH));
1076: assertEquals("Tue 9/6", test.toString("EEE d/M", null));
1077: assertEquals("1970-06-09T10:20:30.040", test.toString(null,
1078: null));
1079: }
1080:
1081: //-----------------------------------------------------------------------
1082: public void testToString_DTFormatter() {
1083: LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30,
1084: 40);
1085: assertEquals("2002 10", test.toString(DateTimeFormat
1086: .forPattern("yyyy HH")));
1087: assertEquals("2002-06-09T10:20:30.040", test
1088: .toString((DateTimeFormatter) null));
1089: }
1090:
1091: //-----------------------------------------------------------------------
1092: private void check(LocalDateTime test, int year, int month,
1093: int day, int hour, int min, int sec, int mil) {
1094: assertEquals(year, test.getYear());
1095: assertEquals(month, test.getMonthOfYear());
1096: assertEquals(day, test.getDayOfMonth());
1097: assertEquals(hour, test.getHourOfDay());
1098: assertEquals(min, test.getMinuteOfHour());
1099: assertEquals(sec, test.getSecondOfMinute());
1100: assertEquals(mil, test.getMillisOfSecond());
1101: }
1102: }
|