< prev index next >

test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 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  */


  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 tck.java.time.format;
  61 
  62 import static org.testng.Assert.assertEquals;
  63 
  64 import java.time.LocalDateTime;
  65 import java.time.OffsetDateTime;
  66 import java.time.ZoneId;
  67 import java.time.ZoneOffset;
  68 import java.time.ZonedDateTime;
  69 import java.time.format.DateTimeFormatter;
  70 import java.time.format.DateTimeFormatterBuilder;
  71 import java.time.format.TextStyle;

  72 
  73 import org.testng.annotations.BeforeMethod;
  74 import org.testng.annotations.DataProvider;
  75 import org.testng.annotations.Test;
  76 
  77 /**
  78  * Test DateTimeFormatterBuilder.appendOffset().
  79  */
  80 @Test
  81 public class TCKOffsetPrinterParser {
  82 
  83     private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
  84     private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
  85     private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
  86     private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
  87     private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
  88     private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45);
  89     private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
  90     private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
  91     private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);


 540             String output2 = f2.format(zdt);
 541             assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
 542 
 543             DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
 544             String output3 = f3.format(zdt);
 545             assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
 546         } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
 547             ZonedDateTime zdt = ldt.atZone(zone);
 548             DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
 549             String output = f.format(zdt);
 550             assertEquals(output, expected);
 551         }
 552     }
 553 
 554     @Test(dataProvider="print_localized")
 555     public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
 556         OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
 557         ZonedDateTime zdt = ldt.atZone(offset);
 558 
 559         DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style)
 560                                                             .toFormatter();
 561         assertEquals(f.format(odt), expected);
 562         assertEquals(f.format(zdt), expected);
 563         assertEquals(f.parse(expected, ZoneOffset::from), offset);
 564 
 565         if (style == TextStyle.FULL) {
 566             f = new DateTimeFormatterBuilder().appendPattern("ZZZZ")
 567                                               .toFormatter();
 568             assertEquals(f.format(odt), expected);
 569             assertEquals(f.format(zdt), expected);
 570             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 571 
 572             f = new DateTimeFormatterBuilder().appendPattern("OOOO")
 573                                               .toFormatter();
 574             assertEquals(f.format(odt), expected);
 575             assertEquals(f.format(zdt), expected);
 576             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 577         }
 578 
 579         if (style == TextStyle.SHORT) {
 580             f = new DateTimeFormatterBuilder().appendPattern("O")
 581                                               .toFormatter();
 582             assertEquals(f.format(odt), expected);
 583             assertEquals(f.format(zdt), expected);
 584             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 585         }
 586     }
 587 
 588     //-----------------------------------------------------------------------
 589     @Test(expectedExceptions=IllegalArgumentException.class)
 590     public void test_print_pattern_X6rejected() {
 591         builder.appendPattern("XXXXXX");
 592     }
 593 
 594     @Test(expectedExceptions=IllegalArgumentException.class)
 595     public void test_print_pattern_x6rejected() {
 596         builder.appendPattern("xxxxxx");
 597     }
 598 
 599     @Test(expectedExceptions=IllegalArgumentException.class)
 600     public void test_print_pattern_Z6rejected() {
 601         builder.appendPattern("ZZZZZZ");


   1 /*
   2  * Copyright (c) 2012, 2019, 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  */


  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 tck.java.time.format;
  61 
  62 import static org.testng.Assert.assertEquals;
  63 
  64 import java.time.LocalDateTime;
  65 import java.time.OffsetDateTime;
  66 import java.time.ZoneId;
  67 import java.time.ZoneOffset;
  68 import java.time.ZonedDateTime;
  69 import java.time.format.DateTimeFormatter;
  70 import java.time.format.DateTimeFormatterBuilder;
  71 import java.time.format.TextStyle;
  72 import java.util.Locale;
  73 
  74 import org.testng.annotations.BeforeMethod;
  75 import org.testng.annotations.DataProvider;
  76 import org.testng.annotations.Test;
  77 
  78 /**
  79  * Test DateTimeFormatterBuilder.appendOffset().
  80  */
  81 @Test
  82 public class TCKOffsetPrinterParser {
  83 
  84     private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
  85     private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
  86     private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
  87     private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
  88     private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
  89     private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45);
  90     private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
  91     private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
  92     private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);


 541             String output2 = f2.format(zdt);
 542             assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
 543 
 544             DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
 545             String output3 = f3.format(zdt);
 546             assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
 547         } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
 548             ZonedDateTime zdt = ldt.atZone(zone);
 549             DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
 550             String output = f.format(zdt);
 551             assertEquals(output, expected);
 552         }
 553     }
 554 
 555     @Test(dataProvider="print_localized")
 556     public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
 557         OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
 558         ZonedDateTime zdt = ldt.atZone(offset);
 559 
 560         DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style)
 561                                                             .toFormatter(Locale.US);
 562         assertEquals(f.format(odt), expected);
 563         assertEquals(f.format(zdt), expected);
 564         assertEquals(f.parse(expected, ZoneOffset::from), offset);
 565 
 566         if (style == TextStyle.FULL) {
 567             f = new DateTimeFormatterBuilder().appendPattern("ZZZZ")
 568                                               .toFormatter(Locale.US);
 569             assertEquals(f.format(odt), expected);
 570             assertEquals(f.format(zdt), expected);
 571             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 572 
 573             f = new DateTimeFormatterBuilder().appendPattern("OOOO")
 574                                               .toFormatter(Locale.US);
 575             assertEquals(f.format(odt), expected);
 576             assertEquals(f.format(zdt), expected);
 577             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 578         }
 579 
 580         if (style == TextStyle.SHORT) {
 581             f = new DateTimeFormatterBuilder().appendPattern("O")
 582                                               .toFormatter(Locale.US);
 583             assertEquals(f.format(odt), expected);
 584             assertEquals(f.format(zdt), expected);
 585             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 586         }
 587     }
 588 
 589     //-----------------------------------------------------------------------
 590     @Test(expectedExceptions=IllegalArgumentException.class)
 591     public void test_print_pattern_X6rejected() {
 592         builder.appendPattern("XXXXXX");
 593     }
 594 
 595     @Test(expectedExceptions=IllegalArgumentException.class)
 596     public void test_print_pattern_x6rejected() {
 597         builder.appendPattern("xxxxxx");
 598     }
 599 
 600     @Test(expectedExceptions=IllegalArgumentException.class)
 601     public void test_print_pattern_Z6rejected() {
 602         builder.appendPattern("ZZZZZZ");


< prev index next >