1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/***************************************************************************************** 4 * 5 * Copyright (C) 1996-2015, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 **/ 8 9/** 10 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormatAPI 11 * Source File: java/text/format/IntlTestDateFormatAPI.java 12 **/ 13 14/* 15 @test 1.4 98/03/06 16 @summary test International Date Format API 17*/ 18 19package com.ibm.icu.dev.test.format; 20 21import java.text.FieldPosition; 22import java.text.ParseException; 23import java.text.ParsePosition; 24import java.util.Date; 25import java.util.Locale; 26 27import org.junit.Test; 28import org.junit.runner.RunWith; 29import org.junit.runners.JUnit4; 30 31import com.ibm.icu.dev.test.TestFmwk; 32import com.ibm.icu.dev.test.TestUtil; 33import com.ibm.icu.dev.test.TestUtil.JavaVendor; 34import com.ibm.icu.text.DateFormat; 35import com.ibm.icu.text.NumberFormat; 36import com.ibm.icu.util.Calendar; 37import com.ibm.icu.util.TimeZone; 38 39@RunWith(JUnit4.class) 40public class IntlTestDateFormatAPI extends TestFmwk 41{ 42 // Test that the equals method works correctly. 43 @Test 44 public void TestEquals() 45 { 46 // Create two objects at different system times 47 DateFormat a = DateFormat.getInstance(); 48 Date start = Calendar.getInstance().getTime(); 49 while (true) { 50 // changed to remove compiler warnings. 51 if (!start.equals(Calendar.getInstance().getTime())) { 52 break; // Wait for time to change 53 } 54 } 55 DateFormat b = DateFormat.getInstance(); 56 57 if (!(a.equals(b))) 58 errln("FAIL: DateFormat objects created at different times are unequal."); 59 60 // Why has this test been disabled??? - aliu 61// if (b instanceof SimpleDateFormat) 62// { 63// //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used 64// try { 65// ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR); 66// if (a.equals(b)) 67// errln("FAIL: DateFormat objects with different two digit start dates are equal."); 68// } 69// catch (Exception e) { 70// errln("FAIL: setTwoDigitStartDate failed."); 71// } 72// } 73 } 74 75 // This test checks various generic API methods in DateFormat to achieve 100% API coverage. 76 @Test 77 public void TestAPI() 78 { 79 logln("DateFormat API test---"); logln(""); 80 Locale.setDefault(Locale.ENGLISH); 81 82 83 // ======= Test constructors 84 85 logln("Testing DateFormat constructors"); 86 87 DateFormat def = DateFormat.getInstance(); 88 DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH); 89 DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN); 90 DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN); 91 92 // ======= Test equality 93 94 logln("Testing equality operator"); 95 96 if( fr.equals(it) ) { 97 errln("ERROR: equals failed"); 98 } 99 100 // ======= Test various format() methods 101 102 logln("Testing various format() methods"); 103 104 Date d = new Date((long)837039928046.0); 105 106 StringBuffer res1 = new StringBuffer(); 107 StringBuffer res2 = new StringBuffer(); 108 String res3 = new String(); 109 FieldPosition pos1 = new FieldPosition(0); 110 FieldPosition pos2 = new FieldPosition(0); 111 112 res1 = fr.format(d, res1, pos1); 113 logln("" + d.getTime() + " formatted to " + res1); 114 115 res2 = it.format(d, res2, pos2); 116 logln("" + d.getTime() + " formatted to " + res2); 117 118 res3 = de.format(d); 119 logln("" + d.getTime() + " formatted to " + res3); 120 121 // ======= Test parse() 122 123 logln("Testing parse()"); 124 125 String text = new String("02/03/76, 2:50 AM, CST"); 126 Object result1 = new Date(); 127 Date result2 = new Date(); 128 Date result3 = new Date(); 129 ParsePosition pos = new ParsePosition(0); 130 ParsePosition pos01 = new ParsePosition(0); 131 132 result1 = def.parseObject(text, pos); 133 if (result1 == null) { 134 errln("ERROR: parseObject() failed for " + text); 135 } 136 logln(text + " parsed into " + ((Date)result1).getTime()); 137 138 try { 139 result2 = def.parse(text); 140 } 141 catch (ParseException e) { 142 errln("ERROR: parse() failed"); 143 } 144 logln(text + " parsed into " + result2.getTime()); 145 146 result3 = def.parse(text, pos01); 147 if (result3 == null) { 148 errln("ERROR: parse() failed for " + text); 149 } 150 logln(text + " parsed into " + result3.getTime()); 151 152 153 // ======= Test getters and setters 154 155 logln("Testing getters and setters"); 156 157 final Locale[] locales = DateFormat.getAvailableLocales(); 158 long count = locales.length; 159 logln("Got " + count + " locales" ); 160 161 // Ticket #6280, #8078 and #11674 162 // These locales should be included in the result 163 boolean missingLocaleNotFatal = 164 TestUtil.getJavaVendor() == JavaVendor.Android || TestUtil.getJavaVersion() >= 7; 165 final Locale[] samples = { 166 new Locale("zh", "CN"), 167 new Locale("zh", "TW"), 168 new Locale("zh", "HK"), 169 new Locale("sr", "RS"), 170 }; 171 boolean[] available = new boolean[samples.length]; 172 for(int i = 0; i < count; i++) { 173 String name; 174 name = locales[i].getDisplayName(); 175 logln(name); 176 for (int j = 0; j < samples.length; j++) { 177 if (locales[i].equals(samples[j])) { 178 available[j] = true; 179 break; 180 } 181 } 182 } 183 for (int i = 0; i < available.length; i++) { 184 if (!available[i]) { 185 if (missingLocaleNotFatal) { 186 // Java 7 supports script field, so zh_Hans_CN is included 187 // in the available locale list. 188 logln("INFO: missing Locale: " + samples[i]); 189 } else { 190 errln("ERROR: missing Locale: " + samples[i]); 191 } 192 } 193 } 194 195 fr.setLenient(it.isLenient()); 196 if(fr.isLenient() != it.isLenient()) { 197 errln("ERROR: setLenient() failed"); 198 } 199 200 final Calendar cal = def.getCalendar(); 201 Calendar newCal = (Calendar) cal.clone(); 202 de.setCalendar(newCal); 203 it.setCalendar(newCal); 204 if( ! de.getCalendar().equals(it.getCalendar())) { 205 errln("ERROR: set Calendar() failed"); 206 } 207 208 final NumberFormat nf = def.getNumberFormat(); 209 NumberFormat newNf = (NumberFormat) nf.clone(); 210 de.setNumberFormat(newNf); 211 it.setNumberFormat(newNf); 212 if( ! de.getNumberFormat().equals(it.getNumberFormat())) { 213 errln("ERROR: set NumberFormat() failed"); 214 } 215 216 final TimeZone tz = def.getTimeZone(); 217 TimeZone newTz = (TimeZone) tz.clone(); 218 de.setTimeZone(newTz); 219 it.setTimeZone(newTz); 220 if( ! de.getTimeZone().equals(it.getTimeZone())) { 221 errln("ERROR: set TimeZone() failed"); 222 } 223 224 // ======= Test getStaticClassID() 225 226// logln("Testing instanceof()"); 227 228// try { 229// DateFormat test = new SimpleDateFormat(); 230 231// if (! (test instanceof SimpleDateFormat)) { 232// errln("ERROR: instanceof failed"); 233// } 234// } 235// catch (Exception e) { 236// errln("ERROR: Couldn't create a DateFormat"); 237// } 238 } 239} 240