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