KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestDateTimeUtils


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 import java.lang.reflect.Modifier JavaDoc;
19 import java.security.AllPermission JavaDoc;
20 import java.security.CodeSource JavaDoc;
21 import java.security.Permission JavaDoc;
22 import java.security.PermissionCollection JavaDoc;
23 import java.security.Permissions JavaDoc;
24 import java.security.Policy JavaDoc;
25 import java.security.ProtectionDomain JavaDoc;
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 /**
37  * This class is a Junit unit test for Instant.
38  *
39  * @author Stephen Colebourne
40  */

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 JavaDoc 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     // Test in 2002/03 as time zones are more well known
58
// (before the late 90's they were all over the place)
59

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     // 2002-06-09
73
private long TEST_TIME_NOW =
74             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
75             
76     // 2002-04-05
77
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     // 2003-05-06
83
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 JavaDoc RESTRICT;
89     private static final Policy JavaDoc ALLOW;
90     static {
91         // don't call Policy.getPolicy()
92
RESTRICT = new Policy JavaDoc() {
93             public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
94                 Permissions JavaDoc p = new Permissions JavaDoc();
95                 p.add(new AllPermission JavaDoc()); // enable everything
96
return p;
97             }
98             public void refresh() {
99             }
100             public boolean implies(ProtectionDomain JavaDoc domain, Permission JavaDoc permission) {
101                 if (permission instanceof JodaTimePermission) {
102                     return false;
103                 }
104                 return true;
105 // return super.implies(domain, permission);
106
}
107         };
108         ALLOW = new Policy JavaDoc() {
109             public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
110                 Permissions JavaDoc p = new Permissions JavaDoc();
111                 p.add(new AllPermission JavaDoc()); // enable everything
112
return p;
113             }
114             public void refresh() {
115             }
116         };
117     }
118     
119     public static void main(String JavaDoc[] 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 JavaDoc name) {
128         super(name);
129     }
130
131     protected void setUp() throws Exception JavaDoc {
132     }
133
134     protected void tearDown() throws Exception JavaDoc {
135     }
136
137     //-----------------------------------------------------------------------
138
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     //-----------------------------------------------------------------------
145
public void testClass() {
146         Class JavaDoc 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     //-----------------------------------------------------------------------
157
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     //-----------------------------------------------------------------------
165
public void testSystemMillisSecurity() {
166         if (OLD_JDK) {
167             return;
168         }
169         try {
170             try {
171                 Policy.setPolicy(RESTRICT);
172                 System.setSecurityManager(new SecurityManager JavaDoc());
173                 DateTimeUtils.setCurrentMillisSystem();
174                 fail();
175             } catch (SecurityException JavaDoc ex) {
176                 // ok
177
} finally {
178                 System.setSecurityManager(null);
179                 Policy.setPolicy(ALLOW);
180             }
181         } finally {
182             DateTimeUtils.setCurrentMillisSystem();
183         }
184     }
185
186     //-----------------------------------------------------------------------
187
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     //-----------------------------------------------------------------------
203
public void testFixedMillisSecurity() {
204         if (OLD_JDK) {
205             return;
206         }
207         try {
208             try {
209                 Policy.setPolicy(RESTRICT);
210                 System.setSecurityManager(new SecurityManager JavaDoc());
211                 DateTimeUtils.setCurrentMillisFixed(0L);
212                 fail();
213             } catch (SecurityException JavaDoc ex) {
214                 // ok
215
} finally {
216                 System.setSecurityManager(null);
217                 Policy.setPolicy(ALLOW);
218             }
219         } finally {
220             DateTimeUtils.setCurrentMillisSystem();
221         }
222     }
223
224     //-----------------------------------------------------------------------
225
public void testOffsetMillis() {
226         try {
227             // set time to one day ago
228
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     //-----------------------------------------------------------------------
245
public void testOffsetMillisToZero() {
246         long now1 = 0L;
247         try {
248             // set time to one day ago
249
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     //-----------------------------------------------------------------------
259
public void testOffsetMillisSecurity() {
260         if (OLD_JDK) {
261             return;
262         }
263         try {
264             try {
265                 Policy.setPolicy(RESTRICT);
266                 System.setSecurityManager(new SecurityManager JavaDoc());
267                 DateTimeUtils.setCurrentMillisOffset(-24 * 60 * 60 * 1000);
268                 fail();
269             } catch (SecurityException JavaDoc ex) {
270                 // ok
271
} finally {
272                 System.setSecurityManager(null);
273                 Policy.setPolicy(ALLOW);
274             }
275         } finally {
276             DateTimeUtils.setCurrentMillisSystem();
277         }
278     }
279
280     //-----------------------------------------------------------------------
281
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     //-----------------------------------------------------------------------
293
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; // testing for this
306
}
307         };
308         assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(ai));
309         
310         assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(null));
311     }
312
313     //-----------------------------------------------------------------------
314
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; // testing for this
323
}
324         };
325         assertEquals(ISOChronology.getInstance(), DateTimeUtils.getIntervalChronology(ai));
326     }
327
328     //-----------------------------------------------------------------------
329
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     //-----------------------------------------------------------------------
339
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     //-----------------------------------------------------------------------
352
public void testGetChronology_Chronology() {
353         assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getChronology(BuddhistChronology.getInstance()));
354         assertEquals(ISOChronology.getInstance(), DateTimeUtils.getChronology(null));
355     }
356
357     //-----------------------------------------------------------------------
358
public void testGetZone_Zone() {
359         assertEquals(PARIS, DateTimeUtils.getZone(PARIS));
360         assertEquals(DateTimeZone.getDefault(), DateTimeUtils.getZone(null));
361     }
362
363     //-----------------------------------------------------------------------
364
public void testGetPeriodType_PeriodType() {
365         assertEquals(PeriodType.dayTime(), DateTimeUtils.getPeriodType(PeriodType.dayTime()));
366         assertEquals(PeriodType.standard(), DateTimeUtils.getPeriodType(null));
367     }
368
369     //-----------------------------------------------------------------------
370
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     //-----------------------------------------------------------------------
377
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 JavaDoc ex) {}
397     }
398
399     //-----------------------------------------------------------------------
400
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 JavaDoc ex) {}
420     }
421
422 }
423
Popular Tags