KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > UnitTestMain


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.*;
24 import java.util.*;
25
26 /**
27  * @sharpen.ignore
28  */

29 public class UnitTestMain {
30     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
31         new UnitTestMain().runTests(args);
32     }
33
34     public final void runTests(String JavaDoc[] args) throws ClassNotFoundException JavaDoc,
35             InstantiationException JavaDoc, IllegalAccessException JavaDoc {
36         TestSuite suite = build(args);
37         TestRunner runner=new TestRunner(suite);
38         runner.run();
39     }
40     
41     protected TestSuiteBuilder builder(Class JavaDoc[] clazzes) {
42         return new ReflectionTestSuiteBuilder(clazzes);
43     }
44
45     private TestSuite build(String JavaDoc[] args)
46             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc,
47             IllegalAccessException JavaDoc {
48         List plainTestMethods=new ArrayList();
49         List plainTestClasses=new ArrayList();
50         for(int idx=0;idx<args.length;idx++) {
51             String JavaDoc testIdentifier = args[idx];
52             int methodSeparatorIndex = testIdentifier.indexOf('#');
53             if(methodSeparatorIndex>0) {
54                 String JavaDoc className=testIdentifier.substring(0,methodSeparatorIndex);
55                 String JavaDoc methodName=testIdentifier.substring(methodSeparatorIndex+1);
56                 TestMethod testMethod = testMethod(className, methodName);
57                 plainTestMethods.add(testMethod);
58                 continue;
59             }
60             Class JavaDoc curClazz=Class.forName(testIdentifier);
61             if(TestCase.class.isAssignableFrom(curClazz)) {
62                 plainTestClasses.add(curClazz);
63             }
64         }
65         Class JavaDoc[] plainTestClassesArray = (Class JavaDoc[])plainTestClasses.toArray(new Class JavaDoc[plainTestClasses.size()]);
66         TestSuiteBuilder classBuilder=builder(plainTestClassesArray);
67         Test[] plainTestMethodArray=(Test[])plainTestMethods.toArray(new Test[plainTestMethods.size()]);
68         TestSuite methodSuite=new TestSuite(plainTestMethodArray);
69         return new TestSuite(new Test[]{classBuilder.build(),methodSuite});
70     }
71
72     private TestMethod testMethod(String JavaDoc className, String JavaDoc methodName)
73             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc,
74             IllegalAccessException JavaDoc {
75         TestCase test = createTestInstance(className);
76         Method method=null;
77         Method[] methods = test.getClass().getMethods();
78         for (int methodIdx = 0; methodIdx < methods.length; methodIdx++) {
79             Method curMethod = methods[methodIdx];
80             if(curMethod.getName().equals(methodName)) {
81                 method=curMethod;
82             }
83         }
84         if(method==null) {
85             return null;
86         }
87         return new TestMethod(test,method);
88     }
89
90     protected TestCase createTestInstance(String JavaDoc className) throws ClassNotFoundException JavaDoc,
91             InstantiationException JavaDoc, IllegalAccessException JavaDoc {
92         Class JavaDoc clazz=Class.forName(className);
93         TestCase test=(TestCase)clazz.newInstance();
94         return test;
95     }
96 }
97
Popular Tags