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 */ 23 24 /* 25 * @test 26 * @bug 8154520 27 * @summary Tests for DateTimeFormatterBuilder.appendLocalizedOffset 28 * @modules jdk.localedata 29 */ 30 31 package test.java.time.format; 32 33 import static org.testng.Assert.assertEquals; 34 35 import java.time.LocalDateTime; 36 import java.time.OffsetDateTime; 37 import java.time.ZoneOffset; 38 import java.time.ZonedDateTime; 39 import java.time.format.DateTimeFormatter; 40 import java.time.format.DateTimeFormatterBuilder; 41 import java.time.format.TextStyle; 42 import java.util.Locale; 43 44 import org.testng.annotations.BeforeMethod; 45 import org.testng.annotations.DataProvider; 46 import org.testng.annotations.Test; 47 48 /** 49 * Test DateTimeFormatterBuilder.appendOffset(). 50 */ 51 @Test 52 public class TestOffsetPrinterParser { 53 54 private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40); 55 56 private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC; 57 private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45); 58 private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45); 59 private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1); 60 private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23); 61 private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45); 62 private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45); 63 64 private static final Locale LOCALE_AM = new Locale("am"); 65 private static final Locale LOCALE_AR = new Locale("ar"); 66 private static final Locale LOCALE_BG = new Locale("bg"); 67 private static final Locale LOCALE_CN = Locale.CHINESE; 68 private static final Locale LOCALE_EN = Locale.ENGLISH; 69 private static final Locale LOCALE_EO = new Locale("eo"); 70 private static final Locale LOCALE_FR = Locale.FRANCE; 71 private static final Locale LOCALE_GA = new Locale("ga"); 72 private static final Locale LOCALE_KM = new Locale("km"); 73 private static final Locale LOCALE_ML = new Locale("ml"); 74 private static final Locale LOCALE_MR = new Locale("mr"); 75 private static final Locale LOCALE_SE = new Locale("se"); 76 private static final Locale LOCALE_YO_BJ = new Locale("yo", "BJ"); 77 78 private DateTimeFormatterBuilder builder; 79 80 @BeforeMethod 81 public void setUp() { 82 builder = new DateTimeFormatterBuilder(); 83 } 84 85 @DataProvider(name="print_localized_custom_locale") 86 Object[][] data_print_localized_custom_locale() { 87 return new Object[][] { 88 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_AM, "\u1302 \u12a4\u121d \u1272-1:23"}, 89 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_AR, "\u063a\u0631\u064a\u0646\u062a\u0634-01:00"}, 90 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_BG, "\u0413\u0440\u0438\u043d\u0443\u0438\u0447+01:23:45"}, 91 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_CN, "GMT-00:00:45"}, 92 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_EN, "GMT+01:23:45"}, 93 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_EO, "GMT-1:23:45"}, 94 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P000045, LOCALE_FR, "UTC+00:00:45"}, 95 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_GA, "MAG-1"}, 96 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_KM, "\u1798\u17c9\u17c4\u1784\u200b\u179f\u1780\u179b"}, 97 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_ML, "\u0d1c\u0d3f\u0d0e\u0d02\u0d1f\u0d3f"}, 98 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_MR, "[GMT]+01:23:45"}, 99 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_SE, "UTC+1:23:45"}, 100 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_YO_BJ, "WAT-01:00"} 101 }; 102 } 103 104 @Test(dataProvider="print_localized_custom_locale") 105 public void test_print_localized_custom_locale(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) { 106 107 OffsetDateTime odt = OffsetDateTime.of(ldt, offset); 108 ZonedDateTime zdt = ldt.atZone(offset); 109 110 builder = new DateTimeFormatterBuilder().appendLocalizedOffset(style); 111 DateTimeFormatter f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); 112 assertEquals(f.format(odt), expected); 113 assertEquals(f.format(zdt), expected); 114 assertEquals(f.parse(expected, ZoneOffset::from), offset); 115 116 if (style == TextStyle.FULL) { 117 builder = new DateTimeFormatterBuilder().appendPattern("ZZZZ"); 118 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); 119 assertEquals(f.format(odt), expected); 120 assertEquals(f.format(zdt), expected); 121 assertEquals(f.parse(expected, ZoneOffset::from), offset); 122 123 builder = new DateTimeFormatterBuilder().appendPattern("OOOO"); 124 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); 125 assertEquals(f.format(odt), expected); 126 assertEquals(f.format(zdt), expected); 127 assertEquals(f.parse(expected, ZoneOffset::from), offset); 128 } 129 130 if (style == TextStyle.SHORT) { 131 builder = new DateTimeFormatterBuilder().appendPattern("O"); 132 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); 133 assertEquals(f.format(odt), expected); 134 assertEquals(f.format(zdt), expected); 135 assertEquals(f.parse(expected, ZoneOffset::from), offset); 136 } 137 138 } 139 140 }