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