001: /*
002: * Copyright 2001-2005 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;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.chrono.BuddhistChronology;
025: import org.joda.time.chrono.CopticChronology;
026: import org.joda.time.chrono.EthiopicChronology;
027: import org.joda.time.chrono.GJChronology;
028: import org.joda.time.chrono.GregorianChronology;
029: import org.joda.time.chrono.ISOChronology;
030: import org.joda.time.chrono.IslamicChronology;
031: import org.joda.time.chrono.JulianChronology;
032: import org.joda.time.chrono.LenientChronology;
033: import org.joda.time.chrono.LimitChronology;
034: import org.joda.time.chrono.StrictChronology;
035: import org.joda.time.chrono.ZonedChronology;
036:
037: /**
038: * This class is a Junit unit test for Chronology.
039: *
040: * @author Stephen Colebourne
041: */
042: public class TestChronology extends TestCase {
043: // Test in 2002/03 as time zones are more well known
044: // (before the late 90's they were all over the place)
045:
046: private static final DateTimeZone PARIS = DateTimeZone
047: .forID("Europe/Paris");
048: private static final DateTimeZone LONDON = DateTimeZone
049: .forID("Europe/London");
050:
051: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
052: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
053: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
054: + 365 + 365 + 366 + 365;
055: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
056: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
057: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
058: + 365 + 365 + 366 + 365 + 365;
059:
060: // 2002-06-09
061: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
062: + 31L + 9L - 1L)
063: * DateTimeConstants.MILLIS_PER_DAY;
064:
065: // 2002-04-05
066: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
067: * DateTimeConstants.MILLIS_PER_DAY + 12L
068: * DateTimeConstants.MILLIS_PER_HOUR + 24L
069: * DateTimeConstants.MILLIS_PER_MINUTE;
070:
071: // 2003-05-06
072: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
073: * DateTimeConstants.MILLIS_PER_DAY
074: + 14L
075: * DateTimeConstants.MILLIS_PER_HOUR
076: + 28L
077: * DateTimeConstants.MILLIS_PER_MINUTE;
078:
079: private DateTimeZone originalDateTimeZone = null;
080: private TimeZone originalTimeZone = null;
081: private Locale originalLocale = null;
082:
083: public static void main(String[] args) {
084: junit.textui.TestRunner.run(suite());
085: }
086:
087: public static TestSuite suite() {
088: return new TestSuite(TestChronology.class);
089: }
090:
091: public TestChronology(String name) {
092: super (name);
093: }
094:
095: protected void setUp() throws Exception {
096: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
097: originalDateTimeZone = DateTimeZone.getDefault();
098: originalTimeZone = TimeZone.getDefault();
099: originalLocale = Locale.getDefault();
100: DateTimeZone.setDefault(LONDON);
101: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
102: Locale.setDefault(Locale.UK);
103: }
104:
105: protected void tearDown() throws Exception {
106: DateTimeUtils.setCurrentMillisSystem();
107: DateTimeZone.setDefault(originalDateTimeZone);
108: TimeZone.setDefault(originalTimeZone);
109: Locale.setDefault(originalLocale);
110: originalDateTimeZone = null;
111: originalTimeZone = null;
112: originalLocale = null;
113: }
114:
115: //-----------------------------------------------------------------------
116: public void testTest() {
117: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
118: TEST_TIME_NOW).toString());
119: assertEquals("2002-04-05T12:24:00.000Z",
120: new Instant(TEST_TIME1).toString());
121: assertEquals("2003-05-06T14:28:00.000Z",
122: new Instant(TEST_TIME2).toString());
123: }
124:
125: //-----------------------------------------------------------------------
126: public void testGetISO() {
127: assertEquals(ISOChronology.getInstance(), Chronology.getISO());
128: }
129:
130: public void testGetISOUTC() {
131: assertEquals(ISOChronology.getInstanceUTC(), Chronology
132: .getISOUTC());
133: }
134:
135: public void testGetISO_Zone() {
136: assertEquals(ISOChronology.getInstance(PARIS), Chronology
137: .getISO(PARIS));
138: assertEquals(ISOChronology.getInstance(), Chronology
139: .getISO(null));
140: }
141:
142: //-----------------------------------------------------------------------
143: public void testGetGJ() {
144: assertEquals(GJChronology.getInstance(), Chronology.getGJ());
145: }
146:
147: public void testGetGJUTC() {
148: assertEquals(GJChronology.getInstanceUTC(), Chronology
149: .getGJUTC());
150: }
151:
152: public void testGetGJ_Zone() {
153: assertEquals(GJChronology.getInstance(PARIS), Chronology
154: .getGJ(PARIS));
155: assertEquals(GJChronology.getInstance(), Chronology.getGJ(null));
156: }
157:
158: //-----------------------------------------------------------------------
159: public void testGetGregorian() {
160: assertEquals(GregorianChronology.getInstance(), Chronology
161: .getGregorian());
162: }
163:
164: public void testGetGregorianUTC() {
165: assertEquals(GregorianChronology.getInstanceUTC(), Chronology
166: .getGregorianUTC());
167: }
168:
169: public void testGetGregorian_Zone() {
170: assertEquals(GregorianChronology.getInstance(PARIS), Chronology
171: .getGregorian(PARIS));
172: assertEquals(GregorianChronology.getInstance(), Chronology
173: .getGregorian(null));
174: }
175:
176: //-----------------------------------------------------------------------
177: public void testGetJulian() {
178: assertEquals(JulianChronology.getInstance(), Chronology
179: .getJulian());
180: }
181:
182: public void testGetJulianUTC() {
183: assertEquals(JulianChronology.getInstanceUTC(), Chronology
184: .getJulianUTC());
185: }
186:
187: public void testGetJulian_Zone() {
188: assertEquals(JulianChronology.getInstance(PARIS), Chronology
189: .getJulian(PARIS));
190: assertEquals(JulianChronology.getInstance(), Chronology
191: .getJulian(null));
192: }
193:
194: //-----------------------------------------------------------------------
195: public void testGetBuddhist() {
196: assertEquals(BuddhistChronology.getInstance(), Chronology
197: .getBuddhist());
198: }
199:
200: public void testGetBuddhistUTC() {
201: assertEquals(BuddhistChronology.getInstanceUTC(), Chronology
202: .getBuddhistUTC());
203: }
204:
205: public void testGetBuddhist_Zone() {
206: assertEquals(BuddhistChronology.getInstance(PARIS), Chronology
207: .getBuddhist(PARIS));
208: assertEquals(BuddhistChronology.getInstance(), Chronology
209: .getBuddhist(null));
210: }
211:
212: //-----------------------------------------------------------------------
213: public void testGetCoptic() {
214: assertEquals(CopticChronology.getInstance(), Chronology
215: .getCoptic());
216: }
217:
218: public void testGetCopticUTC() {
219: assertEquals(CopticChronology.getInstanceUTC(), Chronology
220: .getCopticUTC());
221: }
222:
223: public void testGetCoptic_Zone() {
224: assertEquals(CopticChronology.getInstance(PARIS), Chronology
225: .getCoptic(PARIS));
226: assertEquals(CopticChronology.getInstance(), Chronology
227: .getCoptic(null));
228: }
229:
230: //-----------------------------------------------------------------------
231: public void testEqualsHashCode_ISO() {
232: Chronology chrono1 = ISOChronology.getInstanceUTC();
233: Chronology chrono2 = ISOChronology.getInstanceUTC();
234: Chronology chrono3 = ISOChronology.getInstance();
235:
236: assertEquals(true, chrono1.equals(chrono2));
237: assertEquals(false, chrono1.equals(chrono3));
238:
239: DateTime dt1 = new DateTime(0L, chrono1);
240: DateTime dt2 = new DateTime(0L, chrono2);
241: DateTime dt3 = new DateTime(0L, chrono3);
242:
243: assertEquals(true, dt1.equals(dt2));
244: assertEquals(false, dt1.equals(dt3));
245:
246: assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
247: assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
248: }
249:
250: //-----------------------------------------------------------------------
251: public void testEqualsHashCode_Lenient() {
252: Chronology chrono1 = LenientChronology
253: .getInstance(ISOChronology.getInstanceUTC());
254: Chronology chrono2 = LenientChronology
255: .getInstance(ISOChronology.getInstanceUTC());
256: Chronology chrono3 = LenientChronology
257: .getInstance(ISOChronology.getInstance());
258:
259: assertEquals(true, chrono1.equals(chrono2));
260: assertEquals(false, chrono1.equals(chrono3));
261:
262: DateTime dt1 = new DateTime(0L, chrono1);
263: DateTime dt2 = new DateTime(0L, chrono2);
264: DateTime dt3 = new DateTime(0L, chrono3);
265:
266: assertEquals(true, dt1.equals(dt2));
267: assertEquals(false, dt1.equals(dt3));
268:
269: assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
270: assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
271: }
272:
273: //-----------------------------------------------------------------------
274: public void testEqualsHashCode_Strict() {
275: Chronology chrono1 = StrictChronology.getInstance(ISOChronology
276: .getInstanceUTC());
277: Chronology chrono2 = StrictChronology.getInstance(ISOChronology
278: .getInstanceUTC());
279: Chronology chrono3 = StrictChronology.getInstance(ISOChronology
280: .getInstance());
281:
282: assertEquals(true, chrono1.equals(chrono2));
283: assertEquals(false, chrono1.equals(chrono3));
284:
285: DateTime dt1 = new DateTime(0L, chrono1);
286: DateTime dt2 = new DateTime(0L, chrono2);
287: DateTime dt3 = new DateTime(0L, chrono3);
288:
289: assertEquals(true, dt1.equals(dt2));
290: assertEquals(false, dt1.equals(dt3));
291:
292: assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
293: assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
294: }
295:
296: //-----------------------------------------------------------------------
297: public void testEqualsHashCode_Limit() {
298: DateTime lower = new DateTime(0L);
299: DateTime higherA = new DateTime(1000000L);
300: DateTime higherB = new DateTime(2000000L);
301:
302: Chronology chrono1 = LimitChronology.getInstance(ISOChronology
303: .getInstanceUTC(), lower, higherA);
304: Chronology chrono2A = LimitChronology.getInstance(ISOChronology
305: .getInstanceUTC(), lower, higherA);
306: Chronology chrono2B = LimitChronology.getInstance(ISOChronology
307: .getInstanceUTC(), lower, higherB);
308: Chronology chrono3 = LimitChronology.getInstance(ISOChronology
309: .getInstance(), lower, higherA);
310:
311: assertEquals(true, chrono1.equals(chrono2A));
312: assertEquals(false, chrono1.equals(chrono2B));
313: assertEquals(false, chrono1.equals(chrono3));
314:
315: DateTime dt1 = new DateTime(0L, chrono1);
316: DateTime dt2A = new DateTime(0L, chrono2A);
317: DateTime dt2B = new DateTime(0L, chrono2B);
318: DateTime dt3 = new DateTime(0L, chrono3);
319:
320: assertEquals(true, dt1.equals(dt2A));
321: assertEquals(false, dt1.equals(dt2B));
322: assertEquals(false, dt1.equals(dt3));
323:
324: assertEquals(true, chrono1.hashCode() == chrono2A.hashCode());
325: assertEquals(false, chrono1.hashCode() == chrono2B.hashCode());
326: assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
327: }
328:
329: //-----------------------------------------------------------------------
330: public void testEqualsHashCode_Zoned() {
331: DateTimeZone zoneA = DateTimeZone.forID("Europe/Paris");
332: DateTimeZone zoneB = DateTimeZone.forID("Asia/Tokyo");
333:
334: Chronology chrono1 = ZonedChronology.getInstance(ISOChronology
335: .getInstanceUTC(), zoneA);
336: Chronology chrono2 = ZonedChronology.getInstance(ISOChronology
337: .getInstanceUTC(), zoneA);
338: Chronology chrono3 = ZonedChronology.getInstance(ISOChronology
339: .getInstanceUTC(), zoneB);
340:
341: assertEquals(true, chrono1.equals(chrono2));
342: assertEquals(false, chrono1.equals(chrono3));
343:
344: DateTime dt1 = new DateTime(0L, chrono1);
345: DateTime dt2 = new DateTime(0L, chrono2);
346: DateTime dt3 = new DateTime(0L, chrono3);
347:
348: assertEquals(true, dt1.equals(dt2));
349: assertEquals(false, dt1.equals(dt3));
350:
351: assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
352: assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
353: }
354:
355: //-----------------------------------------------------------------------
356: public void testToString() {
357: DateTimeZone paris = DateTimeZone.forID("Europe/Paris");
358: ISOChronology isoParis = ISOChronology.getInstance(paris);
359:
360: assertEquals("ISOChronology[Europe/Paris]", isoParis.toString());
361: assertEquals("GJChronology[Europe/Paris]", GJChronology
362: .getInstance(paris).toString());
363: assertEquals("GregorianChronology[Europe/Paris]",
364: GregorianChronology.getInstance(paris).toString());
365: assertEquals("JulianChronology[Europe/Paris]", JulianChronology
366: .getInstance(paris).toString());
367: assertEquals("BuddhistChronology[Europe/Paris]",
368: BuddhistChronology.getInstance(paris).toString());
369: assertEquals("CopticChronology[Europe/Paris]", CopticChronology
370: .getInstance(paris).toString());
371: assertEquals("EthiopicChronology[Europe/Paris]",
372: EthiopicChronology.getInstance(paris).toString());
373: assertEquals("IslamicChronology[Europe/Paris]",
374: IslamicChronology.getInstance(paris).toString());
375:
376: assertEquals("LenientChronology[ISOChronology[Europe/Paris]]",
377: LenientChronology.getInstance(isoParis).toString());
378: assertEquals("StrictChronology[ISOChronology[Europe/Paris]]",
379: StrictChronology.getInstance(isoParis).toString());
380: assertEquals(
381: "LimitChronology[ISOChronology[Europe/Paris], NoLimit, NoLimit]",
382: LimitChronology.getInstance(isoParis, null, null)
383: .toString());
384: assertEquals(
385: "ZonedChronology[ISOChronology[UTC], Europe/Paris]",
386: ZonedChronology.getInstance(isoParis, paris).toString());
387: }
388:
389: }
|