< prev index next >

test/java/time/test/java/time/format/TestZoneTextPrinterParser.java

Print this page


   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  */


  32 import java.time.format.DateTimeFormatter;
  33 import java.time.format.DateTimeFormatterBuilder;
  34 import java.time.format.TextStyle;
  35 import java.time.temporal.ChronoField;
  36 import java.time.temporal.TemporalQueries;
  37 import java.time.zone.ZoneRulesProvider;
  38 import java.util.Arrays;
  39 import java.util.Date;
  40 import java.util.HashSet;
  41 import java.util.Locale;
  42 import java.util.Random;
  43 import java.util.Set;
  44 import java.util.TimeZone;
  45 import jdk.testlibrary.RandomFactory;
  46 
  47 import org.testng.annotations.DataProvider;
  48 import org.testng.annotations.Test;
  49 
  50 /*
  51  * @test
  52  * @bug 8081022
  53  * @key randomness
  54  */
  55 
  56 /**
  57  * Test ZoneTextPrinterParser
  58  */
  59 @Test
  60 public class TestZoneTextPrinterParser extends AbstractTestPrinterParser {
  61 
  62     protected static DateTimeFormatter getFormatter(Locale locale, TextStyle style) {
  63         return new DateTimeFormatterBuilder().appendZoneText(style)
  64                                              .toFormatter(locale)
  65                                              .withDecimalStyle(DecimalStyle.of(locale));
  66     }
  67 
  68     public void test_printText() {
  69         Random r = RandomFactory.getRandom();
  70         int N = 8;
  71         Locale[] locales = Locale.getAvailableLocales();
  72         Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
  73         ZonedDateTime zdt = ZonedDateTime.now();
  74 
  75         //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
  76         while (N-- > 0) {
  77             zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
  78                      .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
  79             for (String zid : zids) {
  80                 if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
  81                     continue;      // TBD: match jdk behavior?
  82                 }
  83                 zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
  84                 TimeZone tz = TimeZone.getTimeZone(zid);
  85                 boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
  86                 for (Locale locale : locales) {








  87                     printText(locale, zdt, TextStyle.FULL, tz,
  88                             tz.getDisplayName(isDST, TimeZone.LONG, locale));
  89                     printText(locale, zdt, TextStyle.SHORT, tz,
  90                             tz.getDisplayName(isDST, TimeZone.SHORT, locale));
  91                 }
  92             }
  93         }
  94     }
  95 
  96     private void printText(Locale locale, ZonedDateTime zdt, TextStyle style, TimeZone zone, String expected) {
  97         String result = getFormatter(locale, style).format(zdt);
  98         if (!result.equals(expected)) {
  99             if (result.equals("FooLocation")) { // from rules provider test if same vm
 100                 return;
 101             }
 102             System.out.println("----------------");
 103             System.out.printf("tdz[%s]%n", zdt.toString());
 104             System.out.printf("[%-5s, %5s] :[%s]%n", locale.toString(), style.toString(),result);
 105             System.out.printf(" %5s, %5s  :[%s] %s%n", "", "", expected, zone);
 106         }


   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  */


  32 import java.time.format.DateTimeFormatter;
  33 import java.time.format.DateTimeFormatterBuilder;
  34 import java.time.format.TextStyle;
  35 import java.time.temporal.ChronoField;
  36 import java.time.temporal.TemporalQueries;
  37 import java.time.zone.ZoneRulesProvider;
  38 import java.util.Arrays;
  39 import java.util.Date;
  40 import java.util.HashSet;
  41 import java.util.Locale;
  42 import java.util.Random;
  43 import java.util.Set;
  44 import java.util.TimeZone;
  45 import jdk.testlibrary.RandomFactory;
  46 
  47 import org.testng.annotations.DataProvider;
  48 import org.testng.annotations.Test;
  49 
  50 /*
  51  * @test
  52  * @bug 8081022 8151876
  53  * @key randomness
  54  */
  55 
  56 /**
  57  * Test ZoneTextPrinterParser
  58  */
  59 @Test
  60 public class TestZoneTextPrinterParser extends AbstractTestPrinterParser {
  61 
  62     protected static DateTimeFormatter getFormatter(Locale locale, TextStyle style) {
  63         return new DateTimeFormatterBuilder().appendZoneText(style)
  64                                              .toFormatter(locale)
  65                                              .withDecimalStyle(DecimalStyle.of(locale));
  66     }
  67 
  68     public void test_printText() {
  69         Random r = RandomFactory.getRandom();
  70         int N = 8;
  71         Locale[] locales = Locale.getAvailableLocales();
  72         Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
  73         ZonedDateTime zdt = ZonedDateTime.now();
  74 
  75         //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
  76         while (N-- > 0) {
  77             zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
  78                      .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
  79             for (String zid : zids) {
  80                 if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
  81                     continue;      // TBD: match jdk behavior?
  82                 }
  83                 zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
  84                 TimeZone tz = TimeZone.getTimeZone(zid);
  85                 boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
  86                 for (Locale locale : locales) {
  87                     String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
  88                     String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
  89                     if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
  90                             || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
  91                         printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
  92                         printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
  93                         continue;
  94                     }
  95                     printText(locale, zdt, TextStyle.FULL, tz,
  96                             tz.getDisplayName(isDST, TimeZone.LONG, locale));
  97                     printText(locale, zdt, TextStyle.SHORT, tz,
  98                             tz.getDisplayName(isDST, TimeZone.SHORT, locale));
  99                 }
 100             }
 101         }
 102     }
 103 
 104     private void printText(Locale locale, ZonedDateTime zdt, TextStyle style, TimeZone zone, String expected) {
 105         String result = getFormatter(locale, style).format(zdt);
 106         if (!result.equals(expected)) {
 107             if (result.equals("FooLocation")) { // from rules provider test if same vm
 108                 return;
 109             }
 110             System.out.println("----------------");
 111             System.out.printf("tdz[%s]%n", zdt.toString());
 112             System.out.printf("[%-5s, %5s] :[%s]%n", locale.toString(), style.toString(),result);
 113             System.out.printf(" %5s, %5s  :[%s] %s%n", "", "", expected, zone);
 114         }


< prev index next >