jdk/test/java/util/logging/LocalizedLevelName.java
author malenkov
Fri, 16 May 2014 15:51:57 +0400
changeset 25088 8d4b058368f0
parent 19795 6c628e165476
child 36511 9d0388c6b336
permissions -rw-r--r--
8043152: KSS: javax.swing.plaf.synth.SynthContext Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19795
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     1
/*
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     4
 *
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     8
 *
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    14
 *
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    18
 *
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    21
 * questions.
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    22
 */
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    23
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    24
import java.util.*;
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    25
import java.util.logging.*;
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    26
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    27
/*
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    28
 * @test
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    29
 * @bug 8016127 8024131
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    30
 * @summary test logging.properties localized
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    31
 * @run main/othervm LocalizedLevelName
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    32
 */
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    33
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    34
public class LocalizedLevelName {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    35
    private static Object[] namesMap = {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    36
        "SEVERE",  Locale.ENGLISH, "Severe",        Level.SEVERE,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    37
        "WARNING", Locale.FRENCH,  "Avertissement", Level.WARNING,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    38
        "INFO",    Locale.ITALIAN, "Informazioni",  Level.INFO,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    39
        "SEVERE",  Locale.FRENCH,  "Grave",         Level.SEVERE,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    40
        "CONFIG",  Locale.GERMAN,  "Konfiguration", Level.CONFIG,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    41
        "ALL",     Locale.ROOT,    "All",           Level.ALL,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    42
        "SEVERE",  Locale.ROOT,    "Severe",        Level.SEVERE,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    43
        "WARNING", Locale.ROOT,    "Warning",       Level.WARNING,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    44
        "CONFIG",  Locale.ROOT,    "Config",        Level.CONFIG,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    45
        "INFO",    Locale.ROOT,    "Info",          Level.INFO,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    46
        "FINE",    Locale.ROOT,    "Fine",          Level.FINE,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    47
        "FINER",   Locale.ROOT,    "Finer",         Level.FINER,
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    48
        "FINEST",  Locale.ROOT,    "Finest",        Level.FINEST
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    49
    };
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    50
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    51
    public static void main(String args[]) throws Exception {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    52
        Locale defaultLocale = Locale.getDefault();
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    53
        for (int i=0; i<namesMap.length; i += 4) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    54
            final String key = (String) namesMap[i];
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    55
            final Locale locale = (Locale) namesMap[i+1];
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    56
            final String expectedTranslation = (String) namesMap[i+2];
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    57
            final Level level = (Level) namesMap[i+3];
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    58
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    59
            final String en = getLocalizedMessage(Locale.ENGLISH, key);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    60
            final String other = getLocalizedMessage(locale, key);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    61
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    62
            System.out.println(locale + ": " + key + "=" + expectedTranslation
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    63
                    + ", (Level." + level.getName() + ")");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    64
            System.out.println("     => localized(" + Locale.ENGLISH + ", "
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    65
                    + key + ")=" + en);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    66
            System.out.println("     => localized(" + locale + ", " + key
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    67
                    + ")=" + other);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    68
            if (!key.equals(en.toUpperCase(Locale.ROOT))) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    69
                throw new RuntimeException("Expect " + key
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    70
                        + " equals upperCase(" + en + ")");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    71
            }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    72
            if (!Locale.ENGLISH.equals(locale) && !Locale.ROOT.equals(locale)
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    73
                    && key.equals(other.toUpperCase(Locale.ROOT))) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    74
                throw new RuntimeException("Expect " + key
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    75
                        + " not equals upperCase(" + other +")");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    76
            }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    77
            if ((Locale.ENGLISH.equals(locale) || Locale.ROOT.equals(locale))
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    78
                    && !key.equals(other.toUpperCase(Locale.ROOT))) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    79
                throw new RuntimeException("Expect " + key
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    80
                        + " equals upperCase(" + other +")");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    81
            }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    82
            if (!other.equals(expectedTranslation)) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    83
                throw new RuntimeException("Expected \"" + expectedTranslation
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    84
                        + "\" for '" + locale + "' but got \"" + other + "\"");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    85
            }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    86
            Locale.setDefault(locale);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    87
            final String levelName = level.getLocalizedName();
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    88
            System.out.println("Level.getLocalizedName() is: " + levelName);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    89
            if (!levelName.equals(other.toUpperCase(locale))) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    90
                throw new RuntimeException("Expected \""
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    91
                        + other.toUpperCase(locale) + "\" for '"
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    92
                        + locale + "' but got \"" + levelName + "\"");
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    93
            }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    94
            Locale.setDefault(defaultLocale);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    95
       }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    96
    }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    97
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    98
    private static final String RBNAME = "sun.util.logging.resources.logging";
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
    99
    private static String getLocalizedMessage(Locale locale, String key) {
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
   100
        ResourceBundle rb = ResourceBundle.getBundle(RBNAME, locale);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
   101
        return rb.getString(key);
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
   102
    }
6c628e165476 8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
diff changeset
   103
}