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 */
23package test.java.util;
24
25import static org.testng.Assert.assertEquals;
26
27import java.time.Instant;
28import java.time.LocalTime;
29import java.time.OffsetDateTime;
30import java.time.ZonedDateTime;
31import java.time.ZoneId;
32
33import java.time.chrono.ChronoLocalDate;
34import java.time.chrono.ChronoLocalDateTime;
35import java.time.chrono.ChronoZonedDateTime;
36import java.time.chrono.Chronology;
37
38import java.time.temporal.ChronoField;
39import java.time.temporal.TemporalQueries;
40import java.time.temporal.TemporalAccessor;
41import java.time.temporal.UnsupportedTemporalTypeException;
42
43import java.util.*;
44
45import org.testng.annotations.DataProvider;
46import org.testng.annotations.Test;
47
48/* @test
49 * @summary Unit test for j.u.Formatter threeten date/time support
50 * @bug 8003680 8043387 8012638
51 */
52@Test
53public class TestFormatter {
54
55    // time
56    private static String[] fmtStrTime = new String[] {
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         "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]",
59         "R:[%tR] T:[%1$tT] r:[%1$tr]",
60         "R:[%TR] T:[%1$TT] r:[%1$Tr]"
61    };
62    // date
63    private static String[] fmtStrDate = new String[] {
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        "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]",
66        "D:[%tD] F:[%1$tF]",
67        "D:[%TD] F:[%1$TF]"
68    };
69
70    private int total = 0;
71    private int failure = 0;
72    private boolean verbose = false;
73
74    @DataProvider(name = "calendarsByLocale")
75    Object[][] data_calendars() {
76        return new Object[][] {
77            {"en_US"},
78            {"th_TH"},
79            {"ja-JP-u-ca-japanese"},
80        };
81    }
82
83    @Test(dataProvider="calendarsByLocale")
84    public void test (String calendarLocale) {
85        failure = 0;
86        int N = 12;
87        //locales = Locale.getAvailableLocales();
88        Locale[] locales = new Locale[] {
89           Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};
90        Random r = new Random();
91
92        Locale calLocale = Locale.forLanguageTag(calendarLocale);
93        Chronology chrono = Chronology.ofLocale(calLocale);
94        ChronoLocalDate now = chrono.dateNow();
95        ChronoLocalDateTime<?> ldt0 = now.atTime(LocalTime.now());
96        ChronoZonedDateTime<?>  zdt0 = ldt0.atZone(ZoneId.systemDefault());
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                Calendar cal = Calendar.getInstance(calLocale);
110                cal.setTimeInMillis(instant.toEpochMilli());
111                cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone()));
112                for (Locale locale : locales) {
113                    for (String fmtStr : fmtStrDate) {
114                        testDate(fmtStr, locale, zdt, cal);
115                    }
116                    for (String fmtStr : fmtStrTime) {
117                        testTime(fmtStr, locale, zdt, cal);
118                    }
119                    testZoneId(locale, zdt, cal);
120                    testInstant(locale, instant, zdt, cal);
121                }
122            }
123        }
124        if (verbose) {
125            if (failure != 0) {
126                System.out.println("Total " + failure + "/" + total + " tests failed");
127            } else {
128                System.out.println("All tests (" + total + ") PASSED");
129            }
130        }
131        assertEquals(failure, 0);
132    }
133
134    private String getClassName(Object o) {
135        Class<?> c = o.getClass();
136        String clname = c.getName().substring(c.getPackage().getName().length() + 1);
137        if (o instanceof TemporalAccessor) {
138            Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology());
139            if (chrono != null) {
140                clname = clname + "(" + chrono.getId() + ")";
141            }
142        }
143        if (o instanceof Calendar) {
144            String type = ((Calendar)o).getCalendarType();
145            clname = clname + "(" + type + ")";
146        }
147        return clname;
148    }
149
150    private String test(String fmtStr, Locale locale,
151                               String expected, Object dt) {
152        String out = new Formatter(
153            new StringBuilder(), locale).format(fmtStr, dt).out().toString();
154        if (verbose) {
155            System.out.printf("%-24s  : %s%n", getClassName(dt), out);
156        }
157
158        // expected usually comes from Calendar which only has milliseconds
159        // precision. So we're going to replace it's N:[nanos] stamp with
160        // the correct value for nanos.
161        if ((dt instanceof TemporalAccessor) && expected != null) {
162            try {
163                // Get millis & nanos from the dt
164                final TemporalAccessor ta = (TemporalAccessor) dt;
165                final int nanos = ta.get(ChronoField.NANO_OF_SECOND);
166                final int millis = ta.get(ChronoField.MILLI_OF_SECOND);
167                final String nanstr = String.valueOf(nanos);
168                final String mistr = String.valueOf(millis);
169
170                // Compute the value of the N:[nanos] field that we expect
171                // to find in 'out'
172                final StringBuilder sb = new StringBuilder();
173                sb.append("N:[");
174                for (int i=nanstr.length(); i<9; i++) {
175                    sb.append('0');
176                }
177                sb.append(nanos).append("]");
178
179                // Compute the truncated value of N:[nanos] field that might
180                // be in 'expected' when expected was built from Calendar.
181                final StringBuilder sbm = new StringBuilder();
182                sbm.append("N:[");
183                for (int i=mistr.length(); i<3; i++) {
184                    sbm.append('0');
185                }
186                sbm.append(mistr).append("000000]");
187
188                // if expected contains the truncated value, replace it with
189                // the complete value.
190                expected = expected.replace(sbm.toString(), sb.toString());
191            } catch (UnsupportedTemporalTypeException e) {
192                // nano seconds unsupported - nothing to do...
193            }
194        }
195        if (expected != null && !out.equals(expected)) {
196            System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
197                              getClassName(dt), out, expected);
198            new RuntimeException().printStackTrace(System.out);
199            failure++;
200        }
201        total++;
202        return out;
203    }
204
205    private void printFmtStr(Locale locale, String fmtStr) {
206        if (verbose) {
207            System.out.println("--------------------");
208            System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
209        }
210    }
211
212    private void testDate(String fmtStr, Locale locale,
213                                 ChronoZonedDateTime<?> zdt, Calendar cal) {
214        printFmtStr(locale, fmtStr);
215        String expected = test(fmtStr, locale, null, cal);
216        test(fmtStr, locale, expected, zdt);
217        test(fmtStr, locale, expected, zdt.toLocalDateTime());
218        test(fmtStr, locale, expected, zdt.toLocalDate());
219        if (zdt instanceof ZonedDateTime) {
220            test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime());
221        }
222    }
223
224    private void testTime(String fmtStr, Locale locale,
225                                 ChronoZonedDateTime<?> zdt, Calendar cal) {
226        printFmtStr(locale, fmtStr);
227        String expected = test(fmtStr, locale, null, cal);
228        test(fmtStr, locale, expected, zdt);
229        test(fmtStr, locale, expected, zdt.toLocalDateTime());
230        test(fmtStr, locale, expected, zdt.toLocalTime());
231        if (zdt instanceof ZonedDateTime) {
232            OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
233            test(fmtStr, locale, expected, odt);
234            test(fmtStr, locale, expected, odt.toOffsetTime());
235        }
236    }
237
238    private String toZoneOffsetStr(String expected) {
239        return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}")
240                       .replaceAll("GMT|UTC|UT", "Z");
241    }
242
243    private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) {
244        String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
245        printFmtStr(locale, fmtStr);
246        String expected = test(fmtStr, locale, null, cal);
247        test(fmtStr, locale, expected, zdt);
248        // get a new cal with fixed tz
249        Calendar cal0 = Calendar.getInstance();
250        cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
251        cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
252        expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
253        if (zdt instanceof ZonedDateTime) {
254            OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
255            test(fmtStr, locale, expected, odt);
256            test(fmtStr, locale, expected, odt.toOffsetTime());
257        }
258
259        // datetime + zid
260        fmtStr = "c:[%tc] c:[%1$Tc]";
261        printFmtStr(locale, fmtStr);
262        expected = test(fmtStr, locale, null, cal);
263        test(fmtStr, locale, expected, zdt);
264    }
265
266    private void testInstant(Locale locale, Instant instant,
267                             ChronoZonedDateTime<?> zdt, Calendar cal) {
268        String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
269        printFmtStr(locale, fmtStr);
270        String expected = test(fmtStr, locale, null, cal);
271        test(fmtStr, locale, expected, instant);
272        test(fmtStr, locale, expected, zdt);
273        if (zdt instanceof ZonedDateTime) {
274            OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
275            test(fmtStr, locale, expected, odt);
276        }
277    }
278}
279