KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import com.db4o.*;
26 import com.db4o.config.*;
27 import com.db4o.ext.*;
28 import com.db4o.foundation.*;
29 import com.db4o.test.types.*;
30
31 /**
32  * The original old regression test, without any S.O.D.A. functionality.
33  * Tests will be slowly migrated to AllTests.java.
34  */

35 public class Regression {
36
37     /**
38      * uses a small subset of classes
39      * see method testClasses at the end of this file
40      */

41     private static final boolean DEBUG = false;
42
43     /** no comparisons, used to time performance only */
44     private static final boolean PROFILE_ONLY = false;
45
46     /** number of regression runs */
47     private static final int RUNS = 1;
48
49     /** run the default JDK1 test on all classes */
50     private static final boolean DEFAULT = true;
51
52     /** run JDK2 tests */
53     private static final boolean JDK2 = true;
54
55     /** number of thread regression runs */
56     protected static final int THREAD_RUNS = 300;
57
58     /**
59      * runs the thread test
60      * For a real thread test run, set DEFAULT and JDK 2 to false
61      * Increase runs to a higher value
62      */

63     private static final boolean THREADS = false;
64
65     /** the number of RUNS or THREAD_RUNS between commits */
66     private static final int COMMIT_AFTER = 5;
67
68     /** log class name of current test class */
69     private static final boolean LOG_CLASS_NAMES = true;
70
71     public static void main(String JavaDoc[] args) {
72
73         new java.io.File JavaDoc(FILE).delete();
74
75         Db4o.configure().messageLevel(-1);
76         // Db4o.configure().exceptionsOnNotStorable(true);
77

78         // Db4o.licensedTo("tobi@db4o.com");
79
// Db4o.configure().password("Houilo7");
80
// Db4o.configure().encrypt(true);
81

82         new Regression().run();
83     }
84
85     public void run() {
86         if (isJDK2() && JDK2) {
87             Thread.currentThread().setName("JDK2 Regression Test");
88             try {
89                 Regression regression2 =
90                     (Regression) Class.forName("com.db4o.test.test2.Regression2").newInstance();
91                 run1(regression2.testClasses());
92             } catch (Exception JavaDoc e) {
93                 e.printStackTrace();
94             }
95         }
96         if (DEFAULT) {
97             Thread.currentThread().setName("Default Regression Test");
98             run1(testClasses());
99         }
100
101         if (THREADS) {
102             try {
103                 threadsSharp = true;
104                 new Thread JavaDoc(new Thread1(this)).start();
105                 Thread.sleep(100);
106                 // the server socket neads a little time to come up
107
new Thread JavaDoc(new Thread2(this)).start();
108                 Thread.sleep(100);
109                 new Thread JavaDoc(new Thread3(this)).start();
110
111                 // We don't want to run out of main to allow sequential
112
// execution of Ant tasks.
113
do {
114                     Cool.sleepIgnoringInterruption(300);
115                 } while (threadsSharp);
116
117             } catch (Exception JavaDoc e) {
118                 e.printStackTrace();
119             }
120         } else {
121             completed();
122         }
123     }
124
125     public void run1(RTestable[] clazzes) {
126         openedThreads++;
127         long time = System.currentTimeMillis();
128         mainLoop(clazzes);
129         time = System.currentTimeMillis() - time;
130         System.out.println("\n" + Thread.currentThread().getName() + ": " + time + " ms.");
131         returnedThreads++;
132         if (returnedThreads >= openedThreads) {
133             if (errors.length() == 0) {
134                 ObjectContainer con = open();
135                 int objectCount = con.get(null).size();
136                 closeAllButMemoryFile(con);
137                 System.out.println(
138                     PROFILE_ONLY
139                         ? "Profile run completed."
140                         : "Regression Test Passed. " + objectCount + " objects.");
141             } else {
142                 System.out.println("!!! Regression Test Failed. !!!");
143             }
144             System.out.println(Db4o.version());
145             System.out.println(errors);
146             if (threadsSharp) {
147                 if (openDelegate != null) {
148                     openDelegate.completed();
149                 }
150                 threadsSharp = false;
151             }
152         }
153     }
154
155     public void mainLoop(RTestable[] clazzes) {
156         int commitCounter = 0;
157         int run = 0;
158         ObjectContainer con = open();
159         for (int k = 0; k < runs(); k++) {
160             run++;
161             System.out.println(Thread.currentThread().getName() + " Run:" + run);
162             closeFile = false;
163             for (int i = 0; i < 2; i++) {
164                 for (int j = 0; j < clazzes.length; j++) {
165                     if(! clazzes[j].jdk2() || isJDK2()){
166                         con = cycle(con, clazzes[j], run);
167                     }
168                 }
169                 closeFile = true;
170             }
171             commitCounter++;
172             if (commitCounter > COMMIT_AFTER) {
173                 con.commit();
174                 commitCounter = 0;
175             }
176         }
177
178
179         if(closeFile()){
180             close(con);
181             con = open();
182         }
183
184         // check for no duplicates
185
StoredClass[] intClasses = con.ext().storedClasses();
186         String JavaDoc[] noDuplicates = new String JavaDoc[intClasses.length];
187         for (int i = 0; i < intClasses.length; i++) {
188             noDuplicates[i] = intClasses[i].getName();
189         }
190         for (int i = 0; i < noDuplicates.length; i++) {
191             for (int j = i + 1; j < noDuplicates.length; j++) {
192                 if (noDuplicates[i].equals(noDuplicates[j])) {
193                     addError("Duplicate class definition: " + noDuplicates[i]);
194                 }
195             }
196         }
197         closeAllButMemoryFile(con);
198     }
199
200     ObjectContainer cycle(ObjectContainer con, RTestable clazz, int a_run) {
201         if (LOG_CLASS_NAMES) {
202             System.out.println("Testing class: " + clazz.getClass().getName());
203         }
204         if (clazz.jdk2() && (! isJDK2())) {
205             return con;
206         }
207         Object JavaDoc obj = clazz.newInstance();
208         con = reOpen(con);
209
210         // remove all
211
Object JavaDoc get = clazz.newInstance();
212         con.deactivate(get, Integer.MAX_VALUE);
213         ObjectSet set = con.get(get);
214         while (set.hasNext()) {
215             con.delete(set.next());
216         }
217
218         // STEP1 add one object
219
clazz.set(obj, 1);
220         con.set(obj);
221         specificTest(clazz, con, ONE);
222         con = reOpen(con);
223
224         // The check here has proved invaluable multiple times.
225
// Don't delete the uncommented code.
226

227         // set = con.get(null);
228
// System.out.println(set.size());
229
// while(set.hasNext()){
230
// Logger.log(con, set.next());
231
// }
232

233         // check 1, retrieving no members set
234
compare(con, get, clazz, 1, 1);
235
236         // update 1, retrieving all members set
237
clazz.set(get, 1);
238         compare(con, get, clazz, 1, 1);
239         con = reOpen(con);
240
241         // add 4
242
for (int i = 0; i < 4; i++) {
243             obj = clazz.newInstance();
244             clazz.set(obj, 1);
245             con.set(obj);
246         }
247         con = reOpen(con);
248
249         // check 5
250
compare(con, get, clazz, 1, 5);
251         specificTest(clazz, con, FIVE);
252         con = reOpen(con);
253
254         // delete 1
255
set = con.get(get);
256         obj = set.next();
257         con.delete(obj);
258         con = reOpen(con);
259
260         // check 4
261
compare(con, get, clazz, 1, 4);
262         specificTest(clazz, con, DELETED);
263         con = reOpen(con);
264         
265         // update 1
266
set = con.get(get);
267         obj = set.next();
268         clazz.set(obj, 2);
269         con.set(obj);
270         con = reOpen(con);
271         
272         
273 // Defragment test
274
//
275
// if(closeFile()){
276
// con.close();
277
// new Defragment().run(FILE, true);
278
// configure();
279
// con = open();
280
// }
281

282         // check 3
283
compare(con, get, clazz, 1, 3);
284         specificTest(clazz, con, SAME);
285         con = reOpen(con);
286
287         // check 1
288
clazz.set(get, 2);
289         compare(con, get, clazz, 2, 1);
290         specificTest(clazz, con, UPDATED);
291         con = reOpen(con);
292
293         if (clazz.ver3()) {
294
295             // update another 1 with ver3
296
set = con.get(get);
297             obj = set.next();
298             clazz.set(obj, 3);
299             con.set(obj);
300             con = reOpen(con);
301
302             // check 2
303
clazz.set(get, 1);
304             compare(con, get, clazz, 1, 3);
305             specificTest(clazz, con, SAME);
306             con = reOpen(con);
307
308             // check 1
309
clazz.set(get, 3);
310             compare(con, get, clazz, 3, 1);
311             specificTest(clazz, con, UPDATED);
312             con = reOpen(con);
313         }
314         
315         return con;
316     }
317
318     public void completed() {
319         // virtual
320
}
321
322     public void compare(ObjectContainer con, Object JavaDoc get, RTestable clazz, int ver, int count) {
323         ObjectSet set = con.get(get);
324         if (!PROFILE_ONLY) {
325             set.reset();
326             if (set.size() == count) {
327                 while (set.hasNext()) {
328                     Object JavaDoc res = set.next();
329                     clazz.compare(con, res, ver);
330                     if (DEACTIVATE) {
331                         con.deactivate(res, 1);
332                         con.activate(res, Integer.MAX_VALUE);
333                         clazz.compare(con, res, ver);
334                     }
335                 }
336             } else {
337                 Regression.addError(
338                     clazz.getClass().getName()
339                         + ":offcount:expected"
340                         + count
341                         + ":actual:"
342                         + set.size());
343             }
344         }
345     }
346
347     void specificTest(Object JavaDoc clazz, ObjectContainer con, int step) {
348         String JavaDoc methodName = "specific";
349         Class JavaDoc[] parameterClasses = { ObjectContainer.class, Integer.TYPE };
350         try {
351             Method method = clazz.getClass().getMethod(methodName, parameterClasses);
352             if (method != null) {
353                 method.invoke(clazz, new Object JavaDoc[] { con, new Integer JavaDoc(step)});
354             }
355         } catch (Exception JavaDoc e) {
356         }
357     }
358
359     public ObjectContainer open() {
360         return openContainer();
361     }
362
363     public static void configure() {
364         Configuration config = Db4o.configure();
365         // Set ActivationDepth deep enough for Recursive classes.
366
config.activationDepth(12);
367         ObjectClass oc = config.objectClass("com.db4o.test.DeepUpdate");
368         oc.updateDepth(2);
369         oc = config.objectClass("com.db4o.test.CustomConstructor");
370         oc.translate(new TCustomConstructor());
371         oc = config.objectClass("com.db4o.test.Debug");
372         oc.updateDepth(5);
373     }
374
375     public ObjectContainer openContainer() {
376         if (openDelegate != null) {
377             return openDelegate.openContainer();
378         }
379         configure();
380         return Db4o.openFile(FILE);
381     }
382     
383     public void close(ObjectContainer con) {
384         if (openDelegate != null) {
385             openDelegate.close(con);
386         }
387         if(closeFile()){
388             con.close();
389         }
390     }
391
392     protected int runs() {
393         return RUNS;
394     }
395     
396     protected void closeAllButMemoryFile(ObjectContainer con){
397         if (openDelegate != null) {
398             openDelegate.closeAllButMemoryFile(con);
399         }else{
400             con.close();
401         }
402     }
403
404     protected boolean closeFile() {
405         return closeFile;
406     }
407
408     public ObjectContainer reOpen(ObjectContainer con) {
409         if (closeFile()) {
410             close(con);
411             return open();
412         }
413         return con;
414     }
415
416     Object JavaDoc newInstance(Class JavaDoc a_class) {
417         try {
418             return a_class.newInstance();
419         } catch (Throwable JavaDoc t) {
420             try {
421                 Constructor[] constructors = a_class.getDeclaredConstructors();
422                 for (int i = 0; i < constructors.length; i++) {
423                     try {
424                         if(Platform4.canSetAccessible()){
425                             Platform4.setAccessible(constructors[i]);
426                         }
427                         
428                         Class JavaDoc[] pTypes = constructors[i].getParameterTypes();
429                         Object JavaDoc[] parms = new Object JavaDoc[constructors[i].getParameterTypes().length];
430                         for (int j = 0; j < parms.length; j++) {
431                             for (int k = 0; k < simpleNullWrappers.length; k++) {
432                                 if (pTypes[j] == simpleClasses[k]) {
433                                     parms[j] = simpleNullWrappers[k];
434                                     break;
435                                 }
436                             }
437                         }
438                         Object JavaDoc res = constructors[i].newInstance(parms);
439                         if (res != null) {
440                             return res;
441                         }
442                     } catch (Exception JavaDoc exc) {
443                         System.out.println(exc.getClass().getName());
444                         System.out.println(exc.getMessage());
445                     }
446                 }
447             } catch (Exception JavaDoc ex) {
448             }
449             System.out.println("NewInstance failed:" + a_class.getName());
450             return null;
451         }
452     }
453     
454     boolean isJDK2(){
455         return Platform4.jdk().ver() >= 2;
456     }
457
458     // The following errors are expected.
459
// They occur due to the fact that:
460
// - Byte Objects will be instantiated even if the stored Byte object was null
461
// - the "Empty" objects does not change on update
462
// - the RecursiveTyped objects create more instances of themselves than would be expected
463
static String JavaDoc[] expectedErrors =
464         {
465             "1e3==null:com.db4o.test.types.ArrayTypedPrivate:oByte:",
466             "1e0==null:com.db4o.test.types.ArrayTypedPrivate:nByte:",
467             "1e3==null:com.db4o.test.types.ArrayTypedPublic:oByte:",
468             "1e0==null:com.db4o.test.types.ArrayTypedPublic:nByte:",
469             "com.db4o.test.types.Empty:offcount:expected3:actual:4",
470             "com.db4o.test.types.Empty:offcount:expected1:actual:4",
471             "f1==null:com.db4o.test.types.MasterMonster:ooo:nByte:",
472             "1e3==null:com.db4o.test.types.MasterMonster:ooo:oByte:",
473             "1e0==null:com.db4o.test.types.MasterMonster:ooo:nByte:",
474             "com.db4o.test.types.RecursiveTypedPrivate:offcount:expected1:actual:11",
475             "com.db4o.test.types.RecursiveUnTypedPrivate:offcount:expected1:actual:11",
476             "com.db4o.test.types.RecursiveTypedPublic:offcount:expected1:actual:11",
477             "com.db4o.test.types.RecursiveUnTypedPublic:offcount:expected1:actual:11",
478             "f1==null:com.db4o.test.types.TypedPrivate:nByte:",
479             "f1==null:com.db4o.test.types.TypedPublic:nByte:" };
480
481     static Object JavaDoc[] simpleNullWrappers =
482         {
483             new Integer JavaDoc(0),
484             new Long JavaDoc(0),
485             new Character JavaDoc((char) 0),
486             new Double JavaDoc(0),
487             new Float JavaDoc(0),
488             new Boolean JavaDoc(false),
489             new Short JavaDoc((short) 0),
490             new Byte JavaDoc((byte) 0)};
491
492     static Class JavaDoc[] simpleClasses =
493         {
494             Integer.TYPE,
495             Long.TYPE,
496             Character.TYPE,
497             Double.TYPE,
498             Float.TYPE,
499             Boolean.TYPE,
500             Short.TYPE,
501             Byte.TYPE };
502
503     public static final int ONE = 1;
504     public static final int FIVE = 5;
505     public static final int DELETED = 4;
506     public static final int SAME = 3;
507     public static final int UPDATED = 0;
508
509     public synchronized static void addError(String JavaDoc err) {
510         for (int i = 0; i < expectedErrors.length; i++) {
511             if (err.equals(expectedErrors[i])) {
512                 return;
513             }
514         }
515         errors = errors + err + System.getProperty("line.separator");
516     }
517
518     public static final boolean DEACTIVATE = false;
519     public static String JavaDoc FILE = "regression.yap";
520     protected Regression openDelegate;
521     private static String JavaDoc errors = "";
522     private static int openedThreads = 0;
523     private static int returnedThreads = 0;
524     private static boolean threadsSharp = false;
525     
526     private boolean closeFile;
527
528     public static RTestable[] allClasses() {
529         return new RTestable[] {
530             new ArrayInObjectPrivate(),
531             new ArrayInObjectPublic(),
532             new ArrayMixedInObjectPrivate(),
533             new ArrayMixedInObjectPublic(),
534             new ArrayMixedTypedPrivate(),
535             new ArrayMixedTypedPublic(),
536             new ArrayNDimensionalPrivate(),
537             new ArrayNDimensionalPublic(),
538             new ArrayTypedPrivate(),
539             new ArrayTypedPublic(),
540             new ArrayUntypedPrivate(),
541             new ArrayUntypedPublic(),
542             new BiParentTypedPrivate(),
543             new BiParentTypedPublic(),
544             new BiParentUnTypedPrivate(),
545             new BiParentUnTypedPublic(),
546             new DeepUpdate(),
547             new Empty(),
548             new InterfacePrivate(),
549             new InterfacePublic(),
550             new ObjectSimplePrivate(),
551             new ObjectSimplePublic(),
552             new ParameterConstructor(0),
553             PrivateConstructor.construct(),
554             new RecursiveTypedPrivate(),
555             new RecursiveTypedPublic(),
556             new RecursiveUnTypedPrivate(),
557             new RecursiveUnTypedPublic(),
558             new RHashtable(),
559             new RProperties(),
560             new RStack(),
561             new RVector(),
562             new SelfReference(),
563             new TypedPrivate(),
564             new TypedPublic(),
565             new UntypedPrivate(),
566             new UntypedPublic()
567             // new MasterMonster()
568
};
569     }
570     public RTestable[] testClasses() {
571         if (!DEBUG) {
572             return allClasses();
573         }
574         return new RTestable[] {
575             new UntypedDebug()
576 // new RStack(),
577
// new RVector(),
578
// new SelfReference(),
579
};
580     }
581
582 }
583
Popular Tags