KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > util > SuiteBuilder


1 package com.mockobjects.util;
2
3 import junit.framework.*;
4 import java.lang.reflect.*;
5
6 /**
7  * Singleton to fill in a JUnit Test composite for use in a suite method.
8  */

9
10 public class SuiteBuilder {
11
12     public static TestSuite buildTest(Class JavaDoc allTestsClass) {
13         return buildTest(allTestsClass, new ErrorLogger());
14     }
15
16     public static TestSuite buildTest(
17         Class JavaDoc 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 JavaDoc name = m.getName();
25
26             if (isAddMethod(m)) {
27                 try {
28                     Object JavaDoc[] args = new Object JavaDoc[] { result };
29                     m.invoke(allTestsClass, args);
30                 } catch (Exception JavaDoc 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 JavaDoc name = m.getName();
41         Class JavaDoc[] parameters = m.getParameterTypes();
42         Class JavaDoc 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