Source Code Cross Referenced for TestDateTimeZone.java in  » Development » Joda-Time » org » joda » time » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Joda Time » org.joda.time 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Copyright 2001-2006 Stephen Colebourne
0003:         *
0004:         *  Licensed under the Apache License, Version 2.0 (the "License");
0005:         *  you may not use this file except in compliance with the License.
0006:         *  You may obtain a copy of the License at
0007:         *
0008:         *      http://www.apache.org/licenses/LICENSE-2.0
0009:         *
0010:         *  Unless required by applicable law or agreed to in writing, software
0011:         *  distributed under the License is distributed on an "AS IS" BASIS,
0012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         *  See the License for the specific language governing permissions and
0014:         *  limitations under the License.
0015:         */
0016:        package org.joda.time;
0017:
0018:        import java.io.ByteArrayInputStream;
0019:        import java.io.ByteArrayOutputStream;
0020:        import java.io.ObjectInputStream;
0021:        import java.io.ObjectOutputStream;
0022:        import java.io.PrintStream;
0023:        import java.lang.reflect.Modifier;
0024:        import java.security.AllPermission;
0025:        import java.security.CodeSource;
0026:        import java.security.Permission;
0027:        import java.security.PermissionCollection;
0028:        import java.security.Permissions;
0029:        import java.security.Policy;
0030:        import java.security.ProtectionDomain;
0031:        import java.util.HashSet;
0032:        import java.util.Locale;
0033:        import java.util.Set;
0034:        import java.util.TimeZone;
0035:
0036:        import junit.framework.TestCase;
0037:        import junit.framework.TestSuite;
0038:
0039:        import org.joda.time.tz.DefaultNameProvider;
0040:        import org.joda.time.tz.NameProvider;
0041:        import org.joda.time.tz.Provider;
0042:        import org.joda.time.tz.UTCProvider;
0043:        import org.joda.time.tz.ZoneInfoProvider;
0044:
0045:        /**
0046:         * This class is a JUnit test for DateTimeZone.
0047:         *
0048:         * @author Stephen Colebourne
0049:         */
0050:        public class TestDateTimeZone extends TestCase {
0051:            private static final boolean OLD_JDK;
0052:            static {
0053:                String str = System.getProperty("java.version");
0054:                boolean old = true;
0055:                if (str.length() > 3
0056:                        && str.charAt(0) == '1'
0057:                        && str.charAt(1) == '.'
0058:                        && (str.charAt(2) == '4' || str.charAt(2) == '5' || str
0059:                                .charAt(2) == '6')) {
0060:                    old = false;
0061:                }
0062:                OLD_JDK = old;
0063:            }
0064:
0065:            // Test in 2002/03 as time zones are more well known
0066:            // (before the late 90's they were all over the place)
0067:
0068:            private static final DateTimeZone PARIS = DateTimeZone
0069:                    .forID("Europe/Paris");
0070:            private static final DateTimeZone LONDON = DateTimeZone
0071:                    .forID("Europe/London");
0072:
0073:            long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0074:                    + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0075:                    + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0076:                    + 365 + 365 + 366 + 365;
0077:            long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0078:                    + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0079:                    + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0080:                    + 365 + 365 + 366 + 365 + 365;
0081:
0082:            // 2002-06-09
0083:            private long TEST_TIME_SUMMER = (y2002days + 31L + 28L + 31L + 30L
0084:                    + 31L + 9L - 1L)
0085:                    * DateTimeConstants.MILLIS_PER_DAY;
0086:
0087:            // 2002-01-09
0088:            private long TEST_TIME_WINTER = (y2002days + 9L - 1L)
0089:                    * DateTimeConstants.MILLIS_PER_DAY;
0090:
0091:            //    // 2002-04-05 Fri
0092:            //    private long TEST_TIME1 =
0093:            //            (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
0094:            //            + 12L * DateTimeConstants.MILLIS_PER_HOUR
0095:            //            + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
0096:            //        
0097:            //    // 2003-05-06 Tue
0098:            //    private long TEST_TIME2 =
0099:            //            (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
0100:            //            + 14L * DateTimeConstants.MILLIS_PER_HOUR
0101:            //            + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
0102:
0103:            private static final Policy RESTRICT;
0104:            private static final Policy ALLOW;
0105:            static {
0106:                // don't call Policy.getPolicy()
0107:                RESTRICT = new Policy() {
0108:                    public PermissionCollection getPermissions(
0109:                            CodeSource codesource) {
0110:                        Permissions p = new Permissions();
0111:                        p.add(new AllPermission()); // enable everything
0112:                        return p;
0113:                    }
0114:
0115:                    public void refresh() {
0116:                    }
0117:
0118:                    public boolean implies(ProtectionDomain domain,
0119:                            Permission permission) {
0120:                        if (permission instanceof  JodaTimePermission) {
0121:                            return false;
0122:                        }
0123:                        return true;
0124:                        //                return super.implies(domain, permission);
0125:                    }
0126:                };
0127:                ALLOW = new Policy() {
0128:                    public PermissionCollection getPermissions(
0129:                            CodeSource codesource) {
0130:                        Permissions p = new Permissions();
0131:                        p.add(new AllPermission()); // enable everything
0132:                        return p;
0133:                    }
0134:
0135:                    public void refresh() {
0136:                    }
0137:                };
0138:            }
0139:
0140:            private DateTimeZone zone;
0141:            private Locale locale;
0142:
0143:            public static void main(String[] args) {
0144:                junit.textui.TestRunner.run(suite());
0145:            }
0146:
0147:            public static TestSuite suite() {
0148:                return new TestSuite(TestDateTimeZone.class);
0149:            }
0150:
0151:            public TestDateTimeZone(String name) {
0152:                super (name);
0153:            }
0154:
0155:            protected void setUp() throws Exception {
0156:                locale = Locale.getDefault();
0157:                zone = DateTimeZone.getDefault();
0158:                Locale.setDefault(Locale.UK);
0159:            }
0160:
0161:            protected void tearDown() throws Exception {
0162:                Locale.setDefault(locale);
0163:                DateTimeZone.setDefault(zone);
0164:            }
0165:
0166:            //-----------------------------------------------------------------------
0167:            public void testDefault() {
0168:                assertNotNull(DateTimeZone.getDefault());
0169:
0170:                DateTimeZone.setDefault(PARIS);
0171:                assertSame(PARIS, DateTimeZone.getDefault());
0172:
0173:                try {
0174:                    DateTimeZone.setDefault(null);
0175:                    fail();
0176:                } catch (IllegalArgumentException ex) {
0177:                }
0178:            }
0179:
0180:            public void testDefaultSecurity() {
0181:                if (OLD_JDK) {
0182:                    return;
0183:                }
0184:                try {
0185:                    Policy.setPolicy(RESTRICT);
0186:                    System.setSecurityManager(new SecurityManager());
0187:                    DateTimeZone.setDefault(PARIS);
0188:                    fail();
0189:                } catch (SecurityException ex) {
0190:                    // ok
0191:                } finally {
0192:                    System.setSecurityManager(null);
0193:                    Policy.setPolicy(ALLOW);
0194:                }
0195:            }
0196:
0197:            //-----------------------------------------------------------------------
0198:            public void testForID_String() {
0199:                assertEquals(DateTimeZone.getDefault(), DateTimeZone
0200:                        .forID((String) null));
0201:
0202:                DateTimeZone zone = DateTimeZone.forID("Europe/London");
0203:                assertEquals("Europe/London", zone.getID());
0204:
0205:                zone = DateTimeZone.forID("UTC");
0206:                assertSame(DateTimeZone.UTC, zone);
0207:
0208:                zone = DateTimeZone.forID("+00:00");
0209:                assertSame(DateTimeZone.UTC, zone);
0210:
0211:                zone = DateTimeZone.forID("+00");
0212:                assertSame(DateTimeZone.UTC, zone);
0213:
0214:                zone = DateTimeZone.forID("+01:23");
0215:                assertEquals("+01:23", zone.getID());
0216:                assertEquals(DateTimeConstants.MILLIS_PER_HOUR
0217:                        + (23L * DateTimeConstants.MILLIS_PER_MINUTE), zone
0218:                        .getOffset(TEST_TIME_SUMMER));
0219:
0220:                zone = DateTimeZone.forID("-02:00");
0221:                assertEquals("-02:00", zone.getID());
0222:                assertEquals((-2L * DateTimeConstants.MILLIS_PER_HOUR), zone
0223:                        .getOffset(TEST_TIME_SUMMER));
0224:
0225:                zone = DateTimeZone.forID("-07:05:34.0");
0226:                assertEquals("-07:05:34", zone.getID());
0227:                assertEquals((-7L * DateTimeConstants.MILLIS_PER_HOUR)
0228:                        + (-5L * DateTimeConstants.MILLIS_PER_MINUTE)
0229:                        + (-34L * DateTimeConstants.MILLIS_PER_SECOND), zone
0230:                        .getOffset(TEST_TIME_SUMMER));
0231:
0232:                try {
0233:                    DateTimeZone.forID("SST");
0234:                    fail();
0235:                } catch (IllegalArgumentException ex) {
0236:                }
0237:                try {
0238:                    DateTimeZone.forID("Europe/UK");
0239:                    fail();
0240:                } catch (IllegalArgumentException ex) {
0241:                }
0242:                try {
0243:                    DateTimeZone.forID("+");
0244:                    fail();
0245:                } catch (IllegalArgumentException ex) {
0246:                }
0247:                try {
0248:                    DateTimeZone.forID("+0");
0249:                    fail();
0250:                } catch (IllegalArgumentException ex) {
0251:                }
0252:            }
0253:
0254:            //-----------------------------------------------------------------------
0255:            public void testForOffsetHours_int() {
0256:                assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHours(0));
0257:                assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone
0258:                        .forOffsetHours(3));
0259:                assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone
0260:                        .forOffsetHours(-2));
0261:                try {
0262:                    DateTimeZone.forOffsetHours(999999);
0263:                    fail();
0264:                } catch (IllegalArgumentException ex) {
0265:                }
0266:            }
0267:
0268:            //-----------------------------------------------------------------------
0269:            public void testForOffsetHoursMinutes_int_int() {
0270:                assertEquals(DateTimeZone.UTC, DateTimeZone
0271:                        .forOffsetHoursMinutes(0, 0));
0272:                assertEquals(DateTimeZone.forID("+03:15"), DateTimeZone
0273:                        .forOffsetHoursMinutes(3, 15));
0274:                assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone
0275:                        .forOffsetHoursMinutes(-2, 0));
0276:                assertEquals(DateTimeZone.forID("-02:30"), DateTimeZone
0277:                        .forOffsetHoursMinutes(-2, 30));
0278:                try {
0279:                    DateTimeZone.forOffsetHoursMinutes(2, 60);
0280:                    fail();
0281:                } catch (IllegalArgumentException ex) {
0282:                }
0283:                try {
0284:                    DateTimeZone.forOffsetHoursMinutes(-2, 60);
0285:                    fail();
0286:                } catch (IllegalArgumentException ex) {
0287:                }
0288:                try {
0289:                    DateTimeZone.forOffsetHoursMinutes(2, -1);
0290:                    fail();
0291:                } catch (IllegalArgumentException ex) {
0292:                }
0293:                try {
0294:                    DateTimeZone.forOffsetHoursMinutes(-2, -1);
0295:                    fail();
0296:                } catch (IllegalArgumentException ex) {
0297:                }
0298:                try {
0299:                    DateTimeZone.forOffsetHoursMinutes(999999, 0);
0300:                    fail();
0301:                } catch (IllegalArgumentException ex) {
0302:                }
0303:            }
0304:
0305:            //-----------------------------------------------------------------------
0306:            public void testForOffsetMillis_int() {
0307:                assertSame(DateTimeZone.UTC, DateTimeZone.forOffsetMillis(0));
0308:                assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone
0309:                        .forOffsetMillis(3 * 60 * 60 * 1000));
0310:                assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone
0311:                        .forOffsetMillis(-2 * 60 * 60 * 1000));
0312:                assertEquals(DateTimeZone.forID("+04:45:17.045"), DateTimeZone
0313:                        .forOffsetMillis(4 * 60 * 60 * 1000 + 45 * 60 * 1000
0314:                                + 17 * 1000 + 45));
0315:            }
0316:
0317:            //-----------------------------------------------------------------------
0318:            public void testForTimeZone_TimeZone() {
0319:                assertEquals(DateTimeZone.getDefault(), DateTimeZone
0320:                        .forTimeZone((TimeZone) null));
0321:
0322:                DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone
0323:                        .getTimeZone("Europe/London"));
0324:                assertEquals("Europe/London", zone.getID());
0325:                assertSame(DateTimeZone.UTC, DateTimeZone.forTimeZone(TimeZone
0326:                        .getTimeZone("UTC")));
0327:
0328:                zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("+00:00"));
0329:                assertSame(DateTimeZone.UTC, zone);
0330:
0331:                zone = DateTimeZone.forTimeZone(TimeZone
0332:                        .getTimeZone("GMT+00:00"));
0333:                assertSame(DateTimeZone.UTC, zone);
0334:
0335:                zone = DateTimeZone.forTimeZone(TimeZone
0336:                        .getTimeZone("GMT+00:00"));
0337:                assertSame(DateTimeZone.UTC, zone);
0338:
0339:                zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+00"));
0340:                assertSame(DateTimeZone.UTC, zone);
0341:
0342:                zone = DateTimeZone.forTimeZone(TimeZone
0343:                        .getTimeZone("GMT+01:23"));
0344:                assertEquals("+01:23", zone.getID());
0345:                assertEquals(DateTimeConstants.MILLIS_PER_HOUR
0346:                        + (23L * DateTimeConstants.MILLIS_PER_MINUTE), zone
0347:                        .getOffset(TEST_TIME_SUMMER));
0348:
0349:                zone = DateTimeZone.forTimeZone(TimeZone
0350:                        .getTimeZone("GMT-02:00"));
0351:                assertEquals("-02:00", zone.getID());
0352:                assertEquals((-2L * DateTimeConstants.MILLIS_PER_HOUR), zone
0353:                        .getOffset(TEST_TIME_SUMMER));
0354:
0355:                zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("EST"));
0356:                assertEquals("America/New_York", zone.getID());
0357:            }
0358:
0359:            public void testTimeZoneConversion() {
0360:                TimeZone jdkTimeZone = TimeZone.getTimeZone("GMT-10");
0361:                assertEquals("GMT-10:00", jdkTimeZone.getID());
0362:
0363:                DateTimeZone jodaTimeZone = DateTimeZone
0364:                        .forTimeZone(jdkTimeZone);
0365:                assertEquals("-10:00", jodaTimeZone.getID());
0366:                assertEquals(jdkTimeZone.getRawOffset(), jodaTimeZone
0367:                        .getOffset(0L));
0368:
0369:                TimeZone convertedTimeZone = jodaTimeZone.toTimeZone();
0370:                assertEquals("GMT-10:00", jdkTimeZone.getID());
0371:
0372:                assertEquals(jdkTimeZone.getID(), convertedTimeZone.getID());
0373:                assertEquals(jdkTimeZone.getRawOffset(), convertedTimeZone
0374:                        .getRawOffset());
0375:            }
0376:
0377:            //-----------------------------------------------------------------------
0378:            public void testGetAvailableIDs() {
0379:                assertTrue(DateTimeZone.getAvailableIDs().contains("UTC"));
0380:            }
0381:
0382:            //-----------------------------------------------------------------------
0383:            public void testProvider() {
0384:                try {
0385:                    assertNotNull(DateTimeZone.getProvider());
0386:
0387:                    Provider provider = DateTimeZone.getProvider();
0388:                    DateTimeZone.setProvider(null);
0389:                    assertEquals(provider.getClass(), DateTimeZone
0390:                            .getProvider().getClass());
0391:
0392:                    try {
0393:                        DateTimeZone.setProvider(new MockNullIDSProvider());
0394:                        fail();
0395:                    } catch (IllegalArgumentException ex) {
0396:                    }
0397:                    try {
0398:                        DateTimeZone.setProvider(new MockEmptyIDSProvider());
0399:                        fail();
0400:                    } catch (IllegalArgumentException ex) {
0401:                    }
0402:                    try {
0403:                        DateTimeZone.setProvider(new MockNoUTCProvider());
0404:                        fail();
0405:                    } catch (IllegalArgumentException ex) {
0406:                    }
0407:                    try {
0408:                        DateTimeZone.setProvider(new MockBadUTCProvider());
0409:                        fail();
0410:                    } catch (IllegalArgumentException ex) {
0411:                    }
0412:
0413:                    Provider prov = new MockOKProvider();
0414:                    DateTimeZone.setProvider(prov);
0415:                    assertSame(prov, DateTimeZone.getProvider());
0416:                    assertEquals(2, DateTimeZone.getAvailableIDs().size());
0417:                    assertTrue(DateTimeZone.getAvailableIDs().contains("UTC"));
0418:                    assertTrue(DateTimeZone.getAvailableIDs().contains(
0419:                            "Europe/London"));
0420:                } finally {
0421:                    DateTimeZone.setProvider(null);
0422:                    assertEquals(ZoneInfoProvider.class, DateTimeZone
0423:                            .getProvider().getClass());
0424:                }
0425:
0426:                try {
0427:                    System.setProperty("org.joda.time.DateTimeZone.Provider",
0428:                            "org.joda.time.tz.UTCProvider");
0429:                    DateTimeZone.setProvider(null);
0430:                    assertEquals(UTCProvider.class, DateTimeZone.getProvider()
0431:                            .getClass());
0432:                } finally {
0433:                    System.getProperties().remove(
0434:                            "org.joda.time.DateTimeZone.Provider");
0435:                    DateTimeZone.setProvider(null);
0436:                    assertEquals(ZoneInfoProvider.class, DateTimeZone
0437:                            .getProvider().getClass());
0438:                }
0439:
0440:                PrintStream syserr = System.err;
0441:                try {
0442:                    System.setProperty("org.joda.time.DateTimeZone.Provider",
0443:                            "xxx");
0444:                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
0445:                    System.setErr(new PrintStream(baos));
0446:
0447:                    DateTimeZone.setProvider(null);
0448:
0449:                    assertEquals(ZoneInfoProvider.class, DateTimeZone
0450:                            .getProvider().getClass());
0451:                    String str = new String(baos.toByteArray());
0452:                    assertTrue(str.indexOf("java.lang.ClassNotFoundException") >= 0);
0453:                } finally {
0454:                    System.setErr(syserr);
0455:                    System.getProperties().remove(
0456:                            "org.joda.time.DateTimeZone.Provider");
0457:                    DateTimeZone.setProvider(null);
0458:                    assertEquals(ZoneInfoProvider.class, DateTimeZone
0459:                            .getProvider().getClass());
0460:                }
0461:            }
0462:
0463:            public void testProviderSecurity() {
0464:                if (OLD_JDK) {
0465:                    return;
0466:                }
0467:                try {
0468:                    Policy.setPolicy(RESTRICT);
0469:                    System.setSecurityManager(new SecurityManager());
0470:                    DateTimeZone.setProvider(new MockOKProvider());
0471:                    fail();
0472:                } catch (SecurityException ex) {
0473:                    // ok
0474:                } finally {
0475:                    System.setSecurityManager(null);
0476:                    Policy.setPolicy(ALLOW);
0477:                }
0478:            }
0479:
0480:            static class MockNullIDSProvider implements  Provider {
0481:                public Set getAvailableIDs() {
0482:                    return null;
0483:                }
0484:
0485:                public DateTimeZone getZone(String id) {
0486:                    return null;
0487:                }
0488:            }
0489:
0490:            static class MockEmptyIDSProvider implements  Provider {
0491:                public Set getAvailableIDs() {
0492:                    return new HashSet();
0493:                }
0494:
0495:                public DateTimeZone getZone(String id) {
0496:                    return null;
0497:                }
0498:            }
0499:
0500:            static class MockNoUTCProvider implements  Provider {
0501:                public Set getAvailableIDs() {
0502:                    Set set = new HashSet();
0503:                    set.add("Europe/London");
0504:                    return set;
0505:                }
0506:
0507:                public DateTimeZone getZone(String id) {
0508:                    return null;
0509:                }
0510:            }
0511:
0512:            static class MockBadUTCProvider implements  Provider {
0513:                public Set getAvailableIDs() {
0514:                    Set set = new HashSet();
0515:                    set.add("UTC");
0516:                    set.add("Europe/London");
0517:                    return set;
0518:                }
0519:
0520:                public DateTimeZone getZone(String id) {
0521:                    return null;
0522:                }
0523:            }
0524:
0525:            static class MockOKProvider implements  Provider {
0526:                public Set getAvailableIDs() {
0527:                    Set set = new HashSet();
0528:                    set.add("UTC");
0529:                    set.add("Europe/London");
0530:                    return set;
0531:                }
0532:
0533:                public DateTimeZone getZone(String id) {
0534:                    return DateTimeZone.UTC;
0535:                }
0536:            }
0537:
0538:            //-----------------------------------------------------------------------
0539:            public void testNameProvider() {
0540:                try {
0541:                    assertNotNull(DateTimeZone.getNameProvider());
0542:
0543:                    NameProvider provider = DateTimeZone.getNameProvider();
0544:                    DateTimeZone.setNameProvider(null);
0545:                    assertEquals(provider.getClass(), DateTimeZone
0546:                            .getNameProvider().getClass());
0547:
0548:                    provider = new MockOKButNullNameProvider();
0549:                    DateTimeZone.setNameProvider(provider);
0550:                    assertSame(provider, DateTimeZone.getNameProvider());
0551:
0552:                    assertEquals("+00:00", DateTimeZone.UTC
0553:                            .getShortName(TEST_TIME_SUMMER));
0554:                    assertEquals("+00:00", DateTimeZone.UTC
0555:                            .getName(TEST_TIME_SUMMER));
0556:                } finally {
0557:                    DateTimeZone.setNameProvider(null);
0558:                }
0559:
0560:                try {
0561:                    System.setProperty(
0562:                            "org.joda.time.DateTimeZone.NameProvider",
0563:                            "org.joda.time.tz.DefaultNameProvider");
0564:                    DateTimeZone.setNameProvider(null);
0565:                    assertEquals(DefaultNameProvider.class, DateTimeZone
0566:                            .getNameProvider().getClass());
0567:                } finally {
0568:                    System.getProperties().remove(
0569:                            "org.joda.time.DateTimeZone.NameProvider");
0570:                    DateTimeZone.setNameProvider(null);
0571:                    assertEquals(DefaultNameProvider.class, DateTimeZone
0572:                            .getNameProvider().getClass());
0573:                }
0574:
0575:                PrintStream syserr = System.err;
0576:                try {
0577:                    System.setProperty(
0578:                            "org.joda.time.DateTimeZone.NameProvider", "xxx");
0579:                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
0580:                    System.setErr(new PrintStream(baos));
0581:
0582:                    DateTimeZone.setNameProvider(null);
0583:
0584:                    assertEquals(DefaultNameProvider.class, DateTimeZone
0585:                            .getNameProvider().getClass());
0586:                    String str = new String(baos.toByteArray());
0587:                    assertTrue(str.indexOf("java.lang.ClassNotFoundException") >= 0);
0588:                } finally {
0589:                    System.setErr(syserr);
0590:                    System.getProperties().remove(
0591:                            "org.joda.time.DateTimeZone.NameProvider");
0592:                    DateTimeZone.setNameProvider(null);
0593:                    assertEquals(DefaultNameProvider.class, DateTimeZone
0594:                            .getNameProvider().getClass());
0595:                }
0596:            }
0597:
0598:            public void testNameProviderSecurity() {
0599:                if (OLD_JDK) {
0600:                    return;
0601:                }
0602:                try {
0603:                    Policy.setPolicy(RESTRICT);
0604:                    System.setSecurityManager(new SecurityManager());
0605:                    DateTimeZone
0606:                            .setNameProvider(new MockOKButNullNameProvider());
0607:                    fail();
0608:                } catch (SecurityException ex) {
0609:                    // ok
0610:                } finally {
0611:                    System.setSecurityManager(null);
0612:                    Policy.setPolicy(ALLOW);
0613:                }
0614:            }
0615:
0616:            static class MockOKButNullNameProvider implements  NameProvider {
0617:                public String getShortName(Locale locale, String id,
0618:                        String nameKey) {
0619:                    return null;
0620:                }
0621:
0622:                public String getName(Locale locale, String id, String nameKey) {
0623:                    return null;
0624:                }
0625:            }
0626:
0627:            //-----------------------------------------------------------------------
0628:            public void testConstructor() {
0629:                assertEquals(1,
0630:                        DateTimeZone.class.getDeclaredConstructors().length);
0631:                assertTrue(Modifier.isProtected(DateTimeZone.class
0632:                        .getDeclaredConstructors()[0].getModifiers()));
0633:                try {
0634:                    new DateTimeZone(null) {
0635:                        public String getNameKey(long instant) {
0636:                            return null;
0637:                        }
0638:
0639:                        public int getOffset(long instant) {
0640:                            return 0;
0641:                        }
0642:
0643:                        public int getStandardOffset(long instant) {
0644:                            return 0;
0645:                        }
0646:
0647:                        public boolean isFixed() {
0648:                            return false;
0649:                        }
0650:
0651:                        public long nextTransition(long instant) {
0652:                            return 0;
0653:                        }
0654:
0655:                        public long previousTransition(long instant) {
0656:                            return 0;
0657:                        }
0658:
0659:                        public boolean equals(Object object) {
0660:                            return false;
0661:                        }
0662:                    };
0663:                } catch (IllegalArgumentException ex) {
0664:                }
0665:            }
0666:
0667:            //-----------------------------------------------------------------------
0668:            public void testGetID() {
0669:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0670:                assertEquals("Europe/Paris", zone.getID());
0671:            }
0672:
0673:            public void testGetNameKey() {
0674:                DateTimeZone zone = DateTimeZone.forID("Europe/London");
0675:                assertEquals("BST", zone.getNameKey(TEST_TIME_SUMMER));
0676:                assertEquals("GMT", zone.getNameKey(TEST_TIME_WINTER));
0677:            }
0678:
0679:            public void testGetShortName() {
0680:                DateTimeZone zone = DateTimeZone.forID("Europe/London");
0681:                assertEquals("BST", zone.getShortName(TEST_TIME_SUMMER));
0682:                assertEquals("GMT", zone.getShortName(TEST_TIME_WINTER));
0683:                assertEquals("BST", zone.getShortName(TEST_TIME_SUMMER,
0684:                        Locale.ENGLISH));
0685:            }
0686:
0687:            public void testGetShortNameProviderName() {
0688:                assertEquals(null, DateTimeZone.getNameProvider().getShortName(
0689:                        null, "Europe/London", "BST"));
0690:                assertEquals(null, DateTimeZone.getNameProvider().getShortName(
0691:                        Locale.ENGLISH, null, "BST"));
0692:                assertEquals(null, DateTimeZone.getNameProvider().getShortName(
0693:                        Locale.ENGLISH, "Europe/London", null));
0694:                assertEquals(null, DateTimeZone.getNameProvider().getShortName(
0695:                        null, null, null));
0696:            }
0697:
0698:            public void testGetShortNameNullKey() {
0699:                DateTimeZone zone = new MockDateTimeZone("Europe/London");
0700:                assertEquals("Europe/London", zone.getShortName(
0701:                        TEST_TIME_SUMMER, Locale.ENGLISH));
0702:            }
0703:
0704:            public void testGetName() {
0705:                DateTimeZone zone = DateTimeZone.forID("Europe/London");
0706:                assertEquals("British Summer Time", zone
0707:                        .getName(TEST_TIME_SUMMER));
0708:                assertEquals("Greenwich Mean Time", zone
0709:                        .getName(TEST_TIME_WINTER));
0710:                assertEquals("British Summer Time", zone.getName(
0711:                        TEST_TIME_SUMMER, Locale.ENGLISH));
0712:
0713:            }
0714:
0715:            public void testGetNameProviderName() {
0716:                assertEquals(null, DateTimeZone.getNameProvider().getName(null,
0717:                        "Europe/London", "BST"));
0718:                assertEquals(null, DateTimeZone.getNameProvider().getName(
0719:                        Locale.ENGLISH, null, "BST"));
0720:                assertEquals(null, DateTimeZone.getNameProvider().getName(
0721:                        Locale.ENGLISH, "Europe/London", null));
0722:                assertEquals(null, DateTimeZone.getNameProvider().getName(null,
0723:                        null, null));
0724:            }
0725:
0726:            public void testGetNameNullKey() {
0727:                DateTimeZone zone = new MockDateTimeZone("Europe/London");
0728:                assertEquals("Europe/London", zone.getName(TEST_TIME_SUMMER,
0729:                        Locale.ENGLISH));
0730:            }
0731:
0732:            static class MockDateTimeZone extends DateTimeZone {
0733:                public MockDateTimeZone(String id) {
0734:                    super (id);
0735:                }
0736:
0737:                public String getNameKey(long instant) {
0738:                    return null; // null
0739:                }
0740:
0741:                public int getOffset(long instant) {
0742:                    return 0;
0743:                }
0744:
0745:                public int getStandardOffset(long instant) {
0746:                    return 0;
0747:                }
0748:
0749:                public boolean isFixed() {
0750:                    return false;
0751:                }
0752:
0753:                public long nextTransition(long instant) {
0754:                    return 0;
0755:                }
0756:
0757:                public long previousTransition(long instant) {
0758:                    return 0;
0759:                }
0760:
0761:                public boolean equals(Object object) {
0762:                    return false;
0763:                }
0764:            }
0765:
0766:            //-----------------------------------------------------------------------
0767:            public void testGetOffset_long() {
0768:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0769:                assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone
0770:                        .getOffset(TEST_TIME_SUMMER));
0771:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0772:                        .getOffset(TEST_TIME_WINTER));
0773:
0774:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0775:                        .getStandardOffset(TEST_TIME_SUMMER));
0776:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0777:                        .getStandardOffset(TEST_TIME_WINTER));
0778:
0779:                assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone
0780:                        .getOffsetFromLocal(TEST_TIME_SUMMER));
0781:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0782:                        .getOffsetFromLocal(TEST_TIME_WINTER));
0783:
0784:                assertEquals(false, zone.isStandardOffset(TEST_TIME_SUMMER));
0785:                assertEquals(true, zone.isStandardOffset(TEST_TIME_WINTER));
0786:            }
0787:
0788:            public void testGetOffset_RI() {
0789:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0790:                assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone
0791:                        .getOffset(new Instant(TEST_TIME_SUMMER)));
0792:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0793:                        .getOffset(new Instant(TEST_TIME_WINTER)));
0794:
0795:                assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()),
0796:                        zone.getOffset(null));
0797:            }
0798:
0799:            public void testGetOffsetFixed() {
0800:                DateTimeZone zone = DateTimeZone.forID("+01:00");
0801:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0802:                        .getOffset(TEST_TIME_SUMMER));
0803:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0804:                        .getOffset(TEST_TIME_WINTER));
0805:
0806:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0807:                        .getStandardOffset(TEST_TIME_SUMMER));
0808:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0809:                        .getStandardOffset(TEST_TIME_WINTER));
0810:
0811:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0812:                        .getOffsetFromLocal(TEST_TIME_SUMMER));
0813:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0814:                        .getOffsetFromLocal(TEST_TIME_WINTER));
0815:
0816:                assertEquals(true, zone.isStandardOffset(TEST_TIME_SUMMER));
0817:                assertEquals(true, zone.isStandardOffset(TEST_TIME_WINTER));
0818:            }
0819:
0820:            public void testGetOffsetFixed_RI() {
0821:                DateTimeZone zone = DateTimeZone.forID("+01:00");
0822:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0823:                        .getOffset(new Instant(TEST_TIME_SUMMER)));
0824:                assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone
0825:                        .getOffset(new Instant(TEST_TIME_WINTER)));
0826:
0827:                assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()),
0828:                        zone.getOffset(null));
0829:            }
0830:
0831:            //-----------------------------------------------------------------------
0832:            public void testGetMillisKeepLocal() {
0833:                long millisLondon = TEST_TIME_SUMMER;
0834:                long millisParis = TEST_TIME_SUMMER - 1L
0835:                        * DateTimeConstants.MILLIS_PER_HOUR;
0836:
0837:                assertEquals(millisLondon, LONDON.getMillisKeepLocal(LONDON,
0838:                        millisLondon));
0839:                assertEquals(millisParis, LONDON.getMillisKeepLocal(LONDON,
0840:                        millisParis));
0841:                assertEquals(millisLondon, PARIS.getMillisKeepLocal(PARIS,
0842:                        millisLondon));
0843:                assertEquals(millisParis, PARIS.getMillisKeepLocal(PARIS,
0844:                        millisParis));
0845:
0846:                assertEquals(millisParis, LONDON.getMillisKeepLocal(PARIS,
0847:                        millisLondon));
0848:                assertEquals(millisLondon, PARIS.getMillisKeepLocal(LONDON,
0849:                        millisParis));
0850:
0851:                DateTimeZone zone = DateTimeZone.getDefault();
0852:                try {
0853:                    DateTimeZone.setDefault(LONDON);
0854:                    assertEquals(millisLondon, PARIS.getMillisKeepLocal(null,
0855:                            millisParis));
0856:                } finally {
0857:                    DateTimeZone.setDefault(zone);
0858:                }
0859:            }
0860:
0861:            //-----------------------------------------------------------------------
0862:            public void testIsFixed() {
0863:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0864:                assertEquals(false, zone.isFixed());
0865:                assertEquals(true, DateTimeZone.UTC.isFixed());
0866:            }
0867:
0868:            //-----------------------------------------------------------------------
0869:            public void testTransitionFixed() {
0870:                DateTimeZone zone = DateTimeZone.forID("+01:00");
0871:                assertEquals(TEST_TIME_SUMMER, zone
0872:                        .nextTransition(TEST_TIME_SUMMER));
0873:                assertEquals(TEST_TIME_WINTER, zone
0874:                        .nextTransition(TEST_TIME_WINTER));
0875:                assertEquals(TEST_TIME_SUMMER, zone
0876:                        .previousTransition(TEST_TIME_SUMMER));
0877:                assertEquals(TEST_TIME_WINTER, zone
0878:                        .previousTransition(TEST_TIME_WINTER));
0879:            }
0880:
0881:            //-----------------------------------------------------------------------
0882:            public void testToTimeZone() {
0883:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0884:                TimeZone tz = zone.toTimeZone();
0885:                assertEquals("Europe/Paris", tz.getID());
0886:            }
0887:
0888:            //-----------------------------------------------------------------------
0889:            public void testEqualsHashCode() {
0890:                DateTimeZone zone1 = DateTimeZone.forID("Europe/Paris");
0891:                DateTimeZone zone2 = DateTimeZone.forID("Europe/Paris");
0892:                assertEquals(true, zone1.equals(zone1));
0893:                assertEquals(true, zone1.equals(zone2));
0894:                assertEquals(true, zone2.equals(zone1));
0895:                assertEquals(true, zone2.equals(zone2));
0896:                assertEquals(true, zone1.hashCode() == zone2.hashCode());
0897:
0898:                DateTimeZone zone3 = DateTimeZone.forID("Europe/London");
0899:                assertEquals(true, zone3.equals(zone3));
0900:                assertEquals(false, zone1.equals(zone3));
0901:                assertEquals(false, zone2.equals(zone3));
0902:                assertEquals(false, zone3.equals(zone1));
0903:                assertEquals(false, zone3.equals(zone2));
0904:                assertEquals(false, zone1.hashCode() == zone3.hashCode());
0905:                assertEquals(true, zone3.hashCode() == zone3.hashCode());
0906:
0907:                DateTimeZone zone4 = DateTimeZone.forID("+01:00");
0908:                assertEquals(true, zone4.equals(zone4));
0909:                assertEquals(false, zone1.equals(zone4));
0910:                assertEquals(false, zone2.equals(zone4));
0911:                assertEquals(false, zone3.equals(zone4));
0912:                assertEquals(false, zone4.equals(zone1));
0913:                assertEquals(false, zone4.equals(zone2));
0914:                assertEquals(false, zone4.equals(zone3));
0915:                assertEquals(false, zone1.hashCode() == zone4.hashCode());
0916:                assertEquals(true, zone4.hashCode() == zone4.hashCode());
0917:
0918:                DateTimeZone zone5 = DateTimeZone.forID("+02:00");
0919:                assertEquals(true, zone5.equals(zone5));
0920:                assertEquals(false, zone1.equals(zone5));
0921:                assertEquals(false, zone2.equals(zone5));
0922:                assertEquals(false, zone3.equals(zone5));
0923:                assertEquals(false, zone4.equals(zone5));
0924:                assertEquals(false, zone5.equals(zone1));
0925:                assertEquals(false, zone5.equals(zone2));
0926:                assertEquals(false, zone5.equals(zone3));
0927:                assertEquals(false, zone5.equals(zone4));
0928:                assertEquals(false, zone1.hashCode() == zone5.hashCode());
0929:                assertEquals(true, zone5.hashCode() == zone5.hashCode());
0930:            }
0931:
0932:            //-----------------------------------------------------------------------
0933:            public void testToString() {
0934:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0935:                assertEquals("Europe/Paris", zone.toString());
0936:                assertEquals("UTC", DateTimeZone.UTC.toString());
0937:            }
0938:
0939:            //-----------------------------------------------------------------------
0940:            public void testSerialization1() throws Exception {
0941:                DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
0942:
0943:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
0944:                ObjectOutputStream oos = new ObjectOutputStream(baos);
0945:                oos.writeObject(zone);
0946:                byte[] bytes = baos.toByteArray();
0947:                oos.close();
0948:
0949:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0950:                ObjectInputStream ois = new ObjectInputStream(bais);
0951:                DateTimeZone result = (DateTimeZone) ois.readObject();
0952:                ois.close();
0953:
0954:                assertSame(zone, result);
0955:            }
0956:
0957:            //-----------------------------------------------------------------------
0958:            public void testSerialization2() throws Exception {
0959:                DateTimeZone zone = DateTimeZone.forID("+01:00");
0960:
0961:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
0962:                ObjectOutputStream oos = new ObjectOutputStream(baos);
0963:                oos.writeObject(zone);
0964:                byte[] bytes = baos.toByteArray();
0965:                oos.close();
0966:
0967:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0968:                ObjectInputStream ois = new ObjectInputStream(bais);
0969:                DateTimeZone result = (DateTimeZone) ois.readObject();
0970:                ois.close();
0971:
0972:                assertSame(zone, result);
0973:            }
0974:
0975:            public void testCommentParse() throws Exception {
0976:                // A bug in ZoneInfoCompiler's handling of comments broke Europe/Athens
0977:                // after 1980. This test is included to make sure it doesn't break again.
0978:
0979:                DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
0980:                DateTime dt = new DateTime(2005, 5, 5, 20, 10, 15, 0, zone);
0981:                assertEquals(1115313015000L, dt.getMillis());
0982:            }
0983:
0984:            public void testPatchedNameKeysLondon() throws Exception {
0985:                // the tz database does not have unique name keys [1716305]
0986:                DateTimeZone zone = DateTimeZone.forID("Europe/London");
0987:
0988:                DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
0989:                String str1 = zone.getName(now.getMillis());
0990:                String str2 = zone.getName(now.plusMonths(6).getMillis());
0991:                assertEquals(false, str1.equals(str2));
0992:            }
0993:
0994:            public void testPatchedNameKeysSydney() throws Exception {
0995:                // the tz database does not have unique name keys [1716305]
0996:                DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
0997:
0998:                DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
0999:                String str1 = zone.getName(now.getMillis());
1000:                String str2 = zone.getName(now.plusMonths(6).getMillis());
1001:                assertEquals(false, str1.equals(str2));
1002:            }
1003:
1004:            public void testPatchedNameKeysSydneyHistoric() throws Exception {
1005:                // the tz database does not have unique name keys [1716305]
1006:                DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
1007:
1008:                DateTime now = new DateTime(1996, 1, 1, 0, 0, 0, 0);
1009:                String str1 = zone.getName(now.getMillis());
1010:                String str2 = zone.getName(now.plusMonths(6).getMillis());
1011:                assertEquals(false, str1.equals(str2));
1012:            }
1013:
1014:            public void testPatchedNameKeysGazaHistoric() throws Exception {
1015:                // the tz database does not have unique name keys [1716305]
1016:                DateTimeZone zone = DateTimeZone.forID("Africa/Johannesburg");
1017:
1018:                DateTime now = new DateTime(1943, 1, 1, 0, 0, 0, 0);
1019:                String str1 = zone.getName(now.getMillis());
1020:                String str2 = zone.getName(now.plusMonths(6).getMillis());
1021:                assertEquals(false, str1.equals(str2));
1022:            }
1023:
1024:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.