1 16 package org.joda.time.convert; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Modifier ; 21 import java.util.Arrays ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.GregorianCalendar ; 25 import java.util.TimeZone ; 26 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.joda.time.Chronology; 31 import org.joda.time.DateTimeZone; 32 import org.joda.time.TimeOfDay; 33 import org.joda.time.chrono.BuddhistChronology; 34 import org.joda.time.chrono.GJChronology; 35 import org.joda.time.chrono.GregorianChronology; 36 import org.joda.time.chrono.ISOChronology; 37 import org.joda.time.chrono.JulianChronology; 38 39 44 public class TestCalendarConverter extends TestCase { 45 46 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 47 private static final DateTimeZone MOSCOW = DateTimeZone.forID("Europe/Moscow"); 48 private static Chronology JULIAN; 49 private static Chronology ISO; 50 51 public static void main(String [] args) { 52 junit.textui.TestRunner.run(suite()); 53 } 54 55 public static TestSuite suite() { 56 return new TestSuite(TestCalendarConverter.class); 57 } 58 59 public TestCalendarConverter(String name) { 60 super(name); 61 } 62 63 protected void setUp() throws Exception { 64 JULIAN = JulianChronology.getInstance(); 65 ISO = ISOChronology.getInstance(); 66 } 67 68 public void testSingleton() throws Exception { 70 Class cls = CalendarConverter.class; 71 assertEquals(false, Modifier.isPublic(cls.getModifiers())); 72 assertEquals(false, Modifier.isProtected(cls.getModifiers())); 73 assertEquals(false, Modifier.isPrivate(cls.getModifiers())); 74 75 Constructor con = cls.getDeclaredConstructor((Class []) null); 76 assertEquals(1, cls.getDeclaredConstructors().length); 77 assertEquals(true, Modifier.isProtected(con.getModifiers())); 78 79 Field fld = cls.getDeclaredField("INSTANCE"); 80 assertEquals(false, Modifier.isPublic(fld.getModifiers())); 81 assertEquals(false, Modifier.isProtected(fld.getModifiers())); 82 assertEquals(false, Modifier.isPrivate(fld.getModifiers())); 83 } 84 85 public void testSupportedType() throws Exception { 87 assertEquals(Calendar .class, CalendarConverter.INSTANCE.getSupportedType()); 88 } 89 90 public void testGetInstantMillis_Object_Chronology() throws Exception { 92 GregorianCalendar cal = new GregorianCalendar (); 93 cal.setTime(new Date (123L)); 94 assertEquals(123L, CalendarConverter.INSTANCE.getInstantMillis(cal, JULIAN)); 95 assertEquals(123L, cal.getTime().getTime()); 96 } 97 98 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 } 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 } 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 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 public void testToString() { 178 assertEquals("Converter[java.util.Calendar]", CalendarConverter.INSTANCE.toString()); 179 } 180 181 } 182 | Popular Tags |