001: /*
002: * Copyright 2001-2006 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.lang.reflect.Modifier;
023: import java.util.Date;
024: import java.util.Locale;
025: import java.util.TimeZone;
026:
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.joda.time.base.AbstractInstant;
031: import org.joda.time.chrono.GregorianChronology;
032: import org.joda.time.chrono.ISOChronology;
033:
034: /**
035: * This class is a Junit unit test for Instant.
036: *
037: * @author Stephen Colebourne
038: */
039: public class TestInstant_Basics extends TestCase {
040: // Test in 2002/03 as time zones are more well known
041: // (before the late 90's they were all over the place)
042:
043: private static final DateTimeZone PARIS = DateTimeZone
044: .forID("Europe/Paris");
045: private static final DateTimeZone LONDON = DateTimeZone
046: .forID("Europe/London");
047:
048: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
049: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
050: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
051: + 365 + 365 + 366 + 365;
052: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
053: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
054: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
055: + 365 + 365 + 366 + 365 + 365;
056:
057: // 2002-06-09
058: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
059: + 31L + 9L - 1L)
060: * DateTimeConstants.MILLIS_PER_DAY;
061:
062: // 2002-04-05
063: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
064: * DateTimeConstants.MILLIS_PER_DAY + 12L
065: * DateTimeConstants.MILLIS_PER_HOUR + 24L
066: * DateTimeConstants.MILLIS_PER_MINUTE;
067:
068: // 2003-05-06
069: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
070: * DateTimeConstants.MILLIS_PER_DAY
071: + 14L
072: * DateTimeConstants.MILLIS_PER_HOUR
073: + 28L
074: * DateTimeConstants.MILLIS_PER_MINUTE;
075:
076: private DateTimeZone originalDateTimeZone = null;
077: private TimeZone originalTimeZone = null;
078: private Locale originalLocale = null;
079:
080: public static void main(String[] args) {
081: junit.textui.TestRunner.run(suite());
082: }
083:
084: public static TestSuite suite() {
085: return new TestSuite(TestInstant_Basics.class);
086: }
087:
088: public TestInstant_Basics(String name) {
089: super (name);
090: }
091:
092: protected void setUp() throws Exception {
093: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
094: originalDateTimeZone = DateTimeZone.getDefault();
095: originalTimeZone = TimeZone.getDefault();
096: originalLocale = Locale.getDefault();
097: DateTimeZone.setDefault(LONDON);
098: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
099: Locale.setDefault(Locale.UK);
100: }
101:
102: protected void tearDown() throws Exception {
103: DateTimeUtils.setCurrentMillisSystem();
104: DateTimeZone.setDefault(originalDateTimeZone);
105: TimeZone.setDefault(originalTimeZone);
106: Locale.setDefault(originalLocale);
107: originalDateTimeZone = null;
108: originalTimeZone = null;
109: originalLocale = null;
110: }
111:
112: //-----------------------------------------------------------------------
113: public void testTest() {
114: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
115: TEST_TIME_NOW).toString());
116: assertEquals("2002-04-05T12:24:00.000Z",
117: new Instant(TEST_TIME1).toString());
118: assertEquals("2003-05-06T14:28:00.000Z",
119: new Instant(TEST_TIME2).toString());
120: }
121:
122: //-----------------------------------------------------------------------
123: public void testGet_DateTimeFieldType() {
124: Instant test = new Instant(); // 2002-06-09
125: assertEquals(1, test.get(DateTimeFieldType.era()));
126: assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
127: assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
128: assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
129: assertEquals(2002, test.get(DateTimeFieldType.year()));
130: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
131: assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
132: assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
133: assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
134: assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
135: assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
136: assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
137: assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); // UTC zone
138: assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay())); // UTC zone
139: assertEquals(12, test.get(DateTimeFieldType
140: .clockhourOfHalfday())); // UTC zone
141: assertEquals(0, test.get(DateTimeFieldType.hourOfDay())); // UTC zone
142: assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
143: assertEquals(0, test.get(DateTimeFieldType.minuteOfDay()));
144: assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
145: assertEquals(0, test.get(DateTimeFieldType.secondOfDay()));
146: assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
147: assertEquals(0, test.get(DateTimeFieldType.millisOfDay()));
148: try {
149: test.get((DateTimeFieldType) null);
150: fail();
151: } catch (IllegalArgumentException ex) {
152: }
153: }
154:
155: public void testGet_DateTimeField() {
156: Instant test = new Instant(); // 2002-06-09
157: assertEquals(1, test.get(ISOChronology.getInstance().era()));
158: assertEquals(20, test.get(ISOChronology.getInstance()
159: .centuryOfEra()));
160: assertEquals(2, test.get(ISOChronology.getInstance()
161: .yearOfCentury()));
162: assertEquals(2002, test.get(ISOChronology.getInstance()
163: .yearOfEra()));
164: assertEquals(2002, test.get(ISOChronology.getInstance().year()));
165: assertEquals(6, test.get(ISOChronology.getInstance()
166: .monthOfYear()));
167: assertEquals(9, test.get(ISOChronology.getInstance()
168: .dayOfMonth()));
169: assertEquals(2002, test.get(ISOChronology.getInstance()
170: .weekyear()));
171: assertEquals(23, test.get(ISOChronology.getInstance()
172: .weekOfWeekyear()));
173: assertEquals(7, test.get(ISOChronology.getInstance()
174: .dayOfWeek()));
175: assertEquals(160, test.get(ISOChronology.getInstance()
176: .dayOfYear()));
177: assertEquals(0, test.get(ISOChronology.getInstance()
178: .halfdayOfDay()));
179: assertEquals(1, test.get(ISOChronology.getInstance()
180: .hourOfHalfday()));
181: assertEquals(1, test.get(ISOChronology.getInstance()
182: .clockhourOfDay()));
183: assertEquals(1, test.get(ISOChronology.getInstance()
184: .clockhourOfHalfday()));
185: assertEquals(1, test.get(ISOChronology.getInstance()
186: .hourOfDay()));
187: assertEquals(0, test.get(ISOChronology.getInstance()
188: .minuteOfHour()));
189: assertEquals(60, test.get(ISOChronology.getInstance()
190: .minuteOfDay()));
191: assertEquals(0, test.get(ISOChronology.getInstance()
192: .secondOfMinute()));
193: assertEquals(60 * 60, test.get(ISOChronology.getInstance()
194: .secondOfDay()));
195: assertEquals(0, test.get(ISOChronology.getInstance()
196: .millisOfSecond()));
197: assertEquals(60 * 60 * 1000, test.get(ISOChronology
198: .getInstance().millisOfDay()));
199: try {
200: test.get((DateTimeField) null);
201: fail();
202: } catch (IllegalArgumentException ex) {
203: }
204: }
205:
206: public void testGetMethods() {
207: Instant test = new Instant();
208:
209: assertEquals(ISOChronology.getInstanceUTC(), test
210: .getChronology());
211: assertEquals(DateTimeZone.UTC, test.getZone());
212: assertEquals(TEST_TIME_NOW, test.getMillis());
213: }
214:
215: public void testEqualsHashCode() {
216: Instant test1 = new Instant(TEST_TIME1);
217: Instant test2 = new Instant(TEST_TIME1);
218: assertEquals(true, test1.equals(test2));
219: assertEquals(true, test2.equals(test1));
220: assertEquals(true, test1.equals(test1));
221: assertEquals(true, test2.equals(test2));
222: assertEquals(true, test1.hashCode() == test2.hashCode());
223: assertEquals(true, test1.hashCode() == test1.hashCode());
224: assertEquals(true, test2.hashCode() == test2.hashCode());
225:
226: Instant test3 = new Instant(TEST_TIME2);
227: assertEquals(false, test1.equals(test3));
228: assertEquals(false, test2.equals(test3));
229: assertEquals(false, test3.equals(test1));
230: assertEquals(false, test3.equals(test2));
231: assertEquals(false, test1.hashCode() == test3.hashCode());
232: assertEquals(false, test2.hashCode() == test3.hashCode());
233:
234: assertEquals(false, test1.equals("Hello"));
235: assertEquals(true, test1.equals(new MockInstant()));
236: assertEquals(false, test1.equals(new DateTime(TEST_TIME1)));
237: }
238:
239: class MockInstant extends AbstractInstant {
240: public String toString() {
241: return null;
242: }
243:
244: public long getMillis() {
245: return TEST_TIME1;
246: }
247:
248: public Chronology getChronology() {
249: return ISOChronology.getInstanceUTC();
250: }
251: }
252:
253: public void testCompareTo() {
254: Instant test1 = new Instant(TEST_TIME1);
255: Instant test1a = new Instant(TEST_TIME1);
256: assertEquals(0, test1.compareTo(test1a));
257: assertEquals(0, test1a.compareTo(test1));
258: assertEquals(0, test1.compareTo(test1));
259: assertEquals(0, test1a.compareTo(test1a));
260:
261: Instant test2 = new Instant(TEST_TIME2);
262: assertEquals(-1, test1.compareTo(test2));
263: assertEquals(+1, test2.compareTo(test1));
264:
265: DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology
266: .getInstance(PARIS));
267: assertEquals(-1, test1.compareTo(test3));
268: assertEquals(+1, test3.compareTo(test1));
269: assertEquals(0, test3.compareTo(test2));
270:
271: assertEquals(+1, test2.compareTo(new MockInstant()));
272: assertEquals(0, test1.compareTo(new MockInstant()));
273:
274: try {
275: test1.compareTo(null);
276: fail();
277: } catch (NullPointerException ex) {
278: }
279: try {
280: test1.compareTo(new Date());
281: fail();
282: } catch (ClassCastException ex) {
283: }
284: }
285:
286: //-----------------------------------------------------------------------
287: public void testIsEqual_long() {
288: assertEquals(false, new Instant(TEST_TIME1).isEqual(TEST_TIME2));
289: assertEquals(true, new Instant(TEST_TIME1).isEqual(TEST_TIME1));
290: assertEquals(false, new Instant(TEST_TIME2).isEqual(TEST_TIME1));
291: }
292:
293: public void testIsEqualNow() {
294: assertEquals(false, new Instant(TEST_TIME_NOW - 1).isEqualNow());
295: assertEquals(true, new Instant(TEST_TIME_NOW).isEqualNow());
296: assertEquals(false, new Instant(TEST_TIME_NOW + 1).isEqualNow());
297: }
298:
299: public void testIsEqual_RI() {
300: Instant test1 = new Instant(TEST_TIME1);
301: Instant test1a = new Instant(TEST_TIME1);
302: assertEquals(true, test1.isEqual(test1a));
303: assertEquals(true, test1a.isEqual(test1));
304: assertEquals(true, test1.isEqual(test1));
305: assertEquals(true, test1a.isEqual(test1a));
306:
307: Instant test2 = new Instant(TEST_TIME2);
308: assertEquals(false, test1.isEqual(test2));
309: assertEquals(false, test2.isEqual(test1));
310:
311: DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology
312: .getInstance(PARIS));
313: assertEquals(false, test1.isEqual(test3));
314: assertEquals(false, test3.isEqual(test1));
315: assertEquals(true, test3.isEqual(test2));
316:
317: assertEquals(false, test2.isEqual(new MockInstant()));
318: assertEquals(true, test1.isEqual(new MockInstant()));
319:
320: assertEquals(false, new Instant(TEST_TIME_NOW + 1)
321: .isEqual(null));
322: assertEquals(true, new Instant(TEST_TIME_NOW).isEqual(null));
323: assertEquals(false, new Instant(TEST_TIME_NOW - 1)
324: .isEqual(null));
325: }
326:
327: //-----------------------------------------------------------------------
328: public void testIsBefore_long() {
329: assertEquals(true, new Instant(TEST_TIME1).isBefore(TEST_TIME2));
330: assertEquals(false, new Instant(TEST_TIME1)
331: .isBefore(TEST_TIME1));
332: assertEquals(false, new Instant(TEST_TIME2)
333: .isBefore(TEST_TIME1));
334: }
335:
336: public void testIsBeforeNow() {
337: assertEquals(true, new Instant(TEST_TIME_NOW - 1).isBeforeNow());
338: assertEquals(false, new Instant(TEST_TIME_NOW).isBeforeNow());
339: assertEquals(false, new Instant(TEST_TIME_NOW + 1)
340: .isBeforeNow());
341: }
342:
343: public void testIsBefore_RI() {
344: Instant test1 = new Instant(TEST_TIME1);
345: Instant test1a = new Instant(TEST_TIME1);
346: assertEquals(false, test1.isBefore(test1a));
347: assertEquals(false, test1a.isBefore(test1));
348: assertEquals(false, test1.isBefore(test1));
349: assertEquals(false, test1a.isBefore(test1a));
350:
351: Instant test2 = new Instant(TEST_TIME2);
352: assertEquals(true, test1.isBefore(test2));
353: assertEquals(false, test2.isBefore(test1));
354:
355: DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology
356: .getInstance(PARIS));
357: assertEquals(true, test1.isBefore(test3));
358: assertEquals(false, test3.isBefore(test1));
359: assertEquals(false, test3.isBefore(test2));
360:
361: assertEquals(false, test2.isBefore(new MockInstant()));
362: assertEquals(false, test1.isBefore(new MockInstant()));
363:
364: assertEquals(false, new Instant(TEST_TIME_NOW + 1)
365: .isBefore(null));
366: assertEquals(false, new Instant(TEST_TIME_NOW).isBefore(null));
367: assertEquals(true, new Instant(TEST_TIME_NOW - 1)
368: .isBefore(null));
369: }
370:
371: //-----------------------------------------------------------------------
372: public void testIsAfter_long() {
373: assertEquals(false, new Instant(TEST_TIME1).isAfter(TEST_TIME2));
374: assertEquals(false, new Instant(TEST_TIME1).isAfter(TEST_TIME1));
375: assertEquals(true, new Instant(TEST_TIME2).isAfter(TEST_TIME1));
376: }
377:
378: public void testIsAfterNow() {
379: assertEquals(false, new Instant(TEST_TIME_NOW - 1).isAfterNow());
380: assertEquals(false, new Instant(TEST_TIME_NOW).isAfterNow());
381: assertEquals(true, new Instant(TEST_TIME_NOW + 1).isAfterNow());
382: }
383:
384: public void testIsAfter_RI() {
385: Instant test1 = new Instant(TEST_TIME1);
386: Instant test1a = new Instant(TEST_TIME1);
387: assertEquals(false, test1.isAfter(test1a));
388: assertEquals(false, test1a.isAfter(test1));
389: assertEquals(false, test1.isAfter(test1));
390: assertEquals(false, test1a.isAfter(test1a));
391:
392: Instant test2 = new Instant(TEST_TIME2);
393: assertEquals(false, test1.isAfter(test2));
394: assertEquals(true, test2.isAfter(test1));
395:
396: DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology
397: .getInstance(PARIS));
398: assertEquals(false, test1.isAfter(test3));
399: assertEquals(true, test3.isAfter(test1));
400: assertEquals(false, test3.isAfter(test2));
401:
402: assertEquals(true, test2.isAfter(new MockInstant()));
403: assertEquals(false, test1.isAfter(new MockInstant()));
404:
405: assertEquals(true, new Instant(TEST_TIME_NOW + 1).isAfter(null));
406: assertEquals(false, new Instant(TEST_TIME_NOW).isAfter(null));
407: assertEquals(false, new Instant(TEST_TIME_NOW - 1)
408: .isAfter(null));
409: }
410:
411: //-----------------------------------------------------------------------
412: public void testSerialization() throws Exception {
413: Instant test = new Instant(TEST_TIME_NOW);
414:
415: ByteArrayOutputStream baos = new ByteArrayOutputStream();
416: ObjectOutputStream oos = new ObjectOutputStream(baos);
417: oos.writeObject(test);
418: byte[] bytes = baos.toByteArray();
419: oos.close();
420:
421: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
422: ObjectInputStream ois = new ObjectInputStream(bais);
423: Instant result = (Instant) ois.readObject();
424: ois.close();
425:
426: assertEquals(test, result);
427: }
428:
429: //-----------------------------------------------------------------------
430: public void testToString() {
431: Instant test = new Instant(TEST_TIME_NOW);
432: assertEquals("2002-06-09T00:00:00.000Z", test.toString());
433: }
434:
435: //-----------------------------------------------------------------------
436: public void testToInstant() {
437: Instant test = new Instant(TEST_TIME1);
438: Instant result = test.toInstant();
439: assertSame(test, result);
440: }
441:
442: public void testToDateTime() {
443: Instant test = new Instant(TEST_TIME1);
444: DateTime result = test.toDateTime();
445: assertEquals(TEST_TIME1, result.getMillis());
446: assertEquals(ISOChronology.getInstance(), result
447: .getChronology());
448: }
449:
450: public void testToDateTimeISO() {
451: Instant test = new Instant(TEST_TIME1);
452: DateTime result = test.toDateTimeISO();
453: assertSame(DateTime.class, result.getClass());
454: assertSame(ISOChronology.class, result.getChronology()
455: .getClass());
456: assertEquals(test.getMillis(), result.getMillis());
457: assertEquals(ISOChronology.getInstance(), result
458: .getChronology());
459: }
460:
461: public void testToDateTime_DateTimeZone() {
462: Instant test = new Instant(TEST_TIME1);
463: DateTime result = test.toDateTime(LONDON);
464: assertEquals(test.getMillis(), result.getMillis());
465: assertEquals(ISOChronology.getInstance(LONDON), result
466: .getChronology());
467:
468: test = new Instant(TEST_TIME1);
469: result = test.toDateTime(PARIS);
470: assertEquals(test.getMillis(), result.getMillis());
471: assertEquals(ISOChronology.getInstance(PARIS), result
472: .getChronology());
473:
474: test = new Instant(TEST_TIME1);
475: result = test.toDateTime((DateTimeZone) null);
476: assertEquals(test.getMillis(), result.getMillis());
477: assertEquals(ISOChronology.getInstance(), result
478: .getChronology());
479: }
480:
481: public void testToDateTime_Chronology() {
482: Instant test = new Instant(TEST_TIME1);
483: DateTime result = test.toDateTime(ISOChronology.getInstance());
484: assertEquals(test.getMillis(), result.getMillis());
485: assertEquals(ISOChronology.getInstance(), result
486: .getChronology());
487:
488: test = new Instant(TEST_TIME1);
489: result = test
490: .toDateTime(GregorianChronology.getInstance(PARIS));
491: assertEquals(test.getMillis(), result.getMillis());
492: assertEquals(GregorianChronology.getInstance(PARIS), result
493: .getChronology());
494:
495: test = new Instant(TEST_TIME1);
496: result = test.toDateTime((Chronology) null);
497: assertEquals(ISOChronology.getInstance(), result
498: .getChronology());
499: }
500:
501: public void testToMutableDateTime() {
502: Instant test = new Instant(TEST_TIME1);
503: MutableDateTime result = test.toMutableDateTime();
504: assertEquals(test.getMillis(), result.getMillis());
505: assertEquals(ISOChronology.getInstance(), result
506: .getChronology());
507: }
508:
509: public void testToMutableDateTimeISO() {
510: Instant test = new Instant(TEST_TIME1);
511: MutableDateTime result = test.toMutableDateTimeISO();
512: assertSame(MutableDateTime.class, result.getClass());
513: assertSame(ISOChronology.class, result.getChronology()
514: .getClass());
515: assertEquals(test.getMillis(), result.getMillis());
516: assertEquals(ISOChronology.getInstance(), result
517: .getChronology());
518: }
519:
520: public void testToMutableDateTime_DateTimeZone() {
521: Instant test = new Instant(TEST_TIME1);
522: MutableDateTime result = test.toMutableDateTime(LONDON);
523: assertEquals(test.getMillis(), result.getMillis());
524: assertEquals(ISOChronology.getInstance(), result
525: .getChronology());
526:
527: test = new Instant(TEST_TIME1);
528: result = test.toMutableDateTime(PARIS);
529: assertEquals(test.getMillis(), result.getMillis());
530: assertEquals(ISOChronology.getInstance(PARIS), result
531: .getChronology());
532:
533: test = new Instant(TEST_TIME1);
534: result = test.toMutableDateTime((DateTimeZone) null);
535: assertEquals(test.getMillis(), result.getMillis());
536: assertEquals(ISOChronology.getInstance(), result
537: .getChronology());
538: }
539:
540: public void testToMutableDateTime_Chronology() {
541: Instant test = new Instant(TEST_TIME1);
542: MutableDateTime result = test.toMutableDateTime(ISOChronology
543: .getInstance());
544: assertEquals(test.getMillis(), result.getMillis());
545: assertEquals(ISOChronology.getInstance(), result
546: .getChronology());
547:
548: test = new Instant(TEST_TIME1);
549: result = test.toMutableDateTime(GregorianChronology
550: .getInstance(PARIS));
551: assertEquals(test.getMillis(), result.getMillis());
552: assertEquals(GregorianChronology.getInstance(PARIS), result
553: .getChronology());
554:
555: test = new Instant(TEST_TIME1);
556: result = test.toMutableDateTime((Chronology) null);
557: assertEquals(test.getMillis(), result.getMillis());
558: assertEquals(ISOChronology.getInstance(), result
559: .getChronology());
560: }
561:
562: public void testToDate() {
563: Instant test = new Instant(TEST_TIME1);
564: Date result = test.toDate();
565: assertEquals(test.getMillis(), result.getTime());
566: }
567:
568: //-----------------------------------------------------------------------
569: public void testWithMillis_long() {
570: Instant test = new Instant(TEST_TIME1);
571: Instant result = test.withMillis(TEST_TIME2);
572: assertEquals(TEST_TIME2, result.getMillis());
573: assertEquals(test.getChronology(), result.getChronology());
574:
575: test = new Instant(TEST_TIME1);
576: result = test.withMillis(TEST_TIME1);
577: assertSame(test, result);
578: }
579:
580: //-----------------------------------------------------------------------
581: public void testWithDurationAdded_long_int() {
582: Instant test = new Instant(TEST_TIME1);
583: Instant result = test.withDurationAdded(123456789L, 1);
584: Instant expected = new Instant(TEST_TIME1 + 123456789L);
585: assertEquals(expected, result);
586:
587: result = test.withDurationAdded(123456789L, 0);
588: assertSame(test, result);
589:
590: result = test.withDurationAdded(123456789L, 2);
591: expected = new Instant(TEST_TIME1 + (2L * 123456789L));
592: assertEquals(expected, result);
593:
594: result = test.withDurationAdded(123456789L, -3);
595: expected = new Instant(TEST_TIME1 - (3L * 123456789L));
596: assertEquals(expected, result);
597: }
598:
599: //-----------------------------------------------------------------------
600: public void testWithDurationAdded_RD_int() {
601: Instant test = new Instant(TEST_TIME1);
602: Instant result = test.withDurationAdded(
603: new Duration(123456789L), 1);
604: Instant expected = new Instant(TEST_TIME1 + 123456789L);
605: assertEquals(expected, result);
606:
607: result = test.withDurationAdded(null, 1);
608: assertSame(test, result);
609:
610: result = test.withDurationAdded(new Duration(123456789L), 0);
611: assertSame(test, result);
612:
613: result = test.withDurationAdded(new Duration(123456789L), 2);
614: expected = new Instant(TEST_TIME1 + (2L * 123456789L));
615: assertEquals(expected, result);
616:
617: result = test.withDurationAdded(new Duration(123456789L), -3);
618: expected = new Instant(TEST_TIME1 - (3L * 123456789L));
619: assertEquals(expected, result);
620: }
621:
622: //-----------------------------------------------------------------------
623: public void testPlus_long() {
624: Instant test = new Instant(TEST_TIME1);
625: Instant result = test.plus(123456789L);
626: Instant expected = new Instant(TEST_TIME1 + 123456789L);
627: assertEquals(expected, result);
628: }
629:
630: public void testPlus_RD() {
631: Instant test = new Instant(TEST_TIME1);
632: Instant result = test.plus(new Duration(123456789L));
633: Instant expected = new Instant(TEST_TIME1 + 123456789L);
634: assertEquals(expected, result);
635:
636: result = test.plus((ReadableDuration) null);
637: assertSame(test, result);
638: }
639:
640: //-----------------------------------------------------------------------
641: public void testMinus_long() {
642: Instant test = new Instant(TEST_TIME1);
643: Instant result = test.minus(123456789L);
644: Instant expected = new Instant(TEST_TIME1 - 123456789L);
645: assertEquals(expected, result);
646: }
647:
648: public void testMinus_RD() {
649: Instant test = new Instant(TEST_TIME1);
650: Instant result = test.minus(new Duration(123456789L));
651: Instant expected = new Instant(TEST_TIME1 - 123456789L);
652: assertEquals(expected, result);
653:
654: result = test.minus((ReadableDuration) null);
655: assertSame(test, result);
656: }
657:
658: //-----------------------------------------------------------------------
659: public void testImmutable() {
660: assertTrue(Modifier.isFinal(Instant.class.getModifiers()));
661: }
662:
663: }
|