1 /*
2 * Copyright (c) 2012, 2013, 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 */
76 import java.time.OffsetTime;
77 import java.time.Year;
78 import java.time.YearMonth;
79 import java.time.ZoneId;
80 import java.time.ZoneOffset;
81 import java.time.ZonedDateTime;
82 import java.time.chrono.Chronology;
83 import java.time.chrono.ThaiBuddhistChronology;
84 import java.time.format.DateTimeFormatter;
85 import java.time.format.DateTimeFormatterBuilder;
86 import java.time.format.DecimalStyle;
87 import java.time.format.SignStyle;
88 import java.time.format.TextStyle;
89 import java.time.temporal.Temporal;
90 import java.time.temporal.TemporalAccessor;
91 import java.util.Locale;
92 import java.util.function.Function;
93
94 import org.testng.annotations.DataProvider;
95 import org.testng.annotations.Test;
96
97 /**
98 * Test DateTimeFormatter.
99 * @bug 8085887
100 */
101 @Test
102 public class TestDateTimeFormatter {
103
104 @Test
105 public void test_withLocale_same() throws Exception {
106 DateTimeFormatter base =
107 new DateTimeFormatterBuilder().appendLiteral("ONE")
108 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
109 .toFormatter(Locale.ENGLISH)
110 .withDecimalStyle(DecimalStyle.STANDARD);
111 DateTimeFormatter test = base.withLocale(Locale.ENGLISH);
112 assertSame(test, base);
113 }
114
115 @Test
116 public void test_parse_errorMessage() throws Exception {
117 assertGoodErrorDate(DayOfWeek::from, "DayOfWeek");
118 assertGoodErrorDate(Month::from, "Month");
119 assertGoodErrorDate(YearMonth::from, "YearMonth");
253
254 // Test cases that should throw an exception with a cogent message when missing the ZoneId
255 @Test
256 public void test_throws_message_zone() {
257 ZoneId zone = ZoneId.of("Pacific/Honolulu");
258 DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendChronologyId().toFormatter()
259 .withZone(zone);
260 LocalTime now = LocalTime.now();
261 try {
262 fmt.format(now);
263 fail("Format using appendChronologyId() should have failed");
264 } catch (DateTimeException dte) {
265 String msg = dte.getMessage();
266 // Verify message contains the type that is missing and the temporal value
267 assertTrue(msg.contains("Chronology"),
268 String.format("\"%s\" missing from %s", "Chronology", msg));
269 assertTrue(msg.contains(zone.toString()),
270 String.format("\"%s\" missing from %s", zone.toString(), msg));
271 }
272
273 }
274
275 }
|
1 /*
2 * Copyright (c) 2012, 2018, 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 */
76 import java.time.OffsetTime;
77 import java.time.Year;
78 import java.time.YearMonth;
79 import java.time.ZoneId;
80 import java.time.ZoneOffset;
81 import java.time.ZonedDateTime;
82 import java.time.chrono.Chronology;
83 import java.time.chrono.ThaiBuddhistChronology;
84 import java.time.format.DateTimeFormatter;
85 import java.time.format.DateTimeFormatterBuilder;
86 import java.time.format.DecimalStyle;
87 import java.time.format.SignStyle;
88 import java.time.format.TextStyle;
89 import java.time.temporal.Temporal;
90 import java.time.temporal.TemporalAccessor;
91 import java.util.Locale;
92 import java.util.function.Function;
93
94 import org.testng.annotations.DataProvider;
95 import org.testng.annotations.Test;
96 import static org.testng.Assert.assertEquals;
97
98 /**
99 * Test DateTimeFormatter.
100 * @bug 8085887 8193877
101 */
102 @Test
103 public class TestDateTimeFormatter {
104
105 @Test
106 public void test_withLocale_same() throws Exception {
107 DateTimeFormatter base =
108 new DateTimeFormatterBuilder().appendLiteral("ONE")
109 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
110 .toFormatter(Locale.ENGLISH)
111 .withDecimalStyle(DecimalStyle.STANDARD);
112 DateTimeFormatter test = base.withLocale(Locale.ENGLISH);
113 assertSame(test, base);
114 }
115
116 @Test
117 public void test_parse_errorMessage() throws Exception {
118 assertGoodErrorDate(DayOfWeek::from, "DayOfWeek");
119 assertGoodErrorDate(Month::from, "Month");
120 assertGoodErrorDate(YearMonth::from, "YearMonth");
254
255 // Test cases that should throw an exception with a cogent message when missing the ZoneId
256 @Test
257 public void test_throws_message_zone() {
258 ZoneId zone = ZoneId.of("Pacific/Honolulu");
259 DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendChronologyId().toFormatter()
260 .withZone(zone);
261 LocalTime now = LocalTime.now();
262 try {
263 fmt.format(now);
264 fail("Format using appendChronologyId() should have failed");
265 } catch (DateTimeException dte) {
266 String msg = dte.getMessage();
267 // Verify message contains the type that is missing and the temporal value
268 assertTrue(msg.contains("Chronology"),
269 String.format("\"%s\" missing from %s", "Chronology", msg));
270 assertTrue(msg.contains(zone.toString()),
271 String.format("\"%s\" missing from %s", zone.toString(), msg));
272 }
273
274 }
275
276 @DataProvider(name = "validPatterns")
277 Object[][] valid_Data() {
278 return new Object[][]{
279 {"pdQ", LocalDate.of(2018, 4, 9), "92"},
280 {"ppdM", LocalDate.of(2018, 4, 9), " 94"},
281 {"dppMyy", LocalDate.of(2018, 4, 9), "9 418"},
282 {"ppHmmss", LocalTime.of(1,20,45), " 12045"},
283 {"pppddMMyyyy", LocalDate.of(2018, 4, 9), " 09042018"},
284 {"ppdppMYYYY", LocalDate.of(2018, 4, 9), " 9 42018"}
285 };
286 }
287
288 @Test(dataProvider="validPatterns")
289 public void test_pattern(String pattern, TemporalAccessor input, String expected) throws Exception {
290 String actual = DateTimeFormatter.ofPattern(pattern).format(input);
291 assertEquals(actual, expected);
292 }
293
294 @DataProvider(name = "invalidPatterns")
295 Object[][] invalid_Data() {
296 return new Object[][]{
297 {"pddQ", LocalDate.of(2018, 4, 9)},
298 {"dpMMyy", LocalDate.of(2018, 4, 9)},
299 {"H:pmmss", LocalTime.of(1,20,45)},
300 };
301 }
302
303 @Test(dataProvider="invalidPatterns", expectedExceptions=DateTimeException.class)
304 public void test_invalidPattern(String pattern, TemporalAccessor input) throws Exception {
305 DateTimeFormatter.ofPattern(pattern).format(input);
306 }
307
308 }
|