1 /* 2 * Copyright (c) 2012, 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 24 package test.java.time.format; 25 26 import java.util.Date; 27 import java.util.Locale; 28 import java.util.Random; 29 import java.util.Set; 30 import java.util.TimeZone; 31 32 import java.time.ZonedDateTime; 33 import java.time.ZoneId; 34 import java.time.temporal.ChronoField; 35 import java.time.format.DateTimeFormatSymbols; 36 import java.time.format.DateTimeFormatter; 37 import java.time.format.DateTimeFormatterBuilder; 38 import java.time.format.TextStyle; 39 import java.time.zone.ZoneRulesProvider; 40 41 import org.testng.annotations.Test; 42 import static org.testng.Assert.assertEquals; 43 44 /** 45 * Test ZoneTextPrinterParser 46 */ 47 @Test(groups={"implementation"}) 48 public class TestZoneTextPrinterParser extends AbstractTestPrinterParser { 49 50 protected static DateTimeFormatter getFormatter(Locale locale, TextStyle style) { 51 return new DateTimeFormatterBuilder().appendZoneText(style) 52 .toFormatter(locale) 53 .withSymbols(DateTimeFormatSymbols.of(locale)); 54 } 55 56 public void test_printText() { 57 Random r = new Random(); 58 int N = 50; 59 Locale[] locales = Locale.getAvailableLocales(); 60 Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); 61 ZonedDateTime zdt = ZonedDateTime.now(); 62 63 //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size()); 64 while (N-- > 0) { 65 zdt = zdt.withDayOfYear(r.nextInt(365) + 1) 66 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); 67 for (String zid : zids) { 68 zdt = zdt.withZoneSameLocal(ZoneId.of(zid)); 69 TimeZone tz = TimeZone.getTimeZone(zid); 70 boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli())); 71 for (Locale locale : locales) { 72 printText(locale, zdt, TextStyle.FULL, 73 tz.getDisplayName(isDST, TimeZone.LONG, locale)); 74 printText(locale, zdt, TextStyle.SHORT, 75 tz.getDisplayName(isDST, TimeZone.SHORT, locale)); 76 } 77 } 78 } 79 } 80 81 private void printText(Locale locale, ZonedDateTime zdt, TextStyle style, String expected) { 82 String result = getFormatter(locale, style).print(zdt); 83 if (!result.equals(expected)) { 84 if (result.equals("FooLocation") || // from rules provider test if same vm 85 result.startsWith("Etc/GMT") || result.equals("ROC")) { // TBD: match jdk behavior? 86 return; 87 } 88 System.out.println("----------------"); 89 System.out.printf("tdz[%s]%n", zdt.toString()); 90 System.out.printf("[%-4s, %5s] :[%s]%n", locale.toString(), style.toString(),result); 91 System.out.printf("%4s, %5s :[%s]%n", "", "", expected); 92 } 93 assertEquals(result, expected); 94 } 95 }