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