1 16 package org.joda.time; 17 18 import java.lang.reflect.Modifier ; 19 import java.security.AllPermission ; 20 import java.security.CodeSource ; 21 import java.security.Permission ; 22 import java.security.PermissionCollection ; 23 import java.security.Permissions ; 24 import java.security.Policy ; 25 import java.security.ProtectionDomain ; 26 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.joda.time.base.AbstractInstant; 31 import org.joda.time.chrono.BuddhistChronology; 32 import org.joda.time.chrono.CopticChronology; 33 import org.joda.time.chrono.GJChronology; 34 import org.joda.time.chrono.ISOChronology; 35 36 41 public class TestDateTimeUtils extends TestCase { 42 43 private static final GJChronology GJ = GJChronology.getInstance(); 44 private static final boolean OLD_JDK; 45 static { 46 String str = System.getProperty("java.version"); 47 boolean old = true; 48 if (str.length() > 3 && 49 str.charAt(0) == '1' && 50 str.charAt(1) == '.' && 51 (str.charAt(2) == '4' || str.charAt(2) == '5' || str.charAt(2) == '6')) { 52 old = false; 53 } 54 OLD_JDK = old; 55 } 56 57 60 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 61 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 62 63 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 64 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 65 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 66 366 + 365; 67 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 68 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 69 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 70 366 + 365 + 365; 71 72 private long TEST_TIME_NOW = 74 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 75 76 private long TEST_TIME1 = 78 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 79 + 12L * DateTimeConstants.MILLIS_PER_HOUR 80 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 81 82 private long TEST_TIME2 = 84 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 85 + 14L * DateTimeConstants.MILLIS_PER_HOUR 86 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 87 88 private static final Policy RESTRICT; 89 private static final Policy ALLOW; 90 static { 91 RESTRICT = new Policy () { 93 public PermissionCollection getPermissions(CodeSource codesource) { 94 Permissions p = new Permissions (); 95 p.add(new AllPermission ()); return p; 97 } 98 public void refresh() { 99 } 100 public boolean implies(ProtectionDomain domain, Permission permission) { 101 if (permission instanceof JodaTimePermission) { 102 return false; 103 } 104 return true; 105 } 107 }; 108 ALLOW = new Policy () { 109 public PermissionCollection getPermissions(CodeSource codesource) { 110 Permissions p = new Permissions (); 111 p.add(new AllPermission ()); return p; 113 } 114 public void refresh() { 115 } 116 }; 117 } 118 119 public static void main(String [] args) { 120 junit.textui.TestRunner.run(suite()); 121 } 122 123 public static TestSuite suite() { 124 return new TestSuite(TestDateTimeUtils.class); 125 } 126 127 public TestDateTimeUtils(String name) { 128 super(name); 129 } 130 131 protected void setUp() throws Exception { 132 } 133 134 protected void tearDown() throws Exception { 135 } 136 137 public void testTest() { 139 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString()); 140 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString()); 141 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString()); 142 } 143 144 public void testClass() { 146 Class cls = DateTimeUtils.class; 147 assertEquals(true, Modifier.isPublic(cls.getModifiers())); 148 assertEquals(false, Modifier.isFinal(cls.getModifiers())); 149 150 assertEquals(1, cls.getDeclaredConstructors().length); 151 assertEquals(true, Modifier.isProtected(cls.getDeclaredConstructors()[0].getModifiers())); 152 153 DateTimeUtils utils = new DateTimeUtils() {}; 154 } 155 156 public void testSystemMillis() { 158 long nowSystem = System.currentTimeMillis(); 159 long now = DateTimeUtils.currentTimeMillis(); 160 assertTrue((now >= nowSystem)); 161 assertTrue((now - nowSystem) < 10000L); 162 } 163 164 public void testSystemMillisSecurity() { 166 if (OLD_JDK) { 167 return; 168 } 169 try { 170 try { 171 Policy.setPolicy(RESTRICT); 172 System.setSecurityManager(new SecurityManager ()); 173 DateTimeUtils.setCurrentMillisSystem(); 174 fail(); 175 } catch (SecurityException ex) { 176 } finally { 178 System.setSecurityManager(null); 179 Policy.setPolicy(ALLOW); 180 } 181 } finally { 182 DateTimeUtils.setCurrentMillisSystem(); 183 } 184 } 185 186 public void testFixedMillis() { 188 try { 189 DateTimeUtils.setCurrentMillisFixed(0L); 190 assertEquals(0L, DateTimeUtils.currentTimeMillis()); 191 assertEquals(0L, DateTimeUtils.currentTimeMillis()); 192 assertEquals(0L, DateTimeUtils.currentTimeMillis()); 193 } finally { 194 DateTimeUtils.setCurrentMillisSystem(); 195 } 196 long nowSystem = System.currentTimeMillis(); 197 long now = DateTimeUtils.currentTimeMillis(); 198 assertTrue((now >= nowSystem)); 199 assertTrue((now - nowSystem) < 10000L); 200 } 201 202 public void testFixedMillisSecurity() { 204 if (OLD_JDK) { 205 return; 206 } 207 try { 208 try { 209 Policy.setPolicy(RESTRICT); 210 System.setSecurityManager(new SecurityManager ()); 211 DateTimeUtils.setCurrentMillisFixed(0L); 212 fail(); 213 } catch (SecurityException ex) { 214 } finally { 216 System.setSecurityManager(null); 217 Policy.setPolicy(ALLOW); 218 } 219 } finally { 220 DateTimeUtils.setCurrentMillisSystem(); 221 } 222 } 223 224 public void testOffsetMillis() { 226 try { 227 DateTimeUtils.setCurrentMillisOffset(-24 * 60 * 60 * 1000); 229 long nowSystem = System.currentTimeMillis(); 230 long now = DateTimeUtils.currentTimeMillis(); 231 long nowAdjustDay = now + (24 * 60 * 60 * 1000); 232 assertTrue((now < nowSystem)); 233 assertTrue((nowAdjustDay >= nowSystem)); 234 assertTrue((nowAdjustDay - nowSystem) < 10000L); 235 } finally { 236 DateTimeUtils.setCurrentMillisSystem(); 237 } 238 long nowSystem = System.currentTimeMillis(); 239 long now = DateTimeUtils.currentTimeMillis(); 240 assertTrue((now >= nowSystem)); 241 assertTrue((now - nowSystem) < 10000L); 242 } 243 244 public void testOffsetMillisToZero() { 246 long now1 = 0L; 247 try { 248 DateTimeUtils.setCurrentMillisOffset(0); 250 now1 = DateTimeUtils.currentTimeMillis(); 251 } finally { 252 DateTimeUtils.setCurrentMillisSystem(); 253 } 254 long now2 = DateTimeUtils.currentTimeMillis(); 255 assertEquals(now1, now2); 256 } 257 258 public void testOffsetMillisSecurity() { 260 if (OLD_JDK) { 261 return; 262 } 263 try { 264 try { 265 Policy.setPolicy(RESTRICT); 266 System.setSecurityManager(new SecurityManager ()); 267 DateTimeUtils.setCurrentMillisOffset(-24 * 60 * 60 * 1000); 268 fail(); 269 } catch (SecurityException ex) { 270 } finally { 272 System.setSecurityManager(null); 273 Policy.setPolicy(ALLOW); 274 } 275 } finally { 276 DateTimeUtils.setCurrentMillisSystem(); 277 } 278 } 279 280 public void testGetInstantMillis_RI() { 282 Instant i = new Instant(123L); 283 assertEquals(123L, DateTimeUtils.getInstantMillis(i)); 284 try { 285 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 286 assertEquals(TEST_TIME_NOW, DateTimeUtils.getInstantMillis(null)); 287 } finally { 288 DateTimeUtils.setCurrentMillisSystem(); 289 } 290 } 291 292 public void testGetInstantChronology_RI() { 294 DateTime dt = new DateTime(123L, BuddhistChronology.getInstance()); 295 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getInstantChronology(dt)); 296 297 Instant i = new Instant(123L); 298 assertEquals(ISOChronology.getInstanceUTC(), DateTimeUtils.getInstantChronology(i)); 299 300 AbstractInstant ai = new AbstractInstant() { 301 public long getMillis() { 302 return 0L; 303 } 304 public Chronology getChronology() { 305 return null; } 307 }; 308 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(ai)); 309 310 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(null)); 311 } 312 313 public void testGetIntervalChronology_RInterval() { 315 Interval dt = new Interval(123L, 456L, BuddhistChronology.getInstance()); 316 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt)); 317 318 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(null)); 319 320 MutableInterval ai = new MutableInterval() { 321 public Chronology getChronology() { 322 return null; } 324 }; 325 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(ai)); 326 } 327 328 public void testGetIntervalChronology_RI_RI() { 330 DateTime dt1 = new DateTime(123L, BuddhistChronology.getInstance()); 331 DateTime dt2 = new DateTime(123L, CopticChronology.getInstance()); 332 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt1, dt2)); 333 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt1, null)); 334 assertEquals(CopticChronology.getInstance(), DateTimeUtils.getIntervalChronology(null, dt2)); 335 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(null, null)); 336 } 337 338 public void testGetReadableInterval_ReadableInterval() { 340 ReadableInterval input = new Interval(0, 100L); 341 assertEquals(input, DateTimeUtils.getReadableInterval(input)); 342 343 try { 344 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 345 assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME_NOW), DateTimeUtils.getReadableInterval(null)); 346 } finally { 347 DateTimeUtils.setCurrentMillisSystem(); 348 } 349 } 350 351 public void testGetChronology_Chronology() { 353 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getChronology(BuddhistChronology.getInstance())); 354 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getChronology(null)); 355 } 356 357 public void testGetZone_Zone() { 359 assertEquals(PARIS, DateTimeUtils.getZone(PARIS)); 360 assertEquals(DateTimeZone.getDefault(), DateTimeUtils.getZone(null)); 361 } 362 363 public void testGetPeriodType_PeriodType() { 365 assertEquals(PeriodType.dayTime(), DateTimeUtils.getPeriodType(PeriodType.dayTime())); 366 assertEquals(PeriodType.standard(), DateTimeUtils.getPeriodType(null)); 367 } 368 369 public void testGetDurationMillis_RI() { 371 Duration dur = new Duration(123L); 372 assertEquals(123L, DateTimeUtils.getDurationMillis(dur)); 373 assertEquals(0L, DateTimeUtils.getDurationMillis(null)); 374 } 375 376 public void testIsContiguous_RP() { 378 YearMonthDay ymd = new YearMonthDay(2005, 6, 9); 379 assertEquals(true, DateTimeUtils.isContiguous(ymd)); 380 TimeOfDay tod = new TimeOfDay(12, 20, 30, 0); 381 assertEquals(true, DateTimeUtils.isContiguous(tod)); 382 Partial year = new Partial(DateTimeFieldType.year(), 2005); 383 assertEquals(true, DateTimeUtils.isContiguous(year)); 384 Partial hourOfDay = new Partial(DateTimeFieldType.hourOfDay(), 12); 385 assertEquals(true, DateTimeUtils.isContiguous(hourOfDay)); 386 Partial yearHour = year.with(DateTimeFieldType.hourOfDay(), 12); 387 assertEquals(false, DateTimeUtils.isContiguous(yearHour)); 388 Partial ymdd = new Partial(ymd).with(DateTimeFieldType.dayOfWeek(), 2); 389 assertEquals(false, DateTimeUtils.isContiguous(ymdd)); 390 Partial dd = new Partial(DateTimeFieldType.dayOfMonth(), 13).with(DateTimeFieldType.dayOfWeek(), 5); 391 assertEquals(false, DateTimeUtils.isContiguous(dd)); 392 393 try { 394 DateTimeUtils.isContiguous((ReadablePartial) null); 395 fail(); 396 } catch (IllegalArgumentException ex) {} 397 } 398 399 public void testIsContiguous_RP_GJChronology() { 401 YearMonthDay ymd = new YearMonthDay(2005, 6, 9, GJ); 402 assertEquals(true, DateTimeUtils.isContiguous(ymd)); 403 TimeOfDay tod = new TimeOfDay(12, 20, 30, 0, GJ); 404 assertEquals(true, DateTimeUtils.isContiguous(tod)); 405 Partial year = new Partial(DateTimeFieldType.year(), 2005, GJ); 406 assertEquals(true, DateTimeUtils.isContiguous(year)); 407 Partial hourOfDay = new Partial(DateTimeFieldType.hourOfDay(), 12, GJ); 408 assertEquals(true, DateTimeUtils.isContiguous(hourOfDay)); 409 Partial yearHour = year.with(DateTimeFieldType.hourOfDay(), 12); 410 assertEquals(false, DateTimeUtils.isContiguous(yearHour)); 411 Partial ymdd = new Partial(ymd).with(DateTimeFieldType.dayOfWeek(), 2); 412 assertEquals(false, DateTimeUtils.isContiguous(ymdd)); 413 Partial dd = new Partial(DateTimeFieldType.dayOfMonth(), 13).with(DateTimeFieldType.dayOfWeek(), 5); 414 assertEquals(false, DateTimeUtils.isContiguous(dd)); 415 416 try { 417 DateTimeUtils.isContiguous((ReadablePartial) null); 418 fail(); 419 } catch (IllegalArgumentException ex) {} 420 } 421 422 } 423 | Popular Tags |