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 com.db4o.test;
022:
023: import java.lang.reflect.*;
024: import java.util.*;
025:
026: import com.db4o.*;
027: import com.db4o.config.*;
028:
029: /**
030: * This is the main db4o regression test.
031: *
032: * The parameters of the testing environment and all registered test
033: * cases can be found in AllTestsConfAll.java.
034: *
035: * Derive this class from AllTestsConfSingle if you only want to run
036: * single test cases and enter the test case that you want to run in
037: * the AllTestsConfSingle#TESTS[] array.
038: */
039: public class AllTests extends AllTestsConfAll implements Runnable {
040:
041: public static void main(String[] args) {
042: if (args != null && args.length > 0 && args[0].startsWith("-")) {
043: boolean solo = false;
044: boolean cs = false;
045: if ("-solo".equals(args[0]) || "-full".equals(args[0])) {
046: solo = true;
047: }
048: if ("-cs".equals(args[0]) || "-full".equals(args[0])) {
049: cs = true;
050: }
051: AllTests.run(solo, cs, testCasesFromArgs(args, 1));
052: return;
053: }
054: if (args != null && args.length == 1
055: && args[0].equals("-withExceptions")) {
056: new AllTests(new String[] {}).runWithException();
057: return;
058: }
059: new AllTests(args).run();
060: }
061:
062: public AllTests(String[] testcasenames) {
063:
064: Configuration conf = Db4o.configure();
065: conf.messageLevel(-1);
066:
067: // conf.io(new MemoryIoAdapter());
068:
069: // conf.generateUUIDs(Integer.MAX_VALUE);
070: // conf.generateVersionNumbers(Integer.MAX_VALUE);
071:
072: // conf.blockSize(8);
073: // conf.automaticShutDown(false);
074: // conf.lockDatabaseFile(false);
075: // conf.singleThreadedClient(true);
076: // conf.automaticShutDown(false);
077: // conf.lockDatabaseFile(false);
078: // conf.weakReferences(false);
079: // conf.callbacks(false);
080: // conf.detectSchemaChanges(false);
081: // conf.testConstructors(false);
082: // conf.discardFreeSpace(Integer.MAX_VALUE);
083: // conf.password("hudhoododod");
084: // conf.encrypt(true);
085: // conf.singleThreadedClient(true);
086: //
087:
088: if (testcasenames != null && testcasenames.length > 0) {
089: _testCases = testCasesFromArgs(testcasenames);
090: } else {
091: testCasesFromTestSuites();
092: }
093:
094: Test.currentRunner = this ;
095: }
096:
097: public void run() {
098: printOutResult(runResult());
099: }
100:
101: public void runWithException() {
102: TestResult result = runResult();
103: printOutResult(result);
104: if (result.numFailures() > 0) {
105: throw new RuntimeException("db4o regression test failure: "
106: + result);
107: }
108: }
109:
110: private void printOutResult(TestResult result) {
111: System.out.println("\n\nAllTests completed.\nAssertions: "
112: + result.numAssertions() + "\nTime: "
113: + result.timeTaken() + "ms");
114: if (result.numFailures() == 0) {
115: System.out.println("No errors detected.\n");
116: } else {
117: System.out.println("" + result.numFailures()
118: + " ERRORS DETECTED !!!.\n");
119: }
120: }
121:
122: public TestResult runResult() {
123: logConfiguration();
124:
125: Test.beginTesting();
126:
127: long time = System.currentTimeMillis();
128:
129: if (DELETE_FILE) {
130: Test.delete();
131: }
132:
133: configure();
134:
135: for (Test.run = 1; Test.run <= RUNS; Test.run++) {
136: System.out.println("\ncom.db4o.test.AllTests run "
137: + Test.run + " from " + RUNS + "\n");
138: if (SOLO) {
139: Test.runServer = false;
140: Test.clientServer = false;
141: runTests();
142: }
143: if (CLIENT_SERVER) {
144: Test.runServer = !REMOTE_SERVER;
145: Test.clientServer = true;
146: runTests();
147: }
148: Test.end();
149: }
150: time = System.currentTimeMillis() - time;
151: TestResult result = new TestResult(Test.assertionCount,
152: Test.errorCount, time);
153: return result;
154: }
155:
156: protected void configure() {
157: for (int i = 0; i < _testCases.length; i++) {
158: Object toTest = newInstance(_testCases[i]);
159: runMethod(toTest, "configure");
160: }
161: }
162:
163: private void runTests() {
164: String cs = Test.clientServer ? "C/S" : "SOLO";
165: for (int i = 0; i < _testCases.length; i++) {
166: System.out.println(cs + " testing "
167: + _testCases[i].getName());
168: Object toTest = newInstance(_testCases[i]);
169: Test.open();
170: if (!runStoreOne(toTest)) {
171: runMethod(toTest, "store");
172: }
173: Test.commit();
174: Test.close();
175: Test.open();
176: toTest = newInstance(_testCases[i]);
177: runTestOne(toTest);
178: toTest = newInstance(_testCases[i]);
179: Method[] methods = _testCases[i].getDeclaredMethods();
180: for (int j = 0; j < methods.length; j++) {
181: Method method = methods[j];
182: String methodName = method.getName();
183: if (!methodName.equals("testOne")) {
184: if (method.getName().indexOf("test") == 0) {
185: try {
186: method.invoke(toTest, (Object[]) null);
187: } catch (Exception e) {
188: Test.errorCount++;
189: e.printStackTrace();
190: }
191: }
192: }
193: }
194: Test.close();
195: }
196: }
197:
198: private Object newInstance(Class clazz) {
199: try {
200: return clazz.newInstance();
201: } catch (InstantiationException e) {
202: } catch (IllegalAccessException e) {
203: }
204: System.out.println("Instantiation failed. Class:"
205: + clazz.getName());
206: System.out
207: .println("The class needs a #newInstance() constructor for the test framework.");
208: new Exception().printStackTrace();
209: return null;
210: }
211:
212: private void runMethod(Object onObject, String methodName) {
213: try {
214: Method method = onObject.getClass().getDeclaredMethod(
215: methodName, (Class[]) null);
216: if (method != null) {
217: try {
218: method.invoke(onObject, (Object[]) null);
219: } catch (Exception e1) {
220: e1.printStackTrace();
221: }
222: }
223: } catch (Exception e) {
224: }
225: }
226:
227: private boolean runStoreOne(Object onObject) {
228: try {
229: Method method = onObject.getClass().getDeclaredMethod(
230: "storeOne", (Class[]) null);
231: if (method != null) {
232: try {
233: Test.deleteAllInstances(onObject);
234: method.invoke(onObject, (Object[]) null);
235: Test.store(onObject);
236: return true;
237: } catch (Exception e2) {
238: e2.printStackTrace();
239: }
240: }
241: } catch (Exception e) {
242: }
243: return false;
244: }
245:
246: private boolean runTestOne(Object onObject) {
247: try {
248: Method method = onObject.getClass().getDeclaredMethod(
249: "testOne", (Class[]) null);
250: if (method != null) {
251: try {
252: onObject = Test.getOne(onObject);
253: method.invoke(onObject, (Object[]) null);
254: return true;
255: } catch (Exception e2) {
256: e2.printStackTrace();
257: }
258: }
259: } catch (Exception e) {
260: }
261: return false;
262: }
263:
264: public static void run(Class clazz) {
265: run(new Class[] { clazz });
266: }
267:
268: public static void run(TestSuite suite) {
269: run(suite.tests());
270: }
271:
272: public static void run(Class[] classes) {
273: run(true, true, classes);
274: }
275:
276: public static void runSolo(Class clazz) {
277: runSolo(new Class[] { clazz });
278: }
279:
280: public static void runSolo(TestSuite suite) {
281: runSolo(suite.tests());
282: }
283:
284: public static void runSolo(Class[] classes) {
285: run(true, false, classes);
286: }
287:
288: public static void runClientServer(Class clazz) {
289: runClientServer(new Class[] { clazz });
290: }
291:
292: public static void runClientServer(TestSuite suite) {
293: runClientServer(suite.tests());
294: }
295:
296: public static void runClientServer(Class[] classes) {
297: run(false, true, classes);
298: }
299:
300: public static void run(boolean solo, boolean clientServer,
301: Class[] classes) {
302: AllTests allTests = new AllTests();
303: allTests._testCases = classes;
304: allTests.SOLO = solo;
305: allTests.CLIENT_SERVER = clientServer;
306: allTests.run();
307: }
308:
309: protected void logConfiguration() {
310: System.out.println("Running " + getClass().getName()
311: + " against\n" + Db4o.version() + "\n");
312: System.out.println("Using " + TEST_CONFIGURATION + ".\n");
313: System.out.println("SERVER_HOSTNAME: " + SERVER_HOSTNAME);
314: System.out.println("SERVER_PORT: " + SERVER_PORT);
315: System.out.println("FILE_SERVER: " + FILE_SERVER);
316: if (MEMORY_FILE) {
317: System.out.println("MEMORY_FILE !!!");
318: } else {
319: System.out.println("FILE_SOLO: " + FILE_SOLO);
320: }
321: System.out.println("DELETE_FILE: " + DELETE_FILE);
322: System.out.println("BLOB_PATH: " + BLOB_PATH + "\n");
323:
324: }
325:
326: public AllTests() {
327: this (null);
328: }
329:
330: private void testCasesFromTestSuites() {
331: _testCases = new Class[0];
332: _testSuites = new Vector();
333:
334: addTestSuites(this );
335:
336: Enumeration e = _testSuites.elements();
337: while (e.hasMoreElements()) {
338: TestSuite suite = (TestSuite) e.nextElement();
339: _testCases = concat(_testCases, suite.tests());
340: }
341: }
342:
343: private Class[] concat(Class[] a, Class[] b) {
344: Class[] result = new Class[a.length + b.length];
345: System.arraycopy(a, 0, result, 0, a.length);
346: System.arraycopy(b, 0, result, a.length, b.length);
347: return result;
348: }
349:
350: private static Class[] testCasesFromArgs(String[] testcasenames) {
351: return testCasesFromArgs(testcasenames, 0);
352: }
353:
354: private static Class[] testCasesFromArgs(String[] testcasenames,
355: int offset) {
356: Class[] testCases = new Class[testcasenames.length - offset];
357: for (int testidx = offset; testidx < testcasenames.length; testidx++) {
358: try {
359: testCases[testidx - offset] = Class
360: .forName(testcasenames[testidx]);
361: } catch (ClassNotFoundException e) {
362: System.err.println("Test case class not found: "
363: + testcasenames[testidx]);
364: e.printStackTrace();
365: System.exit(0);
366: }
367: }
368: return testCases;
369: }
370:
371: private Class[] _testCases;
372:
373: }
|