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.lang.ref.Reference; 26 import java.lang.ref.ReferenceQueue; 27 import java.lang.ref.WeakReference; 28 import java.lang.reflect.InvocationTargetException; 29 import java.lang.reflect.Method; 30 import java.net.MalformedURLException; 31 import java.net.URL; 32 import java.net.URLClassLoader; 33 import java.util.*; 34 import java.util.logging.*; 35 36 /* 37 * @test 38 * @bug 8026027 6543126 39 * @summary Test Level.parse to look up custom levels by name and its 40 * localized name 41 * 42 * @run main/othervm CustomLevel 43 */ 44 45 public class CustomLevel extends Level { 46 public CustomLevel(String name, int value, String resourceBundleName) { 47 super(name, value, resourceBundleName); 48 } 49 50 private static final List<Level> levels = new ArrayList<>(); 51 private static final String RB_NAME = "myresource"; 52 private static final String OTHERRB_NAME = "myresource2"; 53 54 private static class CustomLevelReference extends WeakReference<Level> { 55 final String name; 56 final int value; 57 final String resourceBundleName; 58 public CustomLevelReference(Level level, ReferenceQueue<Level> queue) { 59 super(level, queue); 60 name = level.getName(); 61 value = level.intValue(); 62 resourceBundleName = level.getResourceBundleName(); 63 } 64 65 @Override 66 public String toString() { 67 return "CustomLevelReference(\"" + name + "\", " + value + ", \"" 68 + resourceBundleName + "\")"; 69 } 70 71 } 72 73 public static void main(String[] args) throws Exception { 74 setupCustomLevels(); 75 setUpCustomLevelsOtherLoader(); 76 77 // Level.parse will return the custom Level instance 78 for (Level level : levels) { 79 final ResourceBundle rb = getResourceBundle(level); 80 String name = level.getName(); 81 Level l = Level.parse(name); 82 if (!name.equals("WARNING") && !name.equals("INFO") 83 && !name.equals("SEVERE")) { 84 // custom level whose name doesn't conflict with any standard one 85 checkCustomLevel(l, level); 86 } else if (l != Level.WARNING && l != Level.INFO && l != Level.SEVERE 87 || !name.equals(l.getName())) { 88 throw new RuntimeException("Unexpected level " + formatLevel(l)); 89 } 90 System.out.println("Level.parse found expected level: " 91 + formatLevel(l)); 92 String localizedName = rb.getString(level.getName()); 93 l = Level.parse(localizedName); 94 if (l != level) { 95 throw new RuntimeException("Unexpected level " + l + " " 96 + l.getClass() + " for " + localizedName 97 + " in " + rb.getBaseBundleName()); 98 } 99 } 100 101 final long otherLevelCount = levels.stream() 102 .filter(CustomLevel::isCustomLoader) 103 .count(); 104 105 // Now verify that custom level instances are correctly 106 // garbage collected when no longer referenced 107 ReferenceQueue<Level> queue = new ReferenceQueue<>(); 108 List<CustomLevelReference> refs = new ArrayList<>(); 109 List<CustomLevelReference> customRefs = new ArrayList<>(); 110 int otherLevels = 0; 111 while (!levels.isEmpty()) { 112 Level l = levels.stream().findAny().get(); 113 boolean isCustomLoader = isCustomLoader(l); 114 if (isCustomLoader) otherLevels++; 115 116 CustomLevelReference ref = new CustomLevelReference(l, queue); 117 if (isCustomLoader) { 118 customRefs.add(ref); 119 } else { | 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.lang.management.ManagementFactory; 26 import java.lang.management.PlatformLoggingMXBean; 27 import java.lang.ref.Reference; 28 import java.lang.ref.ReferenceQueue; 29 import java.lang.ref.WeakReference; 30 import java.lang.reflect.InvocationTargetException; 31 import java.lang.reflect.Method; 32 import java.net.MalformedURLException; 33 import java.net.URL; 34 import java.net.URLClassLoader; 35 import java.util.*; 36 import java.util.logging.*; 37 38 /* 39 * @test 40 * @bug 8026027 6543126 8187073 41 * @modules java.management 42 * @summary Test Level.parse to look up custom levels by name and its 43 * localized name, as well as severity. 44 * 45 * @run main/othervm CustomLevel 46 */ 47 48 public class CustomLevel extends Level { 49 public CustomLevel(String name, int value, String resourceBundleName) { 50 super(name, value, resourceBundleName); 51 } 52 53 private static final List<Level> levels = new ArrayList<>(); 54 private static final String RB_NAME = "myresource"; 55 private static final String OTHERRB_NAME = "myresource2"; 56 57 private static class CustomLevelReference extends WeakReference<Level> { 58 final String name; 59 final int value; 60 final String resourceBundleName; 61 public CustomLevelReference(Level level, ReferenceQueue<Level> queue) { 62 super(level, queue); 63 name = level.getName(); 64 value = level.intValue(); 65 resourceBundleName = level.getResourceBundleName(); 66 } 67 68 @Override 69 public String toString() { 70 return "CustomLevelReference(\"" + name + "\", " + value + ", \"" 71 + resourceBundleName + "\")"; 72 } 73 74 } 75 76 public static void main(String[] args) throws Exception { 77 setupCustomLevels(); 78 setUpCustomLevelsOtherLoader(); 79 PlatformLoggingMXBean mxbean = ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class); 80 Logger logger = Logger.getLogger("foo.bar"); 81 82 // Level.parse will return the custom Level instance 83 for (Level level : levels) { 84 final ResourceBundle rb = getResourceBundle(level); 85 String name = level.getName(); 86 Level l = Level.parse(name); 87 if (!name.equals("WARNING") && !name.equals("INFO") 88 && !name.equals("SEVERE")) { 89 // custom level whose name doesn't conflict with any standard one 90 checkCustomLevel(l, level); 91 } else if (l != Level.WARNING && l != Level.INFO && l != Level.SEVERE 92 || !name.equals(l.getName())) { 93 throw new RuntimeException("Unexpected level " + formatLevel(l)); 94 } 95 System.out.println("Level.parse found expected level: " 96 + formatLevel(l)); 97 String localizedName = rb.getString(level.getName()); 98 l = Level.parse(localizedName); 99 if (l != level) { 100 throw new RuntimeException("Unexpected level " + l + " " 101 + l.getClass() + " for " + localizedName 102 + " in " + rb.getBaseBundleName()); 103 } 104 l = Level.parse(String.valueOf(level.intValue())); 105 System.out.println("Level.parse(" + level.intValue() + ") returns " + l); 106 if (l != level) { 107 if (l == null || l.intValue() != level.intValue()) { 108 throw new RuntimeException("Unexpected level " + l 109 + (l == null ? "" : (" " + l.getClass())) 110 + " for " + level.intValue()); 111 } 112 } 113 mxbean.setLoggerLevel(logger.getName(), String.valueOf(level.intValue())); 114 Level l2 = logger.getLevel(); 115 if (l2 != level) { 116 if (l2 == null || l2.intValue() != level.intValue()) { 117 throw new RuntimeException("Unexpected level " + l2 118 + (l2 == null ? "" : (" " + l2.getClass())) 119 + " for " + level.intValue()); 120 } 121 } 122 } 123 124 125 final long otherLevelCount = levels.stream() 126 .filter(CustomLevel::isCustomLoader) 127 .count(); 128 129 // Now verify that custom level instances are correctly 130 // garbage collected when no longer referenced 131 ReferenceQueue<Level> queue = new ReferenceQueue<>(); 132 List<CustomLevelReference> refs = new ArrayList<>(); 133 List<CustomLevelReference> customRefs = new ArrayList<>(); 134 int otherLevels = 0; 135 while (!levels.isEmpty()) { 136 Level l = levels.stream().findAny().get(); 137 boolean isCustomLoader = isCustomLoader(l); 138 if (isCustomLoader) otherLevels++; 139 140 CustomLevelReference ref = new CustomLevelReference(l, queue); 141 if (isCustomLoader) { 142 customRefs.add(ref); 143 } else { |