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