1/*
2 * Copyright (c) 2007, 2013, 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 */
26
27import java.text.*;
28import java.util.*;
29import sun.util.locale.provider.*;
30import sun.util.resources.*;
31
32public class LocaleNameProviderTest extends ProviderTest {
33
34    public static void main(String[] s) {
35        new LocaleNameProviderTest();
36    }
37
38    LocaleNameProviderTest() {
39        checkAvailLocValidityTest();
40        variantFallbackTest();
41    }
42
43    void checkAvailLocValidityTest() {
44        com.bar.LocaleNameProviderImpl lnp = new com.bar.LocaleNameProviderImpl();
45        Locale[] availloc = Locale.getAvailableLocales();
46        Locale[] testloc = availloc.clone();
47        List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getLocaleNameProvider().getAvailableLocales());
48        List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());
49
50        for (Locale target: availloc) {
51            // pure JRE implementation
52            OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getLocaleNames(target);
53            boolean jreSupportsTarget = jreimplloc.contains(target);
54
55            for (Locale test: testloc) {
56                // codes
57                String lang = test.getLanguage();
58                String ctry = test.getCountry();
59                String vrnt = test.getVariant();
60
61                // the localized name
62                String langresult = test.getDisplayLanguage(target);
63                String ctryresult = test.getDisplayCountry(target);
64                String vrntresult = test.getDisplayVariant(target);
65
66                // provider's name (if any)
67                String providerslang = null;
68                String providersctry = null;
69                String providersvrnt = null;
70                if (providerloc.contains(target)) {
71                    providerslang = lnp.getDisplayLanguage(lang, target);
72                    providersctry = lnp.getDisplayCountry(ctry, target);
73                    providersvrnt = lnp.getDisplayVariant(vrnt, target);
74                }
75
76                // JRE's name
77                String jreslang = null;
78                String jresctry = null;
79                String jresvrnt = null;
80                if (!lang.equals("")) {
81                    try {
82                        jreslang = rb.getString(lang);
83                    } catch (MissingResourceException mre) {}
84                }
85                if (!ctry.equals("")) {
86                    try {
87                        jresctry = rb.getString(ctry);
88                    } catch (MissingResourceException mre) {}
89                }
90                if (!vrnt.equals("")) {
91                    try {
92                        jresvrnt = rb.getString("%%"+vrnt);
93                    } catch (MissingResourceException mre) {}
94                }
95
96                System.out.print("For key: "+lang+" ");
97                checkValidity(target, jreslang, providerslang, langresult,
98                    jreSupportsTarget && jreslang != null);
99                System.out.print("For key: "+ctry+" ");
100                checkValidity(target, jresctry, providersctry, ctryresult,
101                    jreSupportsTarget && jresctry != null);
102                System.out.print("For key: "+vrnt+" ");
103                checkValidity(target, jresvrnt, providersvrnt, vrntresult,
104                    jreSupportsTarget && jresvrnt != null);
105            }
106        }
107    }
108
109    void variantFallbackTest() {
110        Locale YY = new Locale("yy", "YY", "YYYY");
111        Locale YY_suffix = new Locale("yy", "YY", "YYYY_suffix");
112        String retVrnt = null;
113        String message = "variantFallbackTest() succeeded.";
114
115
116        try {
117            YY.getDisplayVariant(YY_suffix);
118            message = "variantFallbackTest() failed. Either provider wasn't invoked, or invoked without suffix.";
119        } catch (RuntimeException re) {
120            retVrnt = re.getMessage();
121            if (YY_suffix.getVariant().equals(retVrnt)) {
122                System.out.println(message);
123                return;
124}
125            message = "variantFallbackTest() failed. Returned variant: "+retVrnt;
126        }
127
128        throw new RuntimeException(message);
129    }
130}
131