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