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) 2007-2013, 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.Iterator; 13 import java.util.List; 14 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 import com.ibm.icu.dev.test.ModuleTest; 19 import com.ibm.icu.dev.test.ModuleTest.TestDataPair; 20 import com.ibm.icu.dev.test.TestDataModule; 21 import com.ibm.icu.dev.test.TestDataModule.DataMap; 22 import com.ibm.icu.dev.test.TestDataModule.TestData; 23 import com.ibm.icu.dev.test.TestFmwk; 24 import com.ibm.icu.dev.test.util.CalendarFieldsSet; 25 import com.ibm.icu.text.DateFormat; 26 import com.ibm.icu.text.SimpleDateFormat; 27 import com.ibm.icu.util.Calendar; 28 import com.ibm.icu.util.TimeZone; 29 import com.ibm.icu.util.ULocale; 30 31 import junitparams.JUnitParamsRunner; 32 import junitparams.Parameters; 33 34 /** 35 * @author srl 36 * @author sgill 37 * 38 * analog of dadrcal.cpp 39 * 40 */ 41 @RunWith(JUnitParamsRunner.class) 42 public class DataDrivenCalendarTest extends TestFmwk { 43 44 public DataDrivenCalendarTest() { 45 //super("com/ibm/icu/dev/data/testdata/", "calendar"); 46 } 47 48 @SuppressWarnings("unused") 49 private List<TestDataPair> getTestData() throws Exception { 50 return ModuleTest.getTestData("com/ibm/icu/dev/data/testdata/", "calendar"); 51 } 52 53 /* (non-Javadoc) 54 * @see com.ibm.icu.dev.test.ModuleTest#processModules() 55 */ 56 @Test 57 @Parameters(method="getTestData") 58 public void calendarTest(TestDataPair pair) { 59 TestData td = pair.td; 60 DataMap settings = pair.dm; 61 62 String type = settings.getString("Type"); 63 64 if(type.equals("convert_fwd")) { 65 testConvert(td, settings, true); 66 } else if(type.equals("convert_rev")) { 67 testConvert(td, settings, false); 68 } else if(type.equals("ops")) { 69 testOps(td, settings); 70 } else { 71 errln("Unknown type: " + type); 72 } 73 } 74 75 76 void testConvert(String caseString, 77 CalendarFieldsSet fromSet, Calendar fromCalendar, 78 CalendarFieldsSet toSet, Calendar toCalendar, boolean forward) { 79 String thisString = caseString+(forward ? "forward" 80 : "reverse")+" "+fromCalendar.getType()+"->"+toCalendar.getType()+" "; 81 82 fromCalendar.clear(); 83 84 fromSet.setOnCalendar(fromCalendar); 85 86 CalendarFieldsSet diffSet = new CalendarFieldsSet(); 87 88 diffSet.clear(); 89 // Is the calendar sane at the first? 90 if (!fromSet.matches(fromCalendar, diffSet)) { 91 String diffs = diffSet.diffFrom(fromSet); 92 errln((String)"FAIL: "+thisString 93 +", SOURCE calendar was not set: Differences: "+ diffs); 94 } else { 95 logln("PASS: "+thisString+" SOURCE calendar match."); 96 } 97 98 //logln("Set Source calendar: " + from); 99 100 Date fromTime = fromCalendar.getTime(); 101 102 diffSet.clear(); 103 // Is the calendar sane after being set? 104 if (!fromSet.matches(fromCalendar, diffSet)) { 105 String diffs = diffSet.diffFrom(fromSet); 106 errln((String)"FAIL: "+thisString 107 +", SET SOURCE calendar was not set: Differences: "+ diffs); 108 } else { 109 logln("PASS: "+thisString+" SET SOURCE calendar match."); 110 } 111 112 toCalendar.clear(); 113 toCalendar.setTime(fromTime); 114 115 diffSet.clear(); 116 if (!toSet.matches(toCalendar, diffSet)) { 117 String diffs = diffSet.diffFrom(toSet); 118 errln((String)"FAIL: "+thisString+", Differences: "+ diffs); 119 DateFormat fmt = new SimpleDateFormat(new String("EEE MMM dd yyyy G")); 120 String fromString = fmt.format(fromTime); 121 logln("Source Time: "+fromString+", Source Calendar: " 122 +fromCalendar.getType()); 123 } else { 124 logln("PASS: "+thisString+" match."); 125 } 126 } 127 128 private void testConvert(TestDataModule.TestData testData, DataMap settings, boolean forward) { 129 Calendar toCalendar= null; 130 // build to calendar 131 String testSetting = settings.getString("ToCalendar"); 132 ULocale loc = new ULocale(testSetting); 133 toCalendar = Calendar.getInstance(loc); 134 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(); 135 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee"); 136 // Start the processing 137 int n = 0; 138 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) { 139 ++n; 140 DataMap currentCase = (DataMap) iter.next(); 141 142 String caseString = "["+testData.getName()+"#"+n+" "+"]"; 143 String locale = testSetting = currentCase.getString("locale"); 144 ULocale fromLoc = new ULocale(testSetting); 145 Calendar fromCalendar = Calendar.getInstance(fromLoc); 146 147 fromSet.clear(); 148 toSet.clear(); 149 150 String from = currentCase.getString("from"); 151 fromSet.parseFrom(from); 152 String to = currentCase.getString("to"); 153 toSet.parseFrom(to, fromSet); 154 155 // now, do it. 156 if (forward) { 157 logln(caseString +" "+locale+"/"+from+" >>> "+loc+"/" 158 +to); 159 testConvert(caseString, fromSet, fromCalendar, toSet, toCalendar, forward); 160 } else { 161 logln(caseString +" "+locale+"/"+from+" <<< "+loc+"/" 162 +to); 163 testConvert(caseString, toSet, toCalendar, fromSet, fromCalendar, forward); 164 } 165 } 166 } 167 168 private static final String kADD = "add"; 169 private static final String kROLL = "roll"; 170 private static final String kMILLIS = "MILLIS="; 171 172 private void testOps(TestDataModule.TestData testData, DataMap settings) { 173 // Get 'from' time 174 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(), paramsSet = new CalendarFieldsSet(), diffSet = new CalendarFieldsSet(); 175 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee"); 176 // Start the processing 177 int n = 0; 178 long fromDate = 0; 179 long toDate = 0; 180 181 boolean useDate = false; 182 183 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) { 184 ++n; 185 DataMap currentCase = (DataMap) iter.next(); 186 187 String caseString = "[case "+n+"]"; 188 // build to calendar 189 // Headers { "locale","from","operation","params","to" } 190 // #1 locale 191 String param = "locale"; 192 String locale; 193 String testSetting = currentCase.getString(param); 194 locale = testSetting; 195 ULocale loc = new ULocale(locale); 196 Calendar fromCalendar = Calendar.getInstance(loc); 197 198 fromSet.clear(); 199 // #2 'from' info 200 param = "from"; 201 String from = testSetting=currentCase.getString(param); 202 if(from.startsWith(kMILLIS)){ 203 useDate = true; 204 fromDate = Long.parseLong(from.substring(kMILLIS.length())); 205 }else{ 206 fromSet.parseFrom(testSetting); 207 } 208 // System.err.println("fromset: ["+testSetting+"] >> " + fromSet); 209 210 // #4 'operation' info 211 param = "operation"; 212 String operation = testSetting=currentCase.getString(param); 213 paramsSet.clear(); 214 // #3 'params' info 215 param = "params"; 216 String paramsData = testSetting = currentCase.getString(param); 217 paramsSet.parseFrom(paramsData); // parse with inheritance. 218 // System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet); 219 220 toSet.clear(); 221 // #4 'to' info 222 param = "to"; 223 String to = testSetting=currentCase.getString(param); 224 if(to.startsWith(kMILLIS)){ 225 useDate = true; 226 toDate = Long.parseLong(to.substring(kMILLIS.length())); 227 }else{ 228 toSet.parseFrom(testSetting, fromSet); 229 } 230 //toSet.parseFrom(testSetting, fromSet); // parse with inheritance. 231 // System.err.println("toSet: ["+testSetting+"] >> " + toSet); 232 233 String caseContentsString = locale+": from "+from+": " 234 +operation +" [[[ "+paramsSet+" ]]] >>> "+to; 235 logln(caseString+": "+caseContentsString); 236 237 // ------ 238 // now, do it. 239 240 /// prepare calendar 241 if(useDate){ 242 fromCalendar.setTimeInMillis(fromDate); 243 }else { 244 fromSet.setOnCalendar(fromCalendar); 245 } 246 247 // from calendar: 'starting date' 248 249 diffSet.clear(); 250 251 // Is the calendar sane after being set? 252 if (!fromSet.matches(fromCalendar, diffSet)) { 253 String diffs = diffSet.diffFrom(fromSet); 254 errln((String)"FAIL: "+caseString 255 +", SET SOURCE calendar was not set: Differences: "+ diffs); 256 } else { 257 logln(" "+caseString+" SET SOURCE calendar match."); // verifies that the requested fields were set. 258 } 259 260 // to calendar - copy of from calendar 261 Calendar toCalendar = (Calendar)fromCalendar.clone(); 262 263 /// perform op on 'to calendar' 264 for (int q=0; q<paramsSet.fieldCount(); q++) { 265 if (paramsSet.isSet(q)) { 266 if (operation.equals(kROLL)) { 267 toCalendar.roll(q, 268 paramsSet.get(q)); 269 } else if (operation.equals(kADD)) { 270 toCalendar.add(q, 271 paramsSet.get(q)); 272 } else { 273 errln(caseString+ " FAIL: unknown operation "+ operation); 274 } 275 logln(operation + " of "+ paramsSet.get(q)); 276 } 277 } 278 // now - what's the result? 279 diffSet.clear(); 280 281 // toset contains 'expected' 282 283 if(useDate) { 284 if(toCalendar.getTimeInMillis()==toDate) { 285 logln(caseString + " SUCCESS: got=expected="+toDate); 286 logln("PASS: "+caseString+" matched! "); 287 } else { 288 // Note: With JDK TimeZone implementation, tz offset on dates earlier than 289 // mid-1900 might be different from the TZDB. Following test cases are 290 // failing because of this. 291 if ((caseString.equals("[case 31]") || caseString.equals("[case 36]")) 292 && TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) { 293 logln(caseString + " FAIL(expected): got " + 294 toCalendar.getTimeInMillis() + " expected " + 295 toDate); 296 } else { 297 errln(caseString + " FAIL: got " + 298 toCalendar.getTimeInMillis() + " expected " + 299 toDate); 300 } 301 } 302 }else if (!toSet.matches(toCalendar, diffSet)) { 303 String diffs = diffSet.diffFrom(toSet); 304 errln((String)"FAIL: "+caseString+" - , "+caseContentsString 305 +" Differences: "+ diffs ); 306 } else{ 307 logln("PASS: "+caseString+" matched! "); 308 } 309 310 } 311 } 312 } 313