--- /dev/null 2015-12-07 10:37:28.000000000 +0530 +++ new/test/java/time/tck/java/time/format/TCKDTFParsedInstant.java 2015-12-07 10:37:16.310443700 +0530 @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package tck.java.time.format; + +import static org.testng.AssertJUnit.assertEquals; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * Testing DateTimeFormatter Parsing with 4 different test conditions: + * 1. When Zone and Offset not provided + * 2. When Zone and Offset provided + * 3. When Offset is not provided and Zone is provided + * 4. When Zone is not provided and Offset is provided + */ + +@Test +public class TCKDTFParsedInstant { + private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin"); + private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul"); + private static final ZoneId[] ZONE_IDS = { EUROPE_BERLIN, ASIA_ISTANBUL }; + private static DateTimeFormatter dtFormatter; + private static ZonedDateTime zdt1, zdt2; + private static OffsetDateTime odt1, odt2; + private static LocalDateTime ldt1, ldt2; + private static String s; + + @BeforeMethod + public void setUp() throws Exception { + dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; + } + + @Test + private static void testWithoutZoneAndOffset() { + dtFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + ldt1 = LocalDateTime.of(2012, 10, 28, 1, 45, 0); + for (int i = 0; i < 6; i++) { + s = ldt1.format(dtFormatter); + ldt2 = LocalDateTime.parse(s, dtFormatter); + assertEquals(ldt1, ldt2); + ldt1 = ldt1.plus(15, ChronoUnit.MINUTES); + } + } + + @Test + private static void testWithZoneAndOffset() { + dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; + // Sample Offsets to test from -2 to +3 + String[] offsetSamples = { "-02:00", "-01:00", "-00:00", "+00:00", "+01:00", "+02:00", "+03:00" }; + // 3 Sample ZDT Strings which are before, during and after an Overlap period respectively. + String[] zdtStringSamples = { "2012-10-28T01:45:00+00:00[Europe/Berlin]", + "2012-10-28T02:45:00+00:00[Europe/Berlin]", + "2012-10-28T03:45:00+00:00[Europe/Berlin]", + "2012-10-28T02:45:00+00:00[Asia/Istanbul]", + "2012-10-28T03:45:00+00:00[Asia/Istanbul]", + "2012-10-28T04:45:00+00:00[Asia/Istanbul]" + }; + // 3 Sample ZDTs which are before, during and after an Overlap period respectively. + LocalDateTime[] ldtSamples = { LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), + LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), + LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), + LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), + LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), + LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0) + }; + for (int i = 0; i < zdtStringSamples.length; i++) { + for (int j = 0; j < offsetSamples.length; j++) { + zdt1 = ZonedDateTime.ofInstant(ldtSamples[i], ZoneOffset.of(offsetSamples[j]), ZONE_IDS[i / 3]); + s = zdtStringSamples[i].replace(zdtStringSamples[i].substring(19, 25), offsetSamples[j]); + zdt2 = ZonedDateTime.parse(s, dtFormatter); + assertEquals(zdt1, zdt2); + } + } + } + + @Test + private static void testWithoutOffset() { + dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV"); + zdt1 = ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ZoneId.of("Europe/Berlin")); + String s1; + for (int i = 0; i < 6; i++) { + s = zdt1.format(dtFormatter); + zdt2 = ZonedDateTime.parse(s, dtFormatter); + s1 = zdt2.format(dtFormatter); + assertEquals(s, s1); + zdt1 = zdt1.plus(15, ChronoUnit.MINUTES); + } + } + + @Test + private static void testWithoutZone() { + dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + // Sample Offsets + String[] offsetSamples = { "-08:00", "-05:00", "-01:00", "+00:00", "+01:00", "+04:00", "+10:00" }; + // 3 Sample ODT Strings + String[] odtStringSamples = { "2012-10-28T01:45:00+00:00", "2012-10-28T02:45:00+00:00", "2012-10-28T03:45:00+00:00" }; + // 3 Sample ODTs + OffsetDateTime[] odtSamples = { + OffsetDateTime.of(2012, 10, 28, 1, 45, 0, 0, ZoneOffset.of(offsetSamples[0])), + OffsetDateTime.of(2012, 10, 28, 2, 45, 0, 0, ZoneOffset.of(offsetSamples[0])), + OffsetDateTime.of(2012, 10, 28, 3, 45, 0, 0, ZoneOffset.of(offsetSamples[0])) + }; + for (int i = 0; i < odtStringSamples.length; i++) { + for (int j = 0; j < offsetSamples.length; j++) { + odt1 = odtSamples[i].with(ChronoField.OFFSET_SECONDS, ZoneOffset.of(offsetSamples[j]).getTotalSeconds()); + s = odtStringSamples[i].replace(odtStringSamples[i].substring(19, 25), offsetSamples[j]); + odt2 = OffsetDateTime.parse(s, dtFormatter); + assertEquals(odt1, odt2); + } + } + } +}