KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > AllTests


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 com.db4o.test;
22
23 import java.lang.reflect.*;
24 import java.util.*;
25
26 import com.db4o.*;
27 import com.db4o.config.*;
28
29 /**
30  * This is the main db4o regression test.
31  *
32  * The parameters of the testing environment and all registered test
33  * cases can be found in AllTestsConfAll.java.
34  *
35  * Derive this class from AllTestsConfSingle if you only want to run
36  * single test cases and enter the test case that you want to run in
37  * the AllTestsConfSingle#TESTS[] array.
38  */

39 public class AllTests extends AllTestsConfAll implements Runnable JavaDoc {
40
41     public static void main(String JavaDoc[] args) {
42         if(args!=null&&args.length>0&&args[0].startsWith("-")) {
43             boolean solo=false;
44             boolean cs=false;
45             if("-solo".equals(args[0])||"-full".equals(args[0])) {
46                 solo=true;
47             }
48             if("-cs".equals(args[0])||"-full".equals(args[0])) {
49                 cs=true;
50             }
51             AllTests.run(solo, cs, testCasesFromArgs(args,1));
52             return;
53         }
54         if(args!=null&&args.length==1&&args[0].equals("*")) {
55             new AllTests(new String JavaDoc[]{}).runWithException();
56             return;
57         }
58         new AllTests(args).run();
59     }
60     
61     public AllTests(String JavaDoc[] testcasenames) {
62         
63         Configuration conf = Db4o.configure();
64         conf.messageLevel(-1);
65         
66 // conf.io(new MemoryIoAdapter());
67

68 // conf.generateUUIDs(Integer.MAX_VALUE);
69
// conf.generateVersionNumbers(Integer.MAX_VALUE);
70

71 // conf.blockSize(8);
72
// conf.automaticShutDown(false);
73
// conf.lockDatabaseFile(false);
74
// conf.singleThreadedClient(true);
75
// conf.automaticShutDown(false);
76
// conf.lockDatabaseFile(false);
77
// conf.weakReferences(false);
78
// conf.callbacks(false);
79
// conf.detectSchemaChanges(false);
80
// conf.testConstructors(false);
81
// conf.discardFreeSpace(Integer.MAX_VALUE);
82
// conf.password("hudhoododod");
83
// conf.encrypt(true);
84
// conf.singleThreadedClient(true);
85
//
86

87         
88         if(testcasenames!=null&&testcasenames.length>0) {
89             _testCases=testCasesFromArgs(testcasenames);
90         } else{
91             testCasesFromTestSuites();
92         }
93         
94         Test.currentRunner = this;
95     }
96
97     public void run() {
98         printOutResult(runResult());
99     }
100
101     public void runWithException() {
102         TestResult result = runResult();
103         printOutResult(result);
104         if(result.numFailures()>0) {
105             throw new RuntimeException JavaDoc("db4o regression test failure: "+result);
106         }
107     }
108     
109     private void printOutResult(TestResult result) {
110         System.out.println("\n\nAllTests completed.\nAssertions: "
111             + result.numAssertions() + "\nTime: " + result.timeTaken() + "ms");
112         if (result.numFailures() == 0) {
113             System.out.println("No errors detected.\n");
114         } else {
115             System.out
116                 .println("" + result.numFailures() + " ERRORS DETECTED !!!.\n");
117         }
118     }
119
120     public TestResult runResult() {
121         logConfiguration();
122
123         Test.beginTesting();
124
125         long time = System.currentTimeMillis();
126
127         if (DELETE_FILE) {
128             Test.delete();
129         }
130
131         configure();
132
133         for (Test.run = 1; Test.run <= RUNS; Test.run++) {
134             System.out.println("\ncom.db4o.test.AllTests run " + Test.run
135                 + " from " + RUNS + "\n");
136             if (SOLO) {
137                 Test.runServer = false;
138                 Test.clientServer = false;
139                 runTests();
140             }
141             if (CLIENT_SERVER) {
142                 Test.runServer = !REMOTE_SERVER;
143                 Test.clientServer = true;
144                 runTests();
145             }
146             Test.end();
147         }
148         time = System.currentTimeMillis() - time;
149         TestResult result=new TestResult(Test.assertionCount,Test.errorCount,time);
150         return result;
151     }
152
153     protected void configure() {
154         for (int i = 0; i < _testCases.length; i++) {
155             Object JavaDoc toTest = newInstance(_testCases[i]);
156             runMethod(toTest, "configure");
157         }
158     }
159
160     private void runTests() {
161         String JavaDoc cs = Test.clientServer ? "C/S" : "SOLO";
162         for (int i = 0; i < _testCases.length; i++) {
163             System.out.println(cs + " testing " + _testCases[i].getName());
164             Object JavaDoc toTest = newInstance(_testCases[i]);
165             Test.open();
166             if (!runStoreOne(toTest)) {
167                 runMethod(toTest, "store");
168             }
169             Test.commit();
170             Test.close();
171             Test.open();
172             toTest = newInstance(_testCases[i]);
173             runTestOne(toTest);
174             toTest = newInstance(_testCases[i]);
175             Method[] methods = _testCases[i].getDeclaredMethods();
176             for (int j = 0; j < methods.length; j++) {
177                 Method method = methods[j];
178                 String JavaDoc methodName = method.getName();
179                 if (!methodName.equals("testOne")) {
180                     if (method.getName().indexOf("test") == 0) {
181                         try {
182                             method.invoke(toTest, (Object JavaDoc[])null);
183                         } catch (Exception JavaDoc e) {
184                             Test.errorCount++;
185                             e.printStackTrace();
186                         }
187                     }
188                 }
189             }
190             Test.close();
191         }
192     }
193
194     private Object JavaDoc newInstance(Class JavaDoc clazz) {
195         try {
196             return clazz.newInstance();
197         } catch (InstantiationException JavaDoc e) {
198         } catch (IllegalAccessException JavaDoc e) {
199         }
200         System.out.println("Instantiation failed. Class:" + clazz.getName());
201         System.out.println("The class needs a #newInstance() constructor for the test framework.");
202         new Exception JavaDoc().printStackTrace();
203         return null;
204     }
205
206     private void runMethod(Object JavaDoc onObject, String JavaDoc methodName) {
207         try {
208             Method method = onObject.getClass().getDeclaredMethod(methodName,
209                 (Class JavaDoc[])null);
210             if (method != null) {
211                 try {
212                     method.invoke(onObject, (Object JavaDoc[])null);
213                 } catch (Exception JavaDoc e1) {
214                     e1.printStackTrace();
215                 }
216             }
217         } catch (Exception JavaDoc e) {
218         }
219     }
220
221     private boolean runStoreOne(Object JavaDoc onObject) {
222         try {
223             Method method = onObject.getClass().getDeclaredMethod("storeOne",
224                 (Class JavaDoc[])null);
225             if (method != null) {
226                 try {
227                     Test.deleteAllInstances(onObject);
228                     method.invoke(onObject, (Object JavaDoc[])null);
229                     Test.store(onObject);
230                     return true;
231                 } catch (Exception JavaDoc e2) {
232                     e2.printStackTrace();
233                 }
234             }
235         } catch (Exception JavaDoc e) {
236         }
237         return false;
238     }
239
240     private boolean runTestOne(Object JavaDoc onObject) {
241         try {
242             Method method = onObject.getClass().getDeclaredMethod("testOne",
243                 (Class JavaDoc[])null);
244             if (method != null) {
245                 try {
246                     onObject = Test.getOne(onObject);
247                     method.invoke(onObject, (Object JavaDoc[])null);
248                     return true;
249                 } catch (Exception JavaDoc e2) {
250                     e2.printStackTrace();
251                 }
252             }
253         } catch (Exception JavaDoc e) {
254         }
255         return false;
256     }
257     
258     public static void run(Class JavaDoc clazz){
259         run(new Class JavaDoc[]{clazz});
260     }
261     
262     public static void run(TestSuite suite){
263         run(suite.tests());
264     }
265     
266     public static void run(Class JavaDoc[] classes){
267         run(true, true, classes);
268     }
269     
270     public static void runSolo(Class JavaDoc clazz){
271         runSolo(new Class JavaDoc[]{clazz});
272     }
273     
274     public static void runSolo(TestSuite suite){
275         runSolo(suite.tests());
276     }
277     
278     public static void runSolo(Class JavaDoc[] classes){
279         run(true, false, classes);
280     }
281     
282     public static void runClientServer(Class JavaDoc clazz){
283         runClientServer(new Class JavaDoc[]{clazz});
284     }
285     
286     public static void runClientServer(TestSuite suite){
287         runClientServer(suite.tests());
288     }
289     
290     public static void runClientServer(Class JavaDoc[] classes){
291         run(false, true, classes);
292     }
293     
294     public static void run(boolean solo, boolean clientServer, Class JavaDoc[] classes){
295         AllTests allTests = new AllTests();
296         allTests._testCases=classes;
297         allTests.SOLO = solo;
298         allTests.CLIENT_SERVER = clientServer;
299         allTests.run();
300     }
301
302     protected void logConfiguration() {
303         System.out.println("Running " + getClass().getName() + " against\n"
304             + Db4o.version() + "\n");
305         System.out.println("Using " + TEST_CONFIGURATION
306             + ".\n");
307         System.out.println("SERVER_HOSTNAME: " + SERVER_HOSTNAME);
308         System.out.println("SERVER_PORT: " + SERVER_PORT);
309         System.out.println("FILE_SERVER: " + FILE_SERVER);
310         if(MEMORY_FILE) {
311             System.out.println("MEMORY_FILE !!!");
312         }else {
313             System.out.println("FILE_SOLO: " + FILE_SOLO);
314         }
315         System.out.println("DELETE_FILE: " + DELETE_FILE);
316         System.out.println("BLOB_PATH: " + BLOB_PATH + "\n");
317
318     }
319
320     public AllTests() {
321         this(null);
322     }
323     
324     private void testCasesFromTestSuites() {
325         _testCases=new Class JavaDoc[0];
326         _testSuites = new Vector();
327         
328         addTestSuites(this);
329         
330         Enumeration e = _testSuites.elements();
331         while (e.hasMoreElements()) {
332             TestSuite suite = (TestSuite)e.nextElement();
333             _testCases = concat(_testCases, suite.tests());
334         }
335     }
336     
337     private Class JavaDoc[] concat(Class JavaDoc[] a, Class JavaDoc[] b) {
338         Class JavaDoc[] result = new Class JavaDoc[a.length + b.length];
339         System.arraycopy(a,0, result,0 , a.length);
340         System.arraycopy(b,0, result,a.length, b.length);
341         return result;
342     }
343
344     private static Class JavaDoc[] testCasesFromArgs(String JavaDoc[] testcasenames) {
345         return testCasesFromArgs(testcasenames,0);
346     }
347
348     private static Class JavaDoc[] testCasesFromArgs(String JavaDoc[] testcasenames,int offset) {
349         Class JavaDoc[] testCases=new Class JavaDoc[testcasenames.length-offset];
350         for (int testidx = offset; testidx < testcasenames.length; testidx++) {
351             try {
352                 testCases[testidx-offset]=Class.forName(testcasenames[testidx]);
353             } catch (ClassNotFoundException JavaDoc e) {
354                 System.err.println("Test case class not found: "+testcasenames[testidx]);
355                 e.printStackTrace();
356                 System.exit(0);
357             }
358         }
359         return testCases;
360     }
361
362     
363    private Class JavaDoc[] _testCases;
364
365 }
Popular Tags