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.chrono;
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.Chronology;
025: import org.joda.time.DateMidnight;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeUtils;
028: import org.joda.time.DateTimeZone;
029: import org.joda.time.YearMonthDay;
030:
031: /**
032: * This class is a Junit unit test for GregorianChronology.
033: *
034: * @author Stephen Colebourne
035: */
036: public class TestGregorianChronology extends TestCase {
037:
038: private static final DateTimeZone PARIS = DateTimeZone
039: .forID("Europe/Paris");
040: private static final DateTimeZone LONDON = DateTimeZone
041: .forID("Europe/London");
042: private static final DateTimeZone TOKYO = DateTimeZone
043: .forID("Asia/Tokyo");
044:
045: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
046: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
047: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
048: + 365 + 365 + 366 + 365;
049: // 2002-06-09
050: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
051: + 31L + 9L - 1L)
052: * DateTimeConstants.MILLIS_PER_DAY;
053:
054: private DateTimeZone originalDateTimeZone = null;
055: private TimeZone originalTimeZone = null;
056: private Locale originalLocale = null;
057:
058: public static void main(String[] args) {
059: junit.textui.TestRunner.run(suite());
060: }
061:
062: public static TestSuite suite() {
063: return new TestSuite(TestGregorianChronology.class);
064: }
065:
066: public TestGregorianChronology(String name) {
067: super (name);
068: }
069:
070: protected void setUp() throws Exception {
071: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
072: originalDateTimeZone = DateTimeZone.getDefault();
073: originalTimeZone = TimeZone.getDefault();
074: originalLocale = Locale.getDefault();
075: DateTimeZone.setDefault(LONDON);
076: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
077: Locale.setDefault(Locale.UK);
078: }
079:
080: protected void tearDown() throws Exception {
081: DateTimeUtils.setCurrentMillisSystem();
082: DateTimeZone.setDefault(originalDateTimeZone);
083: TimeZone.setDefault(originalTimeZone);
084: Locale.setDefault(originalLocale);
085: originalDateTimeZone = null;
086: originalTimeZone = null;
087: originalLocale = null;
088: }
089:
090: //-----------------------------------------------------------------------
091: public void testFactoryUTC() {
092: assertEquals(DateTimeZone.UTC, GregorianChronology
093: .getInstanceUTC().getZone());
094: assertSame(GregorianChronology.class, GregorianChronology
095: .getInstanceUTC().getClass());
096: }
097:
098: public void testFactory() {
099: assertEquals(LONDON, GregorianChronology.getInstance()
100: .getZone());
101: assertSame(GregorianChronology.class, GregorianChronology
102: .getInstance().getClass());
103: }
104:
105: public void testFactory_Zone() {
106: assertEquals(TOKYO, GregorianChronology.getInstance(TOKYO)
107: .getZone());
108: assertEquals(PARIS, GregorianChronology.getInstance(PARIS)
109: .getZone());
110: assertEquals(LONDON, GregorianChronology.getInstance(null)
111: .getZone());
112: assertSame(GregorianChronology.class, GregorianChronology
113: .getInstance(TOKYO).getClass());
114: }
115:
116: public void testFactory_Zone_int() {
117: GregorianChronology chrono = GregorianChronology.getInstance(
118: TOKYO, 2);
119: assertEquals(TOKYO, chrono.getZone());
120: assertEquals(2, chrono.getMinimumDaysInFirstWeek());
121:
122: try {
123: GregorianChronology.getInstance(TOKYO, 0);
124: fail();
125: } catch (IllegalArgumentException ex) {
126: }
127: try {
128: GregorianChronology.getInstance(TOKYO, 8);
129: fail();
130: } catch (IllegalArgumentException ex) {
131: }
132: }
133:
134: //-----------------------------------------------------------------------
135: public void testEquality() {
136: assertSame(GregorianChronology.getInstance(TOKYO),
137: GregorianChronology.getInstance(TOKYO));
138: assertSame(GregorianChronology.getInstance(LONDON),
139: GregorianChronology.getInstance(LONDON));
140: assertSame(GregorianChronology.getInstance(PARIS),
141: GregorianChronology.getInstance(PARIS));
142: assertSame(GregorianChronology.getInstanceUTC(),
143: GregorianChronology.getInstanceUTC());
144: assertSame(GregorianChronology.getInstance(),
145: GregorianChronology.getInstance(LONDON));
146: }
147:
148: public void testWithUTC() {
149: assertSame(GregorianChronology.getInstanceUTC(),
150: GregorianChronology.getInstance(LONDON).withUTC());
151: assertSame(GregorianChronology.getInstanceUTC(),
152: GregorianChronology.getInstance(TOKYO).withUTC());
153: assertSame(GregorianChronology.getInstanceUTC(),
154: GregorianChronology.getInstanceUTC().withUTC());
155: assertSame(GregorianChronology.getInstanceUTC(),
156: GregorianChronology.getInstance().withUTC());
157: }
158:
159: public void testWithZone() {
160: assertSame(GregorianChronology.getInstance(TOKYO),
161: GregorianChronology.getInstance(TOKYO).withZone(TOKYO));
162: assertSame(GregorianChronology.getInstance(LONDON),
163: GregorianChronology.getInstance(TOKYO).withZone(LONDON));
164: assertSame(GregorianChronology.getInstance(PARIS),
165: GregorianChronology.getInstance(TOKYO).withZone(PARIS));
166: assertSame(GregorianChronology.getInstance(LONDON),
167: GregorianChronology.getInstance(TOKYO).withZone(null));
168: assertSame(GregorianChronology.getInstance(PARIS),
169: GregorianChronology.getInstance().withZone(PARIS));
170: assertSame(GregorianChronology.getInstance(PARIS),
171: GregorianChronology.getInstanceUTC().withZone(PARIS));
172: }
173:
174: public void testToString() {
175: assertEquals("GregorianChronology[Europe/London]",
176: GregorianChronology.getInstance(LONDON).toString());
177: assertEquals("GregorianChronology[Asia/Tokyo]",
178: GregorianChronology.getInstance(TOKYO).toString());
179: assertEquals("GregorianChronology[Europe/London]",
180: GregorianChronology.getInstance().toString());
181: assertEquals("GregorianChronology[UTC]", GregorianChronology
182: .getInstanceUTC().toString());
183: assertEquals("GregorianChronology[UTC,mdfw=2]",
184: GregorianChronology.getInstance(DateTimeZone.UTC, 2)
185: .toString());
186: }
187:
188: //-----------------------------------------------------------------------
189: public void testDurationFields() {
190: assertEquals("eras", GregorianChronology.getInstance().eras()
191: .getName());
192: assertEquals("centuries", GregorianChronology.getInstance()
193: .centuries().getName());
194: assertEquals("years", GregorianChronology.getInstance().years()
195: .getName());
196: assertEquals("weekyears", GregorianChronology.getInstance()
197: .weekyears().getName());
198: assertEquals("months", GregorianChronology.getInstance()
199: .months().getName());
200: assertEquals("weeks", GregorianChronology.getInstance().weeks()
201: .getName());
202: assertEquals("days", GregorianChronology.getInstance().days()
203: .getName());
204: assertEquals("halfdays", GregorianChronology.getInstance()
205: .halfdays().getName());
206: assertEquals("hours", GregorianChronology.getInstance().hours()
207: .getName());
208: assertEquals("minutes", GregorianChronology.getInstance()
209: .minutes().getName());
210: assertEquals("seconds", GregorianChronology.getInstance()
211: .seconds().getName());
212: assertEquals("millis", GregorianChronology.getInstance()
213: .millis().getName());
214:
215: assertEquals(false, GregorianChronology.getInstance().eras()
216: .isSupported());
217: assertEquals(true, GregorianChronology.getInstance()
218: .centuries().isSupported());
219: assertEquals(true, GregorianChronology.getInstance().years()
220: .isSupported());
221: assertEquals(true, GregorianChronology.getInstance()
222: .weekyears().isSupported());
223: assertEquals(true, GregorianChronology.getInstance().months()
224: .isSupported());
225: assertEquals(true, GregorianChronology.getInstance().weeks()
226: .isSupported());
227: assertEquals(true, GregorianChronology.getInstance().days()
228: .isSupported());
229: assertEquals(true, GregorianChronology.getInstance().halfdays()
230: .isSupported());
231: assertEquals(true, GregorianChronology.getInstance().hours()
232: .isSupported());
233: assertEquals(true, GregorianChronology.getInstance().minutes()
234: .isSupported());
235: assertEquals(true, GregorianChronology.getInstance().seconds()
236: .isSupported());
237: assertEquals(true, GregorianChronology.getInstance().millis()
238: .isSupported());
239:
240: assertEquals(false, GregorianChronology.getInstance()
241: .centuries().isPrecise());
242: assertEquals(false, GregorianChronology.getInstance().years()
243: .isPrecise());
244: assertEquals(false, GregorianChronology.getInstance()
245: .weekyears().isPrecise());
246: assertEquals(false, GregorianChronology.getInstance().months()
247: .isPrecise());
248: assertEquals(false, GregorianChronology.getInstance().weeks()
249: .isPrecise());
250: assertEquals(false, GregorianChronology.getInstance().days()
251: .isPrecise());
252: assertEquals(false, GregorianChronology.getInstance()
253: .halfdays().isPrecise());
254: assertEquals(true, GregorianChronology.getInstance().hours()
255: .isPrecise());
256: assertEquals(true, GregorianChronology.getInstance().minutes()
257: .isPrecise());
258: assertEquals(true, GregorianChronology.getInstance().seconds()
259: .isPrecise());
260: assertEquals(true, GregorianChronology.getInstance().millis()
261: .isPrecise());
262:
263: assertEquals(false, GregorianChronology.getInstanceUTC()
264: .centuries().isPrecise());
265: assertEquals(false, GregorianChronology.getInstanceUTC()
266: .years().isPrecise());
267: assertEquals(false, GregorianChronology.getInstanceUTC()
268: .weekyears().isPrecise());
269: assertEquals(false, GregorianChronology.getInstanceUTC()
270: .months().isPrecise());
271: assertEquals(true, GregorianChronology.getInstanceUTC().weeks()
272: .isPrecise());
273: assertEquals(true, GregorianChronology.getInstanceUTC().days()
274: .isPrecise());
275: assertEquals(true, GregorianChronology.getInstanceUTC()
276: .halfdays().isPrecise());
277: assertEquals(true, GregorianChronology.getInstanceUTC().hours()
278: .isPrecise());
279: assertEquals(true, GregorianChronology.getInstanceUTC()
280: .minutes().isPrecise());
281: assertEquals(true, GregorianChronology.getInstanceUTC()
282: .seconds().isPrecise());
283: assertEquals(true, GregorianChronology.getInstanceUTC()
284: .millis().isPrecise());
285: }
286:
287: public void testDateFields() {
288: assertEquals("era", GregorianChronology.getInstance().era()
289: .getName());
290: assertEquals("centuryOfEra", GregorianChronology.getInstance()
291: .centuryOfEra().getName());
292: assertEquals("yearOfCentury", GregorianChronology.getInstance()
293: .yearOfCentury().getName());
294: assertEquals("yearOfEra", GregorianChronology.getInstance()
295: .yearOfEra().getName());
296: assertEquals("year", GregorianChronology.getInstance().year()
297: .getName());
298: assertEquals("monthOfYear", GregorianChronology.getInstance()
299: .monthOfYear().getName());
300: assertEquals("weekyearOfCentury", GregorianChronology
301: .getInstance().weekyearOfCentury().getName());
302: assertEquals("weekyear", GregorianChronology.getInstance()
303: .weekyear().getName());
304: assertEquals("weekOfWeekyear", GregorianChronology
305: .getInstance().weekOfWeekyear().getName());
306: assertEquals("dayOfYear", GregorianChronology.getInstance()
307: .dayOfYear().getName());
308: assertEquals("dayOfMonth", GregorianChronology.getInstance()
309: .dayOfMonth().getName());
310: assertEquals("dayOfWeek", GregorianChronology.getInstance()
311: .dayOfWeek().getName());
312:
313: assertEquals(true, GregorianChronology.getInstance().era()
314: .isSupported());
315: assertEquals(true, GregorianChronology.getInstance()
316: .centuryOfEra().isSupported());
317: assertEquals(true, GregorianChronology.getInstance()
318: .yearOfCentury().isSupported());
319: assertEquals(true, GregorianChronology.getInstance()
320: .yearOfEra().isSupported());
321: assertEquals(true, GregorianChronology.getInstance().year()
322: .isSupported());
323: assertEquals(true, GregorianChronology.getInstance()
324: .monthOfYear().isSupported());
325: assertEquals(true, GregorianChronology.getInstance()
326: .weekyearOfCentury().isSupported());
327: assertEquals(true, GregorianChronology.getInstance().weekyear()
328: .isSupported());
329: assertEquals(true, GregorianChronology.getInstance()
330: .weekOfWeekyear().isSupported());
331: assertEquals(true, GregorianChronology.getInstance()
332: .dayOfYear().isSupported());
333: assertEquals(true, GregorianChronology.getInstance()
334: .dayOfMonth().isSupported());
335: assertEquals(true, GregorianChronology.getInstance()
336: .dayOfWeek().isSupported());
337: }
338:
339: public void testTimeFields() {
340: assertEquals("halfdayOfDay", GregorianChronology.getInstance()
341: .halfdayOfDay().getName());
342: assertEquals("clockhourOfHalfday", GregorianChronology
343: .getInstance().clockhourOfHalfday().getName());
344: assertEquals("hourOfHalfday", GregorianChronology.getInstance()
345: .hourOfHalfday().getName());
346: assertEquals("clockhourOfDay", GregorianChronology
347: .getInstance().clockhourOfDay().getName());
348: assertEquals("hourOfDay", GregorianChronology.getInstance()
349: .hourOfDay().getName());
350: assertEquals("minuteOfDay", GregorianChronology.getInstance()
351: .minuteOfDay().getName());
352: assertEquals("minuteOfHour", GregorianChronology.getInstance()
353: .minuteOfHour().getName());
354: assertEquals("secondOfDay", GregorianChronology.getInstance()
355: .secondOfDay().getName());
356: assertEquals("secondOfMinute", GregorianChronology
357: .getInstance().secondOfMinute().getName());
358: assertEquals("millisOfDay", GregorianChronology.getInstance()
359: .millisOfDay().getName());
360: assertEquals("millisOfSecond", GregorianChronology
361: .getInstance().millisOfSecond().getName());
362:
363: assertEquals(true, GregorianChronology.getInstance()
364: .halfdayOfDay().isSupported());
365: assertEquals(true, GregorianChronology.getInstance()
366: .clockhourOfHalfday().isSupported());
367: assertEquals(true, GregorianChronology.getInstance()
368: .hourOfHalfday().isSupported());
369: assertEquals(true, GregorianChronology.getInstance()
370: .clockhourOfDay().isSupported());
371: assertEquals(true, GregorianChronology.getInstance()
372: .hourOfDay().isSupported());
373: assertEquals(true, GregorianChronology.getInstance()
374: .minuteOfDay().isSupported());
375: assertEquals(true, GregorianChronology.getInstance()
376: .minuteOfHour().isSupported());
377: assertEquals(true, GregorianChronology.getInstance()
378: .secondOfDay().isSupported());
379: assertEquals(true, GregorianChronology.getInstance()
380: .secondOfMinute().isSupported());
381: assertEquals(true, GregorianChronology.getInstance()
382: .millisOfDay().isSupported());
383: assertEquals(true, GregorianChronology.getInstance()
384: .millisOfSecond().isSupported());
385: }
386:
387: public void testMaximumValue() {
388: YearMonthDay ymd1 = new YearMonthDay(1999,
389: DateTimeConstants.FEBRUARY, 1);
390: DateMidnight dm1 = new DateMidnight(1999,
391: DateTimeConstants.FEBRUARY, 1);
392: Chronology chrono = GregorianChronology.getInstance();
393: assertEquals(28, chrono.dayOfMonth().getMaximumValue(ymd1));
394: assertEquals(28, chrono.dayOfMonth().getMaximumValue(
395: dm1.getMillis()));
396: }
397:
398: }
|