01: /**
02: * Copyright (C) 2002
03: */package org.objectweb.util.monolog;
04:
05: import junit.framework.TestCase;
06:
07: import java.lang.reflect.Method;
08:
09: /**
10: *
11: * @author Sebastien Chassande-Barrioz
12: */
13: public class TestSuite extends junit.framework.TestSuite {
14:
15: public TestSuite(Class c, String[] setters, Object[] params)
16: throws Exception {
17: //super();
18: Class[][] paramtypes = new Class[params.length][];
19: for (int i = 0; i < params.length; i++) {
20: paramtypes[i] = new Class[1];
21: if (params != null) {
22: paramtypes[i][0] = params[i].getClass();
23: } else {
24: paramtypes[i][0] = null;
25: }
26: }
27:
28: Method[] ms = c.getMethods();
29: for (int i = 0; i < ms.length; i++) {
30: if (ms[i].getParameterTypes().length == 0
31: && ms[i].getName().startsWith("test")) {
32: addTest(getTest(ms[i].getName(), c, setters, params,
33: paramtypes));
34: }
35: }
36: }
37:
38: private TestCase getTest(String name, Class c, String[] setters,
39: Object[] params, Class[][] paramtypes) throws Exception {
40: TestCase tc = (TestCase) c.newInstance();
41: ;
42: tc.setName(name);
43: for (int param = 0; param < params.length; param++) {
44: Method m = null;
45: debug("Search method: "
46: + setters[param]
47: + "("
48: + (paramtypes[param] == null ? null
49: : paramtypes[param][0]) + ")");
50: if (paramtypes[param] == null) {
51: m = getFirstMethod(c, setters[param]);
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]
58: + " not found");
59: }
60: Object[] p = { params[param] };
61: m.invoke(tc, p);
62: }
63: return tc;
64: }
65:
66: private Method getFirstMethod(Class c, String mn) {
67: Method[] ms = c.getMethods();
68: int i;
69: for (i = 0; i < ms.length && ms[i].getName().equals(mn); i++)
70: ;
71: return (i < ms.length ? ms[i] : null);
72: }
73:
74: protected void debug(String m) {
75: //System.out.println(m);
76: }
77: }
|