1 /* 2 * Copyright (c) 2004, 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 /** 25 * @test 26 * @bug 5008856 5023573 5024917 5062569 7172176 27 * @summary 'missing resource key' error for key = "Operating system" 28 * 29 * @modules jdk.jconsole/sun.tools.jconsole 30 * jdk.jconsole/sun.tools.jconsole.resources:open 31 * 32 * @run main ResourceCheckTest 33 */ 34 35 import java.lang.reflect.Field; 36 import java.lang.reflect.Modifier; 37 import java.lang.reflect.Module; 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.Locale; 42 import java.util.ResourceBundle; 43 44 import sun.tools.jconsole.Messages; 45 import sun.tools.jconsole.Resources; 46 47 /* 48 * Ensures that there is a one-to-one mapping between constants in the 49 * Message class and the keys in the sun.tools.jconsole.resources.messages 50 * bundle. 51 * 52 * An error will be thrown if there is a: 53 * 54 * - key in the resource bundle that doesn't have a public static field with 55 * the same name in the Message class. 56 * 57 * - public static field in the Message class that doesn't have a key with 58 * the same name in the resource bundle. 59 * 60 * - message with a mnemonic identifier(&) for which a mnemonic can't 61 * be looked up using Resources#getMnemonicInt(). 62 * 63 */ 64 public class ResourceCheckTest { 65 private static final String MISSING_RESOURCE_KEY_PREFIX = "missing message for"; 66 private static final String RESOURCE_BUNDLE = "sun.tools.jconsole.resources.messages"; 67 private static final String NEW_LINE = String.format("%n"); 68 69 public static void main(String... args) { 70 List<String> errors = new ArrayList<>(); 71 // Ensure that all Message fields have a corresponding key/value 72 // in the resource bundle and that mnemonics can be looked 73 // up where applicable. 74 Module module = sun.tools.jconsole.Messages.class.getModule(); 75 ResourceBundle rb = ResourceBundle.getBundle(RESOURCE_BUNDLE, module); 76 for (Field field : Messages.class.getFields()) { 77 if (isResourceKeyField(field)) { 78 String resourceKey = field.getName(); 79 String message = readField(field); 80 if (message.startsWith(MISSING_RESOURCE_KEY_PREFIX)) { 81 errors.add("Can't find message (and perhaps mnemonic) for " 82 + Messages.class.getSimpleName() + "." 83 + resourceKey + " in resource bundle."); 84 } else { 85 String resourceMessage = rb.getString(resourceKey); 86 if (hasMnemonicIdentifier(resourceMessage)) { 87 int mi = Resources.getMnemonicInt(message); 88 if (mi == 0) { 89 errors.add("Could not look up mnemonic for message '" 90 + message + "'."); 91 } 92 } 93 } 94 } 95 } 96 97 // Ensure that there is Message class field for every resource key. 98 for (String key : Collections.list(rb.getKeys())) { 99 try { 100 Messages.class.getField(key); 101 } catch (NoSuchFieldException nfe) { 102 errors.add("Can't find static field (" 103 + Messages.class.getSimpleName() + "." + key 104 + ") matching '" + key 105 + "' in resource bundle. Unused message?"); 106 } 107 } 108 109 if (errors.size() > 0) { 110 throwError(errors); 111 } 112 } 113 114 private static String readField(Field field) { 115 try { 116 return (String) field.get(null); 117 } catch (IllegalArgumentException | IllegalAccessException e) { 118 throw new Error("Could not access field " + field.getName() 119 + " when trying to read resource message."); 120 } 121 } 122 123 private static boolean isResourceKeyField(Field field) { 124 int modifiers = field.getModifiers(); 125 return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers); 126 } 127 128 private static boolean hasMnemonicIdentifier(String s) { 129 for (int i = 0; i < s.length() - 1; i++) { 130 if (s.charAt(i) == '&') { 131 if (s.charAt(i + 1) != '&') { 132 return true; 133 } else { 134 i++; 135 } 136 } 137 } 138 return false; 139 } 140 141 private static void throwError(List<String> errors) { 142 StringBuffer buffer = new StringBuffer(); 143 buffer.append("Found "); 144 buffer.append(errors.size()); 145 buffer.append(" error(s) when checking one-to-one mapping "); 146 buffer.append("between Message and resource bundle keys in "); 147 buffer.append(RESOURCE_BUNDLE); 148 buffer.append(" with "); 149 buffer.append(Locale.getDefault()); 150 buffer.append(" locale."); 151 buffer.append(NEW_LINE); 152 int errorIndex = 1; 153 for (String error : errors) { 154 buffer.append("Error "); 155 buffer.append(errorIndex); 156 buffer.append(": "); 157 buffer.append(error); 158 buffer.append(NEW_LINE); 159 errorIndex++; 160 } 161 throw new Error(buffer.toString()); 162 } 163 }