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.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027:
028: /**
029: *
030: */
031: public class CallConstructors {
032:
033: static Hashtable constructorCalledByClass = new Hashtable();
034:
035: static void constructorCalled(Object obj) {
036: constructorCalledByClass.put(obj.getClass(), obj);
037: }
038:
039: static Object[] cases = new Object[] { new CallGlobal(),
040: new CallLocalYes(), new CallLocalNo() };
041:
042: public void configure() {
043: Db4o.configure().callConstructors(false);
044: Db4o.configure().objectClass(new CallLocalYes())
045: .callConstructor(true);
046: Db4o.configure().objectClass(new CallLocalNo())
047: .callConstructor(false);
048: }
049:
050: public void store() {
051: for (int i = 0; i < cases.length; i++) {
052: Test.store(cases[i]);
053: }
054: }
055:
056: public void test() {
057: if (!Test.clientServer) {
058: check(new CallLocalYes(), true);
059: check(new CallLocalNo(), false);
060: check(new CallGlobal(), false);
061: }
062: Db4o.configure().callConstructors(true);
063: Test.reOpen();
064: check(new CallLocalYes(), true);
065: check(new CallLocalNo(), false);
066: check(new CallGlobal(), true);
067: Db4o.configure().callConstructors(false);
068: Test.reOpen();
069: check(new CallLocalYes(), true);
070: check(new CallLocalNo(), false);
071: check(new CallGlobal(), false);
072:
073: }
074:
075: private void check(Object obj, boolean expected) {
076: constructorCalledByClass.clear();
077: Query q = Test.query();
078: q.constrain(obj.getClass());
079: ObjectSet os = q.execute();
080: Test.ensure(os.hasNext());
081: while (os.hasNext()) {
082: os.next();
083: }
084: boolean called = constructorCalledByClass.get(obj.getClass()) != null;
085: Test.ensure(called == expected);
086: }
087:
088: public static class CallCommonBase {
089: public CallCommonBase() {
090: constructorCalled(this );
091: }
092: }
093:
094: public static class CallGlobal extends CallCommonBase {
095: }
096:
097: public static class CallLocalYes extends CallCommonBase {
098: }
099:
100: public static class CallLocalNo extends CallCommonBase {
101: }
102: }
|