View Javadoc

1   /*
2    *  Copyright 2001-2007 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.chrono;
17  
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  
21  import org.joda.time.Chronology;
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.joda.time.MockZone;
25  
26  /**
27   *
28   * @author Brian S O'Neill
29   * @author Blair Martin
30   */
31  public class TestLenientChronology extends TestCase {
32      public static void main(String[] args) {
33          junit.textui.TestRunner.run(suite());
34      }
35  
36      public static TestSuite suite() {
37          return new TestSuite(TestLenientChronology.class);
38      }
39  
40      public TestLenientChronology(String name) {
41          super(name);
42      }
43  
44      protected void setUp() throws Exception {
45      }
46  
47      protected void tearDown() throws Exception {
48      }
49  
50      //-----------------------------------------------------------------------
51      public void test_setYear() {
52          Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
53          DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
54          assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
55          dt = dt.withYear(2008);
56          assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
57      }
58  
59      //-----------------------------------------------------------------------
60      public void test_setMonthOfYear() {
61          Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
62          DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
63          assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
64          dt = dt.withMonthOfYear(13);
65          assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
66          dt = dt.withMonthOfYear(0);
67          assertEquals("2007-12-01T00:00:00.000Z", dt.toString());
68      }
69  
70      //-----------------------------------------------------------------------
71      public void test_setDayOfMonth() {
72          Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
73          DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
74          assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
75          dt = dt.withDayOfMonth(32);
76          assertEquals("2007-02-01T00:00:00.000Z", dt.toString());
77          dt = dt.withDayOfMonth(0);
78          assertEquals("2007-01-31T00:00:00.000Z", dt.toString());
79      }
80  
81      //-----------------------------------------------------------------------
82      public void test_setHourOfDay() {
83          Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
84          DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
85          assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
86          dt = dt.withHourOfDay(24);
87          assertEquals("2007-01-02T00:00:00.000Z", dt.toString());
88          dt = dt.withHourOfDay(-1);
89          assertEquals("2007-01-01T23:00:00.000Z", dt.toString());
90      }
91  
92      //-----------------------------------------------------------------------
93      //------------------------ Bug ------------------------------------------
94      //-----------------------------------------------------------------------
95      public void testNearDstTransition() {
96          // This is just a regression test. Test case provided by Blair Martin.
97  
98          int hour = 23;
99          DateTime dt;
100 
101         dt = new DateTime(2006, 10, 29, hour, 0, 0, 0,
102                           ISOChronology.getInstance(DateTimeZone.forID("America/Los_Angeles")));
103         assertEquals(hour, dt.getHourOfDay()); // OK - no LenientChronology
104 
105         dt = new DateTime(2006, 10, 29, hour, 0, 0, 0,
106                           LenientChronology.getInstance
107                           (ISOChronology.getInstance(DateTimeZone.forOffsetHours(-8))));
108         assertEquals(hour, dt.getHourOfDay()); // OK - no TZ ID
109 
110         dt = new DateTime(2006, 10, 29, hour, 0, 0, 0,
111                           LenientChronology.getInstance
112                           (ISOChronology.getInstance(DateTimeZone.forID("America/Los_Angeles"))));
113 
114         assertEquals(hour, dt.getHourOfDay()); // Used to fail - hour was 22
115     }
116 
117     //-----------------------------------------------------------------------
118     //------------------------ Bug [1755161] --------------------------------
119     //-----------------------------------------------------------------------
120     /** Mock zone simulating America/Grand_Turk cutover at midnight 2007-04-01 */
121     private static long CUTOVER_TURK = 1175403600000L;
122     private static int OFFSET_TURK = -18000000;  // -05:00
123     private static final DateTimeZone MOCK_TURK = new MockZone(CUTOVER_TURK, OFFSET_TURK, 3600);
124 
125     //-----------------------------------------------------------------------
126     public void test_MockTurkIsCorrect() {
127         DateTime pre = new DateTime(CUTOVER_TURK - 1L, MOCK_TURK);
128         assertEquals("2007-03-31T23:59:59.999-05:00", pre.toString());
129         DateTime at = new DateTime(CUTOVER_TURK, MOCK_TURK);
130         assertEquals("2007-04-01T01:00:00.000-04:00", at.toString());
131         DateTime post = new DateTime(CUTOVER_TURK + 1L, MOCK_TURK);
132         assertEquals("2007-04-01T01:00:00.001-04:00", post.toString());
133     }
134 
135     public void test_lenientChrononolgy_Chicago() {
136         DateTimeZone zone = DateTimeZone.forID("America/Chicago");
137         Chronology lenient = LenientChronology.getInstance(ISOChronology.getInstance(zone));
138         DateTime dt = new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
139         assertEquals("2007-03-11T03:30:00.000-05:00", dt.toString());
140     }
141 
142     public void test_lenientChrononolgy_Turk() {
143         Chronology lenient = LenientChronology.getInstance(ISOChronology.getInstance(MOCK_TURK));
144         DateTime dt = new DateTime(2007, 4, 1, 0, 30, 0, 0, lenient);
145         assertEquals("2007-04-01T01:30:00.000-04:00", dt.toString());
146     }
147 
148     public void test_strictChrononolgy_Chicago() {
149         DateTimeZone zone = DateTimeZone.forID("America/Chicago");
150         Chronology lenient = StrictChronology.getInstance(ISOChronology.getInstance(zone));
151         try {
152             new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
153             fail();
154         } catch (IllegalArgumentException ex) {
155             // expected
156         }
157     }
158 
159     public void test_isoChrononolgy_Chicago() {
160         DateTimeZone zone = DateTimeZone.forID("America/Chicago");
161         Chronology lenient = ISOChronology.getInstance(zone);
162         try {
163             new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
164             fail();
165         } catch (IllegalArgumentException ex) {
166             // expected
167         }
168     }
169 
170 }