From 8c862fa54882dd7d8d02cb556992fb5a995b2c1d Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Mon, 13 Aug 2007 19:21:16 +0000 Subject: [PATCH] ICU-4567 data driven fmt test for J X-SVN-Rev: 22365 --- .gitattributes | 2 + .../dev/test/format/DataDrivenFormatTest.java | 157 ++++++++++++++++++ .../com/ibm/icu/dev/test/format/TestAll.java | 1 + .../icu/dev/test/util/DateTimeStyleSet.java | 56 +++++++ 4 files changed, 216 insertions(+) create mode 100644 icu4j/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java create mode 100644 icu4j/src/com/ibm/icu/dev/test/util/DateTimeStyleSet.java diff --git a/.gitattributes b/.gitattributes index d2229e2132..5d26c1d426 100644 --- a/.gitattributes +++ b/.gitattributes @@ -245,6 +245,7 @@ icu4j/src/com/ibm/icu/dev/test/duration/testdata/testdata_zh_Hans.txt -text icu4j/src/com/ibm/icu/dev/test/duration/testdata/testdata_zh_Hans_SG.txt -text icu4j/src/com/ibm/icu/dev/test/duration/testdata/testdata_zh_Hant.txt -text icu4j/src/com/ibm/icu/dev/test/duration/testdata/testdata_zh_Hant_HK.txt -text +icu4j/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java -text icu4j/src/com/ibm/icu/dev/test/perf/data/collation/TestNames_Asian.txt -text icu4j/src/com/ibm/icu/dev/test/perf/data/collation/TestNames_Chinese.txt -text icu4j/src/com/ibm/icu/dev/test/perf/data/collation/TestNames_Japanese.txt -text @@ -295,6 +296,7 @@ icu4j/src/com/ibm/icu/dev/test/serializable/data/ICU_3.6/com.ibm.icu.util.ULocal icu4j/src/com/ibm/icu/dev/test/serializable/data/ICU_3.6/com.ibm.icu.util.UResourceTypeMismatchException.dat -text icu4j/src/com/ibm/icu/dev/test/timezone/TimeZoneRuleTest.java -text icu4j/src/com/ibm/icu/dev/test/util/CalendarFieldsSet.java -text +icu4j/src/com/ibm/icu/dev/test/util/DateTimeStyleSet.java -text icu4j/src/com/ibm/icu/dev/test/util/DebugUtilities.java -text icu4j/src/com/ibm/icu/dev/test/util/DebugUtilitiesData.java -text icu4j/src/com/ibm/icu/dev/test/util/DebugUtilitiesTest.java -text diff --git a/icu4j/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java b/icu4j/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java new file mode 100644 index 0000000000..957bbfbff2 --- /dev/null +++ b/icu4j/src/com/ibm/icu/dev/test/format/DataDrivenFormatTest.java @@ -0,0 +1,157 @@ +/* + ******************************************************************************* + * Copyright (C) 2007, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + */ +package com.ibm.icu.dev.test.format; + +import java.text.FieldPosition; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Iterator; + +import com.ibm.icu.dev.test.ModuleTest; +import com.ibm.icu.dev.test.TestDataModule; +import com.ibm.icu.dev.test.TestDataModule.DataMap; +import com.ibm.icu.dev.test.util.CalendarFieldsSet; +import com.ibm.icu.dev.test.util.DateTimeStyleSet; +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.SimpleDateFormat; +import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.ULocale; + +/** + * @author srl + * + */ +public class DataDrivenFormatTest extends ModuleTest { + + /** + * @param baseName + * @param locName + */ + public DataDrivenFormatTest() { + super("com/ibm/icu/dev/data/testdata/", "format"); + } + + /* (non-Javadoc) + * @see com.ibm.icu.dev.test.ModuleTest#processModules() + */ + public void processModules() { + //String testName = t.getName().toString(); + + for (Iterator siter = t.getSettingsIterator(); siter.hasNext();) { + // Iterate through and get each of the test case to process + DataMap settings = (DataMap) siter.next(); + + String type = settings.getString("Type"); + + if(type.equals("date_format")) { + testConvertDate(t, settings, true); + } else if(type.equals("date_parse")) { + testConvertDate(t, settings, false); + } else { + errln("Unknown type: " + type); + } + } + } + + /** + * @param args + */ + public static void main(String[] args) { + new DataDrivenFormatTest().run(args); + } + + private static final String kPATTERN = "PATTERN="; + private static final String kMILLIS = "MILLIS="; + + private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) { + DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee"); + + int n = 0; + for (Iterator iter = testData.getDataIterator(); iter.hasNext();) { + ++n; + DataMap currentCase = (DataMap) iter.next(); + String caseString = "["+testData.getName()+"#"+n+(fmt?"format":"parse")+"]"; + + String locale = currentCase.getString("locale"); + String spec = currentCase.getString("spec"); + String date = currentCase.getString("date"); + String str = currentCase.getString("str"); + + Date fromDate = null; + boolean useDate = false; + + ULocale loc = new ULocale(locale); + String pattern = null; +// boolean usePattern = false; + DateFormat format = null; + DateTimeStyleSet styleSet; + CalendarFieldsSet fromSet = null; + + // parse 'spec' - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y' + if(spec.startsWith(kPATTERN)) { + pattern = spec.substring(kPATTERN.length()); +// usePattern = true; + format = new SimpleDateFormat(pattern, loc); + } else { + styleSet = new DateTimeStyleSet(); + styleSet.parseFrom(spec); + format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc); + } + + // parse 'date' - either 'MILLIS=12345' or a CalendarFieldsSet + if(date.startsWith(kMILLIS)) { + useDate = true; + fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length()))); + } else { + fromSet = new CalendarFieldsSet(); + fromSet.parseFrom(date); + } + + // run the test + Calendar cal = Calendar.getInstance(loc); + if(fmt) { + StringBuffer output = new StringBuffer(); + cal.clear(); + FieldPosition pos = new FieldPosition(0); + if(useDate) { + output = format.format(date, output, pos); + } else { + fromSet.setOnCalendar(cal); + format.format(cal, output, pos); + } + + if(output.toString().equals(str)) { + logln(caseString + " Success - strings match: " + output); + } else { + errln(caseString + " FAIL: got " + output + " expected " + str); + } + } else { // parse + cal.clear(); + ParsePosition pos = new ParsePosition(0); + format.parse(str, cal, pos); + if(useDate) { + Date gotDate = cal.getTime(); + if(gotDate.equals(fromDate)) { + logln(caseString + " SUCCESS: got=parse="+str); + } else { + errln(caseString + " FAIL: parsed " + str + " but got " + + basicFmt.format(gotDate) + " - " + gotDate + " expected " + + basicFmt.format(fromDate)); + } + } else { + CalendarFieldsSet diffSet = new CalendarFieldsSet(); + if(!fromSet.matches(cal, diffSet)) { + String diffs = diffSet.diffFrom(fromSet); + errln(caseString + " FAIL: differences: " + diffs); + } else { + logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString()); + } + } + } + } + } +} diff --git a/icu4j/src/com/ibm/icu/dev/test/format/TestAll.java b/icu4j/src/com/ibm/icu/dev/test/format/TestAll.java index 9b73ec3c81..c9e7549f17 100644 --- a/icu4j/src/com/ibm/icu/dev/test/format/TestAll.java +++ b/icu4j/src/com/ibm/icu/dev/test/format/TestAll.java @@ -28,6 +28,7 @@ public class TestAll extends TestGroup { "TestAll$PluralFormat", "com.ibm.icu.dev.test.format.BigNumberFormatTest", "com.ibm.icu.dev.test.format.GlobalizationPreferencesTest", + "DataDrivenFormatTest" }, "Formatting Tests"); } diff --git a/icu4j/src/com/ibm/icu/dev/test/util/DateTimeStyleSet.java b/icu4j/src/com/ibm/icu/dev/test/util/DateTimeStyleSet.java new file mode 100644 index 0000000000..c7b82c9afc --- /dev/null +++ b/icu4j/src/com/ibm/icu/dev/test/util/DateTimeStyleSet.java @@ -0,0 +1,56 @@ +/* + ******************************************************************************* + * Copyright (C) 2007, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + */ +package com.ibm.icu.dev.test.util; + +import com.ibm.icu.text.DateFormat; + +/** + * @author srl + * + */ +public class DateTimeStyleSet extends FieldsSet { + + private static final int DTS_DATE = 0; + private static final String kDATE = "DATE"; + private static final int DTS_TIME = 1; + private static final String kTIME = "TIME"; + private static final int DTS_COUNT = 2; + + private int getOrNone(int which) { + if(!isSet(which)) { + return DateFormat.NONE; + } else { + return get(which); + } + } + + public DateTimeStyleSet() { + super(FieldsSet.NO_ENUM, DTS_COUNT); + } + + public int getDateStyle() { + return getOrNone(DTS_DATE); + } + + public int getTimeStyle() { + return getOrNone(DTS_TIME); + } + + protected void handleParseValue(FieldsSet inheritFrom, int field, String substr) { + parseValueEnum(DebugUtilitiesData.UDateFormatStyle, inheritFrom, field, substr); + } + + protected int handleParseName(FieldsSet inheritFrom, String name, String substr) { + if(name.equals(kDATE)) { + return DTS_DATE; + } else if(name.equals(kTIME)) { + return DTS_TIME; + } else { + throw new IllegalArgumentException("Bad field: " + name); + } + } +}