2 *******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.calendar;
10 import java.util.Iterator;
12 import com.ibm.icu.dev.test.ModuleTest;
13 import com.ibm.icu.dev.test.TestDataModule;
14 import com.ibm.icu.dev.test.TestDataModule.DataMap;
15 import com.ibm.icu.dev.test.util.CalendarFieldsSet;
16 import com.ibm.icu.text.DateFormat;
17 import com.ibm.icu.text.SimpleDateFormat;
18 import com.ibm.icu.util.Calendar;
19 import com.ibm.icu.util.TimeZone;
20 import com.ibm.icu.util.ULocale;
25 * analog of dadrcal.cpp
28 public class DataDrivenCalendarTest extends ModuleTest {
30 public DataDrivenCalendarTest() {
31 super("com/ibm/icu/dev/data/testdata/", "calendar");
35 * @see com.ibm.icu.dev.test.ModuleTest#processModules()
37 public void processModules() {
38 //String testName = t.getName().toString();
40 for (Iterator siter = t.getSettingsIterator(); siter.hasNext();) {
41 // Iterate through and get each of the test case to process
42 DataMap settings = (DataMap) siter.next();
44 String type = settings.getString("Type");
46 if(type.equals("convert_fwd")) {
47 testConvert(t, settings, true);
48 } else if(type.equals("convert_rev")) {
49 testConvert(t, settings, false);
50 } else if(type.equals("ops")) {
53 errln("Unknown type: " + type);
59 void testConvert(String caseString,
60 CalendarFieldsSet fromSet, Calendar fromCalendar,
61 CalendarFieldsSet toSet, Calendar toCalendar, boolean forward) {
62 String thisString = caseString+(forward ? "forward"
63 : "reverse")+" "+fromCalendar.getType()+"->"+toCalendar.getType()+" ";
67 fromSet.setOnCalendar(fromCalendar);
69 CalendarFieldsSet diffSet = new CalendarFieldsSet();
72 // Is the calendar sane at the first?
73 if (!fromSet.matches(fromCalendar, diffSet)) {
74 String diffs = diffSet.diffFrom(fromSet);
75 errln((String)"FAIL: "+thisString
76 +", SOURCE calendar was not set: Differences: "+ diffs);
78 logln("PASS: "+thisString+" SOURCE calendar match.");
81 //logln("Set Source calendar: " + from);
83 Date fromTime = fromCalendar.getTime();
86 // Is the calendar sane after being set?
87 if (!fromSet.matches(fromCalendar, diffSet)) {
88 String diffs = diffSet.diffFrom(fromSet);
89 errln((String)"FAIL: "+thisString
90 +", SET SOURCE calendar was not set: Differences: "+ diffs);
92 logln("PASS: "+thisString+" SET SOURCE calendar match.");
96 toCalendar.setTime(fromTime);
99 if (!toSet.matches(toCalendar, diffSet)) {
100 String diffs = diffSet.diffFrom(toSet);
101 errln((String)"FAIL: "+thisString+", Differences: "+ diffs);
102 DateFormat fmt = new SimpleDateFormat(new String("EEE MMM dd yyyy G"));
103 String fromString = fmt.format(fromTime);
104 logln("Source Time: "+fromString+", Source Calendar: "
105 +fromCalendar.getType());
107 logln("PASS: "+thisString+" match.");
113 private void testConvert(TestDataModule.TestData testData, DataMap settings, boolean forward) {
114 Calendar toCalendar= null;
116 String testSetting = settings.getString("ToCalendar");
117 ULocale loc = new ULocale(testSetting);
118 toCalendar = Calendar.getInstance(loc);
119 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet();
120 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
121 // Start the processing
123 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
125 DataMap currentCase = (DataMap) iter.next();
127 String caseString = "["+testData.getName()+"#"+n+" "+"]";
128 String locale = testSetting = currentCase.getString("locale");
129 ULocale fromLoc = new ULocale(testSetting);
130 Calendar fromCalendar = Calendar.getInstance(fromLoc);
135 String from = currentCase.getString("from");
136 fromSet.parseFrom(from);
137 String to = currentCase.getString("to");
138 toSet.parseFrom(to, fromSet);
142 logln(caseString +" "+locale+"/"+from+" >>> "+loc+"/"
144 testConvert(caseString, fromSet, fromCalendar, toSet, toCalendar, forward);
146 logln(caseString +" "+locale+"/"+from+" <<< "+loc+"/"
148 testConvert(caseString, toSet, toCalendar, fromSet, fromCalendar, forward);
153 private static final String kADD = "add";
154 private static final String kROLL = "roll";
155 private static final String kMILLIS = "MILLIS=";
157 private void testOps(TestDataModule.TestData testData, DataMap settings) {
159 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(), paramsSet = new CalendarFieldsSet(), diffSet = new CalendarFieldsSet();
160 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
161 // Start the processing
166 boolean useDate = false;
168 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
170 DataMap currentCase = (DataMap) iter.next();
172 String caseString = "[case "+n+"]";
174 // Headers { "locale","from","operation","params","to" }
176 String param = "locale";
178 String testSetting = currentCase.getString(param);
179 locale = testSetting;
180 ULocale loc = new ULocale(locale);
181 Calendar fromCalendar = Calendar.getInstance(loc);
186 String from = testSetting=currentCase.getString(param);
187 if(from.startsWith(kMILLIS)){
189 fromDate = Long.parseLong(from.substring(kMILLIS.length()));
191 fromSet.parseFrom(testSetting);
193 // System.err.println("fromset: ["+testSetting+"] >> " + fromSet);
195 // #4 'operation' info
197 String operation = testSetting=currentCase.getString(param);
201 String paramsData = testSetting = currentCase.getString(param);
202 paramsSet.parseFrom(paramsData); // parse with inheritance.
203 // System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet);
208 String to = testSetting=currentCase.getString(param);
209 if(to.startsWith(kMILLIS)){
211 toDate = Long.parseLong(to.substring(kMILLIS.length()));
213 toSet.parseFrom(testSetting, fromSet);
215 //toSet.parseFrom(testSetting, fromSet); // parse with inheritance.
216 // System.err.println("toSet: ["+testSetting+"] >> " + toSet);
218 String caseContentsString = locale+": from "+from+": "
219 +operation +" [[[ "+paramsSet+" ]]] >>> "+to;
220 logln(caseString+": "+caseContentsString);
227 fromCalendar.setTimeInMillis(fromDate);
229 fromSet.setOnCalendar(fromCalendar);
232 // from calendar: 'starting date'
236 // Is the calendar sane after being set?
237 if (!fromSet.matches(fromCalendar, diffSet)) {
238 String diffs = diffSet.diffFrom(fromSet);
239 errln((String)"FAIL: "+caseString
240 +", SET SOURCE calendar was not set: Differences: "+ diffs);
242 logln(" "+caseString+" SET SOURCE calendar match."); // verifies that the requested fields were set.
245 // to calendar - copy of from calendar
246 Calendar toCalendar = (Calendar)fromCalendar.clone();
248 /// perform op on 'to calendar'
249 for (int q=0; q<paramsSet.fieldCount(); q++) {
250 if (paramsSet.isSet(q)) {
251 if (operation.equals(kROLL)) {
254 } else if (operation.equals(kADD)) {
258 errln(caseString+ " FAIL: unknown operation "+ operation);
260 logln(operation + " of "+ paramsSet.get(q));
263 // now - what's the result?
266 // toset contains 'expected'
269 if(toCalendar.getTimeInMillis()==toDate) {
270 logln(caseString + " SUCCESS: got=expected="+toDate);
271 logln("PASS: "+caseString+" matched! ");
273 // Note: With JDK TimeZone implementation, tz offset on dates earlier than
274 // mid-1900 might be different from the TZDB. Following test cases are
275 // failing because of this.
276 if ((caseString.equals("[case 31]") || caseString.equals("[case 36]"))
277 && TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
278 logln(caseString + " FAIL(expected): got " +
279 toCalendar.getTimeInMillis() + " expected " +
282 errln(caseString + " FAIL: got " +
283 toCalendar.getTimeInMillis() + " expected " +
287 }else if (!toSet.matches(toCalendar, diffSet)) {
288 String diffs = diffSet.diffFrom(toSet);
289 errln((String)"FAIL: "+caseString+" - , "+caseContentsString
290 +" Differences: "+ diffs );
292 logln("PASS: "+caseString+" matched! ");
303 public static void main(String[] args) throws Exception {
304 new DataDrivenCalendarTest().run(args);