1 /*
2  * Copyright (c) 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 /*
24  * @test
25  * @modules jdk.localedata
26  * @bug 8146750
27  * @summary Test Narrow and NarrowStandalone month names are retrieved correctly.
28  */
29 package test.java.time.format;
30 
31 import static org.testng.Assert.assertEquals;
32 
33 import java.time.DayOfWeek;
34 import java.time.Month;
35 import java.time.format.TextStyle;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Locale;
39 
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42 
43 public class TestNarrowMonthNamesAndDayNames {
44 
45     static {
46         System.setProperty("java.locale.providers", "COMPAT");
47     }
48 
49     private static final List<Month> MONTHVALUES = Arrays.asList(Month.values());
50     private static final List<DayOfWeek> DAYVALUES = Arrays.asList(DayOfWeek.values());
51     private static final List<TextStyle> TEXTSTYLELIST = Arrays.asList(TextStyle.NARROW,
52             TextStyle.NARROW_STANDALONE);
53     private static final List<Locale> LOCARR = Arrays.asList(Locale.US,
54             Locale.GERMANY,
55             Locale.FRANCE,
56             new Locale("no", "NO"));
57 
58     /**
59      * Locale en_US, de_DE, fr_FR, no_NO will have same Narrow and
60      * Narrow_Standalone month Names for COMPAT Provider.
61      */
62     @DataProvider(name = "MonthNarrows")
monthNameData()63     public Object[][] monthNameData() {
64         return new Object[][]{{new String[]{
65             "J",
66             "F",
67             "M",
68             "A",
69             "M",
70             "J",
71             "J",
72             "A",
73             "S",
74             "O",
75             "N",
76             "D"
77         }},};
78     }
79 
80     //-----------------------------------------------------------------------
81     // Check Narrow and Narrow_standalone month name values
82     //-----------------------------------------------------------------------
83     @Test(dataProvider = "MonthNarrows")
compareMonthNarrowValues(String[] monthNarrowExpected)84     public void compareMonthNarrowValues(String[] monthNarrowExpected) {
85         LOCARR.forEach((loc) -> {
86             TEXTSTYLELIST.forEach((style) -> {
87                 MONTHVALUES.forEach((value) -> {
88                     String result = value.getDisplayName(style, loc);
89                     int index = value.ordinal();
90                     assertEquals(result, monthNarrowExpected[index], "Test failed"
91                             + " for COMPAT Provider for locale "
92                             + loc + " for style " + style.name()
93                             + " with Month value " + value.name());
94                 });
95             });
96         });
97     }
98 
99     /**
100      * Locale en_US, de_DE, fr_FR, no_NO will have different Narrow and
101      * Narrow_Standalone Day Names for COMPAT Provider.
102      */
103     @DataProvider(name = "DayNarrows")
dayNameData()104     public Object[][] dayNameData() {
105         return new Object[][]{
106             {Locale.US, new String[]{"M", "T", "W", "T", "F", "S", "S"}},
107             {Locale.GERMANY, new String[]{"M", "D", "M", "D", "F", "S", "S"}},
108             {Locale.FRANCE, new String[]{"L", "M", "M", "J", "V", "S", "D"}},
109             {new Locale("no", "NO"), new String[]{"M", "T", "O", "T", "F", "L", "S"}},};
110     }
111 
112     //-----------------------------------------------------------------------
113     // Check Narrow and Narrow_standalone Day name values
114     //-----------------------------------------------------------------------
115     @Test(dataProvider = "DayNarrows")
compareDayNarrowValues(Locale locale, String[] dayNarrowExpected)116     public void compareDayNarrowValues(Locale locale, String[] dayNarrowExpected) {
117         TEXTSTYLELIST.forEach((style) -> {
118             DAYVALUES.forEach((value) -> {
119                 String result = value.getDisplayName(style, locale);
120                 int index = value.ordinal();
121                 assertEquals(result, dayNarrowExpected[index], "Test failed"
122                         + " for COMPAT Provider for locale "
123                         + locale + " for style " + style.name()
124                         + " with Day value " + value.name());
125             });
126         });
127     }
128 }
129