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