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