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.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.Locale;
023: import java.util.TimeZone;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.joda.time.base.BasePeriod;
029:
030: /**
031: * This class is a Junit unit test for MutableDuration.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestMutablePeriod_Basics extends TestCase {
036: // Test in 2002/03 as time zones are more well known
037: // (before the late 90's they were all over the place)
038:
039: private static final DateTimeZone PARIS = DateTimeZone
040: .forID("Europe/Paris");
041: private static final DateTimeZone LONDON = DateTimeZone
042: .forID("Europe/London");
043:
044: long y2002days = 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;
048: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
049: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
050: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
051: + 365 + 365 + 366 + 365 + 365;
052:
053: // 2002-06-09
054: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
055: + 31L + 9L - 1L)
056: * DateTimeConstants.MILLIS_PER_DAY;
057:
058: // 2002-04-05
059: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
060: * DateTimeConstants.MILLIS_PER_DAY + 12L
061: * DateTimeConstants.MILLIS_PER_HOUR + 24L
062: * DateTimeConstants.MILLIS_PER_MINUTE;
063:
064: // 2003-05-06
065: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
066: * DateTimeConstants.MILLIS_PER_DAY
067: + 14L
068: * DateTimeConstants.MILLIS_PER_HOUR
069: + 28L
070: * DateTimeConstants.MILLIS_PER_MINUTE;
071:
072: private DateTimeZone originalDateTimeZone = null;
073: private TimeZone originalTimeZone = null;
074: private Locale originalLocale = null;
075:
076: public static void main(String[] args) {
077: junit.textui.TestRunner.run(suite());
078: }
079:
080: public static TestSuite suite() {
081: return new TestSuite(TestMutablePeriod_Basics.class);
082: }
083:
084: public TestMutablePeriod_Basics(String name) {
085: super (name);
086: }
087:
088: protected void setUp() throws Exception {
089: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
090: originalDateTimeZone = DateTimeZone.getDefault();
091: originalTimeZone = TimeZone.getDefault();
092: originalLocale = Locale.getDefault();
093: DateTimeZone.setDefault(LONDON);
094: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
095: Locale.setDefault(Locale.UK);
096: }
097:
098: protected void tearDown() throws Exception {
099: DateTimeUtils.setCurrentMillisSystem();
100: DateTimeZone.setDefault(originalDateTimeZone);
101: TimeZone.setDefault(originalTimeZone);
102: Locale.setDefault(originalLocale);
103: originalDateTimeZone = null;
104: originalTimeZone = null;
105: originalLocale = null;
106: }
107:
108: //-----------------------------------------------------------------------
109: public void testTest() {
110: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
111: TEST_TIME_NOW).toString());
112: assertEquals("2002-04-05T12:24:00.000Z",
113: new Instant(TEST_TIME1).toString());
114: assertEquals("2003-05-06T14:28:00.000Z",
115: new Instant(TEST_TIME2).toString());
116: }
117:
118: //-----------------------------------------------------------------------
119: public void testGetPeriodType() {
120: MutablePeriod test = new MutablePeriod();
121: assertEquals(PeriodType.standard(), test.getPeriodType());
122: }
123:
124: public void testGetMethods() {
125: MutablePeriod test = new MutablePeriod();
126: assertEquals(0, test.getYears());
127: assertEquals(0, test.getMonths());
128: assertEquals(0, test.getDays());
129: assertEquals(0, test.getWeeks());
130: assertEquals(0, test.getDays());
131: assertEquals(0, test.getHours());
132: assertEquals(0, test.getMinutes());
133: assertEquals(0, test.getSeconds());
134: assertEquals(0, test.getMillis());
135: }
136:
137: public void testEqualsHashCode() {
138: MutablePeriod test1 = new MutablePeriod(123L);
139: MutablePeriod test2 = new MutablePeriod(123L);
140: assertEquals(true, test1.equals(test2));
141: assertEquals(true, test2.equals(test1));
142: assertEquals(true, test1.equals(test1));
143: assertEquals(true, test2.equals(test2));
144: assertEquals(true, test1.hashCode() == test2.hashCode());
145: assertEquals(true, test1.hashCode() == test1.hashCode());
146: assertEquals(true, test2.hashCode() == test2.hashCode());
147:
148: MutablePeriod test3 = new MutablePeriod(321L);
149: assertEquals(false, test1.equals(test3));
150: assertEquals(false, test2.equals(test3));
151: assertEquals(false, test3.equals(test1));
152: assertEquals(false, test3.equals(test2));
153: assertEquals(false, test1.hashCode() == test3.hashCode());
154: assertEquals(false, test2.hashCode() == test3.hashCode());
155:
156: assertEquals(false, test1.equals("Hello"));
157: assertEquals(true, test1.equals(new MockMutablePeriod(123L)));
158: assertEquals(false, test1.equals(new Period(123L, PeriodType
159: .dayTime())));
160: }
161:
162: class MockMutablePeriod extends BasePeriod {
163: public MockMutablePeriod(long value) {
164: super (value, null, null);
165: }
166: }
167:
168: //-----------------------------------------------------------------------
169: public void testSerialization() throws Exception {
170: MutablePeriod test = new MutablePeriod(123L);
171:
172: ByteArrayOutputStream baos = new ByteArrayOutputStream();
173: ObjectOutputStream oos = new ObjectOutputStream(baos);
174: oos.writeObject(test);
175: byte[] bytes = baos.toByteArray();
176: oos.close();
177:
178: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
179: ObjectInputStream ois = new ObjectInputStream(bais);
180: MutablePeriod result = (MutablePeriod) ois.readObject();
181: ois.close();
182:
183: assertEquals(test, result);
184: }
185:
186: // //-----------------------------------------------------------------------
187: // public void testAddTo1() {
188: // long expected = TEST_TIME_NOW;
189: // expected = ISOChronology.getInstance().years().add(expected, 1);
190: // expected = ISOChronology.getInstance().months().add(expected, 2);
191: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
192: // expected = ISOChronology.getInstance().days().add(expected, 4);
193: // expected = ISOChronology.getInstance().hours().add(expected, 5);
194: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
195: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
196: // expected = ISOChronology.getInstance().millis().add(expected, 8);
197: //
198: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
199: // long added = test.addTo(TEST_TIME_NOW, 1);
200: // assertEquals(expected, added);
201: // }
202: //
203: // public void testAddTo2() {
204: // long expected = TEST_TIME_NOW;
205: // expected = ISOChronology.getInstance().years().add(expected, -2);
206: // expected = ISOChronology.getInstance().months().add(expected, -4);
207: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
208: // expected = ISOChronology.getInstance().days().add(expected, -8);
209: // expected = ISOChronology.getInstance().hours().add(expected, -10);
210: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
211: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
212: // expected = ISOChronology.getInstance().millis().add(expected, -16);
213: //
214: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
215: // long added = test.addTo(TEST_TIME_NOW, -2);
216: // assertEquals(expected, added);
217: // }
218: //
219: // public void testAddTo3() {
220: // long expected = TEST_TIME_NOW;
221: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
222: // long added = test.addTo(TEST_TIME_NOW, 0);
223: // assertEquals(expected, added);
224: // }
225: //
226: // public void testAddTo4() {
227: // long expected = TEST_TIME_NOW + 100L;
228: // MutablePeriod test = new MutablePeriod(100L);
229: // long added = test.addTo(TEST_TIME_NOW, 1);
230: // assertEquals(expected, added);
231: // }
232: //
233: // //-----------------------------------------------------------------------
234: // public void testAddToWithChronology1() {
235: // long expected = TEST_TIME_NOW;
236: // expected = ISOChronology.getInstance().years().add(expected, 1);
237: // expected = ISOChronology.getInstance().months().add(expected, 2);
238: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
239: // expected = ISOChronology.getInstance().days().add(expected, 4);
240: // expected = ISOChronology.getInstance().hours().add(expected, 5);
241: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
242: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
243: // expected = ISOChronology.getInstance().millis().add(expected, 8);
244: //
245: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
246: // long added = test.addTo(TEST_TIME_NOW, 1, ISOChronology.getInstance());
247: // assertEquals(expected, added);
248: // }
249: //
250: // public void testAddToWithChronology2() {
251: // long expected = TEST_TIME_NOW;
252: // expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
253: // expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
254: // expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
255: // expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
256: // expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
257: // expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
258: // expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
259: // expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
260: //
261: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
262: // long added = test.addTo(TEST_TIME_NOW, -2, ISOChronology.getInstanceUTC()); // chrono specified so use it
263: // assertEquals(expected, added);
264: // }
265: //
266: // public void testAddToWithChronology3() {
267: // long expected = TEST_TIME_NOW;
268: // expected = ISOChronology.getInstance().years().add(expected, -2);
269: // expected = ISOChronology.getInstance().months().add(expected, -4);
270: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
271: // expected = ISOChronology.getInstance().days().add(expected, -8);
272: // expected = ISOChronology.getInstance().hours().add(expected, -10);
273: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
274: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
275: // expected = ISOChronology.getInstance().millis().add(expected, -16);
276: //
277: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
278: // long added = test.addTo(TEST_TIME_NOW, -2, null); // no chrono so use default
279: // assertEquals(expected, added);
280: // }
281: //
282: // //-----------------------------------------------------------------------
283: // public void testAddToRI1() {
284: // long expected = TEST_TIME_NOW;
285: // expected = ISOChronology.getInstance().years().add(expected, 1);
286: // expected = ISOChronology.getInstance().months().add(expected, 2);
287: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
288: // expected = ISOChronology.getInstance().days().add(expected, 4);
289: // expected = ISOChronology.getInstance().hours().add(expected, 5);
290: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
291: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
292: // expected = ISOChronology.getInstance().millis().add(expected, 8);
293: //
294: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
295: // DateTime added = test.addTo(new Instant(), 1); // Instant has no time zone, use default
296: // assertEquals(expected, added.getMillis());
297: // assertEquals(ISOChronology.getInstance(), added.getChronology());
298: // }
299: //
300: // public void testAddToRI2() {
301: // long expected = TEST_TIME_NOW;
302: // expected = ISOChronology.getInstance().years().add(expected, -2);
303: // expected = ISOChronology.getInstance().months().add(expected, -4);
304: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
305: // expected = ISOChronology.getInstance().days().add(expected, -8);
306: // expected = ISOChronology.getInstance().hours().add(expected, -10);
307: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
308: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
309: // expected = ISOChronology.getInstance().millis().add(expected, -16);
310: //
311: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
312: // DateTime added = test.addTo(new Instant(), -2); // Instant has no time zone, use default
313: // assertEquals(expected, added.getMillis());
314: // assertEquals(ISOChronology.getInstance(), added.getChronology());
315: // }
316: //
317: // public void testAddToRI3() {
318: // long expected = TEST_TIME_NOW;
319: // expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
320: // expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
321: // expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
322: // expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
323: // expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
324: // expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
325: // expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
326: // expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
327: //
328: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
329: // DateTime added = test.addTo(new DateTime(ISOChronology.getInstanceUTC()), -2); // DateTime has UTC time zone
330: // assertEquals(expected, added.getMillis());
331: // assertEquals(ISOChronology.getInstanceUTC(), added.getChronology());
332: // }
333: //
334: // public void testAddToRI4() {
335: // long expected = TEST_TIME_NOW;
336: // expected = ISOChronology.getInstance(PARIS).years().add(expected, -2);
337: // expected = ISOChronology.getInstance(PARIS).months().add(expected, -4);
338: // expected = ISOChronology.getInstance(PARIS).weeks().add(expected, -6);
339: // expected = ISOChronology.getInstance(PARIS).days().add(expected, -8);
340: // expected = ISOChronology.getInstance(PARIS).hours().add(expected, -10);
341: // expected = ISOChronology.getInstance(PARIS).minutes().add(expected, -12);
342: // expected = ISOChronology.getInstance(PARIS).seconds().add(expected, -14);
343: // expected = ISOChronology.getInstance(PARIS).millis().add(expected, -16);
344: //
345: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
346: // DateTime added = test.addTo(new DateTime(PARIS), -2); // DateTime has PARIS time zone
347: // assertEquals(expected, added.getMillis());
348: // assertEquals(ISOChronology.getInstance(PARIS), added.getChronology());
349: // }
350: //
351: // public void testAddToRI5() {
352: // long expected = TEST_TIME_NOW;
353: // expected = ISOChronology.getInstance().years().add(expected, -2);
354: // expected = ISOChronology.getInstance().months().add(expected, -4);
355: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
356: // expected = ISOChronology.getInstance().days().add(expected, -8);
357: // expected = ISOChronology.getInstance().hours().add(expected, -10);
358: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
359: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
360: // expected = ISOChronology.getInstance().millis().add(expected, -16);
361: //
362: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
363: // DateTime added = test.addTo(null, -2); // null has no time zone, use default
364: // assertEquals(expected, added.getMillis());
365: // assertEquals(ISOChronology.getInstance(), added.getChronology());
366: // }
367: //
368: // //-----------------------------------------------------------------------
369: // public void testAddIntoRWI1() {
370: // long expected = TEST_TIME_NOW;
371: // expected = ISOChronology.getInstance().years().add(expected, 1);
372: // expected = ISOChronology.getInstance().months().add(expected, 2);
373: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
374: // expected = ISOChronology.getInstance().days().add(expected, 4);
375: // expected = ISOChronology.getInstance().hours().add(expected, 5);
376: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
377: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
378: // expected = ISOChronology.getInstance().millis().add(expected, 8);
379: //
380: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
381: // MutableDateTime mdt = new MutableDateTime();
382: // test.addInto(mdt, 1);
383: // assertEquals(expected, mdt.getMillis());
384: // }
385: //
386: // public void testAddIntoRWI2() {
387: // long expected = TEST_TIME_NOW;
388: // expected = ISOChronology.getInstance().years().add(expected, -2);
389: // expected = ISOChronology.getInstance().months().add(expected, -4);
390: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
391: // expected = ISOChronology.getInstance().days().add(expected, -8);
392: // expected = ISOChronology.getInstance().hours().add(expected, -10);
393: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
394: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
395: // expected = ISOChronology.getInstance().millis().add(expected, -16);
396: //
397: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
398: // MutableDateTime mdt = new MutableDateTime();
399: // test.addInto(mdt, -2); // MutableDateTime has a chronology, use it
400: // assertEquals(expected, mdt.getMillis());
401: // }
402: //
403: // public void testAddIntoRWI3() {
404: // MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
405: // try {
406: // test.addInto(null, 1);
407: // fail();
408: // } catch (IllegalArgumentException ex) {}
409: // }
410:
411: //-----------------------------------------------------------------------
412: public void testToString() {
413: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
414: assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString());
415:
416: test = new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 0);
417: assertEquals("PT0S", test.toString());
418:
419: test = new MutablePeriod(12345L);
420: assertEquals("PT12.345S", test.toString());
421: }
422:
423: //-----------------------------------------------------------------------
424: public void testToPeriod() {
425: MutablePeriod test = new MutablePeriod(123L);
426: Period result = test.toPeriod();
427: assertEquals(test, result);
428: }
429:
430: public void testToMutablePeriod() {
431: MutablePeriod test = new MutablePeriod(123L);
432: MutablePeriod result = test.toMutablePeriod();
433: assertEquals(test, result);
434: }
435:
436: //-----------------------------------------------------------------------
437: // public void testToDurationMillisFrom() {
438: // MutablePeriod test = new MutablePeriod(123L);
439: // assertEquals(123L, test.toDurationMillisFrom(0L, null));
440: // }
441:
442: public void testToDurationFrom() {
443: MutablePeriod test = new MutablePeriod(123L);
444: assertEquals(new Duration(123L), test
445: .toDurationFrom(new Instant(0L)));
446: }
447:
448: //-----------------------------------------------------------------------
449: public void testCopy() {
450: MutablePeriod test = new MutablePeriod(123L);
451: MutablePeriod copy = test.copy();
452: assertEquals(test.getPeriodType(), copy.getPeriodType());
453: assertEquals(test, copy);
454: }
455:
456: //-----------------------------------------------------------------------
457: public void testClone() {
458: MutablePeriod test = new MutablePeriod(123L);
459: MutablePeriod copy = (MutablePeriod) test.clone();
460: assertEquals(test.getPeriodType(), copy.getPeriodType());
461: assertEquals(test, copy);
462: }
463:
464: }
|