1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.java.util; 24 25 import static org.testng.Assert.assertEquals; 26 27 import java.time.Instant; 28 import java.time.LocalTime; 29 import java.time.OffsetDateTime; 30 import java.time.ZonedDateTime; 31 import java.time.ZoneId; 32 33 import java.time.chrono.ChronoLocalDate; 34 import java.time.chrono.ChronoLocalDateTime; 35 import java.time.chrono.ChronoZonedDateTime; 36 import java.time.chrono.Chronology; 37 38 import java.time.temporal.ChronoField; 39 import java.time.temporal.TemporalQueries; 40 import java.time.temporal.TemporalAccessor; 41 import java.time.temporal.UnsupportedTemporalTypeException; 42 43 import java.util.*; 44 45 import org.testng.annotations.DataProvider; 46 import org.testng.annotations.Test; 47 48 /* @test 49 * @summary Unit test for j.u.Formatter threeten date/time support 50 * @bug 8003680 8043387 8012638 51 */ 52 @Test 53 public class TestFormatter { 54 55 // time 56 private static String[] fmtStrTime = new String[] { 57 "H:[%tH] I:[%1$tI] k:[%1$tk] l:[%1$tl] M:[%1$tM] S:[%1$tS] L:[%1$tL] N:[%1$tN] p:[%1$tp]", 58 "H:[%TH] I:[%1$TI] k:[%1$Tk] l:[%1$Tl] M:[%1$TM] S:[%1$TS] L:[%1$TL] N:[%1$TN] p:[%1$Tp]", 59 "R:[%tR] T:[%1$tT] r:[%1$tr]", 60 "R:[%TR] T:[%1$TT] r:[%1$Tr]" 61 }; 62 // date 63 private static String[] fmtStrDate = new String[] { 64 "B:[%tB] b:[%1$tb] h:[%1$th] A:[%1$tA] a:[%1$ta] C:[%1$tC] Y:[%1$tY] y:[%1$ty] j:[%1$tj] m:[%1$tm] d:[%1$td] e:[%1$te]", 65 "B:[%TB] b:[%1$Tb] h:[%1$Th] A:[%1$TA] a:[%1$Ta] C:[%1$TC] Y:[%1$TY] y:[%1$Ty] j:[%1$Tj] m:[%1$Tm] d:[%1$Td] e:[%1$Te]", 66 "D:[%tD] F:[%1$tF]", 67 "D:[%TD] F:[%1$TF]" 68 }; 69 70 private int total = 0; 71 private int failure = 0; 72 private boolean verbose = false; 73 74 @DataProvider(name = "calendarsByLocale") data_calendars()75 Object[][] data_calendars() { 76 return new Object[][] { 77 {"en_US"}, 78 {"th_TH"}, 79 {"ja-JP-u-ca-japanese"}, 80 }; 81 } 82 83 @Test(dataProvider="calendarsByLocale") test(String calendarLocale)84 public void test (String calendarLocale) { 85 failure = 0; 86 int N = 12; 87 //locales = Locale.getAvailableLocales(); 88 Locale[] locales = new Locale[] { 89 Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE}; 90 Random r = new Random(); 91 92 Locale calLocale = Locale.forLanguageTag(calendarLocale); 93 Chronology chrono = Chronology.ofLocale(calLocale); 94 ChronoLocalDate now = chrono.dateNow(); 95 ChronoLocalDateTime<?> ldt0 = now.atTime(LocalTime.now()); 96 // Android-changed: use hard-coded zone id instead of system default. 97 ChronoZonedDateTime<?> zdt0 = ldt0.atZone(ZoneId.of("America/Los_Angeles")); 98 ChronoZonedDateTime<?>[] zdts = new ChronoZonedDateTime<?>[] { 99 zdt0, 100 zdt0.withZoneSameLocal(ZoneId.of("UTC")), 101 zdt0.withZoneSameLocal(ZoneId.of("GMT")), 102 zdt0.withZoneSameLocal(ZoneId.of("UT")), 103 }; 104 105 while (N-- > 0) { 106 for (ChronoZonedDateTime<?> zdt : zdts) { 107 zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1)) 108 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); 109 Instant instant = zdt.toInstant(); 110 // Android-changed: Calendar.getInstance() only returns GregorianCalendar. 111 // Manually get a JapaneseImperialCalendar for the test. 112 TimeZone tz = TimeZone.getTimeZone(zdt.getZone()); 113 Calendar cal; 114 if (calLocale.getLanguage().equals("ja")) { 115 cal = Calendar.getJapaneseImperialInstance(tz, calLocale); 116 } else { 117 cal = Calendar.getInstance(tz, calLocale); 118 } 119 cal.setTimeInMillis(instant.toEpochMilli()); 120 for (Locale locale : locales) { 121 for (String fmtStr : fmtStrDate) { 122 testDate(fmtStr, locale, zdt, cal); 123 } 124 for (String fmtStr : fmtStrTime) { 125 testTime(fmtStr, locale, zdt, cal); 126 } 127 testZoneId(locale, zdt, cal); 128 testInstant(locale, instant, zdt, cal); 129 } 130 } 131 } 132 if (verbose) { 133 if (failure != 0) { 134 System.out.println("Total " + failure + "/" + total + " tests failed"); 135 } else { 136 System.out.println("All tests (" + total + ") PASSED"); 137 } 138 } 139 assertEquals(failure, 0); 140 } 141 getClassName(Object o)142 private String getClassName(Object o) { 143 Class<?> c = o.getClass(); 144 String clname = c.getName().substring(c.getPackage().getName().length() + 1); 145 if (o instanceof TemporalAccessor) { 146 Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology()); 147 if (chrono != null) { 148 clname = clname + "(" + chrono.getId() + ")"; 149 } 150 } 151 if (o instanceof Calendar) { 152 String type = ((Calendar)o).getCalendarType(); 153 clname = clname + "(" + type + ")"; 154 } 155 return clname; 156 } 157 test(String fmtStr, Locale locale, String expected, Object dt)158 private String test(String fmtStr, Locale locale, 159 String expected, Object dt) { 160 String out = new Formatter( 161 new StringBuilder(), locale).format(fmtStr, dt).out().toString(); 162 if (verbose) { 163 System.out.printf("%-24s : %s%n", getClassName(dt), out); 164 } 165 166 // expected usually comes from Calendar which only has milliseconds 167 // precision. So we're going to replace it's N:[nanos] stamp with 168 // the correct value for nanos. 169 if ((dt instanceof TemporalAccessor) && expected != null) { 170 try { 171 // Get millis & nanos from the dt 172 final TemporalAccessor ta = (TemporalAccessor) dt; 173 final int nanos = ta.get(ChronoField.NANO_OF_SECOND); 174 final int millis = ta.get(ChronoField.MILLI_OF_SECOND); 175 final String nanstr = String.valueOf(nanos); 176 final String mistr = String.valueOf(millis); 177 178 // Compute the value of the N:[nanos] field that we expect 179 // to find in 'out' 180 final StringBuilder sb = new StringBuilder(); 181 sb.append("N:["); 182 for (int i=nanstr.length(); i<9; i++) { 183 sb.append('0'); 184 } 185 sb.append(nanos).append("]"); 186 187 // Compute the truncated value of N:[nanos] field that might 188 // be in 'expected' when expected was built from Calendar. 189 final StringBuilder sbm = new StringBuilder(); 190 sbm.append("N:["); 191 for (int i=mistr.length(); i<3; i++) { 192 sbm.append('0'); 193 } 194 sbm.append(mistr).append("000000]"); 195 196 // if expected contains the truncated value, replace it with 197 // the complete value. 198 expected = expected.replace(sbm.toString(), sb.toString()); 199 } catch (UnsupportedTemporalTypeException e) { 200 // nano seconds unsupported - nothing to do... 201 } 202 } 203 if (expected != null && !out.equals(expected)) { 204 System.out.printf("%-24s actual: %s%n FAILED; expected: %s%n", 205 getClassName(dt), out, expected); 206 new RuntimeException().printStackTrace(System.out); 207 failure++; 208 } 209 total++; 210 return out; 211 } 212 printFmtStr(Locale locale, String fmtStr)213 private void printFmtStr(Locale locale, String fmtStr) { 214 if (verbose) { 215 System.out.println("--------------------"); 216 System.out.printf("[%s, %s]%n", locale.toString(), fmtStr); 217 } 218 } 219 testDate(String fmtStr, Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)220 private void testDate(String fmtStr, Locale locale, 221 ChronoZonedDateTime<?> zdt, Calendar cal) { 222 printFmtStr(locale, fmtStr); 223 String expected = test(fmtStr, locale, null, cal); 224 test(fmtStr, locale, expected, zdt); 225 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 226 test(fmtStr, locale, expected, zdt.toLocalDate()); 227 if (zdt instanceof ZonedDateTime) { 228 test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime()); 229 } 230 } 231 testTime(String fmtStr, Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)232 private void testTime(String fmtStr, Locale locale, 233 ChronoZonedDateTime<?> zdt, Calendar cal) { 234 printFmtStr(locale, fmtStr); 235 String expected = test(fmtStr, locale, null, cal); 236 test(fmtStr, locale, expected, zdt); 237 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 238 test(fmtStr, locale, expected, zdt.toLocalTime()); 239 if (zdt instanceof ZonedDateTime) { 240 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 241 test(fmtStr, locale, expected, odt); 242 test(fmtStr, locale, expected, odt.toOffsetTime()); 243 } 244 } 245 toZoneOffsetStr(String expected)246 private String toZoneOffsetStr(String expected) { 247 // Android-changed: Android Matcher doesn't support named groups. Also GMT/Z is formatted as 248 // "GMT+00:00" or "GMT". 249 return expected.replaceAll("GMT(?:\\+00:00)?(?=])|UTC|UT", "Z") 250 .replaceAll("(?:GMT|UTC)([+\\-]?[0-9]{2}:[0-9]{2})", "$1"); 251 } 252 testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)253 private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) { 254 String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; 255 printFmtStr(locale, fmtStr); 256 String expected = test(fmtStr, locale, null, cal); 257 test(fmtStr, locale, expected, zdt); 258 // get a new cal with fixed tz 259 Calendar cal0 = Calendar.getInstance(); 260 cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); 261 cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); 262 expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); 263 if (zdt instanceof ZonedDateTime) { 264 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 265 test(fmtStr, locale, expected, odt); 266 test(fmtStr, locale, expected, odt.toOffsetTime()); 267 } 268 269 // datetime + zid 270 fmtStr = "c:[%tc] c:[%1$Tc]"; 271 printFmtStr(locale, fmtStr); 272 expected = test(fmtStr, locale, null, cal); 273 test(fmtStr, locale, expected, zdt); 274 } 275 testInstant(Locale locale, Instant instant, ChronoZonedDateTime<?> zdt, Calendar cal)276 private void testInstant(Locale locale, Instant instant, 277 ChronoZonedDateTime<?> zdt, Calendar cal) { 278 String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]"; 279 printFmtStr(locale, fmtStr); 280 String expected = test(fmtStr, locale, null, cal); 281 test(fmtStr, locale, expected, instant); 282 test(fmtStr, locale, expected, zdt); 283 if (zdt instanceof ZonedDateTime) { 284 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 285 test(fmtStr, locale, expected, odt); 286 } 287 } 288 } 289