2 *******************************************************************************
\r
3 * Copyright (C) 2007-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.calendar;
\r
9 import java.util.Date;
\r
10 import java.util.Iterator;
\r
12 import com.ibm.icu.dev.test.ModuleTest;
\r
13 import com.ibm.icu.dev.test.TestDataModule;
\r
14 import com.ibm.icu.dev.test.TestDataModule.DataMap;
\r
15 import com.ibm.icu.dev.test.util.CalendarFieldsSet;
\r
16 import com.ibm.icu.text.DateFormat;
\r
17 import com.ibm.icu.text.SimpleDateFormat;
\r
18 import com.ibm.icu.util.Calendar;
\r
19 import com.ibm.icu.util.ULocale;
\r
24 * analog of dadrcal.cpp
\r
27 public class DataDrivenCalendarTest extends ModuleTest {
\r
29 public DataDrivenCalendarTest() {
\r
30 super("com/ibm/icu/dev/data/testdata/", "calendar");
\r
34 * @see com.ibm.icu.dev.test.ModuleTest#processModules()
\r
36 public void processModules() {
\r
37 //String testName = t.getName().toString();
\r
39 for (Iterator siter = t.getSettingsIterator(); siter.hasNext();) {
\r
40 // Iterate through and get each of the test case to process
\r
41 DataMap settings = (DataMap) siter.next();
\r
43 String type = settings.getString("Type");
\r
45 if(type.equals("convert_fwd")) {
\r
46 testConvert(t, settings, true);
\r
47 } else if(type.equals("convert_rev")) {
\r
48 testConvert(t, settings, false);
\r
49 } else if(type.equals("ops")) {
\r
50 testOps(t, settings);
\r
52 errln("Unknown type: " + type);
\r
58 void testConvert(String caseString,
\r
59 CalendarFieldsSet fromSet, Calendar fromCalendar,
\r
60 CalendarFieldsSet toSet, Calendar toCalendar, boolean forward) {
\r
61 String thisString = caseString+(forward ? "forward"
\r
62 : "reverse")+" "+fromCalendar.getType()+"->"+toCalendar.getType()+" ";
\r
64 fromCalendar.clear();
\r
66 fromSet.setOnCalendar(fromCalendar);
\r
68 CalendarFieldsSet diffSet = new CalendarFieldsSet();
\r
71 // Is the calendar sane at the first?
\r
72 if (!fromSet.matches(fromCalendar, diffSet)) {
\r
73 String diffs = diffSet.diffFrom(fromSet);
\r
74 errln((String)"FAIL: "+thisString
\r
75 +", SOURCE calendar was not set: Differences: "+ diffs);
\r
77 logln("PASS: "+thisString+" SOURCE calendar match.");
\r
80 //logln("Set Source calendar: " + from);
\r
82 Date fromTime = fromCalendar.getTime();
\r
85 // Is the calendar sane after being set?
\r
86 if (!fromSet.matches(fromCalendar, diffSet)) {
\r
87 String diffs = diffSet.diffFrom(fromSet);
\r
88 errln((String)"FAIL: "+thisString
\r
89 +", SET SOURCE calendar was not set: Differences: "+ diffs);
\r
91 logln("PASS: "+thisString+" SET SOURCE calendar match.");
\r
95 toCalendar.setTime(fromTime);
\r
98 if (!toSet.matches(toCalendar, diffSet)) {
\r
99 String diffs = diffSet.diffFrom(toSet);
\r
100 errln((String)"FAIL: "+thisString+", Differences: "+ diffs);
\r
101 DateFormat fmt = new SimpleDateFormat(new String("EEE MMM dd yyyy G"));
\r
102 String fromString = fmt.format(fromTime);
\r
103 logln("Source Time: "+fromString+", Source Calendar: "
\r
104 +fromCalendar.getType());
\r
106 logln("PASS: "+thisString+" match.");
\r
112 private void testConvert(TestDataModule.TestData testData, DataMap settings, boolean forward) {
\r
113 Calendar toCalendar= null;
\r
114 // build to calendar
\r
115 String testSetting = settings.getString("ToCalendar");
\r
116 ULocale loc = new ULocale(testSetting);
\r
117 toCalendar = Calendar.getInstance(loc);
\r
118 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet();
\r
119 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
\r
120 // Start the processing
\r
122 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
\r
124 DataMap currentCase = (DataMap) iter.next();
\r
126 String caseString = "["+testData.getName()+"#"+n+" "+"]";
\r
127 String locale = testSetting = currentCase.getString("locale");
\r
128 ULocale fromLoc = new ULocale(testSetting);
\r
129 Calendar fromCalendar = Calendar.getInstance(fromLoc);
\r
134 String from = currentCase.getString("from");
\r
135 fromSet.parseFrom(from);
\r
136 String to = currentCase.getString("to");
\r
137 toSet.parseFrom(to, fromSet);
\r
141 logln(caseString +" "+locale+"/"+from+" >>> "+loc+"/"
\r
143 testConvert(caseString, fromSet, fromCalendar, toSet, toCalendar, forward);
\r
145 logln(caseString +" "+locale+"/"+from+" <<< "+loc+"/"
\r
147 testConvert(caseString, toSet, toCalendar, fromSet, fromCalendar, forward);
\r
152 private static final String kADD = "add";
\r
153 private static final String kROLL = "roll";
\r
154 private static final String kMILLIS = "MILLIS=";
\r
156 private void testOps(TestDataModule.TestData testData, DataMap settings) {
\r
157 // Get 'from' time
\r
158 CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(), paramsSet = new CalendarFieldsSet(), diffSet = new CalendarFieldsSet();
\r
159 // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
\r
160 // Start the processing
\r
165 boolean useDate = false;
\r
167 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
\r
169 DataMap currentCase = (DataMap) iter.next();
\r
171 String caseString = "[case "+n+"]";
\r
172 // build to calendar
\r
173 // Headers { "locale","from","operation","params","to" }
\r
175 String param = "locale";
\r
177 String testSetting = currentCase.getString(param);
\r
178 locale = testSetting;
\r
179 ULocale loc = new ULocale(locale);
\r
180 Calendar fromCalendar = Calendar.getInstance(loc);
\r
185 String from = testSetting=currentCase.getString(param);
\r
186 if(from.startsWith(kMILLIS)){
\r
188 fromDate = Long.parseLong(from.substring(kMILLIS.length()));
\r
190 fromSet.parseFrom(testSetting);
\r
192 // System.err.println("fromset: ["+testSetting+"] >> " + fromSet);
\r
194 // #4 'operation' info
\r
195 param = "operation";
\r
196 String operation = testSetting=currentCase.getString(param);
\r
198 // #3 'params' info
\r
200 String paramsData = testSetting = currentCase.getString(param);
\r
201 paramsSet.parseFrom(paramsData); // parse with inheritance.
\r
202 // System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet);
\r
207 String to = testSetting=currentCase.getString(param);
\r
208 if(to.startsWith(kMILLIS)){
\r
210 toDate = Long.parseLong(to.substring(kMILLIS.length()));
\r
212 toSet.parseFrom(testSetting, fromSet);
\r
214 //toSet.parseFrom(testSetting, fromSet); // parse with inheritance.
\r
215 // System.err.println("toSet: ["+testSetting+"] >> " + toSet);
\r
217 String caseContentsString = locale+": from "+from+": "
\r
218 +operation +" [[[ "+paramsSet+" ]]] >>> "+to;
\r
219 logln(caseString+": "+caseContentsString);
\r
224 /// prepare calendar
\r
226 fromCalendar.setTimeInMillis(fromDate);
\r
228 fromSet.setOnCalendar(fromCalendar);
\r
231 // from calendar: 'starting date'
\r
235 // Is the calendar sane after being set?
\r
236 if (!fromSet.matches(fromCalendar, diffSet)) {
\r
237 String diffs = diffSet.diffFrom(fromSet);
\r
238 errln((String)"FAIL: "+caseString
\r
239 +", SET SOURCE calendar was not set: Differences: "+ diffs);
\r
241 logln(" "+caseString+" SET SOURCE calendar match."); // verifies that the requested fields were set.
\r
244 // to calendar - copy of from calendar
\r
245 Calendar toCalendar = (Calendar)fromCalendar.clone();
\r
247 /// perform op on 'to calendar'
\r
248 for (int q=0; q<paramsSet.fieldCount(); q++) {
\r
249 if (paramsSet.isSet(q)) {
\r
250 if (operation.equals(kROLL)) {
\r
253 } else if (operation.equals(kADD)) {
\r
257 errln(caseString+ " FAIL: unknown operation "+ operation);
\r
259 logln(operation + " of "+ paramsSet.get(q));
\r
262 // now - what's the result?
\r
265 // toset contains 'expected'
\r
268 if(toCalendar.getTimeInMillis()==toDate) {
\r
269 logln(caseString + " SUCCESS: got=expected="+toDate);
\r
270 logln("PASS: "+caseString+" matched! ");
\r
272 errln(caseString + " FAIL: got " +
\r
273 toCalendar.getTimeInMillis() + " expected " +
\r
276 }else if (!toSet.matches(toCalendar, diffSet)) {
\r
277 String diffs = diffSet.diffFrom(toSet);
\r
278 errln((String)"FAIL: "+caseString+" - , "+caseContentsString
\r
279 +" Differences: "+ diffs );
\r
281 logln("PASS: "+caseString+" matched! ");
\r
292 public static void main(String[] args) throws Exception {
\r
293 new DataDrivenCalendarTest().run(args);
\r