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