1 54 55 package junitx.tool; 56 57 import junit.framework.Test; 58 59 import java.lang.reflect.Method ; 60 import java.lang.reflect.Modifier ; 61 62 114 public class TestClassValidator { 115 116 117 public static final String BANNER = "TestClassValidator, by Vladimir R. Bossicard"; 118 119 private ClassValidator validator = null; 120 private ClassValidatorListener listener = null; 121 122 public void setValidator(ClassValidator validator) { 123 this.validator = validator; 124 } 125 126 public void setValidatorListener(ClassValidatorListener listener) { 127 this.listener = listener; 128 } 129 130 private void init(String [] args) 131 throws ClassNotFoundException , 132 InstantiationException , 133 IllegalAccessException { 134 this.validator = createValidator(args); 135 this.listener = createValidatorListener(args); 136 go(args); 137 } 138 139 private ClassValidator createValidator(String [] args) 140 throws ClassNotFoundException , 141 InstantiationException , 142 IllegalAccessException { 143 ClassValidator res = null; 144 if (args[0].equals("-validator")) { 145 res = (ClassValidator) Class.forName(args[1]).newInstance(); 146 } else if ((args.length >= 3) && 147 args[2].equals("-validator")) { 148 if (res == null) { 149 throw new IllegalStateException ("Two validators are defined"); 150 } else { 151 res = (ClassValidator) Class.forName(args[3]).newInstance(); 152 } 153 } 154 return res; 155 } 156 157 private ClassValidatorListener createValidatorListener(String [] args) 158 throws ClassNotFoundException , 159 InstantiationException , 160 IllegalAccessException { 161 ClassValidatorListener res = null; 162 if (args[0].equals("-listener")) { 163 res = (ClassValidatorListener) Class.forName(args[1]).newInstance(); 164 } else if ((args.length >= 3) && 165 args[2].equals("-listener")) { 166 if (res == null) { 167 throw new IllegalStateException ("Two listeners are defined"); 168 } else { 169 res = (ClassValidatorListener) Class.forName(args[3]).newInstance(); 170 } 171 } 172 return res; 173 } 174 175 private void go(String [] args) 176 throws ClassNotFoundException { 177 if (this.validator == null) { 178 this.validator = new DefaultClassValidator(); 179 } 180 181 if (this.listener == null) { 182 this.listener = new ClassValidatorListener() { 183 184 public void info(String message) { 185 System.out.println("INFO > " + message); 186 } 187 188 public void warning(String message) { 189 System.out.println("WARN > " + message); 190 } 191 192 public void error(String message) { 193 System.out.println("ERROR> " + message); 194 } 195 }; 196 } 197 198 this.validator.setListener(this.listener); 199 this.validator.validate(Class.forName(args[args.length - 1])); 200 } 201 202 private static void man() { 203 System.out.println("usage: junitx.tool.TestClassValidator [-validator classname] [-listener classname] classname"); 204 } 205 206 public static void main(String [] args) { 207 try { 208 System.out.println(BANNER); 209 new TestClassValidator().init(args); 210 } catch (Exception e) { 211 man(); 212 System.out.println(); 213 e.printStackTrace(System.err); 214 } 215 } 216 217 236 public interface ClassValidator { 237 238 243 public void setListener(ClassValidatorListener listener); 244 245 249 public void validate(Class cls); 250 251 } 252 253 262 public interface ClassValidatorListener { 263 264 267 void info(String message); 268 269 273 void warning(String message); 274 275 279 void error(String message); 280 281 } 282 283 292 public static class DefaultClassValidator 293 implements ClassValidator { 294 295 private ClassValidatorListener listener = null; 296 297 public void setListener(ClassValidatorListener listener) { 298 this.listener = listener; 299 } 300 301 public void validate(Class cls) { 302 Class base = cls; 303 while (base != null) { 304 Method mtds[] = base.getDeclaredMethods(); 305 for (int ii = 0; ii < mtds.length; ii++) { 306 Method mtd = mtds[ii]; 307 String name = mtd.getName(); 308 if (name.equals("suite")) { 309 validateSuiteMethod(mtd); 310 } else if (name.equals("setUp")) { 311 validateSetUpMethod(mtd); 312 } else if (name.equals("tearDown")) { 313 validateTearDownMethod(mtd); 314 } else if (name.startsWith("test")) { 315 validateTestMethod(mtd); 316 } else { 317 validateOtherMethod(mtd); 318 } 319 } 320 base = base.getSuperclass(); 321 } 322 } 323 324 336 protected void validateSuiteMethod(Method method) { 337 int modifier = method.getModifiers(); 338 if (!Modifier.isPublic(modifier)) { 339 fireError(method.getDeclaringClass().getName() + ": method 'suite' must be public"); 340 } 341 if (!Modifier.isStatic(modifier)) { 342 fireError(method.getDeclaringClass().getName() + ": method 'suite' must be static"); 343 } 344 if (method.getReturnType() != Test.class) { 345 fireError(method.getDeclaringClass().getName() + ": method 'suite' must return Test"); 346 } 347 if (method.getParameterTypes().length != 0) { 348 fireError(method.getDeclaringClass().getName() + ": method 'suite' must have no argument"); 349 } 350 } 351 352 361 protected void validateSetUpMethod(Method method) { 362 if (method.getParameterTypes().length != 0) { 363 fireError(method.getDeclaringClass().getName() + ".setUp: method must have no argument"); 364 } 365 } 366 367 376 protected void validateTearDownMethod(Method method) { 377 if (method.getParameterTypes().length != 0) { 378 fireError(method.getDeclaringClass().getName() + ".tearDown: method must have no argument"); 379 } 380 } 381 382 392 protected void validateTestMethod(Method method) { 393 if (method.getParameterTypes().length != 0) { 394 fireError(method.getDeclaringClass().getName() + ": test method must have no argument"); 395 } 396 if (!Modifier.isPublic(method.getModifiers())) { 397 fireError(method.getDeclaringClass().getName() + ": test method must be public"); 398 } 399 } 400 401 405 protected void validateOtherMethod(Method method) { 406 String name = method.getName(); 407 if (name.toLowerCase().equals("setup")) { 408 fireWarning(method.getDeclaringClass().getName() + ": method potentially misspelled <" + name + ">"); 409 } else if (name.toLowerCase().equals("teardown")) { 410 fireWarning(method.getDeclaringClass().getName() + ": method potentially misspelled <" + name + ">"); 411 } else if (name.toLowerCase().equals("suite")) { 412 fireWarning(method.getDeclaringClass().getName() + ": method potentially misspelled <" + name + ">"); 413 } else if (name.indexOf("test") > 0) { 414 fireInfo(method.getDeclaringClass().getName() + ": method seems to be a test <" + name + ">"); 415 } 416 } 417 418 private void fireInfo(String message) { 419 if (this.listener != null) { 420 this.listener.info(message); 421 } 422 } 423 424 private void fireWarning(String message) { 425 if (this.listener != null) { 426 this.listener.warning(message); 427 } 428 } 429 430 private void fireError(String message) { 431 if (this.listener != null) { 432 this.listener.error(message); 433 } 434 } 435 436 } 437 438 } 439 | Popular Tags |