KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > TestJulianChronology


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.chrono;
17
18 import java.util.Locale JavaDoc;
19 import java.util.TimeZone JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.DateTimeConstants;
25 import org.joda.time.DateTimeUtils;
26 import org.joda.time.DateTimeZone;
27
28 /**
29  * This class is a Junit unit test for JulianChronology.
30  *
31  * @author Stephen Colebourne
32  */

33 public class TestJulianChronology extends TestCase {
34
35     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
36     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
37     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
38
39     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
40                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
41                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
42                      366 + 365;
43     // 2002-06-09
44
private long TEST_TIME_NOW =
45             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
46
47     private DateTimeZone originalDateTimeZone = null;
48     private TimeZone JavaDoc originalTimeZone = null;
49     private Locale JavaDoc originalLocale = null;
50
51     public static void main(String JavaDoc[] args) {
52         junit.textui.TestRunner.run(suite());
53     }
54
55     public static TestSuite suite() {
56         return new TestSuite(TestJulianChronology.class);
57     }
58
59     public TestJulianChronology(String JavaDoc name) {
60         super(name);
61     }
62
63     protected void setUp() throws Exception JavaDoc {
64         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
65         originalDateTimeZone = DateTimeZone.getDefault();
66         originalTimeZone = TimeZone.getDefault();
67         originalLocale = Locale.getDefault();
68         DateTimeZone.setDefault(LONDON);
69         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
70         Locale.setDefault(Locale.UK);
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         DateTimeUtils.setCurrentMillisSystem();
75         DateTimeZone.setDefault(originalDateTimeZone);
76         TimeZone.setDefault(originalTimeZone);
77         Locale.setDefault(originalLocale);
78         originalDateTimeZone = null;
79         originalTimeZone = null;
80         originalLocale = null;
81     }
82
83     //-----------------------------------------------------------------------
84
public void testFactoryUTC() {
85         assertEquals(DateTimeZone.UTC, JulianChronology.getInstanceUTC().getZone());
86         assertSame(JulianChronology.class, JulianChronology.getInstanceUTC().getClass());
87     }
88
89     public void testFactory() {
90         assertEquals(LONDON, JulianChronology.getInstance().getZone());
91         assertSame(JulianChronology.class, JulianChronology.getInstance().getClass());
92     }
93
94     public void testFactory_Zone() {
95         assertEquals(TOKYO, JulianChronology.getInstance(TOKYO).getZone());
96         assertEquals(PARIS, JulianChronology.getInstance(PARIS).getZone());
97         assertEquals(LONDON, JulianChronology.getInstance(null).getZone());
98         assertSame(JulianChronology.class, JulianChronology.getInstance(TOKYO).getClass());
99     }
100
101     public void testFactory_Zone_int() {
102         JulianChronology chrono = JulianChronology.getInstance(TOKYO, 2);
103         assertEquals(TOKYO, chrono.getZone());
104         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
105         
106         try {
107             JulianChronology.getInstance(TOKYO, 0);
108             fail();
109         } catch (IllegalArgumentException JavaDoc ex) {}
110         try {
111             JulianChronology.getInstance(TOKYO, 8);
112             fail();
113         } catch (IllegalArgumentException JavaDoc ex) {}
114     }
115
116     //-----------------------------------------------------------------------
117
public void testEquality() {
118         assertSame(JulianChronology.getInstance(TOKYO), JulianChronology.getInstance(TOKYO));
119         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(LONDON));
120         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance(PARIS));
121         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstanceUTC());
122         assertSame(JulianChronology.getInstance(), JulianChronology.getInstance(LONDON));
123     }
124
125     public void testWithUTC() {
126         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance(LONDON).withUTC());
127         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance(TOKYO).withUTC());
128         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstanceUTC().withUTC());
129         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance().withUTC());
130     }
131
132     public void testWithZone() {
133         assertSame(JulianChronology.getInstance(TOKYO), JulianChronology.getInstance(TOKYO).withZone(TOKYO));
134         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(TOKYO).withZone(LONDON));
135         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance(TOKYO).withZone(PARIS));
136         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(TOKYO).withZone(null));
137         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance().withZone(PARIS));
138         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstanceUTC().withZone(PARIS));
139     }
140
141     public void testToString() {
142         assertEquals("JulianChronology[Europe/London]", JulianChronology.getInstance(LONDON).toString());
143         assertEquals("JulianChronology[Asia/Tokyo]", JulianChronology.getInstance(TOKYO).toString());
144         assertEquals("JulianChronology[Europe/London]", JulianChronology.getInstance().toString());
145         assertEquals("JulianChronology[UTC]", JulianChronology.getInstanceUTC().toString());
146         assertEquals("JulianChronology[UTC,mdfw=2]", JulianChronology.getInstance(DateTimeZone.UTC, 2).toString());
147     }
148
149     //-----------------------------------------------------------------------
150
public void testDurationFields() {
151         assertEquals("eras", JulianChronology.getInstance().eras().getName());
152         assertEquals("centuries", JulianChronology.getInstance().centuries().getName());
153         assertEquals("years", JulianChronology.getInstance().years().getName());
154         assertEquals("weekyears", JulianChronology.getInstance().weekyears().getName());
155         assertEquals("months", JulianChronology.getInstance().months().getName());
156         assertEquals("weeks", JulianChronology.getInstance().weeks().getName());
157         assertEquals("days", JulianChronology.getInstance().days().getName());
158         assertEquals("halfdays", JulianChronology.getInstance().halfdays().getName());
159         assertEquals("hours", JulianChronology.getInstance().hours().getName());
160         assertEquals("minutes", JulianChronology.getInstance().minutes().getName());
161         assertEquals("seconds", JulianChronology.getInstance().seconds().getName());
162         assertEquals("millis", JulianChronology.getInstance().millis().getName());
163         
164         assertEquals(false, JulianChronology.getInstance().eras().isSupported());
165         assertEquals(true, JulianChronology.getInstance().centuries().isSupported());
166         assertEquals(true, JulianChronology.getInstance().years().isSupported());
167         assertEquals(true, JulianChronology.getInstance().weekyears().isSupported());
168         assertEquals(true, JulianChronology.getInstance().months().isSupported());
169         assertEquals(true, JulianChronology.getInstance().weeks().isSupported());
170         assertEquals(true, JulianChronology.getInstance().days().isSupported());
171         assertEquals(true, JulianChronology.getInstance().halfdays().isSupported());
172         assertEquals(true, JulianChronology.getInstance().hours().isSupported());
173         assertEquals(true, JulianChronology.getInstance().minutes().isSupported());
174         assertEquals(true, JulianChronology.getInstance().seconds().isSupported());
175         assertEquals(true, JulianChronology.getInstance().millis().isSupported());
176         
177         assertEquals(false, JulianChronology.getInstance().centuries().isPrecise());
178         assertEquals(false, JulianChronology.getInstance().years().isPrecise());
179         assertEquals(false, JulianChronology.getInstance().weekyears().isPrecise());
180         assertEquals(false, JulianChronology.getInstance().months().isPrecise());
181         assertEquals(false, JulianChronology.getInstance().weeks().isPrecise());
182         assertEquals(false, JulianChronology.getInstance().days().isPrecise());
183         assertEquals(false, JulianChronology.getInstance().halfdays().isPrecise());
184         assertEquals(true, JulianChronology.getInstance().hours().isPrecise());
185         assertEquals(true, JulianChronology.getInstance().minutes().isPrecise());
186         assertEquals(true, JulianChronology.getInstance().seconds().isPrecise());
187         assertEquals(true, JulianChronology.getInstance().millis().isPrecise());
188         
189         assertEquals(false, JulianChronology.getInstanceUTC().centuries().isPrecise());
190         assertEquals(false, JulianChronology.getInstanceUTC().years().isPrecise());
191         assertEquals(false, JulianChronology.getInstanceUTC().weekyears().isPrecise());
192         assertEquals(false, JulianChronology.getInstanceUTC().months().isPrecise());
193         assertEquals(true, JulianChronology.getInstanceUTC().weeks().isPrecise());
194         assertEquals(true, JulianChronology.getInstanceUTC().days().isPrecise());
195         assertEquals(true, JulianChronology.getInstanceUTC().halfdays().isPrecise());
196         assertEquals(true, JulianChronology.getInstanceUTC().hours().isPrecise());
197         assertEquals(true, JulianChronology.getInstanceUTC().minutes().isPrecise());
198         assertEquals(true, JulianChronology.getInstanceUTC().seconds().isPrecise());
199         assertEquals(true, JulianChronology.getInstanceUTC().millis().isPrecise());
200     }
201
202     public void testDateFields() {
203         assertEquals("era", JulianChronology.getInstance().era().getName());
204         assertEquals("centuryOfEra", JulianChronology.getInstance().centuryOfEra().getName());
205         assertEquals("yearOfCentury", JulianChronology.getInstance().yearOfCentury().getName());
206         assertEquals("yearOfEra", JulianChronology.getInstance().yearOfEra().getName());
207         assertEquals("year", JulianChronology.getInstance().year().getName());
208         assertEquals("monthOfYear", JulianChronology.getInstance().monthOfYear().getName());
209         assertEquals("weekyearOfCentury", JulianChronology.getInstance().weekyearOfCentury().getName());
210         assertEquals("weekyear", JulianChronology.getInstance().weekyear().getName());
211         assertEquals("weekOfWeekyear", JulianChronology.getInstance().weekOfWeekyear().getName());
212         assertEquals("dayOfYear", JulianChronology.getInstance().dayOfYear().getName());
213         assertEquals("dayOfMonth", JulianChronology.getInstance().dayOfMonth().getName());
214         assertEquals("dayOfWeek", JulianChronology.getInstance().dayOfWeek().getName());
215         
216         assertEquals(true, JulianChronology.getInstance().era().isSupported());
217         assertEquals(true, JulianChronology.getInstance().centuryOfEra().isSupported());
218         assertEquals(true, JulianChronology.getInstance().yearOfCentury().isSupported());
219         assertEquals(true, JulianChronology.getInstance().yearOfEra().isSupported());
220         assertEquals(true, JulianChronology.getInstance().year().isSupported());
221         assertEquals(true, JulianChronology.getInstance().monthOfYear().isSupported());
222         assertEquals(true, JulianChronology.getInstance().weekyearOfCentury().isSupported());
223         assertEquals(true, JulianChronology.getInstance().weekyear().isSupported());
224         assertEquals(true, JulianChronology.getInstance().weekOfWeekyear().isSupported());
225         assertEquals(true, JulianChronology.getInstance().dayOfYear().isSupported());
226         assertEquals(true, JulianChronology.getInstance().dayOfMonth().isSupported());
227         assertEquals(true, JulianChronology.getInstance().dayOfWeek().isSupported());
228     }
229
230     public void testTimeFields() {
231         assertEquals("halfdayOfDay", JulianChronology.getInstance().halfdayOfDay().getName());
232         assertEquals("clockhourOfHalfday", JulianChronology.getInstance().clockhourOfHalfday().getName());
233         assertEquals("hourOfHalfday", JulianChronology.getInstance().hourOfHalfday().getName());
234         assertEquals("clockhourOfDay", JulianChronology.getInstance().clockhourOfDay().getName());
235         assertEquals("hourOfDay", JulianChronology.getInstance().hourOfDay().getName());
236         assertEquals("minuteOfDay", JulianChronology.getInstance().minuteOfDay().getName());
237         assertEquals("minuteOfHour", JulianChronology.getInstance().minuteOfHour().getName());
238         assertEquals("secondOfDay", JulianChronology.getInstance().secondOfDay().getName());
239         assertEquals("secondOfMinute", JulianChronology.getInstance().secondOfMinute().getName());
240         assertEquals("millisOfDay", JulianChronology.getInstance().millisOfDay().getName());
241         assertEquals("millisOfSecond", JulianChronology.getInstance().millisOfSecond().getName());
242         
243         assertEquals(true, JulianChronology.getInstance().halfdayOfDay().isSupported());
244         assertEquals(true, JulianChronology.getInstance().clockhourOfHalfday().isSupported());
245         assertEquals(true, JulianChronology.getInstance().hourOfHalfday().isSupported());
246         assertEquals(true, JulianChronology.getInstance().clockhourOfDay().isSupported());
247         assertEquals(true, JulianChronology.getInstance().hourOfDay().isSupported());
248         assertEquals(true, JulianChronology.getInstance().minuteOfDay().isSupported());
249         assertEquals(true, JulianChronology.getInstance().minuteOfHour().isSupported());
250         assertEquals(true, JulianChronology.getInstance().secondOfDay().isSupported());
251         assertEquals(true, JulianChronology.getInstance().secondOfMinute().isSupported());
252         assertEquals(true, JulianChronology.getInstance().millisOfDay().isSupported());
253         assertEquals(true, JulianChronology.getInstance().millisOfSecond().isSupported());
254     }
255
256 }
257
Popular Tags