1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2005-2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.calendar; 10 11 import java.util.Date; 12 import java.util.Locale; 13 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.junit.runners.JUnit4; 17 18 import com.ibm.icu.impl.LocaleUtility; 19 import com.ibm.icu.text.DateFormat; 20 import com.ibm.icu.text.SimpleDateFormat; 21 import com.ibm.icu.util.Calendar; 22 import com.ibm.icu.util.GregorianCalendar; 23 import com.ibm.icu.util.IndianCalendar; 24 import com.ibm.icu.util.TimeZone; 25 import com.ibm.icu.util.ULocale; 26 27 /** 28 * Tests for the <code>IndianCalendar</code> class. 29 */ 30 @RunWith(JUnit4.class) 31 public class IndianTest extends CalendarTestFmwk 32 { 33 // Months in indian calendar are 0-based. Here taking 1-based names: 34 public static final int CHAITRA = IndianCalendar.CHAITRA + 1; 35 public static final int VAISAKHA = IndianCalendar.VAISAKHA + 1; 36 public static final int JYAISTHA = IndianCalendar.JYAISTHA + 1; 37 public static final int ASADHA = IndianCalendar.ASADHA + 1; 38 public static final int SRAVANA = IndianCalendar.SRAVANA + 1 ; 39 public static final int BHADRA = IndianCalendar.BHADRA + 1 ; 40 public static final int ASVINA = IndianCalendar.ASVINA + 1 ; 41 public static final int KARTIKA = IndianCalendar.KARTIKA + 1 ; 42 public static final int AGRAHAYANA = IndianCalendar.AGRAHAYANA + 1 ; 43 public static final int PAUSA = IndianCalendar.PAUSA + 1 ; 44 public static final int MAGHA = IndianCalendar.MAGHA + 1 ; 45 public static final int PHALGUNA = IndianCalendar.PHALGUNA + 1 ; 46 47 /** Constants to save typing. */ 48 /* Test dates generated from: 49 * http://www.fourmilab.ch/documents/calendar/ 50 51 /** A huge list of test cases to make sure that computeTime and computeFields 52 * work properly for a wide range of data in the Indian civil calendar. 53 */ 54 @Test 55 public void TestCases() 56 { 57 final TestCase[] tests = { 58 // 59 // The months in this table are 1-based rather than 0-based, 60 // because it's easier to edit that way. 61 // Indian 62 // Julian Day Era Year Month Day WkDay Hour Min Sec 63 new TestCase(1770641.5, 0, 57, ASVINA, 10, SUN, 0, 0, 0), 64 new TestCase(1892731.5, 0, 391, PAUSA, 18, WED, 0, 0, 0), 65 new TestCase(1931579.5, 0, 498, VAISAKHA, 30, MON, 0, 0, 0), 66 new TestCase(1974851.5, 0, 616, KARTIKA, 19, SAT, 0, 0, 0), 67 new TestCase(2091164.5, 0, 935, VAISAKHA, 5, SUN, 0, 0, 0), 68 new TestCase(2121509.5, 0, 1018, JYAISTHA, 3, SUN, 0, 0, 0), 69 new TestCase(2155779.5, 0, 1112, CHAITRA, 2, FRI, 0, 0, 0), 70 new TestCase(2174029.5, 0, 1161, PHALGUNA, 20, SAT, 0, 0, 0), 71 new TestCase(2191584.5, 0, 1210, CHAITRA, 13, FRI, 0, 0, 0), 72 new TestCase(2195261.5, 0, 1220, VAISAKHA, 7, SUN, 0, 0, 0), 73 new TestCase(2229274.5, 0, 1313, JYAISTHA, 22, SUN, 0, 0, 0), 74 new TestCase(2245580.5, 0, 1357, MAGHA, 14, WED, 0, 0, 0), 75 new TestCase(2266100.5, 0, 1414, CHAITRA, 20, SAT, 0, 0, 0), 76 new TestCase(2288542.5, 0, 1475, BHADRA, 28, SAT, 0, 0, 0), 77 new TestCase(2290901.5, 0, 1481, PHALGUNA, 15, SAT, 0, 0, 0), 78 new TestCase(2323140.5, 0, 1570, JYAISTHA, 20, WED, 0, 0, 0), 79 new TestCase(2334551.5, 0, 1601, BHADRA, 16, THU, 0, 0, 0), 80 new TestCase(2334581.5, 0, 1601, ASVINA, 15, SAT, 0, 0, 0), 81 new TestCase(2334610.5, 0, 1601, KARTIKA, 14, SUN, 0, 0, 0), 82 new TestCase(2334639.5, 0, 1601, AGRAHAYANA, 13, MON, 0, 0, 0), 83 new TestCase(2334668.5, 0, 1601, PAUSA, 12, TUE, 0, 0, 0), 84 new TestCase(2334698.5, 0, 1601, MAGHA, 12, THU, 0, 0, 0), 85 new TestCase(2334728.5, 0, 1601, PHALGUNA, 12, SAT, 0, 0, 0), 86 new TestCase(2334757.5, 0, 1602, CHAITRA, 11, SUN, 0, 0, 0), 87 new TestCase(2334787.5, 0, 1602, VAISAKHA, 10, TUE, 0, 0, 0), 88 new TestCase(2334816.5, 0, 1602, JYAISTHA, 8, WED, 0, 0, 0), 89 new TestCase(2334846.5, 0, 1602, ASADHA, 7, FRI, 0, 0, 0), 90 new TestCase(2334848.5, 0, 1602, ASADHA, 9, SUN, 0, 0, 0), 91 new TestCase(2348020.5, 0, 1638, SRAVANA, 2, FRI, 0, 0, 0), 92 new TestCase(2334934.5, 0, 1602, ASVINA, 2, TUE, 0, 0, 0), 93 new TestCase(2366978.5, 0, 1690, JYAISTHA, 29, SUN, 0, 0, 0), 94 new TestCase(2385648.5, 0, 1741, SRAVANA, 11, MON, 0, 0, 0), 95 new TestCase(2392825.5, 0, 1761, CHAITRA, 6, WED, 0, 0, 0), 96 new TestCase(2416223.5, 0, 1825, CHAITRA, 29, SUN, 0, 0, 0), 97 new TestCase(2425848.5, 0, 1851, BHADRA, 3, SUN, 0, 0, 0), 98 new TestCase(2430266.5, 0, 1863, ASVINA, 7, MON, 0, 0, 0), 99 new TestCase(2430833.5, 0, 1865, CHAITRA, 29, MON, 0, 0, 0), 100 new TestCase(2431004.5, 0, 1865, ASVINA, 15, THU, 0, 0, 0), 101 new TestCase(2448698.5, 0, 1913, PHALGUNA, 27, TUE, 0, 0, 0), 102 new TestCase(2450138.5, 0, 1917, PHALGUNA, 6, SUN, 0, 0, 0), 103 new TestCase(2465737.5, 0, 1960, KARTIKA, 19, WED, 0, 0, 0), 104 new TestCase(2486076.5, 0, 2016, ASADHA, 27, SUN, 0, 0, 0), 105 }; 106 107 IndianCalendar testCalendar = new IndianCalendar(); 108 testCalendar.setLenient(true); 109 doTestCases(tests, testCalendar); 110 } 111 112 @Test 113 public void TestBasic() { 114 IndianCalendar cal = new IndianCalendar(); 115 cal.clear(); 116 cal.set(1000, 0, 30); 117 logln("1000/0/30-> " + 118 cal.get(YEAR) + "/" + 119 cal.get(MONTH) + "/" + 120 cal.get(DATE)); 121 cal.clear(); 122 cal.set(1, 0, 30); 123 logln("1/0/30 -> " + 124 cal.get(YEAR) + "/" + 125 cal.get(MONTH) + "/" + 126 cal.get(DATE)); 127 } 128 129 @Test 130 public void TestCoverage() { 131 { 132 // new IndianCalendar(TimeZone) 133 IndianCalendar cal = new IndianCalendar(TimeZone.getDefault()); 134 if(cal == null){ 135 errln("could not create IndianCalendar with TimeZone"); 136 } 137 } 138 139 { 140 // new IndianCalendar(ULocale) 141 IndianCalendar cal = new IndianCalendar(ULocale.getDefault()); 142 if(cal == null){ 143 errln("could not create IndianCalendar with ULocale"); 144 } 145 } 146 147 { 148 // new IndianCalendar(Locale) 149 IndianCalendar cal = new IndianCalendar(Locale.getDefault()); 150 if(cal == null){ 151 errln("could not create IndianCalendar with Locale"); 152 } 153 } 154 155 { 156 // new IndianCalendar(TimeZone, Locale) 157 IndianCalendar cal = new IndianCalendar(TimeZone.getDefault(),Locale.getDefault()); 158 if(cal == null){ 159 errln("could not create IndianCalendar with TimeZone, Locale"); 160 } 161 } 162 163 { 164 // new IndianCalendar(TimeZone, ULocale) 165 IndianCalendar cal = new IndianCalendar(TimeZone.getDefault(),ULocale.getDefault()); 166 if(cal == null){ 167 errln("could not create IndianCalendar with TimeZone, ULocale"); 168 } 169 } 170 171 { 172 // new IndianCalendar(Date) 173 IndianCalendar cal = new IndianCalendar(new Date()); 174 if(cal == null){ 175 errln("could not create IndianCalendar with Date"); 176 } 177 } 178 { 179 // new IndianCalendar(int year, int month, int date) 180 IndianCalendar cal = new IndianCalendar(1917, IndianCalendar.CHAITRA, 1); 181 if(cal == null){ 182 errln("could not create IndianCalendar with year,month,date"); 183 } 184 } 185 { 186 // new IndianCalendar(int year, int month, int date, int hour, int minute, int second) 187 IndianCalendar cal = new IndianCalendar(1917, IndianCalendar.CHAITRA, 1, 1, 1, 1); 188 if(cal == null){ 189 errln("could not create IndianCalendar with year,month,date,hour,minute,second"); 190 } 191 } 192 193 { 194 // data 195 String[] calendarLocales = { 196 "bn_IN", "gu_IN", "hi_IN", "kn_IN", "ml_IN", "or_IN", "pa_IN", "ta_IN", "te_IN" 197 }; 198 199 String[] formatLocales = { 200 "en", "fr", "bn", "gu", "hi", "kn", "ml", "or", "pa", "ta", "te" 201 }; 202 203 for (int i = 0; i < calendarLocales.length; ++i) { 204 String calLocName = calendarLocales[i]; 205 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName); 206 IndianCalendar cal = new IndianCalendar(calLocale); 207 cal.set(-1039, 9, 21); 208 209 for (int j = 0; j < formatLocales.length; j++ ) { 210 String locName = formatLocales[j]; 211 Locale formatLocale = LocaleUtility.getLocaleFromName(locName); 212 DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale); 213 logln(calLocName + "/" + locName + " --> " + format.format(cal)); 214 } 215 } 216 } 217 } 218 219 @Test 220 public void TestYear() { 221 // Gregorian Calendar 222 Calendar gCal= new GregorianCalendar(); 223 Date gToday=gCal.getTime(); 224 gCal.add(GregorianCalendar.MONTH,2); 225 Date gFuture=gCal.getTime(); 226 DateFormat gDF = DateFormat.getDateInstance(gCal,DateFormat.FULL); 227 logln("gregorian calendar: " + gDF.format(gToday) + 228 " + 2 months = " + gDF.format(gFuture)); 229 230 // Indian Calendar 231 IndianCalendar iCal= new IndianCalendar(); 232 Date iToday=iCal.getTime(); 233 iCal.add(IndianCalendar.MONTH,2); 234 Date iFuture=iCal.getTime(); 235 DateFormat iDF = DateFormat.getDateInstance(iCal,DateFormat.FULL); 236 logln("Indian calendar: " + iDF.format(iToday) + 237 " + 2 months = " + iDF.format(iFuture)); 238 239 } 240 241 /** 242 * Test limits of the Indian calendar 243 */ 244 @Test 245 public void TestLimits() { 246 Calendar cal = Calendar.getInstance(); 247 cal.set(2007, Calendar.JANUARY, 1); 248 IndianCalendar indian = new IndianCalendar(); 249 doLimitsTest(indian, null, cal.getTime()); 250 doTheoreticalLimitsTest(indian, true); 251 } 252 253 /** 254 * Problem reported by Bruno Haible <bruno.haible (at) de.ibm.com> 255 * -- see ticket 8419 -- http://bugs.icu-project.org/trac/ticket/8419 256 * Problem with months out of range 0-11 257 */ 258 @Test 259 public void TestYearEdge() { 260 // Display dates in ISO 8601 format. 261 DateFormat fmt = new SimpleDateFormat("YYYY-MM-dd", ULocale.US); 262 263 // Instantiate an Indian calendar. 264 ULocale locale = ULocale.US.setKeywordValue("calendar", "indian"); 265 Calendar cal = Calendar.getInstance(locale); 266 267 // Try add() repeatedly. 268 cal.setTimeInMillis(1295568000000L); 269 if (!fmt.format(cal.getTime()).equals("2011-01-20")){ 270 errln("Incorrect calendar value for year edge test"); 271 } 272 cal.add(Calendar.MONTH, 1); 273 if (!fmt.format(cal.getTime()).equals("2011-02-19")){ 274 errln("Incorrect calendar value for year edge test"); 275 } 276 cal.add(Calendar.MONTH, 1); 277 if (!fmt.format(cal.getTime()).equals("2011-03-21")){ 278 errln("Incorrect calendar value for year edge test"); 279 } 280 cal.add(Calendar.MONTH, 1); 281 if (!fmt.format(cal.getTime()).equals("2011-04-20")){ 282 errln("Incorrect calendar value for year edge test"); 283 } 284 } 285 286 @Test 287 public void TestCoverage12424() { 288 class StubCalendar extends IndianCalendar { 289 private static final long serialVersionUID = 1L; 290 public StubCalendar() { 291 assertEquals("Indian month 0 length", 30, handleGetMonthLength(1000, 0)); 292 assertEquals("Indian month 2 length", 31, handleGetMonthLength(1000, 2)); 293 } 294 } 295 296 new StubCalendar(); 297 } 298 } 299