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 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 import org.joda.time.Chronology; 27 import org.joda.time.DateTimeField; 28 import org.joda.time.DateTimeZone; 29 import org.joda.time.ReadablePartial; 30 import org.joda.time.TimeOfDay; 31 import org.joda.time.YearMonthDay; 32 import org.joda.time.base.BasePartial; 33 import org.joda.time.chrono.BuddhistChronology; 34 import org.joda.time.chrono.ISOChronology; 35 import org.joda.time.chrono.JulianChronology; 36 37 42 public class TestReadablePartialConverter extends TestCase { 43 44 private static final DateTimeZone UTC = DateTimeZone.UTC; 45 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 46 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 47 private static Chronology JULIAN; 48 private static Chronology ISO; 49 private static Chronology BUDDHIST; 50 51 private DateTimeZone zone = null; 52 53 public static void main(String [] args) { 54 junit.textui.TestRunner.run(suite()); 55 } 56 57 public static TestSuite suite() { 58 return new TestSuite(TestReadablePartialConverter.class); 59 } 60 61 public TestReadablePartialConverter(String name) { 62 super(name); 63 } 64 65 protected void setUp() throws Exception { 66 JULIAN = JulianChronology.getInstance(); 67 ISO = ISOChronology.getInstance(); 68 BUDDHIST = BuddhistChronology.getInstance(); 69 } 70 71 public void testSingleton() throws Exception { 73 Class cls = ReadablePartialConverter.class; 74 assertEquals(false, Modifier.isPublic(cls.getModifiers())); 75 assertEquals(false, Modifier.isProtected(cls.getModifiers())); 76 assertEquals(false, Modifier.isPrivate(cls.getModifiers())); 77 78 Constructor con = cls.getDeclaredConstructor((Class []) null); 79 assertEquals(1, cls.getDeclaredConstructors().length); 80 assertEquals(true, Modifier.isProtected(con.getModifiers())); 81 82 Field fld = cls.getDeclaredField("INSTANCE"); 83 assertEquals(false, Modifier.isPublic(fld.getModifiers())); 84 assertEquals(false, Modifier.isProtected(fld.getModifiers())); 85 assertEquals(false, Modifier.isPrivate(fld.getModifiers())); 86 } 87 88 public void testSupportedType() throws Exception { 90 assertEquals(ReadablePartial.class, ReadablePartialConverter.INSTANCE.getSupportedType()); 91 } 92 93 public void testGetChronology_Object_Zone() throws Exception { 95 assertEquals(ISO_PARIS, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), PARIS)); 96 assertEquals(ISO, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), DateTimeZone.getDefault())); 97 assertEquals(ISO, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), (DateTimeZone) null)); 98 } 99 100 public void testGetChronology_Object_Chronology() throws Exception { 101 assertEquals(JULIAN, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L, BUDDHIST), JULIAN)); 102 assertEquals(JULIAN, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), JULIAN)); 103 assertEquals(BUDDHIST.withUTC(), ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L, BUDDHIST), (Chronology) null)); 104 } 105 106 public void testGetPartialValues() throws Exception { 108 TimeOfDay tod = new TimeOfDay(); 109 int[] expected = new int[] {1, 2, 3, 4}; 110 int[] actual = ReadablePartialConverter.INSTANCE.getPartialValues(tod, new TimeOfDay(1, 2, 3, 4), ISOChronology.getInstance(PARIS)); 111 assertEquals(true, Arrays.equals(expected, actual)); 112 113 try { 114 ReadablePartialConverter.INSTANCE.getPartialValues(tod, new YearMonthDay(2005, 6, 9), JULIAN); 115 fail(); 116 } catch (IllegalArgumentException ex) {} 117 try { 118 ReadablePartialConverter.INSTANCE.getPartialValues(tod, new MockTOD(), JULIAN); 119 fail(); 120 } catch (IllegalArgumentException ex) {} 121 } 122 123 static class MockTOD extends BasePartial { 124 protected DateTimeField getField(int index, Chronology chrono) { 125 switch (index) { 126 case 0: 127 return chrono.hourOfDay(); 128 case 1: 129 return chrono.minuteOfHour(); 130 case 2: 131 return chrono.year(); 132 case 3: 133 return chrono.era(); 134 } 135 return null; 136 } 137 public int size() { 138 return 4; 139 } 140 } 141 142 public void testToString() { 144 assertEquals("Converter[org.joda.time.ReadablePartial]", ReadablePartialConverter.INSTANCE.toString()); 145 } 146 147 } 148 | Popular Tags |