001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit;
022:
023: import java.lang.reflect.*;
024: import java.util.*;
025:
026: /**
027: * @sharpen.ignore
028: */
029: public class UnitTestMain {
030: public static void main(String[] args) throws Exception {
031: new UnitTestMain().runTests(args);
032: }
033:
034: public final void runTests(String[] args)
035: throws ClassNotFoundException, InstantiationException,
036: IllegalAccessException {
037: TestSuite suite = build(args);
038: TestRunner runner = new TestRunner(suite, false);
039: runner.run();
040: }
041:
042: protected TestSuiteBuilder builder(Class[] clazzes) {
043: return new ReflectionTestSuiteBuilder(clazzes);
044: }
045:
046: private TestSuite build(String[] args)
047: throws ClassNotFoundException, InstantiationException,
048: IllegalAccessException {
049: Vector plainTestMethods = new Vector();
050: Vector plainTestClasses = new Vector();
051: for (int idx = 0; idx < args.length; idx++) {
052: String testIdentifier = args[idx];
053: int methodSeparatorIndex = testIdentifier.indexOf('#');
054: if (methodSeparatorIndex > 0) {
055: String className = testIdentifier.substring(0,
056: methodSeparatorIndex);
057: String methodName = testIdentifier
058: .substring(methodSeparatorIndex + 1);
059: TestMethod testMethod = testMethod(className,
060: methodName);
061: plainTestMethods.addElement(testMethod);
062: continue;
063: }
064: Class curClazz = Class.forName(testIdentifier);
065: if (TestCase.class.isAssignableFrom(curClazz)) {
066: plainTestClasses.addElement(curClazz);
067: }
068: }
069: Class[] plainTestClassesArray = new Class[plainTestClasses
070: .size()];
071: vectorToArray(plainTestClasses, plainTestClassesArray);
072: TestSuiteBuilder classBuilder = builder(plainTestClassesArray);
073: Test[] plainTestMethodArray = new Test[plainTestMethods.size()];
074: vectorToArray(plainTestMethods, plainTestMethodArray);
075: TestSuite methodSuite = new TestSuite(plainTestMethodArray);
076: return new TestSuite(new Test[] { classBuilder.build(),
077: methodSuite });
078: }
079:
080: private void vectorToArray(Vector vector, Object[] array) {
081: int i = 0;
082: Enumeration enumer = vector.elements();
083: while (enumer.hasMoreElements()) {
084: array[i++] = enumer.nextElement();
085: }
086: }
087:
088: private TestMethod testMethod(String className, String methodName)
089: throws ClassNotFoundException, InstantiationException,
090: IllegalAccessException {
091: TestCase test = createTestInstance(className);
092: Method method = null;
093: Method[] methods = test.getClass().getMethods();
094: for (int methodIdx = 0; methodIdx < methods.length; methodIdx++) {
095: Method curMethod = methods[methodIdx];
096: if (curMethod.getName().equals(methodName)) {
097: method = curMethod;
098: }
099: }
100: if (method == null) {
101: return null;
102: }
103: return new TestMethod(test, method);
104: }
105:
106: protected TestCase createTestInstance(String className)
107: throws ClassNotFoundException, InstantiationException,
108: IllegalAccessException {
109: Class clazz = Class.forName(className);
110: TestCase test = (TestCase) clazz.newInstance();
111: return test;
112: }
113: }
|