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