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/*
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*/
33
34import java.util.HashMap;
35import java.util.Locale;
36import java.util.Map;
37import java.util.ResourceBundle;
38import sun.util.locale.provider.LocaleProviderAdapter.Type;
39import sun.util.locale.provider.LocaleProviderAdapter;
40
41public class Bug8154797 {
42    static Map<String, String> expectedResourcesMap = new HashMap<>();
43    static final String GMT_RESOURCE_KEY = "timezone.gmtFormat";
44    static final String HMT_RESOURCE_KEY = "timezone.hourFormat";
45    static final String GMT = "Gmt";
46    static final String HMT = "Hmt";
47
48    static void generateExpectedValues() {
49        expectedResourcesMap.put("FR" + GMT, "UTC{0}");
50        expectedResourcesMap.put("FR" + HMT, "+HH:mm;\u2212HH:mm");
51        expectedResourcesMap.put("FI" + HMT, "+H.mm;-H.mm");
52        expectedResourcesMap.put("FI" + GMT, "UTC{0}");
53        /* For  root locale, en_US, de_DE, hi_IN, ja_JP,Root locale resources
54        * should be returned.
55         */
56        expectedResourcesMap.put(GMT, "GMT{0}"); //Root locale resource
57        expectedResourcesMap.put(HMT, "+HH:mm;-HH:mm"); //Root locale resource
58    }
59
60    static void compareResources(Locale loc) {
61        String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT;
62        ResourceBundle compatBundle, cldrBundle;
63        compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc)
64                .getJavaTimeFormatData();
65        cldrBundle = LocaleProviderAdapter.forType(Type.CLDR)
66                .getLocaleResources(loc).getJavaTimeFormatData();
67        if (loc.getCountry() == "FR" || loc.getCountry() == "FI") {
68            mapKeyHourFormat = loc.getCountry() + HMT;
69            mapKeyGmtFormat = loc.getCountry() + GMT;
70        }
71
72        if (!(expectedResourcesMap.get(mapKeyGmtFormat)
73                .equals(compatBundle.getString(GMT_RESOURCE_KEY))
74                && expectedResourcesMap.get(mapKeyHourFormat)
75                .equals(compatBundle.getString(HMT_RESOURCE_KEY))
76                && expectedResourcesMap.get(mapKeyGmtFormat)
77                .equals(cldrBundle.getString(GMT_RESOURCE_KEY))
78                && expectedResourcesMap.get(mapKeyHourFormat)
79                .equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) {
80
81            throw new RuntimeException("Retrieved resource does not match with "
82                    + "  expected string for Locale " + compatBundle.getLocale());
83
84        }
85
86    }
87
88    public static void main(String args[]) {
89        Bug8154797.generateExpectedValues();
90        Locale[] locArr = {new Locale("hi", "IN"), Locale.UK, new Locale("fi", "FI"),
91                           Locale.ROOT, Locale.GERMAN, Locale.JAPANESE,
92                           Locale.ENGLISH, Locale.FRANCE};
93        for (Locale loc : locArr) {
94            Bug8154797.compareResources(loc);
95        }
96    }
97
98}
99
100