1 /*
   2  * Copyright (c) 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 import java.util.*;
  25 import java.util.logging.*;
  26 
  27 /*
  28  * @test
  29  * @bug 8016127 8024131
  30  * @summary test logging.properties localized
  31  * @modules java.logging/sun.util.logging.resources:open
  32  * @run main/othervm LocalizedLevelName
  33  */
  34 
  35 public class LocalizedLevelName {
  36     private static Object[] namesMap = {
  37         "SEVERE",  Locale.ENGLISH, "Severe",        Level.SEVERE,
  38         "WARNING", Locale.FRENCH,  "Avertissement", Level.WARNING,
  39         "INFO",    Locale.ITALIAN, "Informazioni",  Level.INFO,
  40         "SEVERE",  Locale.FRENCH,  "Grave",         Level.SEVERE,
  41         "CONFIG",  Locale.GERMAN,  "Konfiguration", Level.CONFIG,
  42         "ALL",     Locale.ROOT,    "All",           Level.ALL,
  43         "SEVERE",  Locale.ROOT,    "Severe",        Level.SEVERE,
  44         "WARNING", Locale.ROOT,    "Warning",       Level.WARNING,
  45         "CONFIG",  Locale.ROOT,    "Config",        Level.CONFIG,
  46         "INFO",    Locale.ROOT,    "Info",          Level.INFO,
  47         "FINE",    Locale.ROOT,    "Fine",          Level.FINE,
  48         "FINER",   Locale.ROOT,    "Finer",         Level.FINER,
  49         "FINEST",  Locale.ROOT,    "Finest",        Level.FINEST
  50     };
  51 
  52     public static void main(String args[]) throws Exception {
  53         Locale defaultLocale = Locale.getDefault();
  54         for (int i=0; i<namesMap.length; i += 4) {
  55             final String key = (String) namesMap[i];
  56             final Locale locale = (Locale) namesMap[i+1];
  57             final String expectedTranslation = (String) namesMap[i+2];
  58             final Level level = (Level) namesMap[i+3];
  59 
  60             final String en = getLocalizedMessage(Locale.ENGLISH, key);
  61             final String other = getLocalizedMessage(locale, key);
  62 
  63             System.out.println(locale + ": " + key + "=" + expectedTranslation
  64                     + ", (Level." + level.getName() + ")");
  65             System.out.println("     => localized(" + Locale.ENGLISH + ", "
  66                     + key + ")=" + en);
  67             System.out.println("     => localized(" + locale + ", " + key
  68                     + ")=" + other);
  69             if (!key.equals(en.toUpperCase(Locale.ROOT))) {
  70                 throw new RuntimeException("Expect " + key
  71                         + " equals upperCase(" + en + ")");
  72             }
  73             if (!Locale.ENGLISH.equals(locale) && !Locale.ROOT.equals(locale)
  74                     && key.equals(other.toUpperCase(Locale.ROOT))) {
  75                 throw new RuntimeException("Expect " + key
  76                         + " not equals upperCase(" + other +")");
  77             }
  78             if ((Locale.ENGLISH.equals(locale) || Locale.ROOT.equals(locale))
  79                     && !key.equals(other.toUpperCase(Locale.ROOT))) {
  80                 throw new RuntimeException("Expect " + key
  81                         + " equals upperCase(" + other +")");
  82             }
  83             if (!other.equals(expectedTranslation)) {
  84                 throw new RuntimeException("Expected \"" + expectedTranslation
  85                         + "\" for '" + locale + "' but got \"" + other + "\"");
  86             }
  87             Locale.setDefault(locale);
  88             final String levelName = level.getLocalizedName();
  89             System.out.println("Level.getLocalizedName() is: " + levelName);
  90             if (!levelName.equals(other.toUpperCase(locale))) {
  91                 throw new RuntimeException("Expected \""
  92                         + other.toUpperCase(locale) + "\" for '"
  93                         + locale + "' but got \"" + levelName + "\"");
  94             }
  95             Locale.setDefault(defaultLocale);
  96        }
  97     }
  98 
  99     private static final String RBNAME = "sun.util.logging.resources.logging";
 100     private static String getLocalizedMessage(Locale locale, String key) {
 101         // this test verifies if the logging.properties in the java.logging module
 102         // is localized.
 103         Module module = java.util.logging.Level.class.getModule();
 104         ResourceBundle rb = ResourceBundle.getBundle(RBNAME, locale, module);
 105         return rb.getString(key);
 106     }
 107 }