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