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