KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24
25 import com.db4o.*;
26 import com.db4o.query.*;
27
28
29
30 /**
31  *
32  */

33 public class CallConstructors {
34     
35     static Hashtable constructorCalledByClass = new Hashtable();
36     
37     static void constructorCalled(Object JavaDoc obj){
38         constructorCalledByClass.put(obj.getClass(), obj);
39     }
40     
41     static Object JavaDoc[] cases = new Object JavaDoc[]{
42         new CallGlobal(),
43         new CallLocalYes(),
44         new CallLocalNo()
45     };
46     
47     public void configure(){
48         Db4o.configure().callConstructors(false);
49         Db4o.configure().objectClass(new CallLocalYes()).callConstructor(true);
50         Db4o.configure().objectClass(new CallLocalNo()).callConstructor(false);
51     }
52     
53     public void store(){
54         for (int i = 0; i < cases.length; i++) {
55             Test.store(cases[i]);
56         }
57     }
58     
59     public void test(){
60         if(! Test.clientServer){
61             check(new CallLocalYes(), true);
62             check(new CallLocalNo(), false);
63             check(new CallGlobal(), false);
64         }
65         Db4o.configure().callConstructors(true);
66         Test.reOpen();
67         check(new CallLocalYes(), true);
68         check(new CallLocalNo(), false);
69         check(new CallGlobal(), true);
70         Db4o.configure().callConstructors(false);
71         Test.reOpen();
72         check(new CallLocalYes(), true);
73         check(new CallLocalNo(), false);
74         check(new CallGlobal(), false);
75         
76     }
77     
78     private void check(Object JavaDoc obj, boolean expected){
79         constructorCalledByClass.clear();
80         Query q = Test.query();
81         q.constrain(obj.getClass());
82         ObjectSet os = q.execute();
83         Test.ensure(os.hasNext());
84         while(os.hasNext()){
85             os.next();
86         }
87         boolean called = constructorCalledByClass.get(obj.getClass()) != null;
88         Test.ensure(called == expected);
89     }
90     
91     
92     public static class CallCommonBase{
93         public CallCommonBase(){
94             constructorCalled(this);
95         }
96     }
97     
98     public static class CallGlobal extends CallCommonBase{
99     }
100     
101     public static class CallLocalYes extends CallCommonBase{
102     }
103     
104     public static class CallLocalNo extends CallCommonBase{
105     }
106 }
107
Popular Tags