1 4 5 package org.objectweb.util.monolog; 6 7 import junit.framework.TestCase; 8 9 import java.lang.reflect.Method ; 10 11 15 public class TestSuite extends junit.framework.TestSuite { 16 17 public TestSuite(Class c, String [] setters, Object [] params) 18 throws Exception { 19 Class [][] paramtypes = new Class [params.length][]; 21 for (int i = 0; i < params.length; i++) { 22 paramtypes[i] = new Class [1]; 23 if (params != null) { 24 paramtypes[i][0] = params[i].getClass(); 25 } 26 else { 27 paramtypes[i][0] = null; 28 } 29 } 30 31 Method [] ms = c.getMethods(); 32 for (int i = 0; i < ms.length; i++) { 33 if (ms[i].getParameterTypes().length == 0 34 && ms[i].getName().startsWith("test")) { 35 addTest( 36 getTest(ms[i].getName(), c, setters, params, paramtypes)); 37 } 38 } 39 } 40 41 private TestCase getTest(String name, Class c, String [] setters, 42 Object [] params, 43 Class [][] paramtypes) throws Exception { 44 TestCase tc = (TestCase) c.newInstance();; 45 tc.setName(name); 46 for (int param = 0; param < params.length; param++) { 47 Method m = null; 48 debug("Search method: " + setters[param] + "(" + (paramtypes[param]==null ? null : paramtypes[param][0]) + ")"); 49 if (paramtypes[param]==null) { 50 m = getFirstMethod(c, setters[param]); 51 } 52 else { 53 m = c.getMethod(setters[param], paramtypes[param]); 54 } 55 debug("found method: " + m); 56 if (m==null) { 57 throw new Exception ("Method " + setters[param] + " not found"); 58 } 59 Object [] p = {params[param]}; 60 m.invoke(tc, p); 61 } 62 return tc; 63 } 64 65 private Method getFirstMethod(Class c, String mn) { 66 Method [] ms = c.getMethods(); 67 int i; 68 for(i=0; i<ms.length && ms[i].getName().equals(mn); i++); 69 return (i<ms.length ? ms[i] : null); 70 } 71 72 protected void debug(String m) { 73 } 75 } 76 | Popular Tags |