Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jconsole/ResourceCheckTest.java
38838 views
/*1* Copyright (c) 2004, 2020, 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*25*26* This isn't the test case: ResourceCheckTest.sh is.27* Refer to ResourceCheckTest.sh when running this test.28*29* @bug 5008856 5023573 5024917 5062569 717217630* @summary 'missing resource key' error for key = "Operating system"31*/3233import java.lang.reflect.Field;34import java.lang.reflect.Modifier;35import java.util.ArrayList;36import java.util.Collections;37import java.util.List;38import java.util.Locale;39import java.util.ResourceBundle;4041import sun.tools.jconsole.Messages;42import sun.tools.jconsole.Resources;4344/*45* Ensures that there is a one-to-one mapping between constants in the46* Message class and the keys in the sun.tools.jconsole.resources.messages47* 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 with52* the same name in the Message class.53*54* - public static field in the Message class that doesn't have a key with55* the same name in the resource bundle.56*57* - message with a mnemonic identifier(&) for which a mnemonic can't58* be looked up using Resources#getMnemonicInt().59*60*/61public class ResourceCheckTest {62private static final String MISSING_RESOURCE_KEY_PREFIX = "missing message for";63private static final String RESOURCE_BUNDLE = "sun.tools.jconsole.resources.messages";64private static final String NEW_LINE = String.format("%n");6566public static void main(String... args) {67List<String> errors = new ArrayList<>();68// Ensure that all Message fields have a corresponding key/value69// in the resource bundle and that mnemonics can be looked70// up where applicable.71ResourceBundle rb = ResourceBundle.getBundle(RESOURCE_BUNDLE);72for (Field field : Messages.class.getFields()) {73if (isResourceKeyField(field)) {74String resourceKey = field.getName();75String message = readField(field);76if (message.startsWith(MISSING_RESOURCE_KEY_PREFIX)) {77errors.add("Can't find message (and perhaps mnemonic) for "78+ Messages.class.getSimpleName() + "."79+ resourceKey + " in resource bundle.");80} else {81String resourceMessage = rb.getString(resourceKey);82if (hasMnemonicIdentifier(resourceMessage)) {83int mi = Resources.getMnemonicInt(message);84if (mi == 0) {85errors.add("Could not look up mnemonic for message '"86+ message + "'.");87}88}89}90}91}9293// Ensure that there is Message class field for every resource key.94for (String key : Collections.list(rb.getKeys())) {95try {96Messages.class.getField(key);97} catch (NoSuchFieldException nfe) {98errors.add("Can't find static field ("99+ Messages.class.getSimpleName() + "." + key100+ ") matching '" + key101+ "' in resource bundle. Unused message?");102}103}104105if (errors.size() > 0) {106throwError(errors);107}108}109110private static String readField(Field field) {111try {112return (String) field.get(null);113} catch (IllegalArgumentException | IllegalAccessException e) {114throw new Error("Could not access field " + field.getName()115+ " when trying to read resource message.");116}117}118119private static boolean isResourceKeyField(Field field) {120int modifiers = field.getModifiers();121return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers);122}123124private static boolean hasMnemonicIdentifier(String s) {125for (int i = 0; i < s.length() - 1; i++) {126if (s.charAt(i) == '&') {127if (s.charAt(i + 1) != '&') {128return true;129} else {130i++;131}132}133}134return false;135}136137private static void throwError(List<String> errors) {138StringBuffer buffer = new StringBuffer();139buffer.append("Found ");140buffer.append(errors.size());141buffer.append(" error(s) when checking one-to-one mapping ");142buffer.append("between Message and resource bundle keys in ");143buffer.append(RESOURCE_BUNDLE);144buffer.append(" with ");145buffer.append(Locale.getDefault());146buffer.append(" locale.");147buffer.append(NEW_LINE);148int errorIndex = 1;149for (String error : errors) {150buffer.append("Error ");151buffer.append(errorIndex);152buffer.append(": ");153buffer.append(error);154buffer.append(NEW_LINE);155errorIndex++;156}157throw new Error(buffer.toString());158}159}160161162