1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import java.util.Locale;
19 import java.util.TimeZone;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.chrono.ISOChronology;
25
26 /***
27 * This class is a JUnit test for MutableDateTime.
28 *
29 * @author Stephen Colebourne
30 */
31 public class TestMutableDateTime_Adds extends TestCase {
32
33
34
35 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
36 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
37
38 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
39 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
40 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
41 366 + 365;
42 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
43 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
44 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
45 366 + 365 + 365;
46
47
48 private long TEST_TIME_NOW =
49 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
50
51
52 private long TEST_TIME1 =
53 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
54 + 12L * DateTimeConstants.MILLIS_PER_HOUR
55 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
56
57
58 private long TEST_TIME2 =
59 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
60 + 14L * DateTimeConstants.MILLIS_PER_HOUR
61 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
62
63 private DateTimeZone originalDateTimeZone = null;
64 private TimeZone originalTimeZone = null;
65 private Locale originalLocale = null;
66
67 public static void main(String[] args) {
68 junit.textui.TestRunner.run(suite());
69 }
70
71 public static TestSuite suite() {
72 return new TestSuite(TestMutableDateTime_Adds.class);
73 }
74
75 public TestMutableDateTime_Adds(String name) {
76 super(name);
77 }
78
79 protected void setUp() throws Exception {
80 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
81 originalDateTimeZone = DateTimeZone.getDefault();
82 originalTimeZone = TimeZone.getDefault();
83 originalLocale = Locale.getDefault();
84 DateTimeZone.setDefault(LONDON);
85 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
86 Locale.setDefault(Locale.UK);
87 }
88
89 protected void tearDown() throws Exception {
90 DateTimeUtils.setCurrentMillisSystem();
91 DateTimeZone.setDefault(originalDateTimeZone);
92 TimeZone.setDefault(originalTimeZone);
93 Locale.setDefault(originalLocale);
94 originalDateTimeZone = null;
95 originalTimeZone = null;
96 originalLocale = null;
97 }
98
99
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 }