001 /*
002 * Copyright 2001-2012 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.lang.reflect.Modifier;
019 import java.security.AllPermission;
020 import java.security.CodeSource;
021 import java.security.Permission;
022 import java.security.PermissionCollection;
023 import java.security.Permissions;
024 import java.security.Policy;
025 import java.security.ProtectionDomain;
026
027 import junit.framework.TestCase;
028 import junit.framework.TestSuite;
029
030 import org.joda.time.DateTimeUtils.MillisProvider;
031 import org.joda.time.base.AbstractInstant;
032 import org.joda.time.chrono.BuddhistChronology;
033 import org.joda.time.chrono.CopticChronology;
034 import org.joda.time.chrono.GJChronology;
035 import org.joda.time.chrono.ISOChronology;
036 import org.joda.time.chrono.JulianChronology;
037
038 /**
039 * This class is a Junit unit test for DateTimeUtils.
040 *
041 * @author Stephen Colebourne
042 */
043 public class TestDateTimeUtils extends TestCase {
044
045 private static final GJChronology GJ = GJChronology.getInstance();
046 private static final boolean OLD_JDK;
047 static {
048 String str = System.getProperty("java.version");
049 boolean old = true;
050 if (str.length() > 3 &&
051 str.charAt(0) == '1' &&
052 str.charAt(1) == '.' &&
053 (str.charAt(2) == '4' || str.charAt(2) == '5' || str.charAt(2) == '6')) {
054 old = false;
055 }
056 OLD_JDK = old;
057 }
058
059 // Test in 2002/03 as time zones are more well known
060 // (before the late 90's they were all over the place)
061
062 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
063 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
064
065 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
066 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
067 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
068 366 + 365;
069 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
070 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
071 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
072 366 + 365 + 365;
073
074 // 2002-06-09
075 private long TEST_TIME_NOW =
076 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
077
078 // 2002-04-05
079 private long TEST_TIME1 =
080 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
081 + 12L * DateTimeConstants.MILLIS_PER_HOUR
082 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
083
084 // 2003-05-06
085 private long TEST_TIME2 =
086 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
087 + 14L * DateTimeConstants.MILLIS_PER_HOUR
088 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
089
090 private static final Policy RESTRICT;
091 private static final Policy ALLOW;
092 static {
093 // don't call Policy.getPolicy()
094 RESTRICT = new Policy() {
095 public PermissionCollection getPermissions(CodeSource codesource) {
096 Permissions p = new Permissions();
097 p.add(new AllPermission()); // enable everything
098 return p;
099 }
100 public void refresh() {
101 }
102 public boolean implies(ProtectionDomain domain, Permission permission) {
103 if (permission instanceof JodaTimePermission) {
104 return false;
105 }
106 return true;
107 // return super.implies(domain, permission);
108 }
109 };
110 ALLOW = new Policy() {
111 public PermissionCollection getPermissions(CodeSource codesource) {
112 Permissions p = new Permissions();
113 p.add(new AllPermission()); // enable everything
114 return p;
115 }
116 public void refresh() {
117 }
118 };
119 }
120
121 public static void main(String[] args) {
122 junit.textui.TestRunner.run(suite());
123 }
124
125 public static TestSuite suite() {
126 return new TestSuite(TestDateTimeUtils.class);
127 }
128
129 public TestDateTimeUtils(String name) {
130 super(name);
131 }
132
133 protected void setUp() throws Exception {
134 }
135
136 protected void tearDown() throws Exception {
137 }
138
139 //-----------------------------------------------------------------------
140 public void testTest() {
141 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
142 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
143 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
144 }
145
146 //-----------------------------------------------------------------------
147 public void testClass() {
148 Class cls = DateTimeUtils.class;
149 assertEquals(true, Modifier.isPublic(cls.getModifiers()));
150 assertEquals(false, Modifier.isFinal(cls.getModifiers()));
151
152 assertEquals(1, cls.getDeclaredConstructors().length);
153 assertEquals(true, Modifier.isProtected(cls.getDeclaredConstructors()[0].getModifiers()));
154
155 DateTimeUtils utils = new DateTimeUtils() {};
156 }
157
158 //-----------------------------------------------------------------------
159 public void testSystemMillis() {
160 long nowSystem = System.currentTimeMillis();
161 long now = DateTimeUtils.currentTimeMillis();
162 assertTrue((now >= nowSystem));
163 assertTrue((now - nowSystem) < 10000L);
164 }
165
166 //-----------------------------------------------------------------------
167 public void testSystemMillisSecurity() {
168 if (OLD_JDK) {
169 return;
170 }
171 try {
172 try {
173 Policy.setPolicy(RESTRICT);
174 System.setSecurityManager(new SecurityManager());
175 DateTimeUtils.setCurrentMillisSystem();
176 fail();
177 } catch (SecurityException ex) {
178 // ok
179 } finally {
180 System.setSecurityManager(null);
181 Policy.setPolicy(ALLOW);
182 }
183 } finally {
184 DateTimeUtils.setCurrentMillisSystem();
185 }
186 }
187
188 //-----------------------------------------------------------------------
189 public void testFixedMillis() {
190 try {
191 DateTimeUtils.setCurrentMillisFixed(0L);
192 assertEquals(0L, DateTimeUtils.currentTimeMillis());
193 assertEquals(0L, DateTimeUtils.currentTimeMillis());
194 assertEquals(0L, DateTimeUtils.currentTimeMillis());
195 } finally {
196 DateTimeUtils.setCurrentMillisSystem();
197 }
198 long nowSystem = System.currentTimeMillis();
199 long now = DateTimeUtils.currentTimeMillis();
200 assertTrue((now >= nowSystem));
201 assertTrue((now - nowSystem) < 10000L);
202 }
203
204 //-----------------------------------------------------------------------
205 public void testFixedMillisSecurity() {
206 if (OLD_JDK) {
207 return;
208 }
209 try {
210 try {
211 Policy.setPolicy(RESTRICT);
212 System.setSecurityManager(new SecurityManager());
213 DateTimeUtils.setCurrentMillisFixed(0L);
214 fail();
215 } catch (SecurityException ex) {
216 // ok
217 } finally {
218 System.setSecurityManager(null);
219 Policy.setPolicy(ALLOW);
220 }
221 } finally {
222 DateTimeUtils.setCurrentMillisSystem();
223 }
224 }
225
226 //-----------------------------------------------------------------------
227 public void testOffsetMillis() {
228 try {
229 // set time to one day ago
230 DateTimeUtils.setCurrentMillisOffset(-24 * 60 * 60 * 1000);
231 long nowSystem = System.currentTimeMillis();
232 long now = DateTimeUtils.currentTimeMillis();
233 long nowAdjustDay = now + (24 * 60 * 60 * 1000);
234 assertTrue((now < nowSystem));
235 assertTrue((nowAdjustDay >= nowSystem));
236 assertTrue((nowAdjustDay - nowSystem) < 10000L);
237 } finally {
238 DateTimeUtils.setCurrentMillisSystem();
239 }
240 long nowSystem = System.currentTimeMillis();
241 long now = DateTimeUtils.currentTimeMillis();
242 assertTrue((now >= nowSystem));
243 assertTrue((now - nowSystem) < 10000L);
244 }
245
246 //-----------------------------------------------------------------------
247 public void testOffsetMillisToZero() {
248 long now1 = 0L;
249 try {
250 // set time to one day ago
251 DateTimeUtils.setCurrentMillisOffset(0);
252 now1 = DateTimeUtils.currentTimeMillis();
253 } finally {
254 DateTimeUtils.setCurrentMillisSystem();
255 }
256 long now2 = DateTimeUtils.currentTimeMillis();
257 assertEquals(now1, now2);
258 }
259
260 //-----------------------------------------------------------------------
261 public void testOffsetMillisSecurity() {
262 if (OLD_JDK) {
263 return;
264 }
265 try {
266 try {
267 Policy.setPolicy(RESTRICT);
268 System.setSecurityManager(new SecurityManager());
269 DateTimeUtils.setCurrentMillisOffset(-24 * 60 * 60 * 1000);
270 fail();
271 } catch (SecurityException ex) {
272 // ok
273 } finally {
274 System.setSecurityManager(null);
275 Policy.setPolicy(ALLOW);
276 }
277 } finally {
278 DateTimeUtils.setCurrentMillisSystem();
279 }
280 }
281
282 //-----------------------------------------------------------------------
283 public void testMillisProvider() {
284 try {
285 DateTimeUtils.setCurrentMillisProvider(new MillisProvider() {
286 public long getMillis() {
287 return 1L;
288 }
289 });
290 assertEquals(1L, DateTimeUtils.currentTimeMillis());
291 } finally {
292 DateTimeUtils.setCurrentMillisSystem();
293 }
294 }
295
296 public void testMillisProvider_null() {
297 try {
298 DateTimeUtils.setCurrentMillisProvider(null);
299 } catch (IllegalArgumentException ex) {
300 // expected
301 }
302 }
303
304 //-----------------------------------------------------------------------
305 public void testMillisProviderSecurity() {
306 if (OLD_JDK) {
307 return;
308 }
309 try {
310 try {
311 Policy.setPolicy(RESTRICT);
312 System.setSecurityManager(new SecurityManager());
313 DateTimeUtils.setCurrentMillisProvider(new MillisProvider() {
314 public long getMillis() {
315 return 0L;
316 }
317 });
318 fail();
319 } catch (SecurityException ex) {
320 // ok
321 } finally {
322 System.setSecurityManager(null);
323 Policy.setPolicy(ALLOW);
324 }
325 } finally {
326 DateTimeUtils.setCurrentMillisSystem();
327 }
328 }
329
330 //-----------------------------------------------------------------------
331 public void testGetInstantMillis_RI() {
332 Instant i = new Instant(123L);
333 assertEquals(123L, DateTimeUtils.getInstantMillis(i));
334 try {
335 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
336 assertEquals(TEST_TIME_NOW, DateTimeUtils.getInstantMillis(null));
337 } finally {
338 DateTimeUtils.setCurrentMillisSystem();
339 }
340 }
341
342 //-----------------------------------------------------------------------
343 public void testGetInstantChronology_RI() {
344 DateTime dt = new DateTime(123L, BuddhistChronology.getInstance());
345 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getInstantChronology(dt));
346
347 Instant i = new Instant(123L);
348 assertEquals(ISOChronology.getInstanceUTC(), DateTimeUtils.getInstantChronology(i));
349
350 AbstractInstant ai = new AbstractInstant() {
351 public long getMillis() {
352 return 0L;
353 }
354 public Chronology getChronology() {
355 return null; // testing for this
356 }
357 };
358 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(ai));
359
360 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(null));
361 }
362
363 //-----------------------------------------------------------------------
364 public void testGetIntervalChronology_RInterval() {
365 Interval dt = new Interval(123L, 456L, BuddhistChronology.getInstance());
366 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt));
367
368 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(null));
369
370 MutableInterval ai = new MutableInterval() {
371 public Chronology getChronology() {
372 return null; // testing for this
373 }
374 };
375 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(ai));
376 }
377
378 //-----------------------------------------------------------------------
379 public void testGetIntervalChronology_RI_RI() {
380 DateTime dt1 = new DateTime(123L, BuddhistChronology.getInstance());
381 DateTime dt2 = new DateTime(123L, CopticChronology.getInstance());
382 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt1, dt2));
383 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getIntervalChronology(dt1, null));
384 assertEquals(CopticChronology.getInstance(), DateTimeUtils.getIntervalChronology(null, dt2));
385 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(null, null));
386 }
387
388 //-----------------------------------------------------------------------
389 public void testGetReadableInterval_ReadableInterval() {
390 ReadableInterval input = new Interval(0, 100L);
391 assertEquals(input, DateTimeUtils.getReadableInterval(input));
392
393 try {
394 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
395 assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME_NOW), DateTimeUtils.getReadableInterval(null));
396 } finally {
397 DateTimeUtils.setCurrentMillisSystem();
398 }
399 }
400
401 //-----------------------------------------------------------------------
402 public void testGetChronology_Chronology() {
403 assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getChronology(BuddhistChronology.getInstance()));
404 assertEquals(ISOChronology.getInstance(), DateTimeUtils.getChronology(null));
405 }
406
407 //-----------------------------------------------------------------------
408 public void testGetZone_Zone() {
409 assertEquals(PARIS, DateTimeUtils.getZone(PARIS));
410 assertEquals(DateTimeZone.getDefault(), DateTimeUtils.getZone(null));
411 }
412
413 //-----------------------------------------------------------------------
414 public void testGetPeriodType_PeriodType() {
415 assertEquals(PeriodType.dayTime(), DateTimeUtils.getPeriodType(PeriodType.dayTime()));
416 assertEquals(PeriodType.standard(), DateTimeUtils.getPeriodType(null));
417 }
418
419 //-----------------------------------------------------------------------
420 public void testGetDurationMillis_RI() {
421 Duration dur = new Duration(123L);
422 assertEquals(123L, DateTimeUtils.getDurationMillis(dur));
423 assertEquals(0L, DateTimeUtils.getDurationMillis(null));
424 }
425
426 //-----------------------------------------------------------------------
427 public void testIsContiguous_RP() {
428 YearMonthDay ymd = new YearMonthDay(2005, 6, 9);
429 assertEquals(true, DateTimeUtils.isContiguous(ymd));
430 TimeOfDay tod = new TimeOfDay(12, 20, 30, 0);
431 assertEquals(true, DateTimeUtils.isContiguous(tod));
432 Partial year = new Partial(DateTimeFieldType.year(), 2005);
433 assertEquals(true, DateTimeUtils.isContiguous(year));
434 Partial hourOfDay = new Partial(DateTimeFieldType.hourOfDay(), 12);
435 assertEquals(true, DateTimeUtils.isContiguous(hourOfDay));
436 Partial yearHour = year.with(DateTimeFieldType.hourOfDay(), 12);
437 assertEquals(false, DateTimeUtils.isContiguous(yearHour));
438 Partial ymdd = new Partial(ymd).with(DateTimeFieldType.dayOfWeek(), 2);
439 assertEquals(false, DateTimeUtils.isContiguous(ymdd));
440 Partial dd = new Partial(DateTimeFieldType.dayOfMonth(), 13).with(DateTimeFieldType.dayOfWeek(), 5);
441 assertEquals(false, DateTimeUtils.isContiguous(dd));
442
443 try {
444 DateTimeUtils.isContiguous((ReadablePartial) null);
445 fail();
446 } catch (IllegalArgumentException ex) {}
447 }
448
449 //-----------------------------------------------------------------------
450 public void testIsContiguous_RP_GJChronology() {
451 YearMonthDay ymd = new YearMonthDay(2005, 6, 9, GJ);
452 assertEquals(true, DateTimeUtils.isContiguous(ymd));
453 TimeOfDay tod = new TimeOfDay(12, 20, 30, 0, GJ);
454 assertEquals(true, DateTimeUtils.isContiguous(tod));
455 Partial year = new Partial(DateTimeFieldType.year(), 2005, GJ);
456 assertEquals(true, DateTimeUtils.isContiguous(year));
457 Partial hourOfDay = new Partial(DateTimeFieldType.hourOfDay(), 12, GJ);
458 assertEquals(true, DateTimeUtils.isContiguous(hourOfDay));
459 Partial yearHour = year.with(DateTimeFieldType.hourOfDay(), 12);
460 assertEquals(false, DateTimeUtils.isContiguous(yearHour));
461 Partial ymdd = new Partial(ymd).with(DateTimeFieldType.dayOfWeek(), 2);
462 assertEquals(false, DateTimeUtils.isContiguous(ymdd));
463 Partial dd = new Partial(DateTimeFieldType.dayOfMonth(), 13).with(DateTimeFieldType.dayOfWeek(), 5);
464 assertEquals(false, DateTimeUtils.isContiguous(dd));
465
466 try {
467 DateTimeUtils.isContiguous((ReadablePartial) null);
468 fail();
469 } catch (IllegalArgumentException ex) {}
470 }
471
472 //-----------------------------------------------------------------------
473 public void test_julianDay() {
474 DateTime base = new DateTime(1970, 1, 1, 0, 0, DateTimeZone.UTC);
475
476 assertEquals(2440587.5d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
477 assertEquals(2440588, DateTimeUtils.toJulianDayNumber(base.getMillis()));
478 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(2440587.5d));
479
480 base = base.plusHours(6);
481 assertEquals(2440587.75d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
482 assertEquals(2440588, DateTimeUtils.toJulianDayNumber(base.getMillis()));
483 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(2440587.75d));
484
485 base = base.plusHours(6);
486 assertEquals(2440588d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
487 assertEquals(2440588, DateTimeUtils.toJulianDayNumber(base.getMillis()));
488 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(2440588d));
489
490 base = base.plusHours(6);
491 assertEquals(2440588.25d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
492 assertEquals(2440588, DateTimeUtils.toJulianDayNumber(base.getMillis()));
493 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(2440588.25d));
494
495 base = base.plusHours(6);
496 assertEquals(2440588.5d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
497 assertEquals(2440589, DateTimeUtils.toJulianDayNumber(base.getMillis()));
498 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(2440588.5d));
499
500 base = new DateTime(2012, 8, 31, 23, 50, DateTimeZone.UTC);
501 assertEquals(2456171.4930555555, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
502 assertEquals(2456171, DateTimeUtils.toJulianDayNumber(base.getMillis()));
503
504 base = new DateTime(-4713, 1, 1, 12, 0, JulianChronology.getInstanceUTC());
505 assertEquals(0d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
506 assertEquals(0, DateTimeUtils.toJulianDayNumber(base.getMillis()));
507 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(0d));
508
509 base = new DateTime(-4713, 1, 1, 0, 0, JulianChronology.getInstanceUTC());
510 assertEquals(-0.5d, DateTimeUtils.toJulianDay(base.getMillis()), 0.0001d);
511 assertEquals(0, DateTimeUtils.toJulianDayNumber(base.getMillis()));
512 assertEquals(base.getMillis(), DateTimeUtils.fromJulianDay(-0.5d));
513 }
514
515 }