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
053: .getInstanceUTC());
054: DateTime dt = new DateTime(2007, 1, 1, 0, 0, 0, 0, zone);
055: assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
056: dt = dt.withYear(2008);
057: assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
058: }
059:
060: //-----------------------------------------------------------------------
061: public void test_setMonthOfYear() {
062: Chronology zone = LenientChronology.getInstance(ISOChronology
063: .getInstanceUTC());
064: DateTime dt = new DateTime(2007, 1, 1, 0, 0, 0, 0, zone);
065: assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
066: dt = dt.withMonthOfYear(13);
067: assertEquals("2008-01-01T00:00:00.000Z", dt.toString());
068: dt = dt.withMonthOfYear(0);
069: assertEquals("2007-12-01T00:00:00.000Z", dt.toString());
070: }
071:
072: //-----------------------------------------------------------------------
073: public void test_setDayOfMonth() {
074: Chronology zone = LenientChronology.getInstance(ISOChronology
075: .getInstanceUTC());
076: DateTime dt = new DateTime(2007, 1, 1, 0, 0, 0, 0, zone);
077: assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
078: dt = dt.withDayOfMonth(32);
079: assertEquals("2007-02-01T00:00:00.000Z", dt.toString());
080: dt = dt.withDayOfMonth(0);
081: assertEquals("2007-01-31T00:00:00.000Z", dt.toString());
082: }
083:
084: //-----------------------------------------------------------------------
085: public void test_setHourOfDay() {
086: Chronology zone = LenientChronology.getInstance(ISOChronology
087: .getInstanceUTC());
088: DateTime dt = new DateTime(2007, 1, 1, 0, 0, 0, 0, zone);
089: assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
090: dt = dt.withHourOfDay(24);
091: assertEquals("2007-01-02T00:00:00.000Z", dt.toString());
092: dt = dt.withHourOfDay(-1);
093: assertEquals("2007-01-01T23:00:00.000Z", dt.toString());
094: }
095:
096: //-----------------------------------------------------------------------
097: //------------------------ Bug ------------------------------------------
098: //-----------------------------------------------------------------------
099: public void testNearDstTransition() {
100: // This is just a regression test. Test case provided by Blair Martin.
101:
102: int hour = 23;
103: DateTime dt;
104:
105: dt = new DateTime(2006, 10, 29, hour, 0, 0, 0, ISOChronology
106: .getInstance(DateTimeZone.forID("America/Los_Angeles")));
107: assertEquals(hour, dt.getHourOfDay()); // OK - no LenientChronology
108:
109: dt = new DateTime(2006, 10, 29, hour, 0, 0, 0,
110: LenientChronology.getInstance(ISOChronology
111: .getInstance(DateTimeZone.forOffsetHours(-8))));
112: assertEquals(hour, dt.getHourOfDay()); // OK - no TZ ID
113:
114: dt = new DateTime(2006, 10, 29, hour, 0, 0, 0,
115: LenientChronology.getInstance(ISOChronology
116: .getInstance(DateTimeZone
117: .forID("America/Los_Angeles"))));
118:
119: assertEquals(hour, dt.getHourOfDay()); // Used to fail - hour was 22
120: }
121:
122: //-----------------------------------------------------------------------
123: //------------------------ Bug [1755161] --------------------------------
124: //-----------------------------------------------------------------------
125: /** Mock zone simulating America/Grand_Turk cutover at midnight 2007-04-01 */
126: private static long CUTOVER_TURK = 1175403600000L;
127: private static int OFFSET_TURK = -18000000; // -05:00
128: private static final DateTimeZone MOCK_TURK = new MockZone(
129: CUTOVER_TURK, OFFSET_TURK);
130:
131: //-----------------------------------------------------------------------
132: public void test_MockTurkIsCorrect() {
133: DateTime pre = new DateTime(CUTOVER_TURK - 1L, MOCK_TURK);
134: assertEquals("2007-03-31T23:59:59.999-05:00", pre.toString());
135: DateTime at = new DateTime(CUTOVER_TURK, MOCK_TURK);
136: assertEquals("2007-04-01T01:00:00.000-04:00", at.toString());
137: DateTime post = new DateTime(CUTOVER_TURK + 1L, MOCK_TURK);
138: assertEquals("2007-04-01T01:00:00.001-04:00", post.toString());
139: }
140:
141: public void test_lenientChrononolgy_Chicago() {
142: DateTimeZone zone = DateTimeZone.forID("America/Chicago");
143: Chronology lenient = LenientChronology
144: .getInstance(ISOChronology.getInstance(zone));
145: DateTime dt = new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
146: assertEquals("2007-03-11T03:30:00.000-05:00", dt.toString());
147: }
148:
149: public void test_lenientChrononolgy_Turk() {
150: Chronology lenient = LenientChronology
151: .getInstance(ISOChronology.getInstance(MOCK_TURK));
152: DateTime dt = new DateTime(2007, 4, 1, 0, 30, 0, 0, lenient);
153: assertEquals("2007-04-01T01:30:00.000-04:00", dt.toString());
154: }
155:
156: public void test_strictChrononolgy_Chicago() {
157: DateTimeZone zone = DateTimeZone.forID("America/Chicago");
158: Chronology lenient = StrictChronology.getInstance(ISOChronology
159: .getInstance(zone));
160: try {
161: new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
162: fail();
163: } catch (IllegalArgumentException ex) {
164: // expected
165: }
166: }
167:
168: public void test_isoChrononolgy_Chicago() {
169: DateTimeZone zone = DateTimeZone.forID("America/Chicago");
170: Chronology lenient = ISOChronology.getInstance(zone);
171: try {
172: new DateTime(2007, 3, 11, 2, 30, 0, 0, lenient);
173: fail();
174: } catch (IllegalArgumentException ex) {
175: // expected
176: }
177: }
178:
179: }
|