KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > ReflectionTestSuiteBuilder


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package db4ounit;
22
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 public class ReflectionTestSuiteBuilder implements TestSuiteBuilder {
27     
28     private Class JavaDoc[] _classes;
29     
30     public ReflectionTestSuiteBuilder(Class JavaDoc clazz) {
31         if (null == clazz) throw new IllegalArgumentException JavaDoc("clazz");
32         _classes = new Class JavaDoc[] { clazz };
33     }
34     
35     public ReflectionTestSuiteBuilder(Class JavaDoc[] classes) {
36         if (null == classes) throw new IllegalArgumentException JavaDoc("classes");
37         _classes = classes;
38     }
39     
40     public TestSuite build() {
41         return (1 == _classes.length)
42             ? fromClass(_classes[0])
43             : fromClasses(_classes);
44     }
45     
46     protected TestSuite fromClasses(Class JavaDoc[] classes) {
47         Vector JavaDoc suites = new Vector JavaDoc(classes.length);
48         for (int i = 0; i < classes.length; i++) {
49             TestSuite suite = fromClass(classes[i]);
50             if (suite.getTests().length>0) {
51                 suites.addElement(suite);
52             }
53         }
54         return new TestSuite(toTestArray(suites));
55     }
56     
57     protected TestSuite fromClass(Class JavaDoc clazz) {
58         if(!isApplicable(clazz)) {
59             TestPlatform.emitWarning("DISABLED: " + clazz.getName());
60             return new TestSuite(new Test[0]);
61         }
62         if(TestSuiteBuilder.class.isAssignableFrom(clazz)) {
63             return ((TestSuiteBuilder)newInstance(clazz)).build();
64         }
65         if (Test.class.isAssignableFrom(clazz)) {
66             return new TestSuite(clazz.getName(), new Test[] { (Test)newInstance(clazz) });
67         }
68         if (!(TestCase.class.isAssignableFrom(clazz))) {
69             throw new IllegalArgumentException JavaDoc("" + clazz + " is not marked as " + TestCase.class);
70         }
71         return fromMethods(clazz);
72     }
73
74     protected boolean isApplicable(Class JavaDoc clazz) {
75         return clazz != null; // just removing the 'parameter not used' warning
76
}
77     
78     private TestSuite fromMethods(Class JavaDoc clazz) {
79         Vector JavaDoc tests = new Vector JavaDoc();
80         Method JavaDoc[] methods = clazz.getMethods();
81         for (int i = 0; i < methods.length; i++) {
82             Object JavaDoc instance=newInstance(clazz);
83             Method JavaDoc method = methods[i];
84             if (!isTestMethod(method)) {
85                 emitWarningOnIgnoredTestMethod(instance, method);
86                 continue;
87             }
88             tests.addElement(createTest(instance, method));
89         }
90         return new TestSuite(clazz.getName(), toTestArray(tests));
91     }
92     
93     private void emitWarningOnIgnoredTestMethod(Object JavaDoc subject, Method JavaDoc method) {
94         if (!startsWithIgnoreCase(method.getName(), "_test")) {
95             return;
96         }
97         TestPlatform.emitWarning("IGNORED: " + createTest(subject, method).getLabel());
98     }
99
100     protected boolean isTestMethod(Method JavaDoc method) {
101         return hasTestPrefix(method)
102             && TestPlatform.isPublic(method)
103             && !TestPlatform.isStatic(method)
104             && !TestPlatform.hasParameters(method);
105     }
106
107     private boolean hasTestPrefix(Method JavaDoc method) {
108         return startsWithIgnoreCase(method.getName(), "test");
109     }
110
111     protected boolean startsWithIgnoreCase(final String JavaDoc s, final String JavaDoc prefix) {
112         return s.toUpperCase().startsWith(prefix.toUpperCase());
113     }
114     
115     private static Test[] toTestArray(Vector JavaDoc tests) {
116         Test[] array = new Test[tests.size()];
117         tests.copyInto(array);
118         return array;
119     }
120
121     protected Object JavaDoc newInstance(Class JavaDoc clazz) {
122         try {
123             return clazz.newInstance();
124         } catch (Exception JavaDoc e) {
125             throw new TestException(e);
126         }
127     }
128     
129     protected Test createTest(Object JavaDoc instance, Method JavaDoc method) {
130         return new TestMethod(instance, method);
131     }
132 }
133
Popular Tags