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