1 /* 2 * Copyright (c) 2014, 2021, 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 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2014, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package test.java.time.format; 61 62 import static java.time.temporal.ChronoField.AMPM_OF_DAY; 63 import static java.time.temporal.ChronoField.EPOCH_DAY; 64 import static java.time.temporal.ChronoField.HOUR_OF_AMPM; 65 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 66 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 67 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 68 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 69 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 70 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 71 import static java.util.Locale.US; 72 import static org.testng.Assert.assertEquals; 73 74 import java.time.DateTimeException; 75 import java.time.Instant; 76 import java.time.LocalDateTime; 77 import java.time.ZoneId; 78 import java.time.ZoneOffset; 79 import java.time.ZonedDateTime; 80 import java.time.format.DateTimeFormatter; 81 import java.time.format.DateTimeFormatterBuilder; 82 import java.time.format.DateTimeParseException; 83 import java.time.temporal.TemporalAccessor; 84 85 import org.testng.annotations.DataProvider; 86 import org.testng.annotations.Test; 87 88 /** 89 * @test 90 * @summary Test parsing of edge cases. 91 * @bug 8223773 8272473 92 */ 93 public class TestDateTimeParsing { 94 95 private static final ZoneId PARIS = ZoneId.of("Europe/Paris"); 96 private static final ZoneId NEW_YORK = ZoneId.of("America/New_York"); 97 private static final ZoneOffset OFFSET_0230 = ZoneOffset.ofHoursMinutes(2, 30); 98 99 private static final DateTimeFormatter LOCALFIELDS = new DateTimeFormatterBuilder() 100 .appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter(); 101 private static final DateTimeFormatter LOCALFIELDS_ZONEID = new DateTimeFormatterBuilder() 102 .appendPattern("yyyy-MM-dd HH:mm:ss ").appendZoneId().toFormatter(); 103 private static final DateTimeFormatter LOCALFIELDS_OFFSETID = new DateTimeFormatterBuilder() 104 .appendPattern("yyyy-MM-dd HH:mm:ss ").appendOffsetId().toFormatter(); 105 private static final DateTimeFormatter LOCALFIELDS_WITH_PARIS = LOCALFIELDS.withZone(PARIS); 106 private static final DateTimeFormatter LOCALFIELDS_WITH_0230 = LOCALFIELDS.withZone(OFFSET_0230); 107 private static final DateTimeFormatter INSTANT = new DateTimeFormatterBuilder() 108 .appendInstant().toFormatter(); 109 private static final DateTimeFormatter INSTANT_WITH_PARIS = INSTANT.withZone(PARIS); 110 private static final DateTimeFormatter INSTANT_WITH_0230 = INSTANT.withZone(OFFSET_0230); 111 private static final DateTimeFormatter INSTANT_WITH_NEW_YORK = INSTANT.withZone(NEW_YORK); 112 private static final DateTimeFormatter INSTANT_OFFSETID = new DateTimeFormatterBuilder() 113 .appendInstant().appendLiteral(' ').appendOffsetId().toFormatter(); 114 private static final DateTimeFormatter INSTANT_OFFSETSECONDS = new DateTimeFormatterBuilder() 115 .appendInstant().appendLiteral(' ').appendValue(OFFSET_SECONDS).toFormatter(); 116 private static final DateTimeFormatter INSTANTSECONDS = new DateTimeFormatterBuilder() 117 .appendValue(INSTANT_SECONDS).toFormatter(); 118 private static final DateTimeFormatter INSTANTSECONDS_WITH_PARIS = INSTANTSECONDS.withZone(PARIS); 119 private static final DateTimeFormatter INSTANTSECONDS_NOS = new DateTimeFormatterBuilder() 120 .appendValue(INSTANT_SECONDS).appendLiteral('.').appendValue(NANO_OF_SECOND).toFormatter(); 121 private static final DateTimeFormatter INSTANTSECONDS_NOS_WITH_PARIS = INSTANTSECONDS_NOS.withZone(PARIS); 122 private static final DateTimeFormatter INSTANTSECONDS_OFFSETSECONDS = new DateTimeFormatterBuilder() 123 .appendValue(INSTANT_SECONDS).appendLiteral(' ').appendValue(OFFSET_SECONDS).toFormatter(); 124 private static final DateTimeFormatter INSTANTSECONDS_WITH_NEW_YORK = INSTANTSECONDS.withZone(NEW_YORK); 125 126 private static final String DTPE_MESSAGE = 127 "Invalid value for HourOfAmPm (valid values 0 - 11): 12"; 128 129 @DataProvider(name = "instantZones") data_instantZones()130 Object[][] data_instantZones() { 131 return new Object[][] { 132 {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 Europe/Paris", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, PARIS)}, 133 {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 134 {LOCALFIELDS_OFFSETID, "2014-06-30 01:02:03 +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 135 {LOCALFIELDS_WITH_PARIS, "2014-06-30 01:02:03", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, PARIS)}, 136 {LOCALFIELDS_WITH_0230, "2014-06-30 01:02:03", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 137 {INSTANT_WITH_PARIS, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(PARIS)}, 138 {INSTANT_WITH_0230, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 139 {INSTANT_WITH_NEW_YORK, "2020-11-01T05:00:00Z", ZonedDateTime.of(2020, 11, 1, 5, 0, 0, 0, ZoneOffset.UTC).withZoneSameInstant(NEW_YORK)}, 140 {INSTANT_WITH_NEW_YORK, "2020-11-01T06:00:00Z", ZonedDateTime.of(2020, 11, 1, 6, 0, 0, 0, ZoneOffset.UTC).withZoneSameInstant(NEW_YORK)}, 141 {INSTANT_OFFSETID, "2014-06-30T01:02:03Z +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 142 {INSTANT_OFFSETSECONDS, "2014-06-30T01:02:03Z 9000", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 143 {INSTANTSECONDS_WITH_PARIS, "86402", Instant.ofEpochSecond(86402).atZone(PARIS)}, 144 {INSTANTSECONDS_NOS_WITH_PARIS, "86402.123456789", Instant.ofEpochSecond(86402, 123456789).atZone(PARIS)}, 145 {INSTANTSECONDS_OFFSETSECONDS, "86402 9000", Instant.ofEpochSecond(86402).atZone(OFFSET_0230)}, 146 {INSTANTSECONDS_WITH_NEW_YORK, "1604206800", Instant.ofEpochSecond(1604206800).atZone(NEW_YORK)}, // 2020-11-01T05:00:00 UTC 147 {INSTANTSECONDS_WITH_NEW_YORK, "1604210400", Instant.ofEpochSecond(1604210400).atZone(NEW_YORK)}, // 2020-11-01T06:00:00 UTC 148 }; 149 } 150 151 @Test(dataProvider = "instantZones") test_parse_instantZones_ZDT(DateTimeFormatter formatter, String text, ZonedDateTime expected)152 public void test_parse_instantZones_ZDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 153 TemporalAccessor actual = formatter.parse(text); 154 assertEquals(ZonedDateTime.from(actual), expected); 155 } 156 157 @Test(dataProvider = "instantZones") test_parse_instantZones_LDT(DateTimeFormatter formatter, String text, ZonedDateTime expected)158 public void test_parse_instantZones_LDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 159 TemporalAccessor actual = formatter.parse(text); 160 assertEquals(LocalDateTime.from(actual), expected.toLocalDateTime()); 161 } 162 163 @Test(dataProvider = "instantZones") test_parse_instantZones_Instant(DateTimeFormatter formatter, String text, ZonedDateTime expected)164 public void test_parse_instantZones_Instant(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 165 TemporalAccessor actual = formatter.parse(text); 166 assertEquals(Instant.from(actual), expected.toInstant()); 167 } 168 169 @Test(dataProvider = "instantZones") test_parse_instantZones_supported(DateTimeFormatter formatter, String text, ZonedDateTime expected)170 public void test_parse_instantZones_supported(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 171 TemporalAccessor actual = formatter.parse(text); 172 assertEquals(actual.isSupported(INSTANT_SECONDS), true); 173 assertEquals(actual.isSupported(EPOCH_DAY), true); 174 assertEquals(actual.isSupported(SECOND_OF_DAY), true); 175 assertEquals(actual.isSupported(NANO_OF_SECOND), true); 176 assertEquals(actual.isSupported(MICRO_OF_SECOND), true); 177 assertEquals(actual.isSupported(MILLI_OF_SECOND), true); 178 } 179 180 //----------------------------------------------------------------------- 181 @DataProvider(name = "instantNoZone") data_instantNoZone()182 Object[][] data_instantNoZone() { 183 return new Object[][] { 184 {INSTANT, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).toInstant()}, 185 {INSTANTSECONDS, "86402", Instant.ofEpochSecond(86402)}, 186 {INSTANTSECONDS_NOS, "86402.123456789", Instant.ofEpochSecond(86402, 123456789)}, 187 }; 188 } 189 190 @Test(dataProvider = "instantNoZone", expectedExceptions = DateTimeException.class) test_parse_instantNoZone_ZDT(DateTimeFormatter formatter, String text, Instant expected)191 public void test_parse_instantNoZone_ZDT(DateTimeFormatter formatter, String text, Instant expected) { 192 TemporalAccessor actual = formatter.parse(text); 193 ZonedDateTime.from(actual); 194 } 195 196 @Test(dataProvider = "instantNoZone", expectedExceptions = DateTimeException.class) test_parse_instantNoZone_LDT(DateTimeFormatter formatter, String text, Instant expected)197 public void test_parse_instantNoZone_LDT(DateTimeFormatter formatter, String text, Instant expected) { 198 TemporalAccessor actual = formatter.parse(text); 199 LocalDateTime.from(actual); 200 } 201 202 @Test(dataProvider = "instantNoZone") test_parse_instantNoZone_Instant(DateTimeFormatter formatter, String text, Instant expected)203 public void test_parse_instantNoZone_Instant(DateTimeFormatter formatter, String text, Instant expected) { 204 TemporalAccessor actual = formatter.parse(text); 205 assertEquals(Instant.from(actual), expected); 206 } 207 208 @Test(dataProvider = "instantNoZone") test_parse_instantNoZone_supported(DateTimeFormatter formatter, String text, Instant expected)209 public void test_parse_instantNoZone_supported(DateTimeFormatter formatter, String text, Instant expected) { 210 TemporalAccessor actual = formatter.parse(text); 211 assertEquals(actual.isSupported(INSTANT_SECONDS), true); 212 assertEquals(actual.isSupported(EPOCH_DAY), false); 213 assertEquals(actual.isSupported(SECOND_OF_DAY), false); 214 assertEquals(actual.isSupported(NANO_OF_SECOND), true); 215 assertEquals(actual.isSupported(MICRO_OF_SECOND), true); 216 assertEquals(actual.isSupported(MILLI_OF_SECOND), true); 217 } 218 219 // Bug 8223773: validation check for the range of HourOfAmPm in SMART mode. 220 // Should throw a DateTimeParseException, as 12 is out of range for HourOfAmPm. 221 @Test(expectedExceptions = DateTimeParseException.class) test_validateHourOfAmPm()222 public void test_validateHourOfAmPm() { 223 try { 224 new DateTimeFormatterBuilder() 225 .appendValue(HOUR_OF_AMPM,2) 226 .appendText(AMPM_OF_DAY) 227 .toFormatter(US) 228 .parse("12PM"); 229 } catch (DateTimeParseException e) { 230 Throwable cause = e.getCause(); 231 if (cause == null || 232 !DTPE_MESSAGE.equals(cause.getMessage())) { 233 throw new RuntimeException( 234 "DateTimeParseException was thrown with different reason: " + e); 235 } else { 236 throw e; 237 } 238 } 239 } 240 } 241