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