KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > TestSuite


1 /**
2  * Copyright (C) 2002
3  */

4
5 package org.objectweb.util.monolog;
6
7 import junit.framework.TestCase;
8
9 import java.lang.reflect.Method JavaDoc;
10
11 /**
12  *
13  * @author Sebastien Chassande-Barrioz
14  */

15 public class TestSuite extends junit.framework.TestSuite {
16
17     public TestSuite(Class JavaDoc c, String JavaDoc[] setters, Object JavaDoc[] params)
18         throws Exception JavaDoc {
19         //super();
20
Class JavaDoc[][] paramtypes = new Class JavaDoc[params.length][];
21         for (int i = 0; i < params.length; i++) {
22             paramtypes[i] = new Class JavaDoc[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 JavaDoc[] 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 JavaDoc name, Class JavaDoc c, String JavaDoc[] setters,
42                              Object JavaDoc[] params,
43                              Class JavaDoc[][] paramtypes) throws Exception JavaDoc {
44         TestCase tc = (TestCase) c.newInstance();;
45         tc.setName(name);
46         for (int param = 0; param < params.length; param++) {
47             Method JavaDoc 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 JavaDoc("Method " + setters[param] + " not found");
58             }
59             Object JavaDoc[] p = {params[param]};
60             m.invoke(tc, p);
61         }
62         return tc;
63     }
64
65     private Method JavaDoc getFirstMethod(Class JavaDoc c, String JavaDoc mn) {
66         Method JavaDoc[] 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 JavaDoc m) {
73         //System.out.println(m);
74
}
75 }
76
Popular Tags