< prev index next >

test/java/time/tck/java/time/format/TCKDTFParsedInstant.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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 tck.java.time.format;
  24 
  25 import static org.testng.AssertJUnit.assertEquals;
  26 
  27 import java.time.LocalDateTime;
  28 import java.time.OffsetDateTime;
  29 import java.time.ZoneId;
  30 import java.time.ZoneOffset;
  31 import java.time.ZonedDateTime;
  32 import java.time.format.DateTimeFormatter;

  33 
  34 import org.testng.annotations.BeforeMethod;
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 /**
  39  * Testing DateTimeFormatter Parsing with 4 different test conditions:
  40  * 1. When Zone and Offset not provided
  41  * 2. When Zone and Offset provided
  42  * 3. When Offset is not provided and Zone is provided
  43  * 4. When Zone is not provided and Offset is provided
  44  */
  45 
  46 @Test
  47 public class TCKDTFParsedInstant {
  48 
  49     private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin");
  50     private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul");
  51 
  52     private DateTimeFormatter dtFormatter;


 177 
 178     @DataProvider(name="parseWithZoneWithoutOffset")
 179     Object[][] data_parse_WithZone_WithoutOffset() {
 180         return new Object[][] {
 181             {"28 Oct 00:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 0, 45, 0, 0, EUROPE_BERLIN)},
 182             {"28 Oct 01:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, EUROPE_BERLIN)},
 183             {"28 Oct 02:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, EUROPE_BERLIN)},
 184             {"28 Oct 03:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, EUROPE_BERLIN)},
 185             {"28 Oct 04:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, EUROPE_BERLIN)},
 186 
 187             {"28 Oct 01:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, ASIA_ISTANBUL)},
 188             {"28 Oct 02:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ASIA_ISTANBUL)},
 189             {"28 Oct 03:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, ASIA_ISTANBUL)},
 190             {"28 Oct 04:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, ASIA_ISTANBUL)},
 191             {"28 Oct 05:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 5, 45, 0, 0, ASIA_ISTANBUL)}
 192         };
 193     }
 194 
 195     @Test(dataProvider="parseWithZoneWithoutOffset")
 196     public void testWithZoneWithoutOffset(String withZoneWithoutOffset, ZonedDateTime expectedZDT) {
 197         dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV");
 198         zdt1 = ZonedDateTime.parse(withZoneWithoutOffset, dtFormatter);
 199         assertEquals(expectedZDT, zdt1);
 200     }
 201 
 202     @DataProvider(name="parseWithOffsetWithoutZone")
 203     Object[][] data_parse_WithOffset_WithoutZone() {
 204         return new Object[][] {
 205             {"2015-12-14T00:45:00-11:30", OffsetDateTime.of(2015, 12, 14, 0, 45, 0, 0, ZoneOffset.of("-11:30"))},
 206             {"2015-12-14T01:45:00-05:00", OffsetDateTime.of(2015, 12, 14, 1, 45, 0, 0, ZoneOffset.of("-05:00"))},
 207             {"2015-12-14T02:45:00-00:00", OffsetDateTime.of(2015, 12, 14, 2, 45, 0, 0, ZoneOffset.of("-00:00"))},
 208             {"2015-12-14T03:45:00+00:00", OffsetDateTime.of(2015, 12, 14, 3, 45, 0, 0, ZoneOffset.of("+00:00"))},
 209             {"2015-12-14T04:45:00+03:30", OffsetDateTime.of(2015, 12, 14, 4, 45, 0, 0, ZoneOffset.of("+03:30"))},
 210             {"2015-12-14T05:45:00+10:00", OffsetDateTime.of(2015, 12, 14, 5, 45, 0, 0, ZoneOffset.of("+10:00"))}
 211         };
 212     }
 213 
 214     @Test(dataProvider="parseWithOffsetWithoutZone")
 215     public void testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD) {
 216         dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
 217         odt1 = OffsetDateTime.parse(odtString, dtFormatter);
   1 /*
   2  * Copyright (c) 2015, 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 tck.java.time.format;
  24 
  25 import static org.testng.AssertJUnit.assertEquals;
  26 
  27 import java.time.LocalDateTime;
  28 import java.time.OffsetDateTime;
  29 import java.time.ZoneId;
  30 import java.time.ZoneOffset;
  31 import java.time.ZonedDateTime;
  32 import java.time.format.DateTimeFormatter;
  33 import java.util.Locale;
  34 
  35 import org.testng.annotations.BeforeMethod;
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 
  39 /**
  40  * Testing DateTimeFormatter Parsing with 4 different test conditions:
  41  * 1. When Zone and Offset not provided
  42  * 2. When Zone and Offset provided
  43  * 3. When Offset is not provided and Zone is provided
  44  * 4. When Zone is not provided and Offset is provided
  45  */
  46 
  47 @Test
  48 public class TCKDTFParsedInstant {
  49 
  50     private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin");
  51     private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul");
  52 
  53     private DateTimeFormatter dtFormatter;


 178 
 179     @DataProvider(name="parseWithZoneWithoutOffset")
 180     Object[][] data_parse_WithZone_WithoutOffset() {
 181         return new Object[][] {
 182             {"28 Oct 00:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 0, 45, 0, 0, EUROPE_BERLIN)},
 183             {"28 Oct 01:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, EUROPE_BERLIN)},
 184             {"28 Oct 02:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, EUROPE_BERLIN)},
 185             {"28 Oct 03:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, EUROPE_BERLIN)},
 186             {"28 Oct 04:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, EUROPE_BERLIN)},
 187 
 188             {"28 Oct 01:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, ASIA_ISTANBUL)},
 189             {"28 Oct 02:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ASIA_ISTANBUL)},
 190             {"28 Oct 03:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, ASIA_ISTANBUL)},
 191             {"28 Oct 04:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, ASIA_ISTANBUL)},
 192             {"28 Oct 05:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 5, 45, 0, 0, ASIA_ISTANBUL)}
 193         };
 194     }
 195 
 196     @Test(dataProvider="parseWithZoneWithoutOffset")
 197     public void testWithZoneWithoutOffset(String withZoneWithoutOffset, ZonedDateTime expectedZDT) {
 198         dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV").withLocale(Locale.ENGLISH);
 199         zdt1 = ZonedDateTime.parse(withZoneWithoutOffset, dtFormatter);
 200         assertEquals(expectedZDT, zdt1);
 201     }
 202 
 203     @DataProvider(name="parseWithOffsetWithoutZone")
 204     Object[][] data_parse_WithOffset_WithoutZone() {
 205         return new Object[][] {
 206             {"2015-12-14T00:45:00-11:30", OffsetDateTime.of(2015, 12, 14, 0, 45, 0, 0, ZoneOffset.of("-11:30"))},
 207             {"2015-12-14T01:45:00-05:00", OffsetDateTime.of(2015, 12, 14, 1, 45, 0, 0, ZoneOffset.of("-05:00"))},
 208             {"2015-12-14T02:45:00-00:00", OffsetDateTime.of(2015, 12, 14, 2, 45, 0, 0, ZoneOffset.of("-00:00"))},
 209             {"2015-12-14T03:45:00+00:00", OffsetDateTime.of(2015, 12, 14, 3, 45, 0, 0, ZoneOffset.of("+00:00"))},
 210             {"2015-12-14T04:45:00+03:30", OffsetDateTime.of(2015, 12, 14, 4, 45, 0, 0, ZoneOffset.of("+03:30"))},
 211             {"2015-12-14T05:45:00+10:00", OffsetDateTime.of(2015, 12, 14, 5, 45, 0, 0, ZoneOffset.of("+10:00"))}
 212         };
 213     }
 214 
 215     @Test(dataProvider="parseWithOffsetWithoutZone")
 216     public void testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD) {
 217         dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
 218         odt1 = OffsetDateTime.parse(odtString, dtFormatter);
< prev index next >