001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import java.util.Date;
019: import java.util.Locale;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.chrono.ISOChronology;
025: import org.joda.time.convert.ConverterManager;
026: import org.joda.time.convert.MockZeroNullIntegerConverter;
027:
028: /**
029: * This class is a Junit unit test for Instant.
030: *
031: * @author Stephen Colebourne
032: */
033: public class TestInstant_Constructors extends TestCase {
034:
035: private static final DateTimeZone PARIS = DateTimeZone
036: .forID("Europe/Paris");
037: private static final DateTimeZone LONDON = DateTimeZone
038: .forID("Europe/London");
039:
040: // 1970-06-09
041: private long TEST_TIME_NOW = (31L + 28L + 31L + 30L + 31L + 9L - 1L)
042: * DateTimeConstants.MILLIS_PER_DAY;
043:
044: // 1970-04-05
045: private long TEST_TIME1 = (31L + 28L + 31L + 6L - 1L)
046: * DateTimeConstants.MILLIS_PER_DAY + 12L
047: * DateTimeConstants.MILLIS_PER_HOUR + 24L
048: * DateTimeConstants.MILLIS_PER_MINUTE;
049:
050: // 1971-05-06
051: private long TEST_TIME2 = (365L + 31L + 28L + 31L + 30L + 7L - 1L)
052: * DateTimeConstants.MILLIS_PER_DAY + 14L
053: * DateTimeConstants.MILLIS_PER_HOUR + 28L
054: * DateTimeConstants.MILLIS_PER_MINUTE;
055:
056: private DateTimeZone zone = null;
057: private Locale locale = null;
058:
059: public static void main(String[] args) {
060: junit.textui.TestRunner.run(suite());
061: }
062:
063: public static TestSuite suite() {
064: return new TestSuite(TestInstant_Constructors.class);
065: }
066:
067: public TestInstant_Constructors(String name) {
068: super (name);
069: }
070:
071: protected void setUp() throws Exception {
072: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
073: zone = DateTimeZone.getDefault();
074: locale = Locale.getDefault();
075: DateTimeZone.setDefault(LONDON);
076: java.util.TimeZone.setDefault(LONDON.toTimeZone());
077: Locale.setDefault(Locale.UK);
078: }
079:
080: protected void tearDown() throws Exception {
081: DateTimeUtils.setCurrentMillisSystem();
082: DateTimeZone.setDefault(zone);
083: java.util.TimeZone.setDefault(zone.toTimeZone());
084: Locale.setDefault(locale);
085: zone = null;
086: }
087:
088: //-----------------------------------------------------------------------
089: /**
090: * Test constructor ()
091: */
092: public void testConstructor() throws Throwable {
093: Instant test = new Instant();
094: assertEquals(ISOChronology.getInstanceUTC(), test
095: .getChronology());
096: assertEquals(TEST_TIME_NOW, test.getMillis());
097: }
098:
099: //-----------------------------------------------------------------------
100: /**
101: * Test constructor (long)
102: */
103: public void testConstructor_long1() throws Throwable {
104: Instant test = new Instant(TEST_TIME1);
105: assertEquals(ISOChronology.getInstanceUTC(), test
106: .getChronology());
107: assertEquals(TEST_TIME1, test.getMillis());
108: }
109:
110: /**
111: * Test constructor (long)
112: */
113: public void testConstructor_long2() throws Throwable {
114: Instant test = new Instant(TEST_TIME2);
115: assertEquals(ISOChronology.getInstanceUTC(), test
116: .getChronology());
117: assertEquals(TEST_TIME2, test.getMillis());
118: }
119:
120: //-----------------------------------------------------------------------
121: /**
122: * Test constructor (Object)
123: */
124: public void testConstructor_Object() throws Throwable {
125: Date date = new Date(TEST_TIME1);
126: Instant test = new Instant(date);
127: assertEquals(ISOChronology.getInstanceUTC(), test
128: .getChronology());
129: assertEquals(TEST_TIME1, test.getMillis());
130: }
131:
132: /**
133: * Test constructor (Object)
134: */
135: public void testConstructor_invalidObject() throws Throwable {
136: try {
137: new Instant(new Object());
138: fail();
139: } catch (IllegalArgumentException ex) {
140: }
141: }
142:
143: /**
144: * Test constructor (Object=null)
145: */
146: public void testConstructor_nullObject() throws Throwable {
147: Instant test = new Instant((Object) null);
148: assertEquals(ISOChronology.getInstanceUTC(), test
149: .getChronology());
150: assertEquals(TEST_TIME_NOW, test.getMillis());
151: }
152:
153: /**
154: * Test constructor (Object=null)
155: */
156: public void testConstructor_badconverterObject() throws Throwable {
157: try {
158: ConverterManager.getInstance().addInstantConverter(
159: MockZeroNullIntegerConverter.INSTANCE);
160: Instant test = new Instant(new Integer(0));
161: assertEquals(ISOChronology.getInstanceUTC(), test
162: .getChronology());
163: assertEquals(0L, test.getMillis());
164: } finally {
165: ConverterManager.getInstance().removeInstantConverter(
166: MockZeroNullIntegerConverter.INSTANCE);
167: }
168: }
169:
170: }
|