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