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.base.AbstractInterval;
025: import org.joda.time.chrono.ISOChronology;
026:
027: /**
028: * This class is a Junit unit test for Instant.
029: *
030: * @author Stephen Colebourne
031: */
032: public class TestMutableInterval_Updates extends TestCase {
033: // Test in 2002/03 as time zones are more well known
034: // (before the late 90's they were all over the place)
035:
036: private static final DateTimeZone PARIS = DateTimeZone
037: .forID("Europe/Paris");
038: private static final DateTimeZone LONDON = DateTimeZone
039: .forID("Europe/London");
040:
041: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
042: + 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;
045: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
046: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
047: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
048: + 365 + 365 + 366 + 365 + 365;
049:
050: // 2002-06-09
051: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
052: + 31L + 9L - 1L)
053: * DateTimeConstants.MILLIS_PER_DAY;
054:
055: // 2002-04-05
056: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
057: * DateTimeConstants.MILLIS_PER_DAY + 12L
058: * DateTimeConstants.MILLIS_PER_HOUR + 24L
059: * DateTimeConstants.MILLIS_PER_MINUTE;
060:
061: // 2003-05-06
062: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
063: * DateTimeConstants.MILLIS_PER_DAY
064: + 14L
065: * DateTimeConstants.MILLIS_PER_HOUR
066: + 28L
067: * DateTimeConstants.MILLIS_PER_MINUTE;
068:
069: private DateTimeZone originalDateTimeZone = null;
070: private TimeZone originalTimeZone = null;
071: private Locale originalLocale = null;
072:
073: public static void main(String[] args) {
074: junit.textui.TestRunner.run(suite());
075: }
076:
077: public static TestSuite suite() {
078: return new TestSuite(TestMutableInterval_Updates.class);
079: }
080:
081: public TestMutableInterval_Updates(String name) {
082: super (name);
083: }
084:
085: protected void setUp() throws Exception {
086: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
087: originalDateTimeZone = DateTimeZone.getDefault();
088: originalTimeZone = TimeZone.getDefault();
089: originalLocale = Locale.getDefault();
090: DateTimeZone.setDefault(LONDON);
091: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
092: Locale.setDefault(Locale.UK);
093: }
094:
095: protected void tearDown() throws Exception {
096: DateTimeUtils.setCurrentMillisSystem();
097: DateTimeZone.setDefault(originalDateTimeZone);
098: TimeZone.setDefault(originalTimeZone);
099: Locale.setDefault(originalLocale);
100: originalDateTimeZone = null;
101: originalTimeZone = null;
102: originalLocale = null;
103: }
104:
105: //-----------------------------------------------------------------------
106: public void testTest() {
107: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
108: TEST_TIME_NOW).toString());
109: assertEquals("2002-04-05T12:24:00.000Z",
110: new Instant(TEST_TIME1).toString());
111: assertEquals("2003-05-06T14:28:00.000Z",
112: new Instant(TEST_TIME2).toString());
113: }
114:
115: //-----------------------------------------------------------------------
116: public void testSetInterval_long_long1() {
117: MutableInterval test = new MutableInterval(TEST_TIME1,
118: TEST_TIME2);
119: test.setInterval(TEST_TIME1 - 1, TEST_TIME2 + 1);
120: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
121: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
122: }
123:
124: public void testSetInterval_long_long2() {
125: MutableInterval test = new MutableInterval(TEST_TIME1,
126: TEST_TIME2);
127: try {
128: test.setInterval(TEST_TIME1 - 1, TEST_TIME1 - 2);
129: fail();
130: } catch (IllegalArgumentException ex) {
131: }
132: }
133:
134: //-----------------------------------------------------------------------
135: public void testSetInterval_RI_RI1() {
136: MutableInterval test = new MutableInterval(TEST_TIME1,
137: TEST_TIME2);
138: test.setInterval(new Instant(TEST_TIME1 - 1), new Instant(
139: TEST_TIME2 + 1));
140: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
141: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
142: }
143:
144: public void testSetInterval_RI_RI2() {
145: MutableInterval test = new MutableInterval(TEST_TIME1,
146: TEST_TIME2);
147: try {
148: test.setInterval(new Instant(TEST_TIME1 - 1), new Instant(
149: TEST_TIME1 - 2));
150: fail();
151: } catch (IllegalArgumentException ex) {
152: }
153: }
154:
155: public void testSetInterval_RI_RI3() {
156: MutableInterval test = new MutableInterval(TEST_TIME1,
157: TEST_TIME2);
158: test.setInterval(null, new Instant(TEST_TIME2 + 1));
159: assertEquals(TEST_TIME_NOW, test.getStartMillis());
160: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
161: }
162:
163: public void testSetInterval_RI_RI4() {
164: MutableInterval test = new MutableInterval(TEST_TIME1,
165: TEST_TIME2);
166: test.setInterval(new Instant(TEST_TIME1 - 1), null);
167: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
168: assertEquals(TEST_TIME_NOW, test.getEndMillis());
169: }
170:
171: public void testSetInterval_RI_RI5() {
172: MutableInterval test = new MutableInterval(TEST_TIME1,
173: TEST_TIME2);
174: test.setInterval(null, null);
175: assertEquals(TEST_TIME_NOW, test.getStartMillis());
176: assertEquals(TEST_TIME_NOW, test.getEndMillis());
177: }
178:
179: //-----------------------------------------------------------------------
180: public void testSetInterval_RInterval1() {
181: MutableInterval test = new MutableInterval(TEST_TIME1,
182: TEST_TIME2);
183: test.setInterval(new Interval(TEST_TIME1 - 1, TEST_TIME2 + 1));
184: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
185: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
186: }
187:
188: public void testSetInterval_RInterval2() {
189: MutableInterval test = new MutableInterval(TEST_TIME1,
190: TEST_TIME2);
191: try {
192: test.setInterval(new MockBadInterval());
193: fail();
194: } catch (IllegalArgumentException ex) {
195: }
196: }
197:
198: class MockBadInterval extends AbstractInterval {
199: public Chronology getChronology() {
200: return ISOChronology.getInstance();
201: }
202:
203: public long getStartMillis() {
204: return TEST_TIME1 - 1;
205: }
206:
207: public long getEndMillis() {
208: return TEST_TIME1 - 2;
209: }
210: }
211:
212: public void testSetInterval_RInterval3() {
213: MutableInterval test = new MutableInterval(TEST_TIME1,
214: TEST_TIME2);
215: try {
216: test.setInterval(null);
217: fail();
218: } catch (IllegalArgumentException ex) {
219: }
220: }
221:
222: //-----------------------------------------------------------------------
223: public void testSetStartMillis_long1() {
224: MutableInterval test = new MutableInterval(TEST_TIME1,
225: TEST_TIME2);
226: test.setStartMillis(TEST_TIME1 - 1);
227: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
228: assertEquals(TEST_TIME2, test.getEndMillis());
229: }
230:
231: public void testSetStartMillis_long2() {
232: MutableInterval test = new MutableInterval(TEST_TIME1,
233: TEST_TIME2);
234: try {
235: test.setStartMillis(TEST_TIME2 + 1);
236: fail();
237: } catch (IllegalArgumentException ex) {
238: }
239: }
240:
241: //-----------------------------------------------------------------------
242: public void testSetStart_RI1() {
243: MutableInterval test = new MutableInterval(TEST_TIME1,
244: TEST_TIME2);
245: test.setStart(new Instant(TEST_TIME1 - 1));
246: assertEquals(TEST_TIME1 - 1, test.getStartMillis());
247: assertEquals(TEST_TIME2, test.getEndMillis());
248: }
249:
250: public void testSetStart_RI2() {
251: MutableInterval test = new MutableInterval(TEST_TIME1,
252: TEST_TIME2);
253: try {
254: test.setStart(new Instant(TEST_TIME2 + 1));
255: fail();
256: } catch (IllegalArgumentException ex) {
257: }
258: }
259:
260: public void testSetStart_RI3() {
261: MutableInterval test = new MutableInterval(TEST_TIME1,
262: TEST_TIME2);
263: test.setStart(null);
264: assertEquals(TEST_TIME_NOW, test.getStartMillis());
265: assertEquals(TEST_TIME2, test.getEndMillis());
266: }
267:
268: //-----------------------------------------------------------------------
269: public void testSetEndMillis_long1() {
270: MutableInterval test = new MutableInterval(TEST_TIME1,
271: TEST_TIME2);
272: test.setEndMillis(TEST_TIME2 + 1);
273: assertEquals(TEST_TIME1, test.getStartMillis());
274: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
275: }
276:
277: public void testSetEndMillis_long2() {
278: MutableInterval test = new MutableInterval(TEST_TIME1,
279: TEST_TIME2);
280: try {
281: test.setEndMillis(TEST_TIME1 - 1);
282: fail();
283: } catch (IllegalArgumentException ex) {
284: }
285: }
286:
287: //-----------------------------------------------------------------------
288: public void testSetEnd_RI1() {
289: MutableInterval test = new MutableInterval(TEST_TIME1,
290: TEST_TIME2);
291: test.setEnd(new Instant(TEST_TIME2 + 1));
292: assertEquals(TEST_TIME1, test.getStartMillis());
293: assertEquals(TEST_TIME2 + 1, test.getEndMillis());
294: }
295:
296: public void testSetEnd_RI2() {
297: MutableInterval test = new MutableInterval(TEST_TIME1,
298: TEST_TIME2);
299: try {
300: test.setEnd(new Instant(TEST_TIME1 - 1));
301: fail();
302: } catch (IllegalArgumentException ex) {
303: }
304: }
305:
306: public void testSetEnd_RI3() {
307: MutableInterval test = new MutableInterval(TEST_TIME1,
308: TEST_TIME2);
309: test.setEnd(null);
310: assertEquals(TEST_TIME1, test.getStartMillis());
311: assertEquals(TEST_TIME_NOW, test.getEndMillis());
312: }
313:
314: //-----------------------------------------------------------------------
315: public void testSetDurationAfterStart_long1() {
316: MutableInterval test = new MutableInterval(TEST_TIME1,
317: TEST_TIME2);
318: test.setDurationAfterStart(123L);
319: assertEquals(TEST_TIME1, test.getStartMillis());
320: assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
321: }
322:
323: public void testSeDurationAfterStart_long2() {
324: MutableInterval test = new MutableInterval(TEST_TIME1,
325: TEST_TIME2);
326: try {
327: test.setDurationAfterStart(-1);
328: fail();
329: } catch (IllegalArgumentException ex) {
330: }
331: }
332:
333: //-----------------------------------------------------------------------
334: public void testSetDurationAfterStart_RI1() {
335: MutableInterval test = new MutableInterval(TEST_TIME1,
336: TEST_TIME2);
337: test.setDurationAfterStart(new Duration(123L));
338: assertEquals(TEST_TIME1, test.getStartMillis());
339: assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
340: }
341:
342: public void testSeDurationAfterStart_RI2() {
343: MutableInterval test = new MutableInterval(TEST_TIME1,
344: TEST_TIME2);
345: try {
346: test.setDurationAfterStart(new Duration(-1));
347: fail();
348: } catch (IllegalArgumentException ex) {
349: }
350: }
351:
352: public void testSetDurationAfterStart_RI3() {
353: MutableInterval test = new MutableInterval(TEST_TIME1,
354: TEST_TIME2);
355: test.setDurationAfterStart(null);
356: assertEquals(TEST_TIME1, test.getStartMillis());
357: assertEquals(TEST_TIME1, test.getEndMillis());
358: }
359:
360: //-----------------------------------------------------------------------
361: public void testSetDurationBeforeEnd_long1() {
362: MutableInterval test = new MutableInterval(TEST_TIME1,
363: TEST_TIME2);
364: test.setDurationBeforeEnd(123L);
365: assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
366: assertEquals(TEST_TIME2, test.getEndMillis());
367: }
368:
369: public void testSeDurationBeforeEnd_long2() {
370: MutableInterval test = new MutableInterval(TEST_TIME1,
371: TEST_TIME2);
372: try {
373: test.setDurationBeforeEnd(-1);
374: fail();
375: } catch (IllegalArgumentException ex) {
376: }
377: }
378:
379: //-----------------------------------------------------------------------
380: public void testSetDurationBeforeEnd_RI1() {
381: MutableInterval test = new MutableInterval(TEST_TIME1,
382: TEST_TIME2);
383: test.setDurationBeforeEnd(new Duration(123L));
384: assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
385: assertEquals(TEST_TIME2, test.getEndMillis());
386: }
387:
388: public void testSeDurationBeforeEnd_RI2() {
389: MutableInterval test = new MutableInterval(TEST_TIME1,
390: TEST_TIME2);
391: try {
392: test.setDurationBeforeEnd(new Duration(-1));
393: fail();
394: } catch (IllegalArgumentException ex) {
395: }
396: }
397:
398: public void testSetDurationBeforeEnd_RI3() {
399: MutableInterval test = new MutableInterval(TEST_TIME1,
400: TEST_TIME2);
401: test.setDurationBeforeEnd(null);
402: assertEquals(TEST_TIME2, test.getStartMillis());
403: assertEquals(TEST_TIME2, test.getEndMillis());
404: }
405:
406: //-----------------------------------------------------------------------
407: public void testSetPeriodAfterStart_RI1() {
408: MutableInterval test = new MutableInterval(TEST_TIME1,
409: TEST_TIME2);
410: test.setPeriodAfterStart(new Period(123L));
411: assertEquals(TEST_TIME1, test.getStartMillis());
412: assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
413: }
414:
415: public void testSePeriodAfterStart_RI2() {
416: MutableInterval test = new MutableInterval(TEST_TIME1,
417: TEST_TIME2);
418: try {
419: test.setPeriodAfterStart(new Period(-1L));
420: fail();
421: } catch (IllegalArgumentException ex) {
422: }
423: }
424:
425: public void testSetPeriodAfterStart_RI3() {
426: MutableInterval test = new MutableInterval(TEST_TIME1,
427: TEST_TIME2);
428: test.setPeriodAfterStart(null);
429: assertEquals(TEST_TIME1, test.getStartMillis());
430: assertEquals(TEST_TIME1, test.getEndMillis());
431: }
432:
433: //-----------------------------------------------------------------------
434: public void testSetPeriodBeforeEnd_RI1() {
435: MutableInterval test = new MutableInterval(TEST_TIME1,
436: TEST_TIME2);
437: test.setPeriodBeforeEnd(new Period(123L));
438: assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
439: assertEquals(TEST_TIME2, test.getEndMillis());
440: }
441:
442: public void testSePeriodBeforeEnd_RI2() {
443: MutableInterval test = new MutableInterval(TEST_TIME1,
444: TEST_TIME2);
445: try {
446: test.setPeriodBeforeEnd(new Period(-1L));
447: fail();
448: } catch (IllegalArgumentException ex) {
449: }
450: }
451:
452: public void testSetPeriodBeforeEnd_RI3() {
453: MutableInterval test = new MutableInterval(TEST_TIME1,
454: TEST_TIME2);
455: test.setPeriodBeforeEnd(null);
456: assertEquals(TEST_TIME2, test.getStartMillis());
457: assertEquals(TEST_TIME2, test.getEndMillis());
458: }
459:
460: }
|