1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.PrintStream;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Modifier;
25 import java.security.AllPermission;
26 import java.security.CodeSource;
27 import java.security.Permission;
28 import java.security.PermissionCollection;
29 import java.security.Permissions;
30 import java.security.Policy;
31 import java.security.ProtectionDomain;
32 import java.text.DateFormatSymbols;
33 import java.util.HashSet;
34 import java.util.LinkedHashMap;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.TimeZone;
39
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 import org.joda.time.tz.DefaultNameProvider;
44 import org.joda.time.tz.NameProvider;
45 import org.joda.time.tz.Provider;
46 import org.joda.time.tz.UTCProvider;
47 import org.joda.time.tz.ZoneInfoProvider;
48
49
50
51
52
53
54 public class TestDateTimeZone extends TestCase {
55 private static final boolean OLD_JDK;
56 static {
57 String str = System.getProperty("java.version");
58 boolean old = true;
59 if (str.length() > 3 &&
60 str.charAt(0) == '1' &&
61 str.charAt(1) == '.' &&
62 (str.charAt(2) == '4' || str.charAt(2) == '5' || str.charAt(2) == '6')) {
63 old = false;
64 }
65 OLD_JDK = old;
66 }
67
68
69
70
71 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
72 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
73
74 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
75 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
76 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
77 366 + 365;
78 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
79 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
80 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
81 366 + 365 + 365;
82
83
84 private long TEST_TIME_SUMMER =
85 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
86
87
88 private long TEST_TIME_WINTER =
89 (y2002days + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
90
91
92
93
94
95
96
97
98
99
100
101
102
103 private static final Policy RESTRICT;
104 private static final Policy ALLOW;
105 static {
106
107 RESTRICT = new Policy() {
108 public PermissionCollection getPermissions(CodeSource codesource) {
109 Permissions p = new Permissions();
110 p.add(new AllPermission());
111 return p;
112 }
113 public void refresh() {
114 }
115 public boolean implies(ProtectionDomain domain, Permission permission) {
116 if (permission instanceof JodaTimePermission) {
117 return false;
118 }
119 return true;
120
121 }
122 };
123 ALLOW = new Policy() {
124 public PermissionCollection getPermissions(CodeSource codesource) {
125 Permissions p = new Permissions();
126 p.add(new AllPermission());
127 return p;
128 }
129 public void refresh() {
130 }
131 };
132 }
133
134 private DateTimeZone zone;
135 private Locale locale;
136
137 public static void main(String[] args) {
138 junit.textui.TestRunner.run(suite());
139 }
140
141 public static TestSuite suite() {
142 return new TestSuite(TestDateTimeZone.class);
143 }
144
145 public TestDateTimeZone(String name) {
146 super(name);
147 }
148
149 protected void setUp() throws Exception {
150 locale = Locale.getDefault();
151 zone = DateTimeZone.getDefault();
152 Locale.setDefault(Locale.UK);
153 }
154
155 protected void tearDown() throws Exception {
156 Locale.setDefault(locale);
157 DateTimeZone.setDefault(zone);
158 }
159
160
161 public void testDefault() {
162 assertNotNull(DateTimeZone.getDefault());
163
164 DateTimeZone.setDefault(PARIS);
165 assertSame(PARIS, DateTimeZone.getDefault());
166
167 try {
168 DateTimeZone.setDefault(null);
169 fail();
170 } catch (IllegalArgumentException ex) {}
171 }
172
173 public void testDefaultSecurity() {
174 if (OLD_JDK) {
175 return;
176 }
177 try {
178 Policy.setPolicy(RESTRICT);
179 System.setSecurityManager(new SecurityManager());
180 DateTimeZone.setDefault(PARIS);
181 fail();
182 } catch (SecurityException ex) {
183
184 } finally {
185 System.setSecurityManager(null);
186 Policy.setPolicy(ALLOW);
187 }
188 }
189
190
191 public void testForID_String() {
192 assertEquals(DateTimeZone.getDefault(), DateTimeZone.forID((String) null));
193
194 DateTimeZone zone = DateTimeZone.forID("Europe/London");
195 assertEquals("Europe/London", zone.getID());
196
197 zone = DateTimeZone.forID("UTC");
198 assertSame(DateTimeZone.UTC, zone);
199
200 zone = DateTimeZone.forID("+00:00");
201 assertSame(DateTimeZone.UTC, zone);
202
203 zone = DateTimeZone.forID("+00");
204 assertSame(DateTimeZone.UTC, zone);
205
206 zone = DateTimeZone.forID("+01:23");
207 assertEquals("+01:23", zone.getID());
208 assertEquals(DateTimeConstants.MILLIS_PER_HOUR + (23L * DateTimeConstants.MILLIS_PER_MINUTE),
209 zone.getOffset(TEST_TIME_SUMMER));
210
211 zone = DateTimeZone.forID("-02:00");
212 assertEquals("-02:00", zone.getID());
213 assertEquals((-2L * DateTimeConstants.MILLIS_PER_HOUR),
214 zone.getOffset(TEST_TIME_SUMMER));
215
216 zone = DateTimeZone.forID("-07:05:34.0");
217 assertEquals("-07:05:34", zone.getID());
218 assertEquals((-7L * DateTimeConstants.MILLIS_PER_HOUR) +
219 (-5L * DateTimeConstants.MILLIS_PER_MINUTE) +
220 (-34L * DateTimeConstants.MILLIS_PER_SECOND),
221 zone.getOffset(TEST_TIME_SUMMER));
222
223 try {
224 DateTimeZone.forID("SST");
225 fail();
226 } catch (IllegalArgumentException ex) {}
227 try {
228 DateTimeZone.forID("europe/london");
229 fail();
230 } catch (IllegalArgumentException ex) {}
231 try {
232 DateTimeZone.forID("Europe/UK");
233 fail();
234 } catch (IllegalArgumentException ex) {}
235 try {
236 DateTimeZone.forID("+");
237 fail();
238 } catch (IllegalArgumentException ex) {}
239 try {
240 DateTimeZone.forID("+0");
241 fail();
242 } catch (IllegalArgumentException ex) {}
243 }
244
245 public void testForID_String_old() {
246 Map<String, String> map = new LinkedHashMap<String, String>();
247 map.put("GMT", "UTC");
248 map.put("WET", "WET");
249 map.put("CET", "CET");
250 map.put("MET", "CET");
251 map.put("ECT", "CET");
252 map.put("EET", "EET");
253 map.put("MIT", "Pacific/Apia");
254 map.put("HST", "Pacific/Honolulu");
255 map.put("AST", "America/Anchorage");
256 map.put("PST", "America/Los_Angeles");
257 map.put("MST", "America/Denver");
258 map.put("PNT", "America/Phoenix");
259 map.put("CST", "America/Chicago");
260 map.put("EST", "America/New_York");
261 map.put("IET", "America/Indiana/Indianapolis");
262 map.put("PRT", "America/Puerto_Rico");
263 map.put("CNT", "America/St_Johns");
264 map.put("AGT", "America/Argentina/Buenos_Aires");
265 map.put("BET", "America/Sao_Paulo");
266 map.put("ART", "Africa/Cairo");
267 map.put("CAT", "Africa/Harare");
268 map.put("EAT", "Africa/Addis_Ababa");
269 map.put("NET", "Asia/Yerevan");
270 map.put("PLT", "Asia/Karachi");
271 map.put("IST", "Asia/Kolkata");
272 map.put("BST", "Asia/Dhaka");
273 map.put("VST", "Asia/Ho_Chi_Minh");
274 map.put("CTT", "Asia/Shanghai");
275 map.put("JST", "Asia/Tokyo");
276 map.put("ACT", "Australia/Darwin");
277 map.put("AET", "Australia/Sydney");
278 map.put("SST", "Pacific/Guadalcanal");
279 map.put("NST", "Pacific/Auckland");
280 for (String key : map.keySet()) {
281 String value = map.get(key);
282 TimeZone juZone = TimeZone.getTimeZone(key);
283 DateTimeZone zone = DateTimeZone.forTimeZone(juZone);
284 assertEquals(value, zone.getID());
285
286
287
288
289 }
290 }
291
292
293 public void testForOffsetHours_int() {
294 assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHours(0));
295 assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone.forOffsetHours(3));
296 assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetHours(-2));
297 try {
298 DateTimeZone.forOffsetHours(999999);
299 fail();
300 } catch (IllegalArgumentException ex) {}
301 }
302
303
304 public void testForOffsetHoursMinutes_int_int() {
305 assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHoursMinutes(0, 0));
306 assertEquals(DateTimeZone.forID("+03:15"), DateTimeZone.forOffsetHoursMinutes(3, 15));
307 assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetHoursMinutes(-2, 0));
308 assertEquals(DateTimeZone.forID("-02:30"), DateTimeZone.forOffsetHoursMinutes(-2, 30));
309 try {
310 DateTimeZone.forOffsetHoursMinutes(2, 60);
311 fail();
312 } catch (IllegalArgumentException ex) {}
313 try {
314 DateTimeZone.forOffsetHoursMinutes(-2, 60);
315 fail();
316 } catch (IllegalArgumentException ex) {}
317 try {
318 DateTimeZone.forOffsetHoursMinutes(2, -1);
319 fail();
320 } catch (IllegalArgumentException ex) {}
321 try {
322 DateTimeZone.forOffsetHoursMinutes(-2, -1);
323 fail();
324 } catch (IllegalArgumentException ex) {}
325 try {
326 DateTimeZone.forOffsetHoursMinutes(999999, 0);
327 fail();
328 } catch (IllegalArgumentException ex) {}
329 }
330
331
332 public void testForOffsetMillis_int() {
333 assertSame(DateTimeZone.UTC, DateTimeZone.forOffsetMillis(0));
334 assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone.forOffsetMillis(3 * 60 * 60 * 1000));
335 assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetMillis(-2 * 60 * 60 * 1000));
336 assertEquals(DateTimeZone.forID("+04:45:17.045"),
337 DateTimeZone.forOffsetMillis(
338 4 * 60 * 60 * 1000 + 45 * 60 * 1000 + 17 * 1000 + 45));
339 }
340
341
342 public void testForTimeZone_TimeZone() {
343 assertEquals(DateTimeZone.getDefault(), DateTimeZone.forTimeZone((TimeZone) null));
344
345 DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"));
346 assertEquals("Europe/London", zone.getID());
347 assertSame(DateTimeZone.UTC, DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));
348
349 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("+00:00"));
350 assertSame(DateTimeZone.UTC, zone);
351
352 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+00:00"));
353 assertSame(DateTimeZone.UTC, zone);
354
355 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+00:00"));
356 assertSame(DateTimeZone.UTC, zone);
357
358 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+00"));
359 assertSame(DateTimeZone.UTC, zone);
360
361 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+01:23"));
362 assertEquals("+01:23", zone.getID());
363 assertEquals(DateTimeConstants.MILLIS_PER_HOUR + (23L * DateTimeConstants.MILLIS_PER_MINUTE),
364 zone.getOffset(TEST_TIME_SUMMER));
365
366 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+1:23"));
367 assertEquals("+01:23", zone.getID());
368 assertEquals(DateTimeConstants.MILLIS_PER_HOUR + (23L * DateTimeConstants.MILLIS_PER_MINUTE),
369 zone.getOffset(TEST_TIME_SUMMER));
370
371 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT-02:00"));
372 assertEquals("-02:00", zone.getID());
373 assertEquals((-2L * DateTimeConstants.MILLIS_PER_HOUR), zone.getOffset(TEST_TIME_SUMMER));
374
375 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+2"));
376 assertEquals("+02:00", zone.getID());
377 assertEquals((2L * DateTimeConstants.MILLIS_PER_HOUR), zone.getOffset(TEST_TIME_SUMMER));
378
379 zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("EST"));
380 assertEquals("America/New_York", zone.getID());
381 }
382
383 public void testTimeZoneConversion() {
384 TimeZone jdkTimeZone = TimeZone.getTimeZone("GMT-10");
385 assertEquals("GMT-10:00", jdkTimeZone.getID());
386
387 DateTimeZone jodaTimeZone = DateTimeZone.forTimeZone(jdkTimeZone);
388 assertEquals("-10:00", jodaTimeZone.getID());
389 assertEquals(jdkTimeZone.getRawOffset(), jodaTimeZone.getOffset(0L));
390
391 TimeZone convertedTimeZone = jodaTimeZone.toTimeZone();
392 assertEquals("GMT-10:00", jdkTimeZone.getID());
393
394 assertEquals(jdkTimeZone.getID(), convertedTimeZone.getID());
395 assertEquals(jdkTimeZone.getRawOffset(), convertedTimeZone.getRawOffset());
396 }
397
398
399 public void testGetAvailableIDs() {
400 assertTrue(DateTimeZone.getAvailableIDs().contains("UTC"));
401 }
402
403
404 public void testProvider() {
405 try {
406 assertNotNull(DateTimeZone.getProvider());
407
408 Provider provider = DateTimeZone.getProvider();
409 DateTimeZone.setProvider(null);
410 assertEquals(provider.getClass(), DateTimeZone.getProvider().getClass());
411
412 try {
413 DateTimeZone.setProvider(new MockNullIDSProvider());
414 fail();
415 } catch (IllegalArgumentException ex) {}
416 try {
417 DateTimeZone.setProvider(new MockEmptyIDSProvider());
418 fail();
419 } catch (IllegalArgumentException ex) {}
420 try {
421 DateTimeZone.setProvider(new MockNoUTCProvider());
422 fail();
423 } catch (IllegalArgumentException ex) {}
424 try {
425 DateTimeZone.setProvider(new MockBadUTCProvider());
426 fail();
427 } catch (IllegalArgumentException ex) {}
428
429 Provider prov = new MockOKProvider();
430 DateTimeZone.setProvider(prov);
431 assertSame(prov, DateTimeZone.getProvider());
432 assertEquals(2, DateTimeZone.getAvailableIDs().size());
433 assertTrue(DateTimeZone.getAvailableIDs().contains("UTC"));
434 assertTrue(DateTimeZone.getAvailableIDs().contains("Europe/London"));
435 } finally {
436 DateTimeZone.setProvider(null);
437 assertEquals(ZoneInfoProvider.class, DateTimeZone.getProvider().getClass());
438 }
439
440 try {
441 System.setProperty("org.joda.time.DateTimeZone.Provider", "org.joda.time.tz.UTCProvider");
442 DateTimeZone.setProvider(null);
443 assertEquals(UTCProvider.class, DateTimeZone.getProvider().getClass());
444 } finally {
445 System.getProperties().remove("org.joda.time.DateTimeZone.Provider");
446 DateTimeZone.setProvider(null);
447 assertEquals(ZoneInfoProvider.class, DateTimeZone.getProvider().getClass());
448 }
449
450 PrintStream syserr = System.err;
451 try {
452 System.setProperty("org.joda.time.DateTimeZone.Provider", "xxx");
453 ByteArrayOutputStream baos = new ByteArrayOutputStream();
454 System.setErr(new PrintStream(baos));
455
456 DateTimeZone.setProvider(null);
457
458 assertEquals(ZoneInfoProvider.class, DateTimeZone.getProvider().getClass());
459 String str = new String(baos.toByteArray());
460 assertTrue(str.indexOf("java.lang.ClassNotFoundException") >= 0);
461 } finally {
462 System.setErr(syserr);
463 System.getProperties().remove("org.joda.time.DateTimeZone.Provider");
464 DateTimeZone.setProvider(null);
465 assertEquals(ZoneInfoProvider.class, DateTimeZone.getProvider().getClass());
466 }
467 }
468
469 public void testProviderSecurity() {
470 if (OLD_JDK) {
471 return;
472 }
473 try {
474 Policy.setPolicy(RESTRICT);
475 System.setSecurityManager(new SecurityManager());
476 DateTimeZone.setProvider(new MockOKProvider());
477 fail();
478 } catch (SecurityException ex) {
479
480 } finally {
481 System.setSecurityManager(null);
482 Policy.setPolicy(ALLOW);
483 }
484 }
485
486 static class MockNullIDSProvider implements Provider {
487 public Set getAvailableIDs() {
488 return null;
489 }
490 public DateTimeZone getZone(String id) {
491 return null;
492 }
493 }
494 static class MockEmptyIDSProvider implements Provider {
495 public Set getAvailableIDs() {
496 return new HashSet();
497 }
498 public DateTimeZone getZone(String id) {
499 return null;
500 }
501 }
502 static class MockNoUTCProvider implements Provider {
503 public Set getAvailableIDs() {
504 Set set = new HashSet();
505 set.add("Europe/London");
506 return set;
507 }
508 public DateTimeZone getZone(String id) {
509 return null;
510 }
511 }
512 static class MockBadUTCProvider implements Provider {
513 public Set getAvailableIDs() {
514 Set set = new HashSet();
515 set.add("UTC");
516 set.add("Europe/London");
517 return set;
518 }
519 public DateTimeZone getZone(String id) {
520 return null;
521 }
522 }
523 static class MockOKProvider implements Provider {
524 public Set getAvailableIDs() {
525 Set set = new HashSet();
526 set.add("UTC");
527 set.add("Europe/London");
528 return set;
529 }
530 public DateTimeZone getZone(String id) {
531 return DateTimeZone.UTC;
532 }
533 }
534
535
536 public void testNameProvider() {
537 try {
538 assertNotNull(DateTimeZone.getNameProvider());
539
540 NameProvider provider = DateTimeZone.getNameProvider();
541 DateTimeZone.setNameProvider(null);
542 assertEquals(provider.getClass(), DateTimeZone.getNameProvider().getClass());
543
544 provider = new MockOKButNullNameProvider();
545 DateTimeZone.setNameProvider(provider);
546 assertSame(provider, DateTimeZone.getNameProvider());
547
548 assertEquals("+00:00", DateTimeZone.UTC.getShortName(TEST_TIME_SUMMER));
549 assertEquals("+00:00", DateTimeZone.UTC.getName(TEST_TIME_SUMMER));
550 } finally {
551 DateTimeZone.setNameProvider(null);
552 }
553
554 try {
555 System.setProperty("org.joda.time.DateTimeZone.NameProvider", "org.joda.time.tz.DefaultNameProvider");
556 DateTimeZone.setNameProvider(null);
557 assertEquals(DefaultNameProvider.class, DateTimeZone.getNameProvider().getClass());
558 } finally {
559 System.getProperties().remove("org.joda.time.DateTimeZone.NameProvider");
560 DateTimeZone.setNameProvider(null);
561 assertEquals(DefaultNameProvider.class, DateTimeZone.getNameProvider().getClass());
562 }
563
564 PrintStream syserr = System.err;
565 try {
566 System.setProperty("org.joda.time.DateTimeZone.NameProvider", "xxx");
567 ByteArrayOutputStream baos = new ByteArrayOutputStream();
568 System.setErr(new PrintStream(baos));
569
570 DateTimeZone.setNameProvider(null);
571
572 assertEquals(DefaultNameProvider.class, DateTimeZone.getNameProvider().getClass());
573 String str = new String(baos.toByteArray());
574 assertTrue(str.indexOf("java.lang.ClassNotFoundException") >= 0);
575 } finally {
576 System.setErr(syserr);
577 System.getProperties().remove("org.joda.time.DateTimeZone.NameProvider");
578 DateTimeZone.setNameProvider(null);
579 assertEquals(DefaultNameProvider.class, DateTimeZone.getNameProvider().getClass());
580 }
581 }
582
583 public void testNameProviderSecurity() {
584 if (OLD_JDK) {
585 return;
586 }
587 try {
588 Policy.setPolicy(RESTRICT);
589 System.setSecurityManager(new SecurityManager());
590 DateTimeZone.setNameProvider(new MockOKButNullNameProvider());
591 fail();
592 } catch (SecurityException ex) {
593
594 } finally {
595 System.setSecurityManager(null);
596 Policy.setPolicy(ALLOW);
597 }
598 }
599
600 static class MockOKButNullNameProvider implements NameProvider {
601 public String getShortName(Locale locale, String id, String nameKey) {
602 return null;
603 }
604 public String getName(Locale locale, String id, String nameKey) {
605 return null;
606 }
607 }
608
609
610 public void testConstructor() {
611 assertEquals(1, DateTimeZone.class.getDeclaredConstructors().length);
612 assertTrue(Modifier.isProtected(DateTimeZone.class.getDeclaredConstructors()[0].getModifiers()));
613 try {
614 new DateTimeZone(null) {
615 public String getNameKey(long instant) {
616 return null;
617 }
618 public int getOffset(long instant) {
619 return 0;
620 }
621 public int getStandardOffset(long instant) {
622 return 0;
623 }
624 public boolean isFixed() {
625 return false;
626 }
627 public long nextTransition(long instant) {
628 return 0;
629 }
630 public long previousTransition(long instant) {
631 return 0;
632 }
633 public boolean equals(Object object) {
634 return false;
635 }
636 };
637 } catch (IllegalArgumentException ex) {}
638 }
639
640
641 public void testGetID() {
642 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
643 assertEquals("Europe/Paris", zone.getID());
644 }
645
646 public void testGetNameKey() {
647 DateTimeZone zone = DateTimeZone.forID("Europe/London");
648 assertEquals("BST", zone.getNameKey(TEST_TIME_SUMMER));
649 assertEquals("GMT", zone.getNameKey(TEST_TIME_WINTER));
650 }
651
652 static final boolean JDK6;
653 static {
654 boolean jdk6 = true;
655 try {
656 DateFormatSymbols.class.getMethod("getInstance", new Class[] {Locale.class});
657 } catch (Exception ex) {
658 jdk6 = false;
659 }
660 JDK6 = jdk6;
661 }
662
663 public void testGetShortName() {
664 DateTimeZone zone = DateTimeZone.forID("Europe/London");
665 assertEquals("BST", zone.getShortName(TEST_TIME_SUMMER));
666 assertEquals("GMT", zone.getShortName(TEST_TIME_WINTER));
667 assertEquals("BST", zone.getShortName(TEST_TIME_SUMMER, Locale.ENGLISH));
668 }
669
670 public void testGetShortName_berlin() {
671 DateTimeZone berlin = DateTimeZone.forID("Europe/Berlin");
672 assertEquals("CET", berlin.getShortName(TEST_TIME_WINTER, Locale.ENGLISH));
673 assertEquals("CEST", berlin.getShortName(TEST_TIME_SUMMER, Locale.ENGLISH));
674 if (JDK6) {
675 assertEquals("MEZ", berlin.getShortName(TEST_TIME_WINTER, Locale.GERMAN));
676 assertEquals("MESZ", berlin.getShortName(TEST_TIME_SUMMER, Locale.GERMAN));
677 } else {
678 assertEquals("CET", berlin.getShortName(TEST_TIME_WINTER, Locale.GERMAN));
679 assertEquals("CEST", berlin.getShortName(TEST_TIME_SUMMER, Locale.GERMAN));
680 }
681 }
682
683 public void testGetShortNameProviderName() {
684 assertEquals(null, DateTimeZone.getNameProvider().getShortName(null, "Europe/London", "BST"));
685 assertEquals(null, DateTimeZone.getNameProvider().getShortName(Locale.ENGLISH, null, "BST"));
686 assertEquals(null, DateTimeZone.getNameProvider().getShortName(Locale.ENGLISH, "Europe/London", null));
687 assertEquals(null, DateTimeZone.getNameProvider().getShortName(null, null, null));
688 }
689
690 public void testGetShortNameNullKey() {
691 DateTimeZone zone = new MockDateTimeZone("Europe/London");
692 assertEquals("Europe/London", zone.getShortName(TEST_TIME_SUMMER, Locale.ENGLISH));
693 }
694
695 public void testGetName() {
696 DateTimeZone zone = DateTimeZone.forID("Europe/London");
697 assertEquals("British Summer Time", zone.getName(TEST_TIME_SUMMER));
698 assertEquals("Greenwich Mean Time", zone.getName(TEST_TIME_WINTER));
699 assertEquals("British Summer Time", zone.getName(TEST_TIME_SUMMER, Locale.ENGLISH));
700 }
701
702 public void testGetName_berlin() {
703 DateTimeZone berlin = DateTimeZone.forID("Europe/Berlin");
704 assertEquals("Central European Time", berlin.getName(TEST_TIME_WINTER, Locale.ENGLISH));
705 assertEquals("Central European Summer Time", berlin.getName(TEST_TIME_SUMMER, Locale.ENGLISH));
706 if (JDK6) {
707 assertEquals("Mitteleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN));
708 assertEquals("Mitteleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN));
709 } else {
710 assertEquals("Zentraleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN));
711 assertEquals("Zentraleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN));
712 }
713 }
714
715 public void testGetNameProviderName() {
716 assertEquals(null, DateTimeZone.getNameProvider().getName(null, "Europe/London", "BST"));
717 assertEquals(null, DateTimeZone.getNameProvider().getName(Locale.ENGLISH, null, "BST"));
718 assertEquals(null, DateTimeZone.getNameProvider().getName(Locale.ENGLISH, "Europe/London", null));
719 assertEquals(null, DateTimeZone.getNameProvider().getName(null, null, null));
720 }
721
722 public void testGetNameNullKey() {
723 DateTimeZone zone = new MockDateTimeZone("Europe/London");
724 assertEquals("Europe/London", zone.getName(TEST_TIME_SUMMER, Locale.ENGLISH));
725 }
726
727 static class MockDateTimeZone extends DateTimeZone {
728 public MockDateTimeZone(String id) {
729 super(id);
730 }
731 public String getNameKey(long instant) {
732 return null;
733 }
734 public int getOffset(long instant) {
735 return 0;
736 }
737 public int getStandardOffset(long instant) {
738 return 0;
739 }
740 public boolean isFixed() {
741 return false;
742 }
743 public long nextTransition(long instant) {
744 return 0;
745 }
746 public long previousTransition(long instant) {
747 return 0;
748 }
749 public boolean equals(Object object) {
750 return false;
751 }
752 }
753
754
755 public void testGetOffset_long() {
756 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
757 assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_SUMMER));
758 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_WINTER));
759
760 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_SUMMER));
761 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_WINTER));
762
763 assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_SUMMER));
764 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_WINTER));
765
766 assertEquals(false, zone.isStandardOffset(TEST_TIME_SUMMER));
767 assertEquals(true, zone.isStandardOffset(TEST_TIME_WINTER));
768 }
769
770 public void testGetOffset_RI() {
771 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
772 assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_SUMMER)));
773 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_WINTER)));
774
775 assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()), zone.getOffset(null));
776 }
777
778 public void testGetOffsetFixed() {
779 DateTimeZone zone = DateTimeZone.forID("+01:00");
780 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_SUMMER));
781 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_WINTER));
782
783 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_SUMMER));
784 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_WINTER));
785
786 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_SUMMER));
787 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_WINTER));
788
789 assertEquals(true, zone.isStandardOffset(TEST_TIME_SUMMER));
790 assertEquals(true, zone.isStandardOffset(TEST_TIME_WINTER));
791 }
792
793 public void testGetOffsetFixed_RI() {
794 DateTimeZone zone = DateTimeZone.forID("+01:00");
795 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_SUMMER)));
796 assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_WINTER)));
797
798 assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()), zone.getOffset(null));
799 }
800
801
802 public void testGetMillisKeepLocal() {
803 long millisLondon = TEST_TIME_SUMMER;
804 long millisParis = TEST_TIME_SUMMER - 1L * DateTimeConstants.MILLIS_PER_HOUR;
805
806 assertEquals(millisLondon, LONDON.getMillisKeepLocal(LONDON, millisLondon));
807 assertEquals(millisParis, LONDON.getMillisKeepLocal(LONDON, millisParis));
808 assertEquals(millisLondon, PARIS.getMillisKeepLocal(PARIS, millisLondon));
809 assertEquals(millisParis, PARIS.getMillisKeepLocal(PARIS, millisParis));
810
811 assertEquals(millisParis, LONDON.getMillisKeepLocal(PARIS, millisLondon));
812 assertEquals(millisLondon, PARIS.getMillisKeepLocal(LONDON, millisParis));
813
814 DateTimeZone zone = DateTimeZone.getDefault();
815 try {
816 DateTimeZone.setDefault(LONDON);
817 assertEquals(millisLondon, PARIS.getMillisKeepLocal(null, millisParis));
818 } finally {
819 DateTimeZone.setDefault(zone);
820 }
821 }
822
823
824 public void testIsFixed() {
825 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
826 assertEquals(false, zone.isFixed());
827 assertEquals(true, DateTimeZone.UTC.isFixed());
828 }
829
830
831 public void testTransitionFixed() {
832 DateTimeZone zone = DateTimeZone.forID("+01:00");
833 assertEquals(TEST_TIME_SUMMER, zone.nextTransition(TEST_TIME_SUMMER));
834 assertEquals(TEST_TIME_WINTER, zone.nextTransition(TEST_TIME_WINTER));
835 assertEquals(TEST_TIME_SUMMER, zone.previousTransition(TEST_TIME_SUMMER));
836 assertEquals(TEST_TIME_WINTER, zone.previousTransition(TEST_TIME_WINTER));
837 }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874 public void testIsLocalDateTimeGap_Berlin() {
875 DateTimeZone zone = DateTimeZone.forID("Europe/Berlin");
876 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 1, 0)));
877 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 1, 59, 59, 99)));
878 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 0)));
879 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 30)));
880 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 59, 59, 99)));
881 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 3, 0)));
882 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 4, 0)));
883
884 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 1, 30)));
885 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 2, 30)));
886 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 3, 30)));
887 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 12, 24, 12, 34)));
888 }
889
890
891 public void testIsLocalDateTimeGap_NewYork() {
892 DateTimeZone zone = DateTimeZone.forID("America/New_York");
893 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 1, 0)));
894 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 1, 59, 59, 99)));
895 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 2, 0)));
896 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 2, 30)));
897 assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 2, 59, 59, 99)));
898 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 3, 0)));
899 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 11, 4, 0)));
900
901 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 11, 4, 0, 30)));
902 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 11, 4, 1, 30)));
903 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 11, 4, 2, 30)));
904 assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 12, 24, 12, 34)));
905 }
906
907
908 public void testToTimeZone() {
909 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
910 TimeZone tz = zone.toTimeZone();
911 assertEquals("Europe/Paris", tz.getID());
912 }
913
914
915 public void testEqualsHashCode() {
916 DateTimeZone zone1 = DateTimeZone.forID("Europe/Paris");
917 DateTimeZone zone2 = DateTimeZone.forID("Europe/Paris");
918 assertEquals(true, zone1.equals(zone1));
919 assertEquals(true, zone1.equals(zone2));
920 assertEquals(true, zone2.equals(zone1));
921 assertEquals(true, zone2.equals(zone2));
922 assertEquals(true, zone1.hashCode() == zone2.hashCode());
923
924 DateTimeZone zone3 = DateTimeZone.forID("Europe/London");
925 assertEquals(true, zone3.equals(zone3));
926 assertEquals(false, zone1.equals(zone3));
927 assertEquals(false, zone2.equals(zone3));
928 assertEquals(false, zone3.equals(zone1));
929 assertEquals(false, zone3.equals(zone2));
930 assertEquals(false, zone1.hashCode() == zone3.hashCode());
931 assertEquals(true, zone3.hashCode() == zone3.hashCode());
932
933 DateTimeZone zone4 = DateTimeZone.forID("+01:00");
934 assertEquals(true, zone4.equals(zone4));
935 assertEquals(false, zone1.equals(zone4));
936 assertEquals(false, zone2.equals(zone4));
937 assertEquals(false, zone3.equals(zone4));
938 assertEquals(false, zone4.equals(zone1));
939 assertEquals(false, zone4.equals(zone2));
940 assertEquals(false, zone4.equals(zone3));
941 assertEquals(false, zone1.hashCode() == zone4.hashCode());
942 assertEquals(true, zone4.hashCode() == zone4.hashCode());
943
944 DateTimeZone zone5 = DateTimeZone.forID("+02:00");
945 assertEquals(true, zone5.equals(zone5));
946 assertEquals(false, zone1.equals(zone5));
947 assertEquals(false, zone2.equals(zone5));
948 assertEquals(false, zone3.equals(zone5));
949 assertEquals(false, zone4.equals(zone5));
950 assertEquals(false, zone5.equals(zone1));
951 assertEquals(false, zone5.equals(zone2));
952 assertEquals(false, zone5.equals(zone3));
953 assertEquals(false, zone5.equals(zone4));
954 assertEquals(false, zone1.hashCode() == zone5.hashCode());
955 assertEquals(true, zone5.hashCode() == zone5.hashCode());
956 }
957
958
959 public void testToString() {
960 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
961 assertEquals("Europe/Paris", zone.toString());
962 assertEquals("UTC", DateTimeZone.UTC.toString());
963 }
964
965
966 public void testSerialization1() throws Exception {
967 DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
968
969 ByteArrayOutputStream baos = new ByteArrayOutputStream();
970 ObjectOutputStream oos = new ObjectOutputStream(baos);
971 oos.writeObject(zone);
972 byte[] bytes = baos.toByteArray();
973 oos.close();
974
975 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
976 ObjectInputStream ois = new ObjectInputStream(bais);
977 DateTimeZone result = (DateTimeZone) ois.readObject();
978 ois.close();
979
980 assertSame(zone, result);
981 }
982
983
984 public void testSerialization2() throws Exception {
985 DateTimeZone zone = DateTimeZone.forID("+01:00");
986
987 ByteArrayOutputStream baos = new ByteArrayOutputStream();
988 ObjectOutputStream oos = new ObjectOutputStream(baos);
989 oos.writeObject(zone);
990 byte[] bytes = baos.toByteArray();
991 oos.close();
992
993 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
994 ObjectInputStream ois = new ObjectInputStream(bais);
995 DateTimeZone result = (DateTimeZone) ois.readObject();
996 ois.close();
997
998 assertSame(zone, result);
999 }
1000
1001 public void testCommentParse() throws Exception {
1002
1003
1004
1005 DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
1006 DateTime dt = new DateTime(2005, 5, 5, 20, 10, 15, 0, zone);
1007 assertEquals(1115313015000L, dt.getMillis());
1008 }
1009
1010 public void testPatchedNameKeysLondon() throws Exception {
1011
1012 DateTimeZone zone = DateTimeZone.forID("Europe/London");
1013
1014 DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
1015 String str1 = zone.getName(now.getMillis());
1016 String str2 = zone.getName(now.plusMonths(6).getMillis());
1017 assertEquals(false, str1.equals(str2));
1018 }
1019
1020 public void testPatchedNameKeysSydney() throws Exception {
1021
1022 DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
1023
1024 DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
1025 String str1 = zone.getName(now.getMillis());
1026 String str2 = zone.getName(now.plusMonths(6).getMillis());
1027 assertEquals(false, str1.equals(str2));
1028 }
1029
1030 public void testPatchedNameKeysSydneyHistoric() throws Exception {
1031
1032 DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
1033
1034 DateTime now = new DateTime(1996, 1, 1, 0, 0, 0, 0);
1035 String str1 = zone.getName(now.getMillis());
1036 String str2 = zone.getName(now.plusMonths(6).getMillis());
1037 assertEquals(false, str1.equals(str2));
1038 }
1039
1040 public void testPatchedNameKeysGazaHistoric() throws Exception {
1041
1042 DateTimeZone zone = DateTimeZone.forID("Africa/Johannesburg");
1043
1044 DateTime now = new DateTime(1943, 1, 1, 0, 0, 0, 0);
1045 String str1 = zone.getName(now.getMillis());
1046 String str2 = zone.getName(now.plusMonths(6).getMillis());
1047 assertEquals(false, str1.equals(str2));
1048 }
1049
1050 }