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 package test.java.util;
  24 
  25 import java.time.Instant;
  26 import java.time.OffsetDateTime;
  27 import java.time.ZonedDateTime;
  28 import java.time.ZoneId;
  29 import java.time.temporal.ChronoField;
  30 
  31 import java.util.*;
  32 
  33 import org.testng.annotations.Test;
  34 import static org.testng.Assert.assertEquals;
  35 
  36 /* @test
  37  * @summary Unit test for j.u.Formatter threeten date/time support
  38  * @bug 8012638
  39  */
  40 @Test
  41 public class TestFormatter {
  42 
  43     // time
  44     private static String[] fmtStrTime = new String[] {
  45          "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]",
  46          "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]",
  47          "R:[%tR] T:[%1$tT] r:[%1$tr]",
  48          "R:[%TR] T:[%1$TT] r:[%1$Tr]"
  49     };
  50     // date
  51     private static String[] fmtStrDate = new String[] {
  52         "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]",
  53         "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]",
  54         "D:[%tD] F:[%1$tF]",
  55         "D:[%TD] F:[%1$TF]"
  56     };
  57 
  58     private int total = 0;
  59     private int failure = 0;
  60     private boolean verbose = true;
  61 
  62     @Test
  63     public void test () {
  64 
  65         int N = 12;
  66         //locales = Locale.getAvailableLocales();
  67         Locale[] locales = new Locale[] {
  68            Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};
  69         Random r = new Random();
  70         ZonedDateTime  zdt0 = ZonedDateTime.now();
  71         ZonedDateTime[] zdts = new ZonedDateTime[] {
  72             zdt0,
  73             zdt0.withZoneSameLocal(ZoneId.of("UTC")),
  74             zdt0.withZoneSameLocal(ZoneId.of("GMT")),
  75             zdt0.withZoneSameLocal(ZoneId.of("UT")),
  76         };
  77 
  78         while (N-- > 0) {
  79             for (ZonedDateTime zdt : zdts) {
  80                 zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
  81                          .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
  82                 Instant instant = zdt.toInstant();
  83                 Calendar cal = Calendar.getInstance();
  84                 cal.setTimeInMillis(instant.toEpochMilli());
  85                 cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone()));
  86                 for (Locale locale : locales) {
  87                     for (String fmtStr : fmtStrDate) {
  88                         testDate(fmtStr, locale, zdt, cal);
  89                     }
  90                     for (String fmtStr : fmtStrTime) {
  91                         testTime(fmtStr, locale, zdt, cal);
  92                     }
  93                     testZoneId(locale, zdt, cal);
  94                     testInstant(locale, instant, zdt, cal);
  95                 }
  96             }
  97         }
  98         if (verbose) {
  99             if (failure != 0) {
 100                 System.out.println("Total " + failure + "/" + total + " tests failed");
 101             } else {
 102                 System.out.println("All tests (" + total + ") PASSED");
 103             }
 104         }
 105         assertEquals(failure, 0);
 106     }
 107 
 108     private String getClassName(Object o) {
 109         Class c = o.getClass();
 110         return c.getName().substring(c.getPackage().getName().length() + 1);
 111     }
 112 
 113     private String test(String fmtStr, Locale locale,
 114                                String expected, Object dt) {
 115         String out = new Formatter(
 116             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
 117         if (verbose) {
 118             System.out.printf("%-18s  : %s%n", getClassName(dt), out);
 119         }
 120         if (expected != null && !out.equals(expected)) {
 121             System.out.printf("=====>%-18s  : %s  [ FAILED expected: %s ]%n",
 122                               getClassName(dt), out, expected);
 123             new RuntimeException().printStackTrace();
 124             failure++;
 125         }
 126         total++;
 127         return out;
 128     }
 129 
 130     private void printFmtStr(Locale locale, String fmtStr) {
 131         if (verbose) {
 132             System.out.println("--------------------");
 133             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
 134         }
 135     }
 136 
 137     private void testDate(String fmtStr, Locale locale,
 138                                  ZonedDateTime zdt, Calendar cal) {
 139         printFmtStr(locale, fmtStr);
 140         String expected = test(fmtStr, locale, null, cal);
 141         test(fmtStr, locale, expected, zdt);
 142         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 143         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 144         test(fmtStr, locale, expected, zdt.toLocalDate());
 145     }
 146 
 147     private void testTime(String fmtStr, Locale locale,
 148                                  ZonedDateTime zdt, Calendar cal) {
 149         printFmtStr(locale, fmtStr);
 150         String expected = test(fmtStr, locale, null, cal);
 151         test(fmtStr, locale, expected, zdt);
 152         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 153         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 154         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 155         test(fmtStr, locale, expected, zdt.toLocalTime());
 156     }
 157 
 158     private String toZoneIdStr(String expected) {
 159         return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}");
 160     }
 161 
 162     private String toZoneOffsetStr(String expected) {
 163         return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}")
 164                        .replaceAll("GMT|UTC|UT", "Z");
 165     }
 166 
 167     private void testZoneId(Locale locale, ZonedDateTime zdt, Calendar cal) {
 168         String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
 169         printFmtStr(locale, fmtStr);
 170         String expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 171         test(fmtStr, locale, expected, zdt);
 172         // get a new cal with fixed tz
 173         Calendar cal0 = Calendar.getInstance();
 174         cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
 175         cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
 176         expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
 177         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 178         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 179 
 180         // datetime + zid
 181         fmtStr = "c:[%tc] c:[%1$Tc]";
 182         printFmtStr(locale, fmtStr);
 183         expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 184         test(fmtStr, locale, expected, zdt);
 185     }
 186 
 187     private void testInstant(Locale locale, Instant instant,
 188                              ZonedDateTime zdt, Calendar cal) {
 189         String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
 190         printFmtStr(locale, fmtStr);
 191         String expected = test(fmtStr, locale, null, cal);
 192         test(fmtStr, locale, expected, instant);
 193         test(fmtStr, locale, expected, zdt);
 194         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 195     }
 196 }