duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | /* |
| 24 | @test |
| 25 | @summary Verify a few assertions of the new specification of ResourceBundle.getBundle |
| 26 | @build Test4314141B Test4314141B_fr_CH Test4314141B_es_ES |
| 27 | @run main Test4314141 |
| 28 | @bug 4314141 |
| 29 | */ |
| 30 | |
| 31 | import java.util.ResourceBundle; |
| 32 | import java.util.Locale; |
| 33 | import java.util.MissingResourceException; |
| 34 | |
| 35 | public class Test4314141 { |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | testCandidateOmission(); |
| 39 | testExample(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Tests that candidate bundle names where the final component is an empty string are omitted. |
| 44 | * Previous versions of ResourceBundle might attempt to load bundles with a trailing |
| 45 | * underscore (e.g., "Test4314141_") resulting from concatenation with an empty string. |
| 46 | * This is no longer permitted. |
| 47 | */ |
| 48 | static void testCandidateOmission() { |
| 49 | Locale.setDefault(Locale.US); |
| 50 | doTestCandidateOmission("de", "DE", "EURO", new String[] {"_de", ""}); |
| 51 | doTestCandidateOmission("de", "DE", "", new String[] {"_de", ""}); |
| 52 | doTestCandidateOmission("de", "", "EURO", new String[] {"_de", ""}); |
| 53 | doTestCandidateOmission("de", "", "", new String[] {"_de", ""}); |
| 54 | doTestCandidateOmission("", "DE", "EURO", new String[] {"__DE", ""}); |
| 55 | doTestCandidateOmission("", "DE", "", new String[] {"__DE", ""}); |
| 56 | doTestCandidateOmission("", "", "EURO", new String[] {"___EURO", ""}); |
| 57 | doTestCandidateOmission("", "", "", new String[] {""}); |
| 58 | } |
| 59 | |
| 60 | static void doTestCandidateOmission(String language, String country, String variant, |
| 61 | String[] expectedSuffixes) { |
| 62 | doTest("Test4314141A", language, country, variant, expectedSuffixes); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Verifies the example from the getBundle specification. |
| 67 | */ |
| 68 | static void testExample() { |
| 69 | Locale.setDefault(new Locale("en", "UK")); |
| 70 | doTestExample("fr", "CH", new String[] {"_fr_CH.class", "_fr.properties", ".class"}); |
| 71 | doTestExample("fr", "FR", new String[] {"_fr.properties", ".class"}); |
| 72 | doTestExample("de", "DE", new String[] {"_en.properties", ".class"}); |
| 73 | doTestExample("en", "US", new String[] {"_en.properties", ".class"}); |
| 74 | doTestExample("es", "ES", new String[] {"_es_ES.class", ".class"}); |
| 75 | } |
| 76 | |
| 77 | static void doTestExample(String language, String country, String[] expectedSuffixes) { |
| 78 | doTest("Test4314141B", language, country, "", expectedSuffixes); |
| 79 | } |
| 80 | |
| 81 | static void doTest(String baseName, String language, String country, String variant, |
| 82 | String[] expectedSuffixes) { |
| 83 | System.out.print("Looking for " + baseName + " \"" + language + "\", \"" + country + "\", \"" + variant + "\""); |
| 84 | ResourceBundle bundle = ResourceBundle.getBundle(baseName, new Locale(language, country, variant)); |
| 85 | System.out.print(" => got "); |
| 86 | String previousName = null; |
| 87 | int nameCount = 0; |
| 88 | for (int i = 3; i >= 0; i--) { |
| 89 | String name = bundle.getString("name" + i); |
| 90 | if (!name.equals(previousName)) { |
| 91 | if (previousName != null) { |
| 92 | System.out.print(", "); |
| 93 | } |
| 94 | System.out.print(name); |
| 95 | if (!name.equals(baseName + expectedSuffixes[nameCount++])) { |
| 96 | System.out.println(); |
| 97 | throw new RuntimeException("Error: got unexpected resource bundle"); |
| 98 | } |
| 99 | previousName = name; |
| 100 | } |
| 101 | } |
| 102 | System.out.println(); |
| 103 | if (nameCount != expectedSuffixes.length) { |
| 104 | throw new RuntimeException("Error: parent chain too short"); |
| 105 | } |
| 106 | } |
| 107 | } |