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