Home | History | Annotate | Download | only in util
      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  */
     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 
     42 import java.util.*;
     43 
     44 import org.testng.annotations.DataProvider;
     45 import 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
     52 public 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         // Android-changed: use hard-coded zone id instead of system default.
     96         ChronoZonedDateTime<?>  zdt0 = ldt0.atZone(ZoneId.of("America/Los_Angeles"));
     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                 // Android-changed: Calendar.getInstance() only returns GregorianCalendar.
    110                 // Manually get a JapaneseImperialCalendar for the test.
    111                 TimeZone tz = TimeZone.getTimeZone(zdt.getZone());
    112                 Calendar cal;
    113                 if (calLocale.getLanguage().equals("ja")) {
    114                     cal = Calendar.getJapaneseImperialInstance(tz, calLocale);
    115                 } else {
    116                     cal = Calendar.getInstance(tz, calLocale);
    117                 }
    118                 cal.setTimeInMillis(instant.toEpochMilli());
    119                 for (Locale locale : locales) {
    120                     for (String fmtStr : fmtStrDate) {
    121                         testDate(fmtStr, locale, zdt, cal);
    122                     }
    123                     for (String fmtStr : fmtStrTime) {
    124                         testTime(fmtStr, locale, zdt, cal);
    125                     }
    126                     testZoneId(locale, zdt, cal);
    127                     testInstant(locale, instant, zdt, cal);
    128                 }
    129             }
    130         }
    131         if (verbose) {
    132             if (failure != 0) {
    133                 System.out.println("Total " + failure + "/" + total + " tests failed");
    134             } else {
    135                 System.out.println("All tests (" + total + ") PASSED");
    136             }
    137         }
    138         assertEquals(failure, 0);
    139     }
    140 
    141     private String getClassName(Object o) {
    142         Class<?> c = o.getClass();
    143         String clname = c.getName().substring(c.getPackage().getName().length() + 1);
    144         if (o instanceof TemporalAccessor) {
    145             Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology());
    146             if (chrono != null) {
    147                 clname = clname + "(" + chrono.getId() + ")";
    148             }
    149         }
    150         if (o instanceof Calendar) {
    151             String type = ((Calendar)o).getCalendarType();
    152             clname = clname + "(" + type + ")";
    153         }
    154         return clname;
    155     }
    156 
    157     private String test(String fmtStr, Locale locale,
    158                                String expected, Object dt) {
    159         String out = new Formatter(
    160             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
    161         if (verbose) {
    162             System.out.printf("%-24s  : %s%n", getClassName(dt), out);
    163         }
    164         if (expected != null && !out.equals(expected)) {
    165             System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
    166                               getClassName(dt), out, expected);
    167             new RuntimeException().printStackTrace(System.out);
    168             failure++;
    169         }
    170         total++;
    171         return out;
    172     }
    173 
    174     private void printFmtStr(Locale locale, String fmtStr) {
    175         if (verbose) {
    176             System.out.println("--------------------");
    177             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
    178         }
    179     }
    180 
    181     private void testDate(String fmtStr, Locale locale,
    182                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
    183         printFmtStr(locale, fmtStr);
    184         String expected = test(fmtStr, locale, null, cal);
    185         test(fmtStr, locale, expected, zdt);
    186         test(fmtStr, locale, expected, zdt.toLocalDateTime());
    187         test(fmtStr, locale, expected, zdt.toLocalDate());
    188         if (zdt instanceof ZonedDateTime) {
    189             test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime());
    190         }
    191     }
    192 
    193     private void testTime(String fmtStr, Locale locale,
    194                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
    195         printFmtStr(locale, fmtStr);
    196         String expected = test(fmtStr, locale, null, cal);
    197         test(fmtStr, locale, expected, zdt);
    198         test(fmtStr, locale, expected, zdt.toLocalDateTime());
    199         test(fmtStr, locale, expected, zdt.toLocalTime());
    200         if (zdt instanceof ZonedDateTime) {
    201             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
    202             test(fmtStr, locale, expected, odt);
    203             test(fmtStr, locale, expected, odt.toOffsetTime());
    204         }
    205     }
    206 
    207     private String toZoneIdStr(String expected) {
    208         // Android-changed: java.util.Calendar and java.time types are formatted identically.
    209         return expected;
    210     }
    211 
    212     private String toZoneOffsetStr(String expected) {
    213         // Android-changed: Android Matcher doesn't support named groups. Also GMT/Z is formatted as
    214         // "GMT+00:00" or "GMT".
    215         return expected.replaceAll("GMT(?:\\+00:00)?(?=])|UTC|UT", "Z")
    216                 .replaceAll("(?:GMT|UTC)([+\\-]?[0-9]{2}:[0-9]{2})", "$1");
    217     }
    218 
    219     private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) {
    220         String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
    221         printFmtStr(locale, fmtStr);
    222         String calOutput = test(fmtStr, locale, null, cal);
    223         String expected = toZoneIdStr(calOutput);
    224         test(fmtStr, locale, expected, zdt);
    225         // get a new cal with fixed tz
    226         Calendar cal0 = Calendar.getInstance();
    227         cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
    228         cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
    229         expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
    230         if (zdt instanceof ZonedDateTime) {
    231             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
    232             test(fmtStr, locale, expected, odt);
    233             test(fmtStr, locale, expected, odt.toOffsetTime());
    234         }
    235 
    236         // datetime + zid
    237         fmtStr = "c:[%tc] c:[%1$Tc]";
    238         printFmtStr(locale, fmtStr);
    239         expected = toZoneIdStr(test(fmtStr, locale, null, cal));
    240         test(fmtStr, locale, expected, zdt);
    241     }
    242 
    243     private void testInstant(Locale locale, Instant instant,
    244                              ChronoZonedDateTime<?> zdt, Calendar cal) {
    245         String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
    246         printFmtStr(locale, fmtStr);
    247         String expected = test(fmtStr, locale, null, cal);
    248         test(fmtStr, locale, expected, instant);
    249         test(fmtStr, locale, expected, zdt);
    250         if (zdt instanceof ZonedDateTime) {
    251             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
    252             test(fmtStr, locale, expected, odt);
    253         }
    254     }
    255 }
    256