/*
* Copyright 2001-2009 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.joda.time.base.AbstractInstant;
import org.joda.time.chrono.GregorianChronology;
import org.joda.time.chrono.ISOChronology;
/**
* This class is a Junit unit test for Instant.
*
* @author Stephen Colebourne
*/
public class TestInstant_Basics extends TestCase {
// Test in 2002/03 as time zones are more well known
// (before the late 90's they were all over the place)
private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
366 + 365;
long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
366 + 365 + 365;
// 2002-06-09
private long TEST_TIME_NOW =
(y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
// 2002-04-05
private long TEST_TIME1 =
(y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
+ 12L * DateTimeConstants.MILLIS_PER_HOUR
+ 24L * DateTimeConstants.MILLIS_PER_MINUTE;
// 2003-05-06
private long TEST_TIME2 =
(y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
+ 14L * DateTimeConstants.MILLIS_PER_HOUR
+ 28L * DateTimeConstants.MILLIS_PER_MINUTE;
private DateTimeZone originalDateTimeZone = null;
private TimeZone originalTimeZone = null;
private Locale originalLocale = null;
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
public static TestSuite suite() {
return new TestSuite(TestInstant_Basics.class);
}
public TestInstant_Basics(String name) {
super(name);
}
protected void setUp() throws Exception {
DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
originalDateTimeZone = DateTimeZone.getDefault();
originalTimeZone = TimeZone.getDefault();
originalLocale = Locale.getDefault();
DateTimeZone.setDefault(LONDON);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
Locale.setDefault(Locale.UK);
}
protected void tearDown() throws Exception {
DateTimeUtils.setCurrentMillisSystem();
DateTimeZone.setDefault(originalDateTimeZone);
TimeZone.setDefault(originalTimeZone);
Locale.setDefault(originalLocale);
originalDateTimeZone = null;
originalTimeZone = null;
originalLocale = null;
}
//-----------------------------------------------------------------------
public void testTest() {
assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
}
//-----------------------------------------------------------------------
public void testGet_DateTimeFieldType() {
Instant test = new Instant(); // 2002-06-09
assertEquals(1, test.get(DateTimeFieldType.era()));
assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
assertEquals(2002, test.get(DateTimeFieldType.year()));
assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); // UTC zone
assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay())); // UTC zone
assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); // UTC zone
assertEquals(0, test.get(DateTimeFieldType.hourOfDay())); // UTC zone
assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
assertEquals(0, test.get(DateTimeFieldType.minuteOfDay()));
assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
assertEquals(0, test.get(DateTimeFieldType.secondOfDay()));
assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
assertEquals(0, test.get(DateTimeFieldType.millisOfDay()));
try {
test.get((DateTimeFieldType) null);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testGet_DateTimeField() {
Instant test = new Instant(); // 2002-06-09
assertEquals(1, test.get(ISOChronology.getInstance().era()));
assertEquals(20, test.get(ISOChronology.getInstance().centuryOfEra()));
assertEquals(2, test.get(ISOChronology.getInstance().yearOfCentury()));
assertEquals(2002, test.get(ISOChronology.getInstance().yearOfEra()));
assertEquals(2002, test.get(ISOChronology.getInstance().year()));
assertEquals(6, test.get(ISOChronology.getInstance().monthOfYear()));
assertEquals(9, test.get(ISOChronology.getInstance().dayOfMonth()));
assertEquals(2002, test.get(ISOChronology.getInstance().weekyear()));
assertEquals(23, test.get(ISOChronology.getInstance().weekOfWeekyear()));
assertEquals(7, test.get(ISOChronology.getInstance().dayOfWeek()));
assertEquals(160, test.get(ISOChronology.getInstance().dayOfYear()));
assertEquals(0, test.get(ISOChronology.getInstance().halfdayOfDay()));
assertEquals(1, test.get(ISOChronology.getInstance().hourOfHalfday()));
assertEquals(1, test.get(ISOChronology.getInstance().clockhourOfDay()));
assertEquals(1, test.get(ISOChronology.getInstance().clockhourOfHalfday()));
assertEquals(1, test.get(ISOChronology.getInstance().hourOfDay()));
assertEquals(0, test.get(ISOChronology.getInstance().minuteOfHour()));
assertEquals(60, test.get(ISOChronology.getInstance().minuteOfDay()));
assertEquals(0, test.get(ISOChronology.getInstance().secondOfMinute()));
assertEquals(60 * 60, test.get(ISOChronology.getInstance().secondOfDay()));
assertEquals(0, test.get(ISOChronology.getInstance().millisOfSecond()));
assertEquals(60 * 60 * 1000, test.get(ISOChronology.getInstance().millisOfDay()));
try {
test.get((DateTimeField) null);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testGetMethods() {
Instant test = new Instant();
assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
assertEquals(DateTimeZone.UTC, test.getZone());
assertEquals(TEST_TIME_NOW, test.getMillis());
}
public void testEqualsHashCode() {
Instant test1 = new Instant(TEST_TIME1);
Instant test2 = new Instant(TEST_TIME1);
assertEquals(true, test1.equals(test2));
assertEquals(true, test2.equals(test1));
assertEquals(true, test1.equals(test1));
assertEquals(true, test2.equals(test2));
assertEquals(true, test1.hashCode() == test2.hashCode());
assertEquals(true, test1.hashCode() == test1.hashCode());
assertEquals(true, test2.hashCode() == test2.hashCode());
Instant test3 = new Instant(TEST_TIME2);
assertEquals(false, test1.equals(test3));
assertEquals(false, test2.equals(test3));
assertEquals(false, test3.equals(test1));
assertEquals(false, test3.equals(test2));
assertEquals(false, test1.hashCode() == test3.hashCode());
assertEquals(false, test2.hashCode() == test3.hashCode());
assertEquals(false, test1.equals("Hello"));
assertEquals(true, test1.equals(new MockInstant()));
assertEquals(false, test1.equals(new DateTime(TEST_TIME1)));
}
class MockInstant extends AbstractInstant {
public String toString() {
return null;
}
public long getMillis() {
return TEST_TIME1;
}
public Chronology getChronology() {
return ISOChronology.getInstanceUTC();
}
}
public void testCompareTo() {
Instant test1 = new Instant(TEST_TIME1);
Instant test1a = new Instant(TEST_TIME1);
assertEquals(0, test1.compareTo(test1a));
assertEquals(0, test1a.compareTo(test1));
assertEquals(0, test1.compareTo(test1));
assertEquals(0, test1a.compareTo(test1a));
Instant test2 = new Instant(TEST_TIME2);
assertEquals(-1, test1.compareTo(test2));
assertEquals(+1, test2.compareTo(test1));
DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
assertEquals(-1, test1.compareTo(test3));
assertEquals(+1, test3.compareTo(test1));
assertEquals(0, test3.compareTo(test2));
assertEquals(+1, test2.compareTo(new MockInstant()));
assertEquals(0, test1.compareTo(new MockInstant()));
try {
test1.compareTo(null);
fail();
} catch (NullPointerException ex) {}
// try {
// test1.compareTo(new Date());
// fail();
// } catch (ClassCastException ex) {}
}
//-----------------------------------------------------------------------
public void testIsEqual_long() {
assertEquals(false, new Instant(TEST_TIME1).isEqual(TEST_TIME2));
assertEquals(true, new Instant(TEST_TIME1).isEqual(TEST_TIME1));
assertEquals(false, new Instant(TEST_TIME2).isEqual(TEST_TIME1));
}
public void testIsEqualNow() {
assertEquals(false, new Instant(TEST_TIME_NOW - 1).isEqualNow());
assertEquals(true, new Instant(TEST_TIME_NOW).isEqualNow());
assertEquals(false, new Instant(TEST_TIME_NOW + 1).isEqualNow());
}
public void testIsEqual_RI() {
Instant test1 = new Instant(TEST_TIME1);
Instant test1a = new Instant(TEST_TIME1);
assertEquals(true, test1.isEqual(test1a));
assertEquals(true, test1a.isEqual(test1));
assertEquals(true, test1.isEqual(test1));
assertEquals(true, test1a.isEqual(test1a));
Instant test2 = new Instant(TEST_TIME2);
assertEquals(false, test1.isEqual(test2));
assertEquals(false, test2.isEqual(test1));
DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
assertEquals(false, test1.isEqual(test3));
assertEquals(false, test3.isEqual(test1));
assertEquals(true, test3.isEqual(test2));
assertEquals(false, test2.isEqual(new MockInstant()));
assertEquals(true, test1.isEqual(new MockInstant()));
assertEquals(false, new Instant(TEST_TIME_NOW + 1).isEqual(null));
assertEquals(true, new Instant(TEST_TIME_NOW).isEqual(null));
assertEquals(false, new Instant(TEST_TIME_NOW - 1).isEqual(null));
}
//-----------------------------------------------------------------------
public void testIsBefore_long() {
assertEquals(true, new Instant(TEST_TIME1).isBefore(TEST_TIME2));
assertEquals(false, new Instant(TEST_TIME1).isBefore(TEST_TIME1));
assertEquals(false, new Instant(TEST_TIME2).isBefore(TEST_TIME1));
}
public void testIsBeforeNow() {
assertEquals(true, new Instant(TEST_TIME_NOW - 1).isBeforeNow());
assertEquals(false, new Instant(TEST_TIME_NOW).isBeforeNow());
assertEquals(false, new Instant(TEST_TIME_NOW + 1).isBeforeNow());
}
public void testIsBefore_RI() {
Instant test1 = new Instant(TEST_TIME1);
Instant test1a = new Instant(TEST_TIME1);
assertEquals(false, test1.isBefore(test1a));
assertEquals(false, test1a.isBefore(test1));
assertEquals(false, test1.isBefore(test1));
assertEquals(false, test1a.isBefore(test1a));
Instant test2 = new Instant(TEST_TIME2);
assertEquals(true, test1.isBefore(test2));
assertEquals(false, test2.isBefore(test1));
DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
assertEquals(true, test1.isBefore(test3));
assertEquals(false, test3.isBefore(test1));
assertEquals(false, test3.isBefore(test2));
assertEquals(false, test2.isBefore(new MockInstant()));
assertEquals(false, test1.isBefore(new MockInstant()));
assertEquals(false, new Instant(TEST_TIME_NOW + 1).isBefore(null));
assertEquals(false, new Instant(TEST_TIME_NOW).isBefore(null));
assertEquals(true, new Instant(TEST_TIME_NOW - 1).isBefore(null));
}
//-----------------------------------------------------------------------
public void testIsAfter_long() {
assertEquals(false, new Instant(TEST_TIME1).isAfter(TEST_TIME2));
assertEquals(false, new Instant(TEST_TIME1).isAfter(TEST_TIME1));
assertEquals(true, new Instant(TEST_TIME2).isAfter(TEST_TIME1));
}
public void testIsAfterNow() {
assertEquals(false, new Instant(TEST_TIME_NOW - 1).isAfterNow());
assertEquals(false, new Instant(TEST_TIME_NOW).isAfterNow());
assertEquals(true, new Instant(TEST_TIME_NOW + 1).isAfterNow());
}
public void testIsAfter_RI() {
Instant test1 = new Instant(TEST_TIME1);
Instant test1a = new Instant(TEST_TIME1);
assertEquals(false, test1.isAfter(test1a));
assertEquals(false, test1a.isAfter(test1));
assertEquals(false, test1.isAfter(test1));
assertEquals(false, test1a.isAfter(test1a));
Instant test2 = new Instant(TEST_TIME2);
assertEquals(false, test1.isAfter(test2));
assertEquals(true, test2.isAfter(test1));
DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
assertEquals(false, test1.isAfter(test3));
assertEquals(true, test3.isAfter(test1));
assertEquals(false, test3.isAfter(test2));
assertEquals(true, test2.isAfter(new MockInstant()));
assertEquals(false, test1.isAfter(new MockInstant()));
assertEquals(true, new Instant(TEST_TIME_NOW + 1).isAfter(null));
assertEquals(false, new Instant(TEST_TIME_NOW).isAfter(null));
assertEquals(false, new Instant(TEST_TIME_NOW - 1).isAfter(null));
}
//-----------------------------------------------------------------------
public void testSerialization() throws Exception {
Instant test = new Instant(TEST_TIME_NOW);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(test);
oos.close();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Instant result = (Instant) ois.readObject();
ois.close();
assertEquals(test, result);
}
//-----------------------------------------------------------------------
public void testToString() {
Instant test = new Instant(TEST_TIME_NOW);
assertEquals("2002-06-09T00:00:00.000Z", test.toString());
}
//-----------------------------------------------------------------------
public void testToInstant() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.toInstant();
assertSame(test, result);
}
public void testToDateTime() {
Instant test = new Instant(TEST_TIME1);
DateTime result = test.toDateTime();
assertEquals(TEST_TIME1, result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToDateTimeISO() {
Instant test = new Instant(TEST_TIME1);
DateTime result = test.toDateTimeISO();
assertSame(DateTime.class, result.getClass());
assertSame(ISOChronology.class, result.getChronology().getClass());
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToDateTime_DateTimeZone() {
Instant test = new Instant(TEST_TIME1);
DateTime result = test.toDateTime(LONDON);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(LONDON), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toDateTime(PARIS);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toDateTime((DateTimeZone) null);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToDateTime_Chronology() {
Instant test = new Instant(TEST_TIME1);
DateTime result = test.toDateTime(ISOChronology.getInstance());
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toDateTime(GregorianChronology.getInstance(PARIS));
assertEquals(test.getMillis(), result.getMillis());
assertEquals(GregorianChronology.getInstance(PARIS), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toDateTime((Chronology) null);
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToMutableDateTime() {
Instant test = new Instant(TEST_TIME1);
MutableDateTime result = test.toMutableDateTime();
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToMutableDateTimeISO() {
Instant test = new Instant(TEST_TIME1);
MutableDateTime result = test.toMutableDateTimeISO();
assertSame(MutableDateTime.class, result.getClass());
assertSame(ISOChronology.class, result.getChronology().getClass());
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToMutableDateTime_DateTimeZone() {
Instant test = new Instant(TEST_TIME1);
MutableDateTime result = test.toMutableDateTime(LONDON);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toMutableDateTime(PARIS);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toMutableDateTime((DateTimeZone) null);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToMutableDateTime_Chronology() {
Instant test = new Instant(TEST_TIME1);
MutableDateTime result = test.toMutableDateTime(ISOChronology.getInstance());
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toMutableDateTime(GregorianChronology.getInstance(PARIS));
assertEquals(test.getMillis(), result.getMillis());
assertEquals(GregorianChronology.getInstance(PARIS), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.toMutableDateTime((Chronology) null);
assertEquals(test.getMillis(), result.getMillis());
assertEquals(ISOChronology.getInstance(), result.getChronology());
}
public void testToDate() {
Instant test = new Instant(TEST_TIME1);
Date result = test.toDate();
assertEquals(test.getMillis(), result.getTime());
}
//-----------------------------------------------------------------------
public void testWithMillis_long() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.withMillis(TEST_TIME2);
assertEquals(TEST_TIME2, result.getMillis());
assertEquals(test.getChronology(), result.getChronology());
test = new Instant(TEST_TIME1);
result = test.withMillis(TEST_TIME1);
assertSame(test, result);
}
//-----------------------------------------------------------------------
public void testWithDurationAdded_long_int() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.withDurationAdded(123456789L, 1);
Instant expected = new Instant(TEST_TIME1 + 123456789L);
assertEquals(expected, result);
result = test.withDurationAdded(123456789L, 0);
assertSame(test, result);
result = test.withDurationAdded(123456789L, 2);
expected = new Instant(TEST_TIME1 + (2L * 123456789L));
assertEquals(expected, result);
result = test.withDurationAdded(123456789L, -3);
expected = new Instant(TEST_TIME1 - (3L * 123456789L));
assertEquals(expected, result);
}
//-----------------------------------------------------------------------
public void testWithDurationAdded_RD_int() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.withDurationAdded(new Duration(123456789L), 1);
Instant expected = new Instant(TEST_TIME1 + 123456789L);
assertEquals(expected, result);
result = test.withDurationAdded(null, 1);
assertSame(test, result);
result = test.withDurationAdded(new Duration(123456789L), 0);
assertSame(test, result);
result = test.withDurationAdded(new Duration(123456789L), 2);
expected = new Instant(TEST_TIME1 + (2L * 123456789L));
assertEquals(expected, result);
result = test.withDurationAdded(new Duration(123456789L), -3);
expected = new Instant(TEST_TIME1 - (3L * 123456789L));
assertEquals(expected, result);
}
//-----------------------------------------------------------------------
public void testPlus_long() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.plus(123456789L);
Instant expected = new Instant(TEST_TIME1 + 123456789L);
assertEquals(expected, result);
}
public void testPlus_RD() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.plus(new Duration(123456789L));
Instant expected = new Instant(TEST_TIME1 + 123456789L);
assertEquals(expected, result);
result = test.plus((ReadableDuration) null);
assertSame(test, result);
}
//-----------------------------------------------------------------------
public void testMinus_long() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.minus(123456789L);
Instant expected = new Instant(TEST_TIME1 - 123456789L);
assertEquals(expected, result);
}
public void testMinus_RD() {
Instant test = new Instant(TEST_TIME1);
Instant result = test.minus(new Duration(123456789L));
Instant expected = new Instant(TEST_TIME1 - 123456789L);
assertEquals(expected, result);
result = test.minus((ReadableDuration) null);
assertSame(test, result);
}
//-----------------------------------------------------------------------
public void testImmutable() {
assertTrue(Modifier.isFinal(Instant.class.getModifiers()));
}
}