TestFormatter.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 2012, 2013, 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 */ 23package test.java.util; 24 25import static org.testng.Assert.assertEquals; 26 27import java.time.Instant; 28import java.time.LocalTime; 29import java.time.OffsetDateTime; 30import java.time.ZonedDateTime; 31import java.time.ZoneId; 32 33import java.time.chrono.ChronoLocalDate; 34import java.time.chrono.ChronoLocalDateTime; 35import java.time.chrono.ChronoZonedDateTime; 36import java.time.chrono.Chronology; 37 38import java.time.temporal.ChronoField; 39import java.time.temporal.TemporalQueries; 40import java.time.temporal.TemporalAccessor; 41 42import java.util.*; 43 44import org.testng.annotations.DataProvider; 45import org.testng.annotations.Test; 46 47/* @test 48 * @summary Unit test for j.u.Formatter threeten date/time support 49 * @bug 8003680 8012638 50 */ 51@Test 52public class TestFormatter { 53 54 // time 55 private static String[] fmtStrTime = new String[] { 56 "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]", 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 "R:[%tR] T:[%1$tT] r:[%1$tr]", 59 "R:[%TR] T:[%1$TT] r:[%1$Tr]" 60 }; 61 // date 62 private static String[] fmtStrDate = new String[] { 63 "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]", 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 "D:[%tD] F:[%1$tF]", 66 "D:[%TD] F:[%1$TF]" 67 }; 68 69 private int total = 0; 70 private int failure = 0; 71 private boolean verbose = false; 72 73 @DataProvider(name = "calendarsByLocale") 74 Object[][] data_calendars() { 75 return new Object[][] { 76 {"en_US"}, 77 {"th_TH"}, 78 {"ja-JP-u-ca-japanese"}, 79 }; 80 } 81 82 @Test(dataProvider="calendarsByLocale") 83 public void test (String calendarLocale) { 84 failure = 0; 85 int N = 12; 86 //locales = Locale.getAvailableLocales(); 87 Locale[] locales = new Locale[] { 88 Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE}; 89 Random r = new Random(); 90 91 Locale calLocale = Locale.forLanguageTag(calendarLocale); 92 Chronology chrono = Chronology.ofLocale(calLocale); 93 ChronoLocalDate now = chrono.dateNow(); 94 ChronoLocalDateTime<?> ldt0 = now.atTime(LocalTime.now()); 95 ChronoZonedDateTime<?> zdt0 = ldt0.atZone(ZoneId.systemDefault()); 96 ChronoZonedDateTime<?>[] zdts = new ChronoZonedDateTime<?>[] { 97 zdt0, 98 zdt0.withZoneSameLocal(ZoneId.of("UTC")), 99 zdt0.withZoneSameLocal(ZoneId.of("GMT")), 100 zdt0.withZoneSameLocal(ZoneId.of("UT")), 101 }; 102 103 while (N-- > 0) { 104 for (ChronoZonedDateTime<?> zdt : zdts) { 105 zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1)) 106 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); 107 Instant instant = zdt.toInstant(); 108 Calendar cal = Calendar.getInstance(calLocale); 109 cal.setTimeInMillis(instant.toEpochMilli()); 110 cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone())); 111 for (Locale locale : locales) { 112 for (String fmtStr : fmtStrDate) { 113 testDate(fmtStr, locale, zdt, cal); 114 } 115 for (String fmtStr : fmtStrTime) { 116 testTime(fmtStr, locale, zdt, cal); 117 } 118 testZoneId(locale, zdt, cal); 119 testInstant(locale, instant, zdt, cal); 120 } 121 } 122 } 123 if (verbose) { 124 if (failure != 0) { 125 System.out.println("Total " + failure + "/" + total + " tests failed"); 126 } else { 127 System.out.println("All tests (" + total + ") PASSED"); 128 } 129 } 130 assertEquals(failure, 0); 131 } 132 133 private String getClassName(Object o) { 134 Class<?> c = o.getClass(); 135 String clname = c.getName().substring(c.getPackage().getName().length() + 1); 136 if (o instanceof TemporalAccessor) { 137 Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology()); 138 if (chrono != null) { 139 clname = clname + "(" + chrono.getId() + ")"; 140 } 141 } 142 if (o instanceof Calendar) { 143 String type = ((Calendar)o).getCalendarType(); 144 clname = clname + "(" + type + ")"; 145 } 146 return clname; 147 } 148 149 private String test(String fmtStr, Locale locale, 150 String expected, Object dt) { 151 String out = new Formatter( 152 new StringBuilder(), locale).format(fmtStr, dt).out().toString(); 153 if (verbose) { 154 System.out.printf("%-24s : %s%n", getClassName(dt), out); 155 } 156 if (expected != null && !out.equals(expected)) { 157 System.out.printf("%-24s actual: %s%n FAILED; expected: %s%n", 158 getClassName(dt), out, expected); 159 new RuntimeException().printStackTrace(System.out); 160 failure++; 161 } 162 total++; 163 return out; 164 } 165 166 private void printFmtStr(Locale locale, String fmtStr) { 167 if (verbose) { 168 System.out.println("--------------------"); 169 System.out.printf("[%s, %s]%n", locale.toString(), fmtStr); 170 } 171 } 172 173 private void testDate(String fmtStr, Locale locale, 174 ChronoZonedDateTime<?> zdt, Calendar cal) { 175 printFmtStr(locale, fmtStr); 176 String expected = test(fmtStr, locale, null, cal); 177 test(fmtStr, locale, expected, zdt); 178 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 179 test(fmtStr, locale, expected, zdt.toLocalDate()); 180 if (zdt instanceof ZonedDateTime) { 181 test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime()); 182 } 183 } 184 185 private void testTime(String fmtStr, Locale locale, 186 ChronoZonedDateTime<?> zdt, Calendar cal) { 187 printFmtStr(locale, fmtStr); 188 String expected = test(fmtStr, locale, null, cal); 189 test(fmtStr, locale, expected, zdt); 190 test(fmtStr, locale, expected, zdt.toLocalDateTime()); 191 test(fmtStr, locale, expected, zdt.toLocalTime()); 192 if (zdt instanceof ZonedDateTime) { 193 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 194 test(fmtStr, locale, expected, odt); 195 test(fmtStr, locale, expected, odt.toOffsetTime()); 196 } 197 } 198 199 private String toZoneIdStr(String expected) { 200 return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}"); 201 } 202 203 private String toZoneOffsetStr(String expected) { 204 return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}") 205 .replaceAll("GMT|UTC|UT", "Z"); 206 } 207 208 private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) { 209 String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; 210 printFmtStr(locale, fmtStr); 211 String expected = toZoneIdStr(test(fmtStr, locale, null, cal)); 212 test(fmtStr, locale, expected, zdt); 213 // get a new cal with fixed tz 214 Calendar cal0 = Calendar.getInstance(); 215 cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); 216 cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); 217 expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); 218 if (zdt instanceof ZonedDateTime) { 219 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 220 test(fmtStr, locale, expected, odt); 221 test(fmtStr, locale, expected, odt.toOffsetTime()); 222 } 223 224 // datetime + zid 225 fmtStr = "c:[%tc] c:[%1$Tc]"; 226 printFmtStr(locale, fmtStr); 227 expected = toZoneIdStr(test(fmtStr, locale, null, cal)); 228 test(fmtStr, locale, expected, zdt); 229 } 230 231 private void testInstant(Locale locale, Instant instant, 232 ChronoZonedDateTime<?> zdt, Calendar cal) { 233 String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]"; 234 printFmtStr(locale, fmtStr); 235 String expected = test(fmtStr, locale, null, cal); 236 test(fmtStr, locale, expected, instant); 237 test(fmtStr, locale, expected, zdt); 238 if (zdt instanceof ZonedDateTime) { 239 OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); 240 test(fmtStr, locale, expected, odt); 241 } 242 } 243} 244