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