1 package com.mockobjects.util; 2 3 import junit.framework.*; 4 import java.lang.reflect.*; 5 6 9 10 public class SuiteBuilder { 11 12 public static TestSuite buildTest(Class allTestsClass) { 13 return buildTest(allTestsClass, new ErrorLogger()); 14 } 15 16 public static TestSuite buildTest( 17 Class allTestsClass, 18 ErrorLogger logger) { 19 TestSuite result = new TestSuite(); 20 21 Method[] methods = allTestsClass.getDeclaredMethods(); 22 for (int i = 0; i < methods.length; i++) { 23 Method m = methods[i]; 24 String name = m.getName(); 25 26 if (isAddMethod(m)) { 27 try { 28 Object [] args = new Object [] { result }; 29 m.invoke(allTestsClass, args); 30 } catch (Exception ex) { 31 logger.error("Error creating Test from " + name, ex); 32 } 33 } 34 } 35 36 return result; 37 } 38 39 public static boolean isAddMethod(Method m) { 40 String name = m.getName(); 41 Class [] parameters = m.getParameterTypes(); 42 Class returnType = m.getReturnType(); 43 return parameters.length == 1 44 && name.startsWith("add") 45 && returnType.equals(Void.TYPE) 46 && Modifier.isPublic(m.getModifiers()) 47 && Modifier.isStatic(m.getModifiers()); 48 } 49 } 50 | Popular Tags |