001 /*
002 * Copyright 2001-2006 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.io.ByteArrayInputStream;
019 import java.io.ByteArrayOutputStream;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022
023 import junit.framework.TestCase;
024 import junit.framework.TestSuite;
025
026 /**
027 * This class is a Junit unit test for Days.
028 *
029 * @author Stephen Colebourne
030 */
031 public class TestDays 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 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
035
036 public static void main(String[] args) {
037 junit.textui.TestRunner.run(suite());
038 }
039
040 public static TestSuite suite() {
041 return new TestSuite(TestDays.class);
042 }
043
044 public TestDays(String name) {
045 super(name);
046 }
047
048 protected void setUp() throws Exception {
049 }
050
051 protected void tearDown() throws Exception {
052 }
053
054 //-----------------------------------------------------------------------
055 public void testConstants() {
056 assertEquals(0, Days.ZERO.getDays());
057 assertEquals(1, Days.ONE.getDays());
058 assertEquals(2, Days.TWO.getDays());
059 assertEquals(3, Days.THREE.getDays());
060 assertEquals(4, Days.FOUR.getDays());
061 assertEquals(5, Days.FIVE.getDays());
062 assertEquals(6, Days.SIX.getDays());
063 assertEquals(7, Days.SEVEN.getDays());
064 assertEquals(Integer.MAX_VALUE, Days.MAX_VALUE.getDays());
065 assertEquals(Integer.MIN_VALUE, Days.MIN_VALUE.getDays());
066 }
067
068 //-----------------------------------------------------------------------
069 public void testFactory_days_int() {
070 assertSame(Days.ZERO, Days.days(0));
071 assertSame(Days.ONE, Days.days(1));
072 assertSame(Days.TWO, Days.days(2));
073 assertSame(Days.THREE, Days.days(3));
074 assertSame(Days.FOUR, Days.days(4));
075 assertSame(Days.FIVE, Days.days(5));
076 assertSame(Days.SIX, Days.days(6));
077 assertSame(Days.SEVEN, Days.days(7));
078 assertSame(Days.MAX_VALUE, Days.days(Integer.MAX_VALUE));
079 assertSame(Days.MIN_VALUE, Days.days(Integer.MIN_VALUE));
080 assertEquals(-1, Days.days(-1).getDays());
081 assertEquals(8, Days.days(8).getDays());
082 }
083
084 //-----------------------------------------------------------------------
085 public void testFactory_daysBetween_RInstant() {
086 DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
087 DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
088 DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
089
090 assertEquals(3, Days.daysBetween(start, end1).getDays());
091 assertEquals(0, Days.daysBetween(start, start).getDays());
092 assertEquals(0, Days.daysBetween(end1, end1).getDays());
093 assertEquals(-3, Days.daysBetween(end1, start).getDays());
094 assertEquals(6, Days.daysBetween(start, end2).getDays());
095 }
096
097 public void testFactory_daysBetween_RPartial() {
098 LocalDate start = new LocalDate(2006, 6, 9);
099 LocalDate end1 = new LocalDate(2006, 6, 12);
100 YearMonthDay end2 = new YearMonthDay(2006, 6, 15);
101
102 assertEquals(3, Days.daysBetween(start, end1).getDays());
103 assertEquals(0, Days.daysBetween(start, start).getDays());
104 assertEquals(0, Days.daysBetween(end1, end1).getDays());
105 assertEquals(-3, Days.daysBetween(end1, start).getDays());
106 assertEquals(6, Days.daysBetween(start, end2).getDays());
107 }
108
109 public void testFactory_daysIn_RInterval() {
110 DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
111 DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
112 DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
113
114 assertEquals(0, Days.daysIn((ReadableInterval) null).getDays());
115 assertEquals(3, Days.daysIn(new Interval(start, end1)).getDays());
116 assertEquals(0, Days.daysIn(new Interval(start, start)).getDays());
117 assertEquals(0, Days.daysIn(new Interval(end1, end1)).getDays());
118 assertEquals(6, Days.daysIn(new Interval(start, end2)).getDays());
119 }
120
121 public void testFactory_standardDaysIn_RPeriod() {
122 assertEquals(0, Days.standardDaysIn((ReadablePeriod) null).getDays());
123 assertEquals(0, Days.standardDaysIn(Period.ZERO).getDays());
124 assertEquals(1, Days.standardDaysIn(new Period(0, 0, 0, 1, 0, 0, 0, 0)).getDays());
125 assertEquals(123, Days.standardDaysIn(Period.days(123)).getDays());
126 assertEquals(-987, Days.standardDaysIn(Period.days(-987)).getDays());
127 assertEquals(1, Days.standardDaysIn(Period.hours(47)).getDays());
128 assertEquals(2, Days.standardDaysIn(Period.hours(48)).getDays());
129 assertEquals(2, Days.standardDaysIn(Period.hours(49)).getDays());
130 assertEquals(14, Days.standardDaysIn(Period.weeks(2)).getDays());
131 try {
132 Days.standardDaysIn(Period.months(1));
133 fail();
134 } catch (IllegalArgumentException ex) {
135 // expeceted
136 }
137 }
138
139 public void testFactory_parseDays_String() {
140 assertEquals(0, Days.parseDays((String) null).getDays());
141 assertEquals(0, Days.parseDays("P0D").getDays());
142 assertEquals(1, Days.parseDays("P1D").getDays());
143 assertEquals(-3, Days.parseDays("P-3D").getDays());
144 assertEquals(2, Days.parseDays("P0Y0M2D").getDays());
145 assertEquals(2, Days.parseDays("P2DT0H0M").getDays());
146 try {
147 Days.parseDays("P1Y1D");
148 fail();
149 } catch (IllegalArgumentException ex) {
150 // expeceted
151 }
152 try {
153 Days.parseDays("P1DT1H");
154 fail();
155 } catch (IllegalArgumentException ex) {
156 // expeceted
157 }
158 }
159
160 //-----------------------------------------------------------------------
161 public void testGetMethods() {
162 Days test = Days.days(20);
163 assertEquals(20, test.getDays());
164 }
165
166 public void testGetFieldType() {
167 Days test = Days.days(20);
168 assertEquals(DurationFieldType.days(), test.getFieldType());
169 }
170
171 public void testGetPeriodType() {
172 Days test = Days.days(20);
173 assertEquals(PeriodType.days(), test.getPeriodType());
174 }
175
176 //-----------------------------------------------------------------------
177 public void testIsGreaterThan() {
178 assertEquals(true, Days.THREE.isGreaterThan(Days.TWO));
179 assertEquals(false, Days.THREE.isGreaterThan(Days.THREE));
180 assertEquals(false, Days.TWO.isGreaterThan(Days.THREE));
181 assertEquals(true, Days.ONE.isGreaterThan(null));
182 assertEquals(false, Days.days(-1).isGreaterThan(null));
183 }
184
185 public void testIsLessThan() {
186 assertEquals(false, Days.THREE.isLessThan(Days.TWO));
187 assertEquals(false, Days.THREE.isLessThan(Days.THREE));
188 assertEquals(true, Days.TWO.isLessThan(Days.THREE));
189 assertEquals(false, Days.ONE.isLessThan(null));
190 assertEquals(true, Days.days(-1).isLessThan(null));
191 }
192
193 //-----------------------------------------------------------------------
194 public void testToString() {
195 Days test = Days.days(20);
196 assertEquals("P20D", test.toString());
197
198 test = Days.days(-20);
199 assertEquals("P-20D", test.toString());
200 }
201
202 //-----------------------------------------------------------------------
203 public void testSerialization() throws Exception {
204 Days test = Days.SEVEN;
205
206 ByteArrayOutputStream baos = new ByteArrayOutputStream();
207 ObjectOutputStream oos = new ObjectOutputStream(baos);
208 oos.writeObject(test);
209 byte[] bytes = baos.toByteArray();
210 oos.close();
211
212 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
213 ObjectInputStream ois = new ObjectInputStream(bais);
214 Days result = (Days) ois.readObject();
215 ois.close();
216
217 assertSame(test, result);
218 }
219
220 //-----------------------------------------------------------------------
221 public void testToStandardWeeks() {
222 Days test = Days.days(14);
223 Weeks expected = Weeks.weeks(2);
224 assertEquals(expected, test.toStandardWeeks());
225 }
226
227 public void testToStandardHours() {
228 Days test = Days.days(2);
229 Hours expected = Hours.hours(2 * 24);
230 assertEquals(expected, test.toStandardHours());
231
232 try {
233 Days.MAX_VALUE.toStandardHours();
234 fail();
235 } catch (ArithmeticException ex) {
236 // expected
237 }
238 }
239
240 public void testToStandardMinutes() {
241 Days test = Days.days(2);
242 Minutes expected = Minutes.minutes(2 * 24 * 60);
243 assertEquals(expected, test.toStandardMinutes());
244
245 try {
246 Days.MAX_VALUE.toStandardMinutes();
247 fail();
248 } catch (ArithmeticException ex) {
249 // expected
250 }
251 }
252
253 public void testToStandardSeconds() {
254 Days test = Days.days(2);
255 Seconds expected = Seconds.seconds(2 * 24 * 60 * 60);
256 assertEquals(expected, test.toStandardSeconds());
257
258 try {
259 Days.MAX_VALUE.toStandardSeconds();
260 fail();
261 } catch (ArithmeticException ex) {
262 // expected
263 }
264 }
265
266 public void testToStandardDuration() {
267 Days test = Days.days(20);
268 Duration expected = new Duration(20L * DateTimeConstants.MILLIS_PER_DAY);
269 assertEquals(expected, test.toStandardDuration());
270
271 expected = new Duration(((long) Integer.MAX_VALUE) * DateTimeConstants.MILLIS_PER_DAY);
272 assertEquals(expected, Days.MAX_VALUE.toStandardDuration());
273 }
274
275 //-----------------------------------------------------------------------
276 public void testPlus_int() {
277 Days test2 = Days.days(2);
278 Days result = test2.plus(3);
279 assertEquals(2, test2.getDays());
280 assertEquals(5, result.getDays());
281
282 assertEquals(1, Days.ONE.plus(0).getDays());
283
284 try {
285 Days.MAX_VALUE.plus(1);
286 fail();
287 } catch (ArithmeticException ex) {
288 // expected
289 }
290 }
291
292 public void testPlus_Days() {
293 Days test2 = Days.days(2);
294 Days test3 = Days.days(3);
295 Days result = test2.plus(test3);
296 assertEquals(2, test2.getDays());
297 assertEquals(3, test3.getDays());
298 assertEquals(5, result.getDays());
299
300 assertEquals(1, Days.ONE.plus(Days.ZERO).getDays());
301 assertEquals(1, Days.ONE.plus((Days) null).getDays());
302
303 try {
304 Days.MAX_VALUE.plus(Days.ONE);
305 fail();
306 } catch (ArithmeticException ex) {
307 // expected
308 }
309 }
310
311 public void testMinus_int() {
312 Days test2 = Days.days(2);
313 Days result = test2.minus(3);
314 assertEquals(2, test2.getDays());
315 assertEquals(-1, result.getDays());
316
317 assertEquals(1, Days.ONE.minus(0).getDays());
318
319 try {
320 Days.MIN_VALUE.minus(1);
321 fail();
322 } catch (ArithmeticException ex) {
323 // expected
324 }
325 }
326
327 public void testMinus_Days() {
328 Days test2 = Days.days(2);
329 Days test3 = Days.days(3);
330 Days result = test2.minus(test3);
331 assertEquals(2, test2.getDays());
332 assertEquals(3, test3.getDays());
333 assertEquals(-1, result.getDays());
334
335 assertEquals(1, Days.ONE.minus(Days.ZERO).getDays());
336 assertEquals(1, Days.ONE.minus((Days) null).getDays());
337
338 try {
339 Days.MIN_VALUE.minus(Days.ONE);
340 fail();
341 } catch (ArithmeticException ex) {
342 // expected
343 }
344 }
345
346 public void testMultipliedBy_int() {
347 Days test = Days.days(2);
348 assertEquals(6, test.multipliedBy(3).getDays());
349 assertEquals(2, test.getDays());
350 assertEquals(-6, test.multipliedBy(-3).getDays());
351 assertSame(test, test.multipliedBy(1));
352
353 Days halfMax = Days.days(Integer.MAX_VALUE / 2 + 1);
354 try {
355 halfMax.multipliedBy(2);
356 fail();
357 } catch (ArithmeticException ex) {
358 // expected
359 }
360 }
361
362 public void testDividedBy_int() {
363 Days test = Days.days(12);
364 assertEquals(6, test.dividedBy(2).getDays());
365 assertEquals(12, test.getDays());
366 assertEquals(4, test.dividedBy(3).getDays());
367 assertEquals(3, test.dividedBy(4).getDays());
368 assertEquals(2, test.dividedBy(5).getDays());
369 assertEquals(2, test.dividedBy(6).getDays());
370 assertSame(test, test.dividedBy(1));
371
372 try {
373 Days.ONE.dividedBy(0);
374 fail();
375 } catch (ArithmeticException ex) {
376 // expected
377 }
378 }
379
380 public void testNegated() {
381 Days test = Days.days(12);
382 assertEquals(-12, test.negated().getDays());
383 assertEquals(12, test.getDays());
384
385 try {
386 Days.MIN_VALUE.negated();
387 fail();
388 } catch (ArithmeticException ex) {
389 // expected
390 }
391 }
392
393 //-----------------------------------------------------------------------
394 public void testAddToLocalDate() {
395 Days test = Days.days(20);
396 LocalDate date = new LocalDate(2006, 6, 1);
397 LocalDate expected = new LocalDate(2006, 6, 21);
398 assertEquals(expected, date.plus(test));
399 }
400
401 }