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.io.*;



  25 import java.util.*;
  26 import java.util.logging.*;
  27 
  28 /*
  29  * @test
  30  * @bug 8026027
  31  * @summary Test Level.parse to look up custom levels by name and its
  32  *          localized name
  33  *
  34  * @run main/othervm CustomLevel
  35  */
  36 
  37 public class CustomLevel extends Level {
  38     public CustomLevel(String name, int value, String resourceBundleName) {
  39         super(name, value, resourceBundleName);
  40     }
  41 
  42     private static final List<Level> levels = new ArrayList<>();
  43     private static final String RB_NAME = "myresource";



















  44     public static void main(String[] args) throws Exception {
  45         setupCustomLevels();
  46 
  47         // Level.parse will return the custom Level instance
  48         ResourceBundle rb = ResourceBundle.getBundle(RB_NAME);
  49         for (Level level : levels) {
  50             String name = level.getName();

  51             if (!name.equals("WARNING") && !name.equals("INFO")) {
  52                 // custom level whose name doesn't conflict with any standard one
  53                 checkCustomLevel(Level.parse(name), level);



  54             }


  55             String localizedName = rb.getString(level.getName());
  56             Level l = Level.parse(localizedName);
  57             if (l != level) {
  58                 throw new RuntimeException("Unexpected level " + l + " " + l.getClass());
  59             }
  60         }

















































  61     }
  62 
  63     private static void setupCustomLevels() throws IOException {
  64         levels.add(new CustomLevel("EMERGENCY", 1090, RB_NAME));
  65         levels.add(new CustomLevel("ALERT", 1060, RB_NAME));
  66         levels.add(new CustomLevel("CRITICAL", 1030, RB_NAME));
  67         levels.add(new CustomLevel("WARNING", 1010, RB_NAME));
  68         levels.add(new CustomLevel("INFO", 1000, RB_NAME));
  69     }
  70     static void checkCustomLevel(Level level, Level expected) {
  71         // Level value must be the same
  72         if (!level.equals(expected)) {
  73             throw new RuntimeException(formatLevel(level) + " != " + formatLevel(expected));
  74         }
  75 
  76         if (!level.getName().equals(expected.getName())) {
  77             throw new RuntimeException(formatLevel(level) + " != " + formatLevel(expected));
  78         }
  79 
  80         // Level.parse is expected to return the custom Level
  81         if (level != expected) {
  82             throw new RuntimeException(formatLevel(level) + " != " + formatLevel(expected));
  83         }
  84 
  85         ResourceBundle rb = ResourceBundle.getBundle(RB_NAME);
  86         String name = rb.getString(level.getName());
  87         if (!level.getLocalizedName().equals(name)) {
  88             // must have the same localized name
  89             throw new RuntimeException(level.getLocalizedName() + " != " + name);
  90         }
  91     }
  92 
  93     static String formatLevel(Level l) {
  94         return l + ":" + l.intValue() + ":" + l.getClass().getName();
  95     }
  96 }
--- EOF ---