Debian Codegraph @

test/utils/TestMessages.java

raw
1 package test.utils;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.Test;
5 import junit.framework.TestCase;
6 import junit.framework.TestSuite;
7
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.util.Enumeration;
11 import java.util.Vector;
12
13
14 /**
15 * This TestCase verifies:
16 * - the contents of resource.properties for well-formedness, and
17 * - tests calls to Messages.getMessage.
18 * - tests Messages extension mechanism
19 */
20 public class TestMessages extends TestCase {
21 public TestMessages(String name) {
22 super(name);
23 } // ctor
24
25 public static Test suite() {
26 return new TestSuite(TestMessages.class);
27 }
28
29 /**
30 * Call getMessage for each key in resource.properties
31 * to make sure they are all well formed.
32 */
33 private static final int expectedNumberKeysThreshold = 500;
34 public void testAllMessages() {
35 String arg0 = "arg0";
36 String arg1 = "arg1";
37 String[] args = {arg0, arg1, "arg2"};
38
39 int count = 0;
40 Enumeration keys = Messages.getResourceBundle().getKeys();
41 while (keys.hasMoreElements()) {
42 count++;
43 String key = (String) keys.nextElement();
44 try {
45 String message = Messages.getMessage(key);
46 message = Messages.getMessage(key, arg0);
47 message = Messages.getMessage(key, arg0, arg1);
48 message = Messages.getMessage(key, args);
49 }
50 catch (IllegalArgumentException iae) {
51 throw new AssertionFailedError("Test failure on key = " + key + ": " + iae.getMessage());
52 }
53 }
54
55 assertTrue("expected # keys greater than " + expectedNumberKeysThreshold + ", only got " + count + "! VERIFY HIERARCHICAL MESSAGES WORK/LINKED CORRECTLY",
56 count > expectedNumberKeysThreshold);
57 } // testAllMessages
58
59 /**
60 * Make sure the test messages come out as we expect them to.
61 */
62 public void testTestMessages() {
63 try {
64 String message = Messages.getMessage("test00");
65 String expected = "...";
66 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
67 message = Messages.getMessage("test00", new String[0]);
68 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
69 message = Messages.getMessage("test00", new String[] {"one", "two"});
70 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
71 message = Messages.getMessage("test01");
72 expected = ".{0}.";
73 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
74 message = Messages.getMessage("test01", "one");
75 expected = ".one.";
76 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
77 message = Messages.getMessage("test01", new String[0]);
78 expected = ".{0}.";
79 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
80 message = Messages.getMessage("test01", new String[] {"one"});
81 expected = ".one.";
82 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
83 message = Messages.getMessage("test01", new String[] {"one", "two"});
84 expected = ".one.";
85 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
86 message = Messages.getMessage("test02");
87 expected = "{0}, {1}.";
88 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
89 message = Messages.getMessage("test02", new String[0]);
90 expected = "{0}, {1}.";
91 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
92 message = Messages.getMessage("test02", new String[] {"one"});
93 expected = "one, {1}.";
94 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
95 message = Messages.getMessage("test02", new String[] {"one", "two"});
96 expected = "one, two.";
97 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
98 message = Messages.getMessage("test03", new String[] {"one", "two", "three"});
99 expected = ".three, one, two.";
100 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
101 message = Messages.getMessage("test04", new String[] {"one", "two", "three", "four", "five", "six"});
102 expected = ".one two three ... four three five six.";
103 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
104 }
105 catch (Throwable t) {
106 throw new AssertionFailedError("Test failure: " + t.getMessage());
107 }
108 } // testTestMessages
109
110
111 /**
112 * Make sure the extended test messages come out as we expect them to.
113 */
114 public void testTestExtendedMessages() {
115 try {
116 String message = Messages.getMessage("extended.test00");
117 String expected = "message in extension file";
118 assertTrue("expected (" + expected + ") got (" + message + ")", expected.equals(message));
119 }
120 catch (Throwable t) {
121 throw new AssertionFailedError("Test failure: " + t.getMessage());
122 }
123 } // testTestExtendedMessages
124
125
126 private static final String LS = System.getProperty("line.separator");
127
128 private String errors = "";
129
130 /**
131 * If this test is run from xml-axis/java, then walk through the source
132 * tree looking for all calls to Messages.getMessage. For each of these
133 * calls:
134 * 1. Make sure the message key exists in resource.properties
135 * 2. Make sure the actual number of parameters (in resource.properties)
136 * matches the excpected number of parameters (in the source code).
137 */
138 public void testForMissingMessages() {
139 String baseDir = System.getProperty("user.dir");
140 char sep = File.separatorChar;
141 String srcDirStr = baseDir + sep + "src";
142
143 File srcDir = new File(srcDirStr);
144 if (srcDir.exists()) {
145 walkTree(srcDir);
146 }
147 if (!errors.equals("")) {
148 throw new AssertionFailedError(errors);
149 }
150 } // testForMissingMessages
151
152 /**
153 * Walk the source tree
154 */
155 private void walkTree(File srcDir) {
156 File[] files = srcDir.listFiles();
157 for (int i = 0; i < files.length; ++i) {
158 if (files[i].isDirectory()) {
159 walkTree(files[i]);
160 }
161 else if (files[i].getName().endsWith(".java")) {
162 checkMessages(files[i]);
163 }
164 }
165 } // walkTree
166
167 /**
168 * Check all calls to Messages.getMessages:
169 * 1. Make sure the message key exists in resource.properties
170 * 2. Make sure the actual number of parameters (in resource.properties)
171 * matches the expected number of parameters (in the source code).
172 */
173 private void checkMessages(File file) {
174 try {
175 FileInputStream fis = new FileInputStream(file);
176 byte[] bytes = new byte[fis.available()];
177 fis.read(bytes);
178 final String pattern = "Messages.getMessage(";
179 String string = new String(bytes);
180 while (true) {
181 int index = string.indexOf(pattern);
182 if (index < 0) break;
183
184 // Bump the string past the pattern-string
185 string = string.substring(index + pattern.length());
186
187 // Get the arguments for the getMessage call
188 String[] msgArgs = args(string);
189
190 // The first argument is the key.
191 // If the key is a literal string, check the usage.
192 // If the key is not a literal string, accept the usage.
193 if (msgArgs[0].startsWith("\"")) {
194 String key = msgArgs[0].substring(1, msgArgs[0].length() - 1);
195
196 // Get the raw message
197 String value = null;
198 try {
199 value = Messages.getMessage(key);
200 }
201 catch (Throwable t) {
202 errors = errors + "File: " + file.getPath() + " " + t.getMessage() + LS;
203 }
204 // The realParms count is the number of strings in the
205 // message of the form: {X} where X is 0..9
206 int realParms = count(value);
207
208 // The providedParms count is the number of arguments to
209 // getMessage, minus the first argument (key).
210 int providedParms = msgArgs.length - 1;
211 if (realParms != providedParms) {
212 errors = errors + "File: '" + file.getPath() + "', Key '" + key + "' specifies " + realParms + " {X} parameters, but " + providedParms + " parameter(s) provided." + LS;
213 }
214 }
215 }
216 }
217 catch (Throwable t) {
218 errors = errors + "File: " + file.getPath() + " " + t.getMessage() + LS;
219 }
220 } // checkMessages
221
222 /**
223 * For the given method call string, return the parameter strings.
224 * This means that everything between the first "(" and the last ")",
225 * and each "," encountered at the top level delimits a parameter.
226 */
227 private String[] args (String string) {
228 int innerParens = 0;
229 Vector args = new Vector();
230 String arg = "";
231 while (true) {
232 if (string.startsWith("\"")) {
233
234 // Make sure we don't look for the following characters within quotes:
235 // , ' " ( )
236 String quote = readQuote(string);
237 arg = arg + quote;
238 string = string.substring(quote.length());
239 }
240 else if (string.startsWith("'")) {
241
242 // Make sure we ignore a quoted character
243 arg = arg + string.substring(0, 2);
244 string = string.substring(2);
245 }
246 else if (string.startsWith(",")) {
247
248 // If we're at the top level (ie., not inside inner parens like:
249 // (X, Y, new String(str, 0))), then we are seeing the end of an argument.
250 if (innerParens == 0) {
251 args.add(arg);
252 arg = "";
253 }
254 else {
255 arg = arg + ',';
256 }
257 string = string.substring(1);
258 }
259 else if (string.startsWith("(")) {
260
261 // We are stepping within a subexpression delimited by parens
262 ++innerParens;
263 arg = arg + '(';
264 string = string.substring(1);
265 }
266 else if (string.startsWith(")")) {
267
268 // We are either stepping out of a subexpression delimited by parens, or we
269 // have reached the end of the parameter list.
270 if (innerParens == 0) {
271 args.add(arg);
272 String[] argsArray = new String[args.size()];
273 args.toArray(argsArray);
274 return argsArray;
275 }
276 else {
277 --innerParens;
278 arg = arg + ')';
279 string = string.substring(1);
280 }
281 }
282 else {
283
284 // We aren't looking at any special character, just add it to the arg string
285 // we're building.
286 if (!Character.isWhitespace(string.charAt(0))) {
287 arg = arg + string.charAt(0);
288 }
289 string = string.substring(1);
290 }
291 }
292 } // args
293
294 /**
295 * Collect a quoted string, making sure we really end when the string ends.
296 */
297 private String readQuote(String string) {
298 String quote = "\"";
299 string = string.substring(1);
300 while (true) {
301 int index = string.indexOf('"');
302 if (index == 0 || string.charAt(index - 1) != '\\') {
303 quote = quote + string.substring(0, index + 1);
304 return quote;
305 }
306 else {
307 quote = quote + string.substring(0, index + 1);
308 string = string.substring(index);
309 }
310 }
311 } // readQuote
312
313 /**
314 * Count the number of strings of the form {X} where X = 0..9.
315 */
316 private int count(String string) {
317 int parms = 0;
318 int index = string.indexOf("{");
319 while (index >= 0) {
320 try {
321 char parmNumber = string.charAt(index + 1);
322 if (parmNumber >= '0' && parmNumber <= '9' && string.charAt(index + 2) == '}') {
323 ++parms;
324 }
325 string = string.substring(index + 1);
326 index = string.indexOf("{");
327 } catch (Throwable t) {
328 }
329 }
330 return parms;
331 } // count
332 }