]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/calendar/DataDrivenCalendarTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / calendar / DataDrivenCalendarTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2007-2013, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.calendar;
8
9 import java.util.Date;
10 import java.util.Iterator;
11
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;
21
22 /**
23  * @author srl
24  * 
25  * analog of dadrcal.cpp
26  *
27  */
28 public class DataDrivenCalendarTest extends ModuleTest {
29
30     public DataDrivenCalendarTest() {
31         super("com/ibm/icu/dev/data/testdata/", "calendar");
32     }
33         
34     /* (non-Javadoc)
35      * @see com.ibm.icu.dev.test.ModuleTest#processModules()
36      */
37     public void processModules() {
38         //String testName = t.getName().toString();
39
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();
43             
44             String type = settings.getString("Type");
45             
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")) {
51                 testOps(t, settings);
52             } else {
53                 errln("Unknown type: " + type);
54             }
55         }
56     }
57     
58
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()+" ";
64
65         fromCalendar.clear();
66
67         fromSet.setOnCalendar(fromCalendar);
68
69         CalendarFieldsSet diffSet = new CalendarFieldsSet();
70
71         diffSet.clear();
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);
77         } else {
78             logln("PASS: "+thisString+" SOURCE calendar match.");
79         }
80
81         //logln("Set Source calendar: " + from);
82
83         Date fromTime = fromCalendar.getTime();
84
85         diffSet.clear();
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);
91         } else {
92             logln("PASS: "+thisString+" SET SOURCE calendar match.");
93         }
94
95         toCalendar.clear();
96         toCalendar.setTime(fromTime);
97
98         diffSet.clear();
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());
106         } else {
107             logln("PASS: "+thisString+" match.");
108         }
109     }
110
111
112     
113     private void testConvert(TestDataModule.TestData testData, DataMap settings, boolean forward) {
114         Calendar toCalendar= null;
115         // build to calendar
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
122         int n = 0;
123         for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
124             ++n;
125             DataMap currentCase = (DataMap) iter.next();
126             
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);
131             
132             fromSet.clear();
133             toSet.clear();
134
135             String from = currentCase.getString("from");
136             fromSet.parseFrom(from);
137             String to = currentCase.getString("to");
138             toSet.parseFrom(to, fromSet);
139
140             // now, do it.
141             if (forward) {
142                 logln(caseString +" "+locale+"/"+from+" >>> "+loc+"/"
143                         +to);
144                 testConvert(caseString, fromSet, fromCalendar, toSet, toCalendar, forward);
145             } else {
146                 logln(caseString +" "+locale+"/"+from+" <<< "+loc+"/"
147                         +to);
148                 testConvert(caseString, toSet, toCalendar, fromSet, fromCalendar, forward);
149             }
150         }
151     }
152     
153     private static final String kADD = "add";
154     private static final String kROLL = "roll";
155     private static final String kMILLIS = "MILLIS=";
156     
157     private void testOps(TestDataModule.TestData testData, DataMap settings) {
158         // Get 'from' time 
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
162         int n = 0;
163         long fromDate = 0;
164         long toDate = 0;
165         
166         boolean useDate = false;
167         
168         for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
169             ++n;
170             DataMap currentCase = (DataMap) iter.next();
171             
172             String caseString = "[case "+n+"]";
173             // build to calendar
174             //             Headers { "locale","from","operation","params","to" }
175             // #1 locale
176             String param = "locale";
177             String locale;
178             String testSetting = currentCase.getString(param);
179             locale = testSetting;
180             ULocale loc = new ULocale(locale);
181             Calendar fromCalendar = Calendar.getInstance(loc);
182
183             fromSet.clear();
184             // #2 'from' info
185             param = "from";
186             String from = testSetting=currentCase.getString(param);
187             if(from.startsWith(kMILLIS)){
188                 useDate = true;
189                 fromDate = Long.parseLong(from.substring(kMILLIS.length()));
190             }else{
191                 fromSet.parseFrom(testSetting);
192             }
193 //            System.err.println("fromset: ["+testSetting+"] >> " + fromSet);
194
195             // #4 'operation' info
196             param = "operation";
197             String operation = testSetting=currentCase.getString(param);
198             paramsSet.clear();
199             // #3 'params' info
200             param = "params";
201             String paramsData = testSetting =  currentCase.getString(param);
202             paramsSet.parseFrom(paramsData); // parse with inheritance.
203 //            System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet);
204             
205             toSet.clear();
206             // #4 'to' info
207             param = "to";
208             String to = testSetting=currentCase.getString(param);
209            if(to.startsWith(kMILLIS)){
210                 useDate = true;
211                 toDate = Long.parseLong(to.substring(kMILLIS.length()));
212             }else{ 
213                 toSet.parseFrom(testSetting, fromSet);
214            }
215             //toSet.parseFrom(testSetting, fromSet); // parse with inheritance.
216 //            System.err.println("toSet: ["+testSetting+"] >> " + toSet);
217
218             String caseContentsString = locale+":  from "+from+": "
219                     +operation +" [[[ "+paramsSet+" ]]]   >>> "+to;
220             logln(caseString+": "+caseContentsString);
221
222             // ------
223             // now, do it.
224
225             /// prepare calendar
226             if(useDate){
227                 fromCalendar.setTimeInMillis(fromDate);
228             }else {
229                 fromSet.setOnCalendar(fromCalendar);
230             }
231             
232             // from calendar:  'starting date'
233             
234             diffSet.clear();
235             
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);
241             }  else {
242                 logln(" "+caseString+" SET SOURCE calendar match."); // verifies that the requested fields were set.
243             }
244             
245             // to calendar - copy of from calendar
246             Calendar toCalendar = (Calendar)fromCalendar.clone();
247
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)) {
252                         toCalendar.roll(q,
253                                 paramsSet.get(q));
254                     } else if (operation.equals(kADD)) {
255                         toCalendar.add(q,
256                                 paramsSet.get(q));
257                     } else {
258                         errln(caseString+ " FAIL: unknown operation "+ operation);
259                     }
260                     logln(operation + " of "+ paramsSet.get(q));
261                 }
262             }
263             // now - what's the result?
264             diffSet.clear();
265
266             // toset contains 'expected'
267             
268             if(useDate) {
269                     if(toCalendar.getTimeInMillis()==toDate) {
270                         logln(caseString + " SUCCESS: got=expected="+toDate);
271                         logln("PASS: "+caseString+" matched! ");
272                     } else {
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 " + 
280                                     toDate);
281                         } else {
282                             errln(caseString + " FAIL: got " + 
283                                 toCalendar.getTimeInMillis() + "  expected " + 
284                                 toDate);
285                         }
286                     }
287             }else if (!toSet.matches(toCalendar, diffSet)) {
288                 String diffs = diffSet.diffFrom(toSet);
289                 errln((String)"FAIL: "+caseString+" - , "+caseContentsString
290                         +" Differences: "+ diffs );
291             } else{
292                 logln("PASS: "+caseString+" matched! ");
293             }
294             
295         }
296     }
297
298    
299
300     /**
301      * @param args
302      */
303     public static void main(String[] args) throws Exception {
304         new DataDrivenCalendarTest().run(args);
305     }
306
307 }