1 /* 2 * Copyright (c) 2012, 2015, 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 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") 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") 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 ChronoZonedDateTime<?> zdt0 = ldt0.atZone(ZoneId.systemDefault()); 97 ChronoZonedDateTime<?>[] zdts = new ChronoZonedDateTime<?>[] { 98 zdt0, 99 zdt0.withZoneSameLocal(ZoneId.of("UTC")), 100 zdt0.withZoneSameLocal(ZoneId.of("GMT")), 101 zdt0.withZoneSameLocal(ZoneId.of("UT")), 102 }; 103 104 while (N-- > 0) { 105 for (ChronoZonedDateTime<?> zdt : zdts) { 106 zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1)) 107 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); 108 Instant instant = zdt.toInstant(); 109 Calendar cal = Calendar.getInstance(calLocale); 110 cal.setTimeInMillis(instant.toEpochMilli()); 111 cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone())); 112 for (Locale locale : locales) { 113 for (String fmtStr : fmtStrDate) { 114 testDate(fmtStr, locale, zdt, cal); 115 } 116 for (String fmtStr : fmtStrTime) { 117 testTime(fmtStr, locale, zdt, cal); 118 } 119 testZoneId(locale, zdt, cal); 120 testInstant(locale, instant, zdt, cal); 121 } 122 } 123 } 124 if (verbose) { 125 if (failure != 0) { 126 System.out.println("Total " + failure + "/" + total + " tests failed"); 127 } else { 128 System.out.println("All tests (" + total + ") PASSED"); 129 } 130 } 131 assertEquals(failure, 0); 132 } 133 134 private String getClassName(Object o) { 135 Class<?> c = o.getClass(); 136 String clname = c.getName().substring(c.getPackage().getName().length() + 1); 137 if (o instanceof TemporalAccessor) { 138 Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology()); 139 if (chrono != null) { 140 clname = clname + "(" + chrono.getId() + ")"; 141 } 142 } 143 if (o instanceof Calendar) { 144 String type = ((Calendar)o).getCalendarType(); 145 clname = clname + "(" + type + ")"; 146 } 147 return clname; 148 } 149 150 private String test(String fmtStr, Locale locale, 151 String expected, Object dt) { 152 String out = new Formatter( 153 new StringBuilder(), locale).format(fmtStr, dt).out().toString(); 154 if (verbose) { 155 System.out.printf("%-24s : %s%n", getClassName(dt), out); 156 } 157 158 // expected usually comes from Calendar which only has milliseconds 159 // precision. So we're going to replace it's N:[nanos] stamp with 160 // the correct value for nanos. 161 if ((dt instanceof TemporalAccessor) && expected != null) { 162 try { 163 // Get millis & nanos from the dt 164 final TemporalAccessor ta = (TemporalAccessor) dt; 165 final int nanos = ta.get(ChronoField.NANO_OF_SECOND); 166 final int millis = ta.get(ChronoField.MILLI_OF_SECOND); 167 final String nanstr = String.valueOf(nanos); 168 final String mistr = String.valueOf(millis); 169 170 // Compute the value of the N:[nanos] field that we expect 171 // to find in 'out' 172 final StringBuilder sb = new StringBuilder(); 173 sb.append("N:["); 174 for (int i=nanstr.length(); i<9; i++) { 175 sb.append('0'); 176 } 177 sb.append(nanos).append("]"); 178 179 // Compute the truncated value of N:[nanos] field that might 180 // be in 'expected' when expected was built from Calendar. 181 final StringBuilder sbm = new StringBuilder(); 182 sbm.append("N:["); 183 for (int i=mistr.length(); i<3; i++) { 184 sbm.append('0'); 185 } 186 sbm.append(mistr).append("000000]"); 187 188 // if expected contains the truncated value, replace it with 189 // the complete value. 190 expected = expected.replace(sbm.toString(), sb.toString()); 191 //System.out.println("Replaced: " + expected); 192 } catch (UnsupportedTemporalTypeException e) { 193 // nano seconds unsupported - nothing to do... 194 //System.out.println(e + " for " + getClassName(dt)); 195 //System.out.println("\t"+out); 196 } 197 } 198 if (expected != null && !out.equals(expected)) { 199 System.out.printf("%-24s actual: %s%n FAILED; expected: %s%n", 200 getClassName(dt), out, expected); 201 new RuntimeException().printStackTrace(System.out); 202 failure++; 203 } 204 total++; 205 return out; 206 } 207 208 private void printFmtStr(Locale locale, String fmtStr) { 209 if (verbose) { 210 System.out.println("--------------------"); 211 System.out.printf("[%s, %s]%n", locale.toString(), fmtStr); 212 } 213 } 214 215 private void testDate(String fmtStr, Locale locale, 216 ChronoZonedDateTime<?> zdt, Calendar cal) { 217 printFmtStr(locale, fmtStr); 218 String expected = test(fmtStr, locale, null, cal); 219 test(fmtStr, locale, expected, zdt); 220 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 221 test(fmtStr, locale, expected, zdt.toLocalDate()); 222 if (zdt instanceof ZonedDateTime) { 223 test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime()); 224 } 225 } 226 227 private void testTime(String fmtStr, Locale locale, 228 ChronoZonedDateTime<?> zdt, Calendar cal) { 229 printFmtStr(locale, fmtStr); 230 String expected = test(fmtStr, locale, null, cal); 231 test(fmtStr, locale, expected, zdt); 232 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 233 test(fmtStr, locale, expected, zdt.toLocalTime()); 234 if (zdt instanceof ZonedDateTime) { 235 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 236 test(fmtStr, locale, expected, odt); 237 test(fmtStr, locale, expected, odt.toOffsetTime()); 238 } 239 } 240 241 private String toZoneIdStr(String expected) { 242 return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}"); 243 } 244 245 private String toZoneOffsetStr(String expected) { 246 return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}") 247 .replaceAll("GMT|UTC|UT", "Z"); 248 } 249 250 private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) { 251 String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; 252 printFmtStr(locale, fmtStr); 253 String expected = toZoneIdStr(test(fmtStr, locale, null, cal)); 254 test(fmtStr, locale, expected, zdt); 255 // get a new cal with fixed tz 256 Calendar cal0 = Calendar.getInstance(); 257 cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); 258 cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); 259 expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); 260 if (zdt instanceof ZonedDateTime) { 261 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 262 test(fmtStr, locale, expected, odt); 263 test(fmtStr, locale, expected, odt.toOffsetTime()); 264 } 265 266 // datetime + zid 267 fmtStr = "c:[%tc] c:[%1$Tc]"; 268 printFmtStr(locale, fmtStr); 269 expected = toZoneIdStr(test(fmtStr, locale, null, cal)); 270 test(fmtStr, locale, expected, zdt); 271 } 272 273 private void testInstant(Locale locale, Instant instant, 274 ChronoZonedDateTime<?> zdt, Calendar cal) { 275 String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]"; 276 printFmtStr(locale, fmtStr); 277 String expected = test(fmtStr, locale, null, cal); 278 test(fmtStr, locale, expected, instant); 279 test(fmtStr, locale, expected, zdt); 280 if (zdt instanceof ZonedDateTime) { 281 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 282 test(fmtStr, locale, expected, odt); 283 } 284 } 285 }