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.convert;
017:
018: import java.lang.reflect.Constructor;
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Modifier;
021: import java.util.Arrays;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.GregorianCalendar;
025: import java.util.TimeZone;
026:
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.joda.time.Chronology;
031: import org.joda.time.DateTimeZone;
032: import org.joda.time.TimeOfDay;
033: import org.joda.time.chrono.BuddhistChronology;
034: import org.joda.time.chrono.GJChronology;
035: import org.joda.time.chrono.GregorianChronology;
036: import org.joda.time.chrono.ISOChronology;
037: import org.joda.time.chrono.JulianChronology;
038:
039: /**
040: * This class is a Junit unit test for CalendarConverter.
041: *
042: * @author Stephen Colebourne
043: */
044: public class TestCalendarConverter extends TestCase {
045:
046: private static final DateTimeZone PARIS = DateTimeZone
047: .forID("Europe/Paris");
048: private static final DateTimeZone MOSCOW = DateTimeZone
049: .forID("Europe/Moscow");
050: private static Chronology JULIAN;
051: private static Chronology ISO;
052:
053: public static void main(String[] args) {
054: junit.textui.TestRunner.run(suite());
055: }
056:
057: public static TestSuite suite() {
058: return new TestSuite(TestCalendarConverter.class);
059: }
060:
061: public TestCalendarConverter(String name) {
062: super (name);
063: }
064:
065: protected void setUp() throws Exception {
066: JULIAN = JulianChronology.getInstance();
067: ISO = ISOChronology.getInstance();
068: }
069:
070: //-----------------------------------------------------------------------
071: public void testSingleton() throws Exception {
072: Class cls = CalendarConverter.class;
073: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
074: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
075: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
076:
077: Constructor con = cls.getDeclaredConstructor((Class[]) null);
078: assertEquals(1, cls.getDeclaredConstructors().length);
079: assertEquals(true, Modifier.isProtected(con.getModifiers()));
080:
081: Field fld = cls.getDeclaredField("INSTANCE");
082: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
083: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
084: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
085: }
086:
087: //-----------------------------------------------------------------------
088: public void testSupportedType() throws Exception {
089: assertEquals(Calendar.class, CalendarConverter.INSTANCE
090: .getSupportedType());
091: }
092:
093: //-----------------------------------------------------------------------
094: public void testGetInstantMillis_Object_Chronology()
095: throws Exception {
096: GregorianCalendar cal = new GregorianCalendar();
097: cal.setTime(new Date(123L));
098: assertEquals(123L, CalendarConverter.INSTANCE.getInstantMillis(
099: cal, JULIAN));
100: assertEquals(123L, cal.getTime().getTime());
101: }
102:
103: //-----------------------------------------------------------------------
104: public void testGetChronology_Object_Zone() throws Exception {
105: GregorianCalendar cal = new GregorianCalendar(TimeZone
106: .getTimeZone("Europe/Paris"));
107: assertEquals(GJChronology.getInstance(MOSCOW),
108: CalendarConverter.INSTANCE.getChronology(cal, MOSCOW));
109:
110: cal = new GregorianCalendar(TimeZone
111: .getTimeZone("Europe/Moscow"));
112: assertEquals(GJChronology.getInstance(),
113: CalendarConverter.INSTANCE.getChronology(cal,
114: (DateTimeZone) null));
115:
116: cal = new GregorianCalendar(TimeZone
117: .getTimeZone("Europe/Moscow"));
118: cal.setGregorianChange(new Date(0L));
119: assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4),
120: CalendarConverter.INSTANCE.getChronology(cal, MOSCOW));
121:
122: cal = new GregorianCalendar(TimeZone
123: .getTimeZone("Europe/Moscow"));
124: cal.setGregorianChange(new Date(Long.MAX_VALUE));
125: assertEquals(JulianChronology.getInstance(PARIS),
126: CalendarConverter.INSTANCE.getChronology(cal, PARIS));
127:
128: cal = new GregorianCalendar(TimeZone
129: .getTimeZone("Europe/Moscow"));
130: cal.setGregorianChange(new Date(Long.MIN_VALUE));
131: assertEquals(GregorianChronology.getInstance(PARIS),
132: CalendarConverter.INSTANCE.getChronology(cal, PARIS));
133:
134: Calendar uc = new MockUnknownCalendar(TimeZone
135: .getTimeZone("Europe/Moscow"));
136: assertEquals(ISOChronology.getInstance(PARIS),
137: CalendarConverter.INSTANCE.getChronology(uc, PARIS));
138:
139: try {
140: Calendar bc = (Calendar) Class.forName(
141: "sun.util.BuddhistCalendar").newInstance();
142: bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
143: assertEquals(BuddhistChronology.getInstance(PARIS),
144: CalendarConverter.INSTANCE.getChronology(bc, PARIS));
145: } catch (ClassNotFoundException ex) {
146: // ignore
147: }
148: }
149:
150: public void testGetChronology_Object_nullChronology()
151: throws Exception {
152: GregorianCalendar cal = new GregorianCalendar(TimeZone
153: .getTimeZone("Europe/Paris"));
154: assertEquals(GJChronology.getInstance(PARIS),
155: CalendarConverter.INSTANCE.getChronology(cal,
156: (Chronology) null));
157:
158: cal = new GregorianCalendar(TimeZone
159: .getTimeZone("Europe/Moscow"));
160: cal.setGregorianChange(new Date(0L));
161: assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4),
162: CalendarConverter.INSTANCE.getChronology(cal,
163: (Chronology) null));
164:
165: cal = new GregorianCalendar(TimeZone
166: .getTimeZone("Europe/Moscow"));
167: cal.setGregorianChange(new Date(Long.MAX_VALUE));
168: assertEquals(JulianChronology.getInstance(MOSCOW),
169: CalendarConverter.INSTANCE.getChronology(cal,
170: (Chronology) null));
171:
172: cal = new GregorianCalendar(TimeZone
173: .getTimeZone("Europe/Moscow"));
174: cal.setGregorianChange(new Date(Long.MIN_VALUE));
175: assertEquals(GregorianChronology.getInstance(MOSCOW),
176: CalendarConverter.INSTANCE.getChronology(cal,
177: (Chronology) null));
178:
179: cal = new GregorianCalendar(new MockUnknownTimeZone());
180: assertEquals(GJChronology.getInstance(),
181: CalendarConverter.INSTANCE.getChronology(cal,
182: (Chronology) null));
183:
184: Calendar uc = new MockUnknownCalendar(TimeZone
185: .getTimeZone("Europe/Moscow"));
186: assertEquals(ISOChronology.getInstance(MOSCOW),
187: CalendarConverter.INSTANCE.getChronology(uc,
188: (Chronology) null));
189:
190: try {
191: Calendar bc = (Calendar) Class.forName(
192: "sun.util.BuddhistCalendar").newInstance();
193: bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
194: assertEquals(BuddhistChronology.getInstance(MOSCOW),
195: CalendarConverter.INSTANCE.getChronology(bc,
196: (Chronology) null));
197: } catch (ClassNotFoundException ex) {
198: // ignore
199: }
200: }
201:
202: public void testGetChronology_Object_Chronology() throws Exception {
203: GregorianCalendar cal = new GregorianCalendar(TimeZone
204: .getTimeZone("Europe/Paris"));
205: assertEquals(JULIAN, CalendarConverter.INSTANCE.getChronology(
206: cal, JULIAN));
207: }
208:
209: //-----------------------------------------------------------------------
210: public void testGetPartialValues() throws Exception {
211: GregorianCalendar cal = new GregorianCalendar();
212: cal.setTime(new Date(12345678L));
213: TimeOfDay tod = new TimeOfDay();
214: int[] expected = ISO.get(tod, 12345678L);
215: int[] actual = CalendarConverter.INSTANCE.getPartialValues(tod,
216: cal, ISO);
217: assertEquals(true, Arrays.equals(expected, actual));
218: }
219:
220: //-----------------------------------------------------------------------
221: public void testToString() {
222: assertEquals("Converter[java.util.Calendar]",
223: CalendarConverter.INSTANCE.toString());
224: }
225:
226: }
|