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.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: /**
025: * This class is a JUnit test for Duration.
026: *
027: * @author Stephen Colebourne
028: */
029: public class TestDuration_Constructors extends TestCase {
030: // Test in 2002/03 as time zones are more well known
031: // (before the late 90's they were all over the place)
032:
033: private static final DateTimeZone PARIS = DateTimeZone
034: .forID("Europe/Paris");
035: private static final DateTimeZone LONDON = DateTimeZone
036: .forID("Europe/London");
037:
038: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
039: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
040: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
041: + 365 + 365 + 366 + 365;
042: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
043: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
044: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
045: + 365 + 365 + 366 + 365 + 365;
046:
047: // 2002-06-09
048: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
049: + 31L + 9L - 1L)
050: * DateTimeConstants.MILLIS_PER_DAY;
051:
052: // 2002-04-05
053: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
054: * DateTimeConstants.MILLIS_PER_DAY + 12L
055: * DateTimeConstants.MILLIS_PER_HOUR + 24L
056: * DateTimeConstants.MILLIS_PER_MINUTE;
057:
058: // 2003-05-06
059: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
060: * DateTimeConstants.MILLIS_PER_DAY
061: + 14L
062: * DateTimeConstants.MILLIS_PER_HOUR
063: + 28L
064: * DateTimeConstants.MILLIS_PER_MINUTE;
065:
066: private DateTimeZone originalDateTimeZone = null;
067: private TimeZone originalTimeZone = null;
068: private Locale originalLocale = null;
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(suite());
072: }
073:
074: public static TestSuite suite() {
075: return new TestSuite(TestDuration_Constructors.class);
076: }
077:
078: public TestDuration_Constructors(String name) {
079: super (name);
080: }
081:
082: protected void setUp() throws Exception {
083: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
084: originalDateTimeZone = DateTimeZone.getDefault();
085: originalTimeZone = TimeZone.getDefault();
086: originalLocale = Locale.getDefault();
087: DateTimeZone.setDefault(LONDON);
088: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
089: Locale.setDefault(Locale.UK);
090: }
091:
092: protected void tearDown() throws Exception {
093: DateTimeUtils.setCurrentMillisSystem();
094: DateTimeZone.setDefault(originalDateTimeZone);
095: TimeZone.setDefault(originalTimeZone);
096: Locale.setDefault(originalLocale);
097: originalDateTimeZone = null;
098: originalTimeZone = null;
099: originalLocale = null;
100: }
101:
102: //-----------------------------------------------------------------------
103: /**
104: * Test constructor ()
105: */
106: public void testZERO() throws Throwable {
107: Duration test = Duration.ZERO;
108: assertEquals(0, test.getMillis());
109: }
110:
111: //-----------------------------------------------------------------------
112: public void testConstructor_long1() throws Throwable {
113: long length = 4 * DateTimeConstants.MILLIS_PER_DAY + 5
114: * DateTimeConstants.MILLIS_PER_HOUR + 6
115: * DateTimeConstants.MILLIS_PER_MINUTE + 7
116: * DateTimeConstants.MILLIS_PER_SECOND + 8;
117: Duration test = new Duration(length);
118: assertEquals(length, test.getMillis());
119: }
120:
121: //-----------------------------------------------------------------------
122: public void testConstructor_long_long1() throws Throwable {
123: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
124: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
125: Duration test = new Duration(dt1.getMillis(), dt2.getMillis());
126: assertEquals(dt2.getMillis() - dt1.getMillis(), test
127: .getMillis());
128: }
129:
130: //-----------------------------------------------------------------------
131: public void testConstructor_RI_RI1() throws Throwable {
132: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
133: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
134: Duration test = new Duration(dt1, dt2);
135: assertEquals(dt2.getMillis() - dt1.getMillis(), test
136: .getMillis());
137: }
138:
139: public void testConstructor_RI_RI2() throws Throwable {
140: DateTime dt1 = null; // 2002-06-09T01:00+01:00
141: DateTime dt2 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
142: Duration test = new Duration(dt1, dt2);
143: assertEquals(dt2.getMillis() - TEST_TIME_NOW, test.getMillis());
144: }
145:
146: public void testConstructor_RI_RI3() throws Throwable {
147: DateTime dt1 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
148: DateTime dt2 = null; // 2002-06-09T01:00+01:00
149: Duration test = new Duration(dt1, dt2);
150: assertEquals(TEST_TIME_NOW - dt1.getMillis(), test.getMillis());
151: }
152:
153: public void testConstructor_RI_RI4() throws Throwable {
154: DateTime dt1 = null; // 2002-06-09T01:00+01:00
155: DateTime dt2 = null; // 2002-06-09T01:00+01:00
156: Duration test = new Duration(dt1, dt2);
157: assertEquals(0L, test.getMillis());
158: }
159:
160: //-----------------------------------------------------------------------
161: /**
162: * Test constructor (Object)
163: */
164: public void testConstructor_Object1() throws Throwable {
165: Duration test = new Duration("PT72.345S");
166: assertEquals(72345, test.getMillis());
167: }
168:
169: public void testConstructor_Object2() throws Throwable {
170: Duration test = new Duration((Object) null);
171: assertEquals(0L, test.getMillis());
172: }
173:
174: public void testConstructor_Object3() throws Throwable {
175: long length = 4 * DateTimeConstants.MILLIS_PER_DAY + 5
176: * DateTimeConstants.MILLIS_PER_HOUR + 6
177: * DateTimeConstants.MILLIS_PER_MINUTE + 7
178: * DateTimeConstants.MILLIS_PER_SECOND + 8;
179: Long base = new Long(length);
180: Duration test = new Duration(base);
181: assertEquals(length, test.getMillis());
182: }
183:
184: public void testConstructor_Object4() throws Throwable {
185: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
186: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
187: Duration base = new Duration(dt1, dt2);
188: Duration test = new Duration(base);
189: assertEquals(dt2.getMillis() - dt1.getMillis(), test
190: .getMillis());
191: }
192:
193: public void testConstructor_Object5() throws Throwable {
194: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
195: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
196: Interval base = new Interval(dt1, dt2);
197: Duration test = new Duration(base);
198: assertEquals(dt2.getMillis() - dt1.getMillis(), test
199: .getMillis());
200: }
201:
202: }
|