1 /*
  2  * Copyright (c) 2016, 2023, 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 8154797
 27  * @modules java.base/sun.util.locale.provider
 28  *          java.base/sun.util.resources
 29  *          jdk.localedata
 30  * @summary Test for checking HourFormat and GmtFormat resources are retrieved from
 31  * COMPAT and CLDR Providers.
 32  * @run junit CompareProviderFormats
 33 */
 34 
 35 import java.util.HashMap;
 36 import java.util.Locale;
 37 import java.util.Map;
 38 import java.util.ResourceBundle;
 39 import java.util.stream.Stream;
 40 
 41 import sun.util.locale.provider.LocaleProviderAdapter.Type;
 42 import sun.util.locale.provider.LocaleProviderAdapter;
 43 
 44 import org.junit.jupiter.api.BeforeAll;
 45 import org.junit.jupiter.params.ParameterizedTest;
 46 import org.junit.jupiter.params.provider.MethodSource;
 47 
 48 public class CompareProviderFormats {
 49     static Map<String, String> expectedResourcesMap = new HashMap<>();
 50     static final String GMT_RESOURCE_KEY = "timezone.gmtFormat";
 51     static final String HMT_RESOURCE_KEY = "timezone.hourFormat";
 52     static final String GMT = "Gmt";
 53     static final String HMT = "Hmt";
 54 
 55     /**
 56      * Fill the expectedResourcesMap with the desired key / values
 57      */
 58     @BeforeAll
 59     static void populateResourcesMap() {
 60         expectedResourcesMap.put("FR" + GMT, "UTC{0}");
 61         expectedResourcesMap.put("FR" + HMT, "+HH:mm;\u2212HH:mm");
 62         expectedResourcesMap.put("FI" + HMT, "+H.mm;-H.mm");
 63         expectedResourcesMap.put("FI" + GMT, "UTC{0}");
 64         /* For root locale, en_US, de_DE, hi_IN, ja_JP, Root locale resources
 65          * should be returned.
 66          */
 67         expectedResourcesMap.put(GMT, "GMT{0}"); // Root locale resource
 68         expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); // Root locale resource
 69     }
 70 
 71     /**
 72      * For each locale, ensure that the returned resources for gmt and hmt match
 73      * the expected resources for both COMPAT and CLDR
 74      */
 75     @ParameterizedTest
 76     @MethodSource("localeProvider")
 77     public void compareResourcesTest(Locale loc) {
 78         compareResources(loc);
 79     }
 80 
 81     private void compareResources(Locale loc) {
 82         String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT;
 83         ResourceBundle compatBundle, cldrBundle;
 84         compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc)
 85                 .getJavaTimeFormatData();
 86         cldrBundle = LocaleProviderAdapter.forType(Type.CLDR)
 87                 .getLocaleResources(loc).getJavaTimeFormatData();
 88 
 89         if (loc.getCountry().equals("FR") || loc.getCountry().equals("FI")) {
 90             mapKeyHourFormat = loc.getCountry() + HMT;
 91             mapKeyGmtFormat = loc.getCountry() + GMT;
 92         }
 93 
 94         if (!(expectedResourcesMap.get(mapKeyGmtFormat)
 95                 .equals(compatBundle.getString(GMT_RESOURCE_KEY))
 96                 && expectedResourcesMap.get(mapKeyHourFormat)
 97                 .equals(compatBundle.getString(HMT_RESOURCE_KEY))
 98                 && expectedResourcesMap.get(mapKeyGmtFormat)
 99                 .equals(cldrBundle.getString(GMT_RESOURCE_KEY))
100                 && expectedResourcesMap.get(mapKeyHourFormat)
101                 .equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) {
102             throw new RuntimeException("Retrieved resource does not match with "
103                     + "  expected string for Locale " + compatBundle.getLocale());
104         }
105     }
106 
107     private static Stream<Locale> localeProvider() {
108         return Stream.of(
109                 Locale.of("hi", "IN"),
110                 Locale.UK, Locale.of("fi", "FI"),
111                 Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
112                 Locale.ENGLISH, Locale.FRANCE
113         );
114     }
115 }