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: import org.joda.time.chrono.ISOChronology;
025:
026: /**
027: * This class is a JUnit test for MutableDateTime.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestMutableDateTime_Adds extends TestCase {
032: // Test in 2002/03 as time zones are more well known
033: // (before the late 90's they were all over the place)
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: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
041: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
042: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
043: + 365 + 365 + 366 + 365;
044: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
045: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
046: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
047: + 365 + 365 + 366 + 365 + 365;
048:
049: // 2002-06-09
050: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
051: + 31L + 9L - 1L)
052: * DateTimeConstants.MILLIS_PER_DAY;
053:
054: // 2002-04-05
055: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
056: * DateTimeConstants.MILLIS_PER_DAY + 12L
057: * DateTimeConstants.MILLIS_PER_HOUR + 24L
058: * DateTimeConstants.MILLIS_PER_MINUTE;
059:
060: // 2003-05-06
061: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
062: * DateTimeConstants.MILLIS_PER_DAY
063: + 14L
064: * DateTimeConstants.MILLIS_PER_HOUR
065: + 28L
066: * DateTimeConstants.MILLIS_PER_MINUTE;
067:
068: private DateTimeZone originalDateTimeZone = null;
069: private TimeZone originalTimeZone = null;
070: private Locale originalLocale = null;
071:
072: public static void main(String[] args) {
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: public static TestSuite suite() {
077: return new TestSuite(TestMutableDateTime_Adds.class);
078: }
079:
080: public TestMutableDateTime_Adds(String name) {
081: super (name);
082: }
083:
084: protected void setUp() throws Exception {
085: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086: originalDateTimeZone = DateTimeZone.getDefault();
087: originalTimeZone = TimeZone.getDefault();
088: originalLocale = Locale.getDefault();
089: DateTimeZone.setDefault(LONDON);
090: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
091: Locale.setDefault(Locale.UK);
092: }
093:
094: protected void tearDown() throws Exception {
095: DateTimeUtils.setCurrentMillisSystem();
096: DateTimeZone.setDefault(originalDateTimeZone);
097: TimeZone.setDefault(originalTimeZone);
098: Locale.setDefault(originalLocale);
099: originalDateTimeZone = null;
100: originalTimeZone = null;
101: originalLocale = null;
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testTest() {
106: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
107: TEST_TIME_NOW).toString());
108: assertEquals("2002-04-05T12:24:00.000Z",
109: new Instant(TEST_TIME1).toString());
110: assertEquals("2003-05-06T14:28:00.000Z",
111: new Instant(TEST_TIME2).toString());
112: }
113:
114: //-----------------------------------------------------------------------
115: public void testAdd_long1() {
116: MutableDateTime test = new MutableDateTime(TEST_TIME1);
117: test.add(123456L);
118: assertEquals(TEST_TIME1 + 123456L, test.getMillis());
119: assertEquals(ISOChronology.getInstance(), test.getChronology());
120: }
121:
122: //-----------------------------------------------------------------------
123: public void testAdd_RD1() {
124: MutableDateTime test = new MutableDateTime(TEST_TIME1);
125: test.add(new Duration(123456L));
126: assertEquals(TEST_TIME1 + 123456L, test.getMillis());
127: }
128:
129: public void testAdd_RD2() {
130: MutableDateTime test = new MutableDateTime(TEST_TIME1);
131: test.add((ReadableDuration) null);
132: assertEquals(TEST_TIME1, test.getMillis());
133: }
134:
135: //-----------------------------------------------------------------------
136: public void testAdd_RD_int1() {
137: MutableDateTime test = new MutableDateTime(TEST_TIME1);
138: test.add(new Duration(123456L), -2);
139: assertEquals(TEST_TIME1 - (2L * 123456L), test.getMillis());
140: }
141:
142: public void testAdd_RD_int2() {
143: MutableDateTime test = new MutableDateTime(TEST_TIME1);
144: test.add((ReadableDuration) null, 1);
145: assertEquals(TEST_TIME1, test.getMillis());
146: }
147:
148: //-----------------------------------------------------------------------
149: public void testAdd_RP1() {
150: Period d = new Period(1, 1, 0, 1, 1, 1, 1, 1);
151: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
152: 8);
153: assertEquals("2002-06-09T05:06:07.008+01:00", test.toString());
154: test.add(d);
155: assertEquals("2003-07-10T06:07:08.009+01:00", test.toString());
156: }
157:
158: public void testAdd_RP2() {
159: MutableDateTime test = new MutableDateTime(TEST_TIME1);
160: test.add((ReadablePeriod) null);
161: assertEquals(TEST_TIME1, test.getMillis());
162: }
163:
164: //-----------------------------------------------------------------------
165: public void testAdd_RP_int1() {
166: Period d = new Period(0, 0, 0, 0, 0, 0, 1, 2);
167: MutableDateTime test = new MutableDateTime(TEST_TIME1);
168: test.add(d, -2);
169: assertEquals(TEST_TIME1 - (2L * 1002L), test.getMillis());
170: }
171:
172: public void testAdd_RP_int2() {
173: MutableDateTime test = new MutableDateTime(TEST_TIME1);
174: test.add((ReadablePeriod) null, 1);
175: assertEquals(TEST_TIME1, test.getMillis());
176: }
177:
178: //-----------------------------------------------------------------------
179: public void testAdd_DurationFieldType_int1() {
180: MutableDateTime test = new MutableDateTime(TEST_TIME1);
181: test.add(DurationFieldType.years(), 8);
182: assertEquals(2010, test.getYear());
183: }
184:
185: public void testAdd_DurationFieldType_int2() {
186: MutableDateTime test = new MutableDateTime(TEST_TIME1);
187: try {
188: test.add((DurationFieldType) null, 0);
189: fail();
190: } catch (IllegalArgumentException ex) {
191: }
192: assertEquals(TEST_TIME1, test.getMillis());
193: }
194:
195: public void testAdd_DurationFieldType_int3() {
196: MutableDateTime test = new MutableDateTime(TEST_TIME1);
197: try {
198: test.add((DurationFieldType) null, 6);
199: fail();
200: } catch (IllegalArgumentException ex) {
201: }
202: assertEquals(TEST_TIME1, test.getMillis());
203: }
204:
205: //-----------------------------------------------------------------------
206: public void testAddYears_int1() {
207: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
208: 8);
209: test.addYears(8);
210: assertEquals("2010-06-09T05:06:07.008+01:00", test.toString());
211: }
212:
213: //-----------------------------------------------------------------------
214: public void testAddMonths_int1() {
215: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
216: 8);
217: test.addMonths(6);
218: assertEquals("2002-12-09T05:06:07.008Z", test.toString());
219: }
220:
221: //-----------------------------------------------------------------------
222: public void testAddDays_int1() {
223: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
224: 8);
225: test.addDays(17);
226: assertEquals("2002-06-26T05:06:07.008+01:00", test.toString());
227: }
228:
229: //-----------------------------------------------------------------------
230: public void testAddWeekyears_int1() {
231: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
232: 8);
233: test.addWeekyears(-1);
234: assertEquals("2001-06-10T05:06:07.008+01:00", test.toString());
235: }
236:
237: //-----------------------------------------------------------------------
238: public void testAddWeeks_int1() {
239: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
240: 8);
241: test.addWeeks(-21);
242: assertEquals("2002-01-13T05:06:07.008Z", test.toString());
243: }
244:
245: //-----------------------------------------------------------------------
246: public void testAddHours_int1() {
247: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
248: 8);
249: test.addHours(13);
250: assertEquals("2002-06-09T18:06:07.008+01:00", test.toString());
251: }
252:
253: //-----------------------------------------------------------------------
254: public void testAddMinutes_int1() {
255: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
256: 8);
257: test.addMinutes(13);
258: assertEquals("2002-06-09T05:19:07.008+01:00", test.toString());
259: }
260:
261: //-----------------------------------------------------------------------
262: public void testAddSeconds_int1() {
263: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
264: 8);
265: test.addSeconds(13);
266: assertEquals("2002-06-09T05:06:20.008+01:00", test.toString());
267: }
268:
269: //-----------------------------------------------------------------------
270: public void testAddMillis_int1() {
271: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
272: 8);
273: test.addMillis(13);
274: assertEquals("2002-06-09T05:06:07.021+01:00", test.toString());
275: }
276:
277: }
|