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