1 /* 2 * Copyright (c) 2014, 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.EPOCH_DAY; 63 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 64 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 65 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 66 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 67 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 68 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 69 import static org.testng.Assert.assertEquals; 70 71 import java.time.DateTimeException; 72 import java.time.Instant; 73 import java.time.LocalDateTime; 74 import java.time.ZoneId; 75 import java.time.ZoneOffset; 76 import java.time.ZonedDateTime; 77 import java.time.format.DateTimeFormatter; 78 import java.time.format.DateTimeFormatterBuilder; 79 import java.time.temporal.TemporalAccessor; 80 81 import org.testng.annotations.DataProvider; 82 import org.testng.annotations.Test; 83 84 /** 85 * Test parsing of edge cases. 86 */ 87 @Test 88 public class TestDateTimeParsing { 89 90 private static final ZoneId PARIS = ZoneId.of("Europe/Paris"); 91 private static final ZoneOffset OFFSET_0230 = ZoneOffset.ofHoursMinutes(2, 30); 92 93 private static final DateTimeFormatter LOCALFIELDS = new DateTimeFormatterBuilder() 94 .appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter(); 95 private static final DateTimeFormatter LOCALFIELDS_ZONEID = new DateTimeFormatterBuilder() 96 .appendPattern("yyyy-MM-dd HH:mm:ss ").appendZoneId().toFormatter(); 97 private static final DateTimeFormatter LOCALFIELDS_OFFSETID = new DateTimeFormatterBuilder() 98 .appendPattern("yyyy-MM-dd HH:mm:ss ").appendOffsetId().toFormatter(); 99 private static final DateTimeFormatter LOCALFIELDS_WITH_PARIS = LOCALFIELDS.withZone(PARIS); 100 private static final DateTimeFormatter LOCALFIELDS_WITH_0230 = LOCALFIELDS.withZone(OFFSET_0230); 101 private static final DateTimeFormatter INSTANT = new DateTimeFormatterBuilder() 102 .appendInstant().toFormatter(); 103 private static final DateTimeFormatter INSTANT_WITH_PARIS = INSTANT.withZone(PARIS); 104 private static final DateTimeFormatter INSTANT_WITH_0230 = INSTANT.withZone(OFFSET_0230); 105 private static final DateTimeFormatter INSTANT_OFFSETID = new DateTimeFormatterBuilder() 106 .appendInstant().appendLiteral(' ').appendOffsetId().toFormatter(); 107 private static final DateTimeFormatter INSTANT_OFFSETSECONDS = new DateTimeFormatterBuilder() 108 .appendInstant().appendLiteral(' ').appendValue(OFFSET_SECONDS).toFormatter(); 109 private static final DateTimeFormatter INSTANTSECONDS = new DateTimeFormatterBuilder() 110 .appendValue(INSTANT_SECONDS).toFormatter(); 111 private static final DateTimeFormatter INSTANTSECONDS_WITH_PARIS = INSTANTSECONDS.withZone(PARIS); 112 private static final DateTimeFormatter INSTANTSECONDS_NOS = new DateTimeFormatterBuilder() 113 .appendValue(INSTANT_SECONDS).appendLiteral('.').appendValue(NANO_OF_SECOND).toFormatter(); 114 private static final DateTimeFormatter INSTANTSECONDS_NOS_WITH_PARIS = INSTANTSECONDS_NOS.withZone(PARIS); 115 private static final DateTimeFormatter INSTANTSECONDS_OFFSETSECONDS = new DateTimeFormatterBuilder() 116 .appendValue(INSTANT_SECONDS).appendLiteral(' ').appendValue(OFFSET_SECONDS).toFormatter(); 117 118 @DataProvider(name = "instantZones") data_instantZones()119 Object[][] data_instantZones() { 120 return new Object[][] { 121 {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 Europe/Paris", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, PARIS)}, 122 {LOCALFIELDS_ZONEID, "2014-06-30 01:02:03 +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 123 {LOCALFIELDS_OFFSETID, "2014-06-30 01:02:03 +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 124 {LOCALFIELDS_WITH_PARIS, "2014-06-30 01:02:03", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, PARIS)}, 125 {LOCALFIELDS_WITH_0230, "2014-06-30 01:02:03", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, OFFSET_0230)}, 126 {INSTANT_WITH_PARIS, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(PARIS)}, 127 {INSTANT_WITH_0230, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 128 {INSTANT_OFFSETID, "2014-06-30T01:02:03Z +02:30", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 129 {INSTANT_OFFSETSECONDS, "2014-06-30T01:02:03Z 9000", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).withZoneSameInstant(OFFSET_0230)}, 130 {INSTANTSECONDS_WITH_PARIS, "86402", Instant.ofEpochSecond(86402).atZone(PARIS)}, 131 {INSTANTSECONDS_NOS_WITH_PARIS, "86402.123456789", Instant.ofEpochSecond(86402, 123456789).atZone(PARIS)}, 132 {INSTANTSECONDS_OFFSETSECONDS, "86402 9000", Instant.ofEpochSecond(86402).atZone(OFFSET_0230)}, 133 }; 134 } 135 136 @Test(dataProvider = "instantZones") test_parse_instantZones_ZDT(DateTimeFormatter formatter, String text, ZonedDateTime expected)137 public void test_parse_instantZones_ZDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 138 TemporalAccessor actual = formatter.parse(text); 139 assertEquals(ZonedDateTime.from(actual), expected); 140 } 141 142 @Test(dataProvider = "instantZones") test_parse_instantZones_LDT(DateTimeFormatter formatter, String text, ZonedDateTime expected)143 public void test_parse_instantZones_LDT(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 144 TemporalAccessor actual = formatter.parse(text); 145 assertEquals(LocalDateTime.from(actual), expected.toLocalDateTime()); 146 } 147 148 @Test(dataProvider = "instantZones") test_parse_instantZones_Instant(DateTimeFormatter formatter, String text, ZonedDateTime expected)149 public void test_parse_instantZones_Instant(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 150 TemporalAccessor actual = formatter.parse(text); 151 assertEquals(Instant.from(actual), expected.toInstant()); 152 } 153 154 @Test(dataProvider = "instantZones") test_parse_instantZones_supported(DateTimeFormatter formatter, String text, ZonedDateTime expected)155 public void test_parse_instantZones_supported(DateTimeFormatter formatter, String text, ZonedDateTime expected) { 156 TemporalAccessor actual = formatter.parse(text); 157 assertEquals(actual.isSupported(INSTANT_SECONDS), true); 158 assertEquals(actual.isSupported(EPOCH_DAY), true); 159 assertEquals(actual.isSupported(SECOND_OF_DAY), true); 160 assertEquals(actual.isSupported(NANO_OF_SECOND), true); 161 assertEquals(actual.isSupported(MICRO_OF_SECOND), true); 162 assertEquals(actual.isSupported(MILLI_OF_SECOND), true); 163 } 164 165 //----------------------------------------------------------------------- 166 @DataProvider(name = "instantNoZone") data_instantNoZone()167 Object[][] data_instantNoZone() { 168 return new Object[][] { 169 {INSTANT, "2014-06-30T01:02:03Z", ZonedDateTime.of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset.UTC).toInstant()}, 170 {INSTANTSECONDS, "86402", Instant.ofEpochSecond(86402)}, 171 {INSTANTSECONDS_NOS, "86402.123456789", Instant.ofEpochSecond(86402, 123456789)}, 172 }; 173 } 174 175 @Test(dataProvider = "instantNoZone", expectedExceptions = DateTimeException.class) test_parse_instantNoZone_ZDT(DateTimeFormatter formatter, String text, Instant expected)176 public void test_parse_instantNoZone_ZDT(DateTimeFormatter formatter, String text, Instant expected) { 177 TemporalAccessor actual = formatter.parse(text); 178 ZonedDateTime.from(actual); 179 } 180 181 @Test(dataProvider = "instantNoZone", expectedExceptions = DateTimeException.class) test_parse_instantNoZone_LDT(DateTimeFormatter formatter, String text, Instant expected)182 public void test_parse_instantNoZone_LDT(DateTimeFormatter formatter, String text, Instant expected) { 183 TemporalAccessor actual = formatter.parse(text); 184 LocalDateTime.from(actual); 185 } 186 187 @Test(dataProvider = "instantNoZone") test_parse_instantNoZone_Instant(DateTimeFormatter formatter, String text, Instant expected)188 public void test_parse_instantNoZone_Instant(DateTimeFormatter formatter, String text, Instant expected) { 189 TemporalAccessor actual = formatter.parse(text); 190 assertEquals(Instant.from(actual), expected); 191 } 192 193 @Test(dataProvider = "instantNoZone") test_parse_instantNoZone_supported(DateTimeFormatter formatter, String text, Instant expected)194 public void test_parse_instantNoZone_supported(DateTimeFormatter formatter, String text, Instant expected) { 195 TemporalAccessor actual = formatter.parse(text); 196 assertEquals(actual.isSupported(INSTANT_SECONDS), true); 197 assertEquals(actual.isSupported(EPOCH_DAY), false); 198 assertEquals(actual.isSupported(SECOND_OF_DAY), false); 199 assertEquals(actual.isSupported(NANO_OF_SECOND), true); 200 assertEquals(actual.isSupported(MICRO_OF_SECOND), true); 201 assertEquals(actual.isSupported(MILLI_OF_SECOND), true); 202 } 203 204 } 205