]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / format / DataDrivenFormatTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2007-2012, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.format;
8
9 import java.text.FieldPosition;
10 import java.text.ParsePosition;
11 import java.util.Date;
12 import java.util.Iterator;
13
14 import com.ibm.icu.dev.test.ModuleTest;
15 import com.ibm.icu.dev.test.TestDataModule;
16 import com.ibm.icu.dev.test.TestDataModule.DataMap;
17 import com.ibm.icu.dev.test.util.CalendarFieldsSet;
18 import com.ibm.icu.dev.test.util.DateTimeStyleSet;
19 import com.ibm.icu.text.DateFormat;
20 import com.ibm.icu.text.SimpleDateFormat;
21 import com.ibm.icu.util.Calendar;
22 import com.ibm.icu.util.TimeZone;
23 import com.ibm.icu.util.ULocale;
24
25 /**
26  * @author srl
27  *
28  */
29 public class DataDrivenFormatTest extends ModuleTest {
30
31     /**
32      * @param baseName
33      * @param locName
34      */
35     public DataDrivenFormatTest() {
36         super("com/ibm/icu/dev/data/testdata/", "format");
37     }
38
39     /* (non-Javadoc)
40      * @see com.ibm.icu.dev.test.ModuleTest#processModules()
41      */
42     public void processModules() {
43         //String testName = t.getName().toString();
44
45         for (Iterator siter = t.getSettingsIterator(); siter.hasNext();) {
46             // Iterate through and get each of the test case to process
47             DataMap settings = (DataMap) siter.next();
48             
49             String type = settings.getString("Type");
50
51             if(type.equals("date_format")) {
52                 testConvertDate(t, settings, true);
53             } else if(type.equals("date_parse")) {
54                 testConvertDate(t, settings, false);
55             } else {
56                 errln("Unknown type: " + type);
57             }
58         }
59     }
60
61     /**
62      * @param args
63      */
64     public static void main(String[] args) {
65          new DataDrivenFormatTest().run(args);
66     }
67    
68     private static final String kPATTERN = "PATTERN=";
69     private static final String kMILLIS = "MILLIS=";
70     private static final String kRELATIVE_MILLIS = "RELATIVE_MILLIS=";
71     private static final String kRELATIVE_ADD = "RELATIVE_ADD:";
72     
73     private void testConvertDate(TestDataModule.TestData testData, DataMap  settings, boolean fmt) {
74         DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
75
76         int n = 0;
77         for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
78             ++n;
79             long now = System.currentTimeMillis();
80             DataMap currentCase = (DataMap) iter.next();
81             String caseString = "["+testData.getName()+"#"+n+(fmt?"format":"parse")+"]";
82             
83             String locale = currentCase.getString("locale");
84             String zone = currentCase.getString("zone");
85             String spec = currentCase.getString("spec");
86             String date = currentCase.getString("date");
87             String str = currentCase.getString("str");
88             
89             Date fromDate = null;
90             boolean useDate = false;
91             
92             ULocale loc = new ULocale(locale);
93             String pattern = null;
94 //            boolean usePattern = false;
95             DateFormat format = null;
96             DateTimeStyleSet styleSet;
97             CalendarFieldsSet fromSet = null;
98             
99             // parse 'spec'  - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
100             if(spec.startsWith(kPATTERN)) {
101                 pattern = spec.substring(kPATTERN.length());
102 //                usePattern = true;
103                 format = new SimpleDateFormat(pattern, loc);
104             } else {
105                 styleSet = new DateTimeStyleSet();
106                 styleSet.parseFrom(spec);
107                 format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
108             }
109
110             Calendar cal = Calendar.getInstance(loc);
111
112             if (zone.length() > 0) {
113                 TimeZone tz = TimeZone.getFrozenTimeZone(zone);
114                 cal.setTimeZone(tz);
115                 format.setTimeZone(tz);
116             }
117             
118             // parse 'date' - either 'MILLIS=12345' or  a CalendarFieldsSet
119             if(date.startsWith(kMILLIS)) {
120                 useDate = true;
121                 fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
122             } else if(date.startsWith(kRELATIVE_MILLIS)) {
123                 useDate = true;
124                 fromDate = new Date(now+Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
125             } else if(date.startsWith(kRELATIVE_ADD)) {
126                 String add = date.substring(kRELATIVE_ADD.length()); // "add" is a string indicating which fields to add
127                 CalendarFieldsSet addSet = new CalendarFieldsSet();
128                 addSet.parseFrom(add);
129                 useDate = true;
130                 cal.clear();
131                 cal.setTimeInMillis(now);
132
133                 /// perform op on 'to calendar'
134                 for (int q=0; q<addSet.fieldCount(); q++) {
135                     if (addSet.isSet(q)) {
136                         if (q == Calendar.DATE) {
137                             cal.add(q,addSet.get(q));
138                         } else {
139                             cal.set(q,addSet.get(q));
140                         }
141                     }
142                 }
143
144                 fromDate = cal.getTime();
145             } else {
146                 fromSet = new CalendarFieldsSet();
147                 fromSet.parseFrom(date);
148             }
149             
150             // run the test
151             if(fmt) {
152                 StringBuffer output = new StringBuffer();
153                 cal.clear();
154                 FieldPosition pos = new FieldPosition(0);
155                 if(useDate) {
156                     output = format.format(fromDate, output, pos);
157                 } else {
158                     fromSet.setOnCalendar(cal);
159                     format.format(cal, output, pos);
160                 }
161                 
162                 if(output.toString().equals(str)) {
163                     logln(caseString + " Success - strings match: " + output);
164                 } else {
165                     errln(caseString + " FAIL: got " + output + " expected " + str);
166                 }
167             } else { // parse
168                 cal.clear();
169                 ParsePosition pos = new ParsePosition(0);
170                 format.parse(str, cal, pos);
171                 if(useDate) {
172                     Date gotDate = cal.getTime(); 
173                     if(gotDate.equals(fromDate)) {
174                         logln(caseString + " SUCCESS: got=parse="+str);
175                     } else {
176                         errln(caseString + " FAIL: parsed " + str + " but got " + 
177                                 basicFmt.format(gotDate) + " - " + gotDate + "  expected " + 
178                                 basicFmt.format(fromDate));
179                     }
180                 } else  {
181                     CalendarFieldsSet diffSet = new CalendarFieldsSet();
182                     if(!fromSet.matches(cal, diffSet)) {
183                         String diffs = diffSet.diffFrom(fromSet);
184                         errln(caseString + " FAIL:  differences: " + diffs);
185                     } else {
186                         logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
187                     }
188                 }
189             }
190         }
191     }
192 }