8187073: The java.util.logging.Level.findLevel() will not correctly find a Level by it's int value
authordfuchs
Fri, 08 Dec 2017 17:40:57 +0000
changeset 48226 ea47055160ef
parent 48225 718669e6b375
child 48227 584a3ba42176
8187073: The java.util.logging.Level.findLevel() will not correctly find a Level by it's int value Reviewed-by: rriggs
src/java.logging/share/classes/java/util/logging/Level.java
test/jdk/java/util/logging/Level/CustomLevel.java
--- a/src/java.logging/share/classes/java/util/logging/Level.java	Fri Dec 08 16:41:30 2017 +0000
+++ b/src/java.logging/share/classes/java/util/logging/Level.java	Fri Dec 08 17:40:57 2017 +0000
@@ -389,14 +389,15 @@
         try {
             int x = Integer.parseInt(name);
             level = KnownLevel.findByValue(x, KnownLevel::mirrored);
-            if (!level.isPresent()) {
-                // add new Level
-                Level levelObject = new Level(name, x);
-                // There's no need to use a reachability fence here because
-                // KnownLevel keeps a strong reference on the level when
-                // level.getClass() == Level.class.
-                return KnownLevel.findByValue(x, KnownLevel::mirrored).get();
+            if (level.isPresent()) {
+                return level.get();
             }
+            // add new Level
+            Level levelObject = new Level(name, x);
+            // There's no need to use a reachability fence here because
+            // KnownLevel keeps a strong reference on the level when
+            // level.getClass() == Level.class.
+            return KnownLevel.findByValue(x, KnownLevel::mirrored).get();
         } catch (NumberFormatException ex) {
             // Not an integer.
             // Drop through.
--- a/test/jdk/java/util/logging/Level/CustomLevel.java	Fri Dec 08 16:41:30 2017 +0000
+++ b/test/jdk/java/util/logging/Level/CustomLevel.java	Fri Dec 08 17:40:57 2017 +0000
@@ -22,6 +22,8 @@
  */
 
 import java.io.*;
+import java.lang.management.ManagementFactory;
+import java.lang.management.PlatformLoggingMXBean;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
@@ -35,9 +37,11 @@
 
 /*
  * @test
- * @bug 8026027 6543126
+ * @bug 8026027 6543126 8187073
+ * @modules java.logging
+  *         java.management
  * @summary Test Level.parse to look up custom levels by name and its
- *          localized name
+ *          localized name, as well as severity.
  *
  * @run main/othervm CustomLevel
  */
@@ -73,6 +77,8 @@
     public static void main(String[] args) throws Exception {
         setupCustomLevels();
         setUpCustomLevelsOtherLoader();
+        PlatformLoggingMXBean mxbean = ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class);
+        Logger logger = Logger.getLogger("foo.bar");
 
         // Level.parse will return the custom Level instance
         for (Level level : levels) {
@@ -96,8 +102,27 @@
                     + l.getClass() + " for " + localizedName
                     + " in " + rb.getBaseBundleName());
             }
+            l = Level.parse(String.valueOf(level.intValue()));
+            System.out.println("Level.parse(" + level.intValue() + ") returns " + l);
+            if (l != level) {
+                if (l == null || l.intValue() != level.intValue()) {
+                    throw new RuntimeException("Unexpected level " + l
+                            + (l == null ? "" : (" " + l.getClass()))
+                            + " for " + level.intValue());
+                }
+            }
+            mxbean.setLoggerLevel(logger.getName(), String.valueOf(level.intValue()));
+            Level l2 = logger.getLevel();
+            if (l2 != level) {
+                if (l2 == null || l2.intValue() != level.intValue()) {
+                    throw new RuntimeException("Unexpected level " + l2
+                            + (l2 == null ? "" : (" " + l2.getClass()))
+                            + " for " + level.intValue());
+                }
+            }
         }
 
+
         final long otherLevelCount = levels.stream()
             .filter(CustomLevel::isCustomLoader)
             .count();