1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.crawler.selftest;
24
25 import java.lang.reflect.Method;
26 import java.lang.reflect.Modifier;
27 import java.util.Vector;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 /***
33 * Variant TestSuite that can build tests including methods with an alternate
34 * prefix (other than 'test'). Copies code from TestSuite because necessary
35 * methods to change are private rather than protected.
36 *
37 * @author gojomo
38 * @version $Id: MaxLinkHopsSelfTest.java 4667 2006-09-26 20:38:48 +0000 (Tue, 26 Sep 2006) paul_jack $
39 */
40 public class AltTestSuite extends TestSuite {
41 /*** a method prefix other than 'test' that is also recognized as tests */
42 String altPrefix;
43
44 /***
45 * Constructs a TestSuite from the given class. Copied from superclass so
46 * that local alternate addTestMethod() will be visible, which in turn uses
47 * an isTestMethod() that accepts methods with the altPrefix in addition
48 * to 'test'.
49 * @param theClass Class from which to build suite
50 * @param prefix alternate method prefix to also find test methods
51 */
52 public AltTestSuite(final Class theClass, String prefix) {
53 this.altPrefix = prefix;
54 setName(theClass.getName());
55 try {
56 getTestConstructor(theClass);
57 } catch (NoSuchMethodException e) {
58 addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"));
59 return;
60 }
61
62 if (!Modifier.isPublic(theClass.getModifiers())) {
63 addTest(warning("Class "+theClass.getName()+" is not public"));
64 return;
65 }
66
67 Class superClass= theClass;
68 Vector names= new Vector();
69 while (Test.class.isAssignableFrom(superClass)) {
70 Method[] methods= superClass.getDeclaredMethods();
71 for (int i= 0; i < methods.length; i++) {
72 addTestMethod(methods[i], names, theClass);
73 }
74 superClass= superClass.getSuperclass();
75 }
76 if (testCount() == 0)
77 addTest(warning("No tests found in "+theClass.getName()));
78 }
79
80
81 private void addTestMethod(Method m, Vector names, Class theClass) {
82 String name= m.getName();
83 if (names.contains(name))
84 return;
85 if (! isPublicTestMethod(m)) {
86 if (isTestMethod(m))
87 addTest(warning("Test method isn't public: "+m.getName()));
88 return;
89 }
90 names.addElement(name);
91 addTest(createTest(theClass, name));
92 }
93
94
95 private boolean isPublicTestMethod(Method m) {
96 return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
97 }
98
99
100 private boolean isTestMethod(Method m) {
101 String name= m.getName();
102 Class[] parameters= m.getParameterTypes();
103 Class returnType= m.getReturnType();
104 return parameters.length == 0
105 && (name.startsWith("test")||name.startsWith(altPrefix))
106 && returnType.equals(Void.TYPE);
107 }
108
109
110 public AltTestSuite() {
111 super();
112 }
113
114 public void testNoop() {
115
116 }
117 }