• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 import java.time.temporal.UnsupportedTemporalTypeException;
42 
43 import java.util.*;
44 
45 import org.testng.annotations.DataProvider;
46 import 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
53 public 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")
data_calendars()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")
test(String calendarLocale)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         // Android-changed: use hard-coded zone id instead of system default.
97         ChronoZonedDateTime<?>  zdt0 = ldt0.atZone(ZoneId.of("America/Los_Angeles"));
98         ChronoZonedDateTime<?>[] zdts = new ChronoZonedDateTime<?>[] {
99             zdt0,
100             zdt0.withZoneSameLocal(ZoneId.of("UTC")),
101             zdt0.withZoneSameLocal(ZoneId.of("GMT")),
102             zdt0.withZoneSameLocal(ZoneId.of("UT")),
103         };
104 
105         while (N-- > 0) {
106             for (ChronoZonedDateTime<?> zdt : zdts) {
107                 zdt = zdt.with(ChronoField.DAY_OF_YEAR, (r.nextInt(365) + 1))
108                          .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
109                 Instant instant = zdt.toInstant();
110                 // Android-changed: Calendar.getInstance() only returns GregorianCalendar.
111                 // Manually get a JapaneseImperialCalendar for the test.
112                 TimeZone tz = TimeZone.getTimeZone(zdt.getZone());
113                 Calendar cal;
114                 if (calLocale.getLanguage().equals("ja")) {
115                     cal = Calendar.getJapaneseImperialInstance(tz, calLocale);
116                 } else {
117                     cal = Calendar.getInstance(tz, calLocale);
118                 }
119                 cal.setTimeInMillis(instant.toEpochMilli());
120                 for (Locale locale : locales) {
121                     for (String fmtStr : fmtStrDate) {
122                         testDate(fmtStr, locale, zdt, cal);
123                     }
124                     for (String fmtStr : fmtStrTime) {
125                         testTime(fmtStr, locale, zdt, cal);
126                     }
127                     testZoneId(locale, zdt, cal);
128                     testInstant(locale, instant, zdt, cal);
129                 }
130             }
131         }
132         if (verbose) {
133             if (failure != 0) {
134                 System.out.println("Total " + failure + "/" + total + " tests failed");
135             } else {
136                 System.out.println("All tests (" + total + ") PASSED");
137             }
138         }
139         assertEquals(failure, 0);
140     }
141 
getClassName(Object o)142     private String getClassName(Object o) {
143         Class<?> c = o.getClass();
144         String clname = c.getName().substring(c.getPackage().getName().length() + 1);
145         if (o instanceof TemporalAccessor) {
146             Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology());
147             if (chrono != null) {
148                 clname = clname + "(" + chrono.getId() + ")";
149             }
150         }
151         if (o instanceof Calendar) {
152             String type = ((Calendar)o).getCalendarType();
153             clname = clname + "(" + type + ")";
154         }
155         return clname;
156     }
157 
test(String fmtStr, Locale locale, String expected, Object dt)158     private String test(String fmtStr, Locale locale,
159                                String expected, Object dt) {
160         String out = new Formatter(
161             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
162         if (verbose) {
163             System.out.printf("%-24s  : %s%n", getClassName(dt), out);
164         }
165 
166         // expected usually comes from Calendar which only has milliseconds
167         // precision. So we're going to replace it's N:[nanos] stamp with
168         // the correct value for nanos.
169         if ((dt instanceof TemporalAccessor) && expected != null) {
170             try {
171                 // Get millis & nanos from the dt
172                 final TemporalAccessor ta = (TemporalAccessor) dt;
173                 // Android-changed: Formatter doesn't support nanosecond yet.
174                 // TODO: Revert this patch when Formatter is upgraded from 8 to 11 version.
175                 // final int nanos = ta.get(ChronoField.NANO_OF_SECOND);
176                 final int millis = ta.get(ChronoField.MILLI_OF_SECOND);
177                 final int nanos = millis * 1000000;
178                 final String nanstr = String.valueOf(nanos);
179                 final String mistr = String.valueOf(millis);
180 
181                 // Compute the value of the N:[nanos] field that we expect
182                 // to find in 'out'
183                 final StringBuilder sb = new StringBuilder();
184                 sb.append("N:[");
185                 for (int i=nanstr.length(); i<9; i++) {
186                     sb.append('0');
187                 }
188                 sb.append(nanos).append("]");
189 
190                 // Compute the truncated value of N:[nanos] field that might
191                 // be in 'expected' when expected was built from Calendar.
192                 final StringBuilder sbm = new StringBuilder();
193                 sbm.append("N:[");
194                 for (int i=mistr.length(); i<3; i++) {
195                     sbm.append('0');
196                 }
197                 sbm.append(mistr).append("000000]");
198 
199                 // if expected contains the truncated value, replace it with
200                 // the complete value.
201                 expected = expected.replace(sbm.toString(), sb.toString());
202             } catch (UnsupportedTemporalTypeException e) {
203                 // nano seconds unsupported - nothing to do...
204             }
205         }
206         if (expected != null && !out.equals(expected)) {
207             System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
208                               getClassName(dt), out, expected);
209             new RuntimeException().printStackTrace(System.out);
210             failure++;
211         }
212         total++;
213         return out;
214     }
215 
printFmtStr(Locale locale, String fmtStr)216     private void printFmtStr(Locale locale, String fmtStr) {
217         if (verbose) {
218             System.out.println("--------------------");
219             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
220         }
221     }
222 
testDate(String fmtStr, Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)223     private void testDate(String fmtStr, Locale locale,
224                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
225         printFmtStr(locale, fmtStr);
226         String expected = test(fmtStr, locale, null, cal);
227         test(fmtStr, locale, expected, zdt);
228         test(fmtStr, locale, expected, zdt.toLocalDateTime());
229         test(fmtStr, locale, expected, zdt.toLocalDate());
230         if (zdt instanceof ZonedDateTime) {
231             test(fmtStr, locale, expected, ((ZonedDateTime)zdt).toOffsetDateTime());
232         }
233     }
234 
testTime(String fmtStr, Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)235     private void testTime(String fmtStr, Locale locale,
236                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
237         printFmtStr(locale, fmtStr);
238         String expected = test(fmtStr, locale, null, cal);
239         test(fmtStr, locale, expected, zdt);
240         test(fmtStr, locale, expected, zdt.toLocalDateTime());
241         test(fmtStr, locale, expected, zdt.toLocalTime());
242         if (zdt instanceof ZonedDateTime) {
243             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
244             test(fmtStr, locale, expected, odt);
245             test(fmtStr, locale, expected, odt.toOffsetTime());
246         }
247     }
248 
toZoneOffsetStr(String expected)249     private String toZoneOffsetStr(String expected) {
250         // Android-changed: Android Matcher doesn't support named groups. Also GMT/Z is formatted as
251         // "GMT+00:00" or "GMT".
252         return expected.replaceAll("GMT(?:\\+00:00)?(?=])|UTC|UT", "Z")
253                 .replaceAll("(?:GMT|UTC)([+\\-]?[0-9]{2}:[0-9]{2})", "$1");
254     }
255 
testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal)256     private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) {
257         String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
258         printFmtStr(locale, fmtStr);
259         String expected = test(fmtStr, locale, null, cal);
260         test(fmtStr, locale, expected, zdt);
261         // get a new cal with fixed tz
262         Calendar cal0 = Calendar.getInstance();
263         cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
264         cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
265         expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
266         if (zdt instanceof ZonedDateTime) {
267             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
268             test(fmtStr, locale, expected, odt);
269             test(fmtStr, locale, expected, odt.toOffsetTime());
270         }
271 
272         // datetime + zid
273         fmtStr = "c:[%tc] c:[%1$Tc]";
274         printFmtStr(locale, fmtStr);
275         expected = test(fmtStr, locale, null, cal);
276         test(fmtStr, locale, expected, zdt);
277     }
278 
testInstant(Locale locale, Instant instant, ChronoZonedDateTime<?> zdt, Calendar cal)279     private void testInstant(Locale locale, Instant instant,
280                              ChronoZonedDateTime<?> zdt, Calendar cal) {
281         String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
282         printFmtStr(locale, fmtStr);
283         String expected = test(fmtStr, locale, null, cal);
284         test(fmtStr, locale, expected, instant);
285         test(fmtStr, locale, expected, zdt);
286         if (zdt instanceof ZonedDateTime) {
287             OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime();
288             test(fmtStr, locale, expected, odt);
289         }
290     }
291 }
292