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

Print this page




   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 }


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

  96                 return;
  97             }
  98             System.out.println("----------------");
  99             System.out.printf("tdz[%s]%n", zdt.toString());
 100             System.out.printf("[%-4s, %5s] :[%s]%n", locale.toString(), style.toString(),result);
 101             System.out.printf("%4s, %5s  :[%s]%n", "", "", expected);
 102         }
 103         assertEquals(result, expected);
 104     }
 105 
 106     public void test_ParseText() {
 107         Locale[] locales = new Locale[] { Locale.ENGLISH, Locale.JAPANESE, Locale.FRENCH };
 108         Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
 109         for (Locale locale : locales) {
 110             parseText(zids, locale, TextStyle.FULL, false);
 111             parseText(zids, locale, TextStyle.FULL, true);
 112             parseText(zids, locale, TextStyle.SHORT, false);
 113             parseText(zids, locale, TextStyle.SHORT, true);
 114         }
 115     }
 116 
 117     private static Set<ZoneId> preferred = new HashSet<>(Arrays.asList(new ZoneId[] {
 118         ZoneId.of("EST"),
 119         ZoneId.of("Asia/Taipei"),
 120         ZoneId.of("CET"),
 121     }));
 122 
 123     private static Set<ZoneId> preferred_s = new HashSet<>(Arrays.asList(new ZoneId[] {
 124          ZoneId.of("EST"),
 125          ZoneId.of("CET"),
 126          ZoneId.of("Australia/South"),
 127          ZoneId.of("Australia/West"),
 128          ZoneId.of("Asia/Shanghai"),
 129     }));
 130 
 131     private static Set<ZoneId> none = new HashSet<>();
 132 
 133     @DataProvider(name="preferredZones")
 134     Object[][] data_preferredZones() {
 135         return new Object[][] {
 136             {"America/New_York", "Eastern Standard Time", none,      Locale.ENGLISH, TextStyle.FULL},
 137             {"EST",              "Eastern Standard Time", preferred, Locale.ENGLISH, TextStyle.FULL},
 138             {"Europe/Paris",     "Central European Time", none,      Locale.ENGLISH, TextStyle.FULL},
 139             {"CET",              "Central European Time", preferred, Locale.ENGLISH, TextStyle.FULL},
 140             {"Asia/Shanghai",    "China Standard Time",   none,      Locale.ENGLISH, TextStyle.FULL},
 141             {"Asia/Taipei",      "China Standard Time",   preferred, Locale.ENGLISH, TextStyle.FULL},
 142             {"America/Chicago",  "CST",                   none,      Locale.ENGLISH, TextStyle.SHORT},
 143             {"Asia/Taipei",      "CST",                   preferred, Locale.ENGLISH, TextStyle.SHORT},
 144             {"Australia/South",  "CST",                   preferred_s, Locale.ENGLISH, TextStyle.SHORT},
 145             {"America/Chicago",  "CDT",                   none,        Locale.ENGLISH, TextStyle.SHORT},
 146             {"Asia/Shanghai",    "CDT",                   preferred_s, Locale.ENGLISH, TextStyle.SHORT},
 147        };
 148     }
 149 
 150     @Test(dataProvider="preferredZones")
 151     public void test_ParseText(String expected, String text, Set<ZoneId> preferred, Locale locale, TextStyle style) {
 152         DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneText(style, preferred)
 153                                                               .toFormatter(locale)
 154                                                               .withSymbols(DateTimeFormatSymbols.of(locale));
 155 
 156         String ret = fmt.parse(text, Queries.zone()).getId();
 157 
 158         System.out.printf("[%-5s %s] %24s -> %s(%s)%n",
 159                           locale.toString(),
 160                           style == TextStyle.FULL ? " full" :"short",
 161                           text, ret, expected);
 162 
 163         assertEquals(ret, expected);
 164 
 165     }
 166 
 167 
 168     private void parseText(Set<String> zids, Locale locale, TextStyle style, boolean ci) {
 169         System.out.println("---------------------------------------");
 170         DateTimeFormatter fmt = getFormatter(locale, style, ci);
 171         for (String[] names : new DateFormatSymbols(locale).getZoneStrings()) {
 172             if (!zids.contains(names[0])) {
 173                 continue;
 174             }
 175             String zid = names[0];
 176             String expected = ZoneName.toZid(zid, locale);
 177 
 178             parse(fmt, zid, expected, zid, locale, style, ci);
 179             int i = style == TextStyle.FULL ? 1 : 2;
 180             for (; i < names.length; i += 2) {
 181                 parse(fmt, zid, expected, names[i], locale, style, ci);
 182             }
 183         }
 184     }
 185 
 186     private void parse(DateTimeFormatter fmt,
 187                        String zid, String expected, String text,
 188                        Locale locale, TextStyle style, boolean ci) {
 189         if (ci) {
 190             text = text.toUpperCase();
 191         }
 192         String ret = fmt.parse(text, Queries.zone()).getId();
 193         // TBD: need an excluding list
 194         // assertEquals(...);
 195         if (ret.equals(expected) ||
 196             ret.equals(zid) ||
 197             ret.equals(ZoneName.toZid(zid)) ||
 198             ret.equals(expected.replace("UTC", "UCT"))) {
 199             return;
 200         }
 201         System.out.printf("[%-5s %s %s %16s] %24s -> %s(%s)%n",
 202                           locale.toString(),
 203                           ci ? "ci" : "  ",
 204                           style == TextStyle.FULL ? " full" :"short",
 205                           zid, text, ret, expected);
 206     }
 207 
 208     private DateTimeFormatter getFormatter(Locale locale, TextStyle style, boolean ci) {
 209         DateTimeFormatterBuilder db = new DateTimeFormatterBuilder();
 210         if (ci) {
 211             db = db.parseCaseInsensitive();
 212         }
 213         return db.appendZoneText(style)
 214                  .toFormatter(locale)
 215                  .withSymbols(DateTimeFormatSymbols.of(locale));
 216     }
 217 
 218 }