2 *******************************************************************************
3 * Copyright (C) 2007-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.format;
9 import java.text.FieldPosition;
10 import java.text.ParsePosition;
11 import java.util.Date;
12 import java.util.Iterator;
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;
29 public class DataDrivenFormatTest extends ModuleTest {
35 public DataDrivenFormatTest() {
36 super("com/ibm/icu/dev/data/testdata/", "format");
40 * @see com.ibm.icu.dev.test.ModuleTest#processModules()
42 public void processModules() {
43 //String testName = t.getName().toString();
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();
49 String type = settings.getString("Type");
51 if(type.equals("date_format")) {
52 testConvertDate(t, settings, true);
53 } else if(type.equals("date_parse")) {
54 testConvertDate(t, settings, false);
56 errln("Unknown type: " + type);
64 public static void main(String[] args) {
65 new DataDrivenFormatTest().run(args);
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:";
73 private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
74 DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
77 for (Iterator iter = testData.getDataIterator(); iter.hasNext();) {
79 long now = System.currentTimeMillis();
80 DataMap currentCase = (DataMap) iter.next();
81 String caseString = "["+testData.getName()+"#"+n+(fmt?"format":"parse")+"]";
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");
90 boolean useDate = false;
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;
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);
105 styleSet = new DateTimeStyleSet();
106 styleSet.parseFrom(spec);
107 format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
110 Calendar cal = Calendar.getInstance(loc);
112 if (zone.length() > 0) {
113 TimeZone tz = TimeZone.getFrozenTimeZone(zone);
115 format.setTimeZone(tz);
118 // parse 'date' - either 'MILLIS=12345' or a CalendarFieldsSet
119 if(date.startsWith(kMILLIS)) {
121 fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
122 } else if(date.startsWith(kRELATIVE_MILLIS)) {
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);
131 cal.setTimeInMillis(now);
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));
139 cal.set(q,addSet.get(q));
144 fromDate = cal.getTime();
146 fromSet = new CalendarFieldsSet();
147 fromSet.parseFrom(date);
152 StringBuffer output = new StringBuffer();
154 FieldPosition pos = new FieldPosition(0);
156 output = format.format(fromDate, output, pos);
158 fromSet.setOnCalendar(cal);
159 format.format(cal, output, pos);
162 if(output.toString().equals(str)) {
163 logln(caseString + " Success - strings match: " + output);
165 errln(caseString + " FAIL: got " + output + " expected " + str);
169 ParsePosition pos = new ParsePosition(0);
170 format.parse(str, cal, pos);
172 Date gotDate = cal.getTime();
173 if(gotDate.equals(fromDate)) {
174 logln(caseString + " SUCCESS: got=parse="+str);
176 errln(caseString + " FAIL: parsed " + str + " but got " +
177 basicFmt.format(gotDate) + " - " + gotDate + " expected " +
178 basicFmt.format(fromDate));
181 CalendarFieldsSet diffSet = new CalendarFieldsSet();
182 if(!fromSet.matches(cal, diffSet)) {
183 String diffs = diffSet.diffFrom(fromSet);
184 errln(caseString + " FAIL: differences: " + diffs);
186 logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());