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.forID("Europe/Paris");
047 private static final DateTimeZone MOSCOW = DateTimeZone.forID("Europe/Moscow");
048 private static Chronology JULIAN;
049 private static Chronology ISO;
050
051 public static void main(String[] args) {
052 junit.textui.TestRunner.run(suite());
053 }
054
055 public static TestSuite suite() {
056 return new TestSuite(TestCalendarConverter.class);
057 }
058
059 public TestCalendarConverter(String name) {
060 super(name);
061 }
062
063 protected void setUp() throws Exception {
064 JULIAN = JulianChronology.getInstance();
065 ISO = ISOChronology.getInstance();
066 }
067
068 //-----------------------------------------------------------------------
069 public void testSingleton() throws Exception {
070 Class cls = CalendarConverter.class;
071 assertEquals(false, Modifier.isPublic(cls.getModifiers()));
072 assertEquals(false, Modifier.isProtected(cls.getModifiers()));
073 assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
074
075 Constructor con = cls.getDeclaredConstructor((Class[]) null);
076 assertEquals(1, cls.getDeclaredConstructors().length);
077 assertEquals(true, Modifier.isProtected(con.getModifiers()));
078
079 Field fld = cls.getDeclaredField("INSTANCE");
080 assertEquals(false, Modifier.isPublic(fld.getModifiers()));
081 assertEquals(false, Modifier.isProtected(fld.getModifiers()));
082 assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
083 }
084
085 //-----------------------------------------------------------------------
086 public void testSupportedType() throws Exception {
087 assertEquals(Calendar.class, CalendarConverter.INSTANCE.getSupportedType());
088 }
089
090 //-----------------------------------------------------------------------
091 public void testGetInstantMillis_Object_Chronology() throws Exception {
092 GregorianCalendar cal = new GregorianCalendar();
093 cal.setTime(new Date(123L));
094 assertEquals(123L, CalendarConverter.INSTANCE.getInstantMillis(cal, JULIAN));
095 assertEquals(123L, cal.getTime().getTime());
096 }
097
098 //-----------------------------------------------------------------------
099 public void testGetChronology_Object_Zone() throws Exception {
100 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
101 assertEquals(GJChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, MOSCOW));
102
103 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
104 assertEquals(GJChronology.getInstance(), CalendarConverter.INSTANCE.getChronology(cal, (DateTimeZone) null));
105
106 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
107 cal.setGregorianChange(new Date(0L));
108 assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4), CalendarConverter.INSTANCE.getChronology(cal, MOSCOW));
109
110 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
111 cal.setGregorianChange(new Date(Long.MAX_VALUE));
112 assertEquals(JulianChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, PARIS));
113
114 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
115 cal.setGregorianChange(new Date(Long.MIN_VALUE));
116 assertEquals(GregorianChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, PARIS));
117
118 Calendar uc = new MockUnknownCalendar(TimeZone.getTimeZone("Europe/Moscow"));
119 assertEquals(ISOChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(uc, PARIS));
120
121 try {
122 Calendar bc = (Calendar) Class.forName("sun.util.BuddhistCalendar").newInstance();
123 bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
124 assertEquals(BuddhistChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(bc, PARIS));
125 } catch (ClassNotFoundException ex) {
126 // ignore
127 }
128 }
129
130 public void testGetChronology_Object_nullChronology() throws Exception {
131 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
132 assertEquals(GJChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null));
133
134 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
135 cal.setGregorianChange(new Date(0L));
136 assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null));
137
138 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
139 cal.setGregorianChange(new Date(Long.MAX_VALUE));
140 assertEquals(JulianChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null));
141
142 cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
143 cal.setGregorianChange(new Date(Long.MIN_VALUE));
144 assertEquals(GregorianChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null));
145
146 cal = new GregorianCalendar(new MockUnknownTimeZone());
147 assertEquals(GJChronology.getInstance(), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null));
148
149 Calendar uc = new MockUnknownCalendar(TimeZone.getTimeZone("Europe/Moscow"));
150 assertEquals(ISOChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(uc, (Chronology) null));
151
152 try {
153 Calendar bc = (Calendar) Class.forName("sun.util.BuddhistCalendar").newInstance();
154 bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
155 assertEquals(BuddhistChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(bc, (Chronology) null));
156 } catch (ClassNotFoundException ex) {
157 // ignore
158 }
159 }
160
161 public void testGetChronology_Object_Chronology() throws Exception {
162 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
163 assertEquals(JULIAN, CalendarConverter.INSTANCE.getChronology(cal, JULIAN));
164 }
165
166 //-----------------------------------------------------------------------
167 public void testGetPartialValues() throws Exception {
168 GregorianCalendar cal = new GregorianCalendar();
169 cal.setTime(new Date(12345678L));
170 TimeOfDay tod = new TimeOfDay();
171 int[] expected = ISO.get(tod, 12345678L);
172 int[] actual = CalendarConverter.INSTANCE.getPartialValues(tod, cal, ISO);
173 assertEquals(true, Arrays.equals(expected, actual));
174 }
175
176 //-----------------------------------------------------------------------
177 public void testToString() {
178 assertEquals("Converter[java.util.Calendar]", CalendarConverter.INSTANCE.toString());
179 }
180
181 }