001 /*
002 * Copyright 2001-2007 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time.chrono;
017
018 import junit.framework.TestCase;
019 import junit.framework.TestSuite;
020
021 import org.joda.time.Chronology;
022 import org.joda.time.DateTime;
023 import org.joda.time.DateTimeZone;
024 import org.joda.time.MockZone;
025
026 /**
027 *
028 * @author Brian S O'Neill
029 * @author Blair Martin
030 */
031 public class TestLenientChronology extends TestCase {
032 public static void main(String[] args) {
033 junit.textui.TestRunner.run(suite());
034 }
035
036 public static TestSuite suite() {
037 return new TestSuite(TestLenientChronology.class);
038 }
039
040 public TestLenientChronology(String name) {
041 super(name);
042 }
043
044 protected void setUp() throws Exception {
045 }
046
047 protected void tearDown() throws Exception {
048 }
049
050 //-----------------------------------------------------------------------
051 public void test_setYear() {
052 Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
053 DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
054 assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
055 dt = dt.withYear(2008);
056 assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
057 }
058
059 //-----------------------------------------------------------------------
060 public void test_setMonthOfYear() {
061 Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
062 DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
063 assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
064 dt = dt.withMonthOfYear(13);
065 assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
066 dt = dt.withMonthOfYear(0);
067 assertEquals("2007-12-01T00:00:00.000Z", dt.toString());
068 }
069
070 //-----------------------------------------------------------------------
071 public void test_setDayOfMonth() {
072 Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
073 DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
074 assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
075 dt = dt.withDayOfMonth(32);
076 assertEquals("2007-02-01T00:00:00.000Z", dt.toString());
077 dt = dt.withDayOfMonth(0);
078 assertEquals("2007-01-31T00:00:00.000Z", dt.toString());
079 }
080
081 //-----------------------------------------------------------------------
082 public void test_setHourOfDay() {
083 Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
084 DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
085 assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
086 dt = dt.withHourOfDay(24);
087 assertEquals("2007-01-02T00:00:00.000Z", dt.toString());
088 dt = dt.withHourOfDay(-1);
089 assertEquals("2007-01-01T23:00:00.000Z", dt.toString());
090 }
091
092 //-----------------------------------------------------------------------
093 //------------------------ Bug ------------------------------------------
094 //-----------------------------------------------------------------------
095 public void testNearDstTransition() {
096 // This is just a regression test. Test case provided by Blair Martin.
097
098 int hour = 23;
099 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 }