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