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