Path: blob/master/test/jdk/sun/tools/jconsole/ResourceCheckTest.java
41149 views
/*1* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 5008856 5023573 5024917 5062569 717217626* @summary 'missing resource key' error for key = "Operating system"27*28* @modules jdk.jconsole/sun.tools.jconsole29* jdk.jconsole/sun.tools.jconsole.resources:open30*31* @run main ResourceCheckTest32*/3334import java.lang.reflect.Field;35import java.lang.reflect.Modifier;36import java.util.ArrayList;37import java.util.Collections;38import java.util.List;39import java.util.Locale;40import java.util.ResourceBundle;4142import sun.tools.jconsole.Messages;43import sun.tools.jconsole.Resources;4445/*46* Ensures that there is a one-to-one mapping between constants in the47* Message class and the keys in the sun.tools.jconsole.resources.messages48* 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 with53* the same name in the Message class.54*55* - public static field in the Message class that doesn't have a key with56* the same name in the resource bundle.57*58* - message with a mnemonic identifier(&) for which a mnemonic can't59* be looked up using Resources#getMnemonicInt().60*61*/62public class ResourceCheckTest {63private static final String MISSING_RESOURCE_KEY_PREFIX = "missing message for";64private static final String RESOURCE_BUNDLE = "sun.tools.jconsole.resources.messages";65private static final String NEW_LINE = String.format("%n");6667public static void main(String... args) {68List<String> errors = new ArrayList<>();69// Ensure that all Message fields have a corresponding key/value70// in the resource bundle and that mnemonics can be looked71// up where applicable.72Module module = sun.tools.jconsole.Messages.class.getModule();73ResourceBundle rb = ResourceBundle.getBundle(RESOURCE_BUNDLE, module);74for (Field field : Messages.class.getFields()) {75if (isResourceKeyField(field)) {76String resourceKey = field.getName();77String message = readField(field);78if (message.startsWith(MISSING_RESOURCE_KEY_PREFIX)) {79errors.add("Can't find message (and perhaps mnemonic) for "80+ Messages.class.getSimpleName() + "."81+ resourceKey + " in resource bundle.");82} else {83String resourceMessage = rb.getString(resourceKey);84if (hasMnemonicIdentifier(resourceMessage)) {85int mi = Resources.getMnemonicInt(message);86if (mi == 0) {87errors.add("Could not look up mnemonic for message '"88+ message + "'.");89}90}91}92}93}9495// Ensure that there is Message class field for every resource key.96for (String key : Collections.list(rb.getKeys())) {97try {98Messages.class.getField(key);99} catch (NoSuchFieldException nfe) {100errors.add("Can't find static field ("101+ Messages.class.getSimpleName() + "." + key102+ ") matching '" + key103+ "' in resource bundle. Unused message?");104}105}106107if (errors.size() > 0) {108throwError(errors);109}110}111112private static String readField(Field field) {113try {114return (String) field.get(null);115} catch (IllegalArgumentException | IllegalAccessException e) {116throw new Error("Could not access field " + field.getName()117+ " when trying to read resource message.");118}119}120121private static boolean isResourceKeyField(Field field) {122int modifiers = field.getModifiers();123return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers);124}125126private static boolean hasMnemonicIdentifier(String s) {127for (int i = 0; i < s.length() - 1; i++) {128if (s.charAt(i) == '&') {129if (s.charAt(i + 1) != '&') {130return true;131} else {132i++;133}134}135}136return false;137}138139private static void throwError(List<String> errors) {140StringBuffer buffer = new StringBuffer();141buffer.append("Found ");142buffer.append(errors.size());143buffer.append(" error(s) when checking one-to-one mapping ");144buffer.append("between Message and resource bundle keys in ");145buffer.append(RESOURCE_BUNDLE);146buffer.append(" with ");147buffer.append(Locale.getDefault());148buffer.append(" locale.");149buffer.append(NEW_LINE);150int errorIndex = 1;151for (String error : errors) {152buffer.append("Error ");153buffer.append(errorIndex);154buffer.append(": ");155buffer.append(error);156buffer.append(NEW_LINE);157errorIndex++;158}159throw new Error(buffer.toString());160}161}162163164