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 20 public class TestMessages extends TestCase { 21 public TestMessages(String name) { 22 super(name); 23 } 25 public static Test suite() { 26 return new TestSuite(TestMessages.class); 27 } 28 29 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 } 59 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 } 110 111 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 } 125 126 private static final String LS = System.getProperty("line.separator"); 127 128 private String errors = ""; 129 130 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 } 152 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 } 167 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 string = string.substring(index + pattern.length()); 186 187 String [] msgArgs = args(string); 189 190 if (msgArgs[0].startsWith("\"")) { 194 String key = msgArgs[0].substring(1, msgArgs[0].length() - 1); 195 196 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 int realParms = count(value); 207 208 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 } 222 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 String quote = readQuote(string); 237 arg = arg + quote; 238 string = string.substring(quote.length()); 239 } 240 else if (string.startsWith("'")) { 241 242 arg = arg + string.substring(0, 2); 244 string = string.substring(2); 245 } 246 else if (string.startsWith(",")) { 247 248 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 ++innerParens; 263 arg = arg + '('; 264 string = string.substring(1); 265 } 266 else if (string.startsWith(")")) { 267 268 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 if (!Character.isWhitespace(string.charAt(0))) { 287 arg = arg + string.charAt(0); 288 } 289 string = string.substring(1); 290 } 291 } 292 } 294 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 } 313 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 } } 333 | Popular Tags |