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 com.db4o.*;
024: import com.db4o.foundation.*;
025:
026: public class CallbacksTestCase {
027: static boolean returnValue = true;
028: static final Object lock = new Object();
029:
030: static final int ACTIVATE = 0;
031: static final int DEACTIVATE = 1;
032: static final int DELETE = 2;
033: static final int NEW = 3;
034: static final int UPDATE = 4;
035: static final int CAN_ACTIVATE = 5;
036: static final int CAN_DEACTIVATE = 6;
037: static final int CAN_DELETE = 7;
038: static final int CAN_NEW = 8;
039: static final int CAN_UPDATE = 9;
040:
041: static boolean[] called = new boolean[CAN_UPDATE + 1];
042:
043: public String name;
044: public CallbackHelper helper;
045:
046: public void storeOne() {
047: // helper = new CallbackHelper();
048: // helper.parent = this;
049: name = "stored";
050: noneCalled();
051: }
052:
053: public void testOne() {
054:
055: ObjectContainer oc = Test.objectContainer();
056:
057: ensure(ACTIVATE);
058: ensureNot(DEACTIVATE);
059: ensureNot(DELETE);
060: ensure(NEW);
061: ensureNot(UPDATE);
062:
063: ensure(CAN_ACTIVATE);
064: ensureNot(CAN_DEACTIVATE);
065: ensureNot(CAN_DELETE);
066: ensure(CAN_NEW);
067: ensureNot(CAN_UPDATE);
068: noneCalled();
069:
070: setReturn(false);
071: oc.deactivate(this , 3);
072: ensure(CAN_DEACTIVATE);
073: ensureNot(DEACTIVATE);
074: Test.ensure(name.equals("stored"));
075: noneCalled();
076:
077: setReturn(true);
078: oc.deactivate(this , 3);
079: ensure(CAN_DEACTIVATE);
080: ensure(DEACTIVATE);
081: Test.ensure(name == null);
082: noneCalled();
083:
084: setReturn(false);
085: oc.activate(this , 3);
086: ensure(CAN_ACTIVATE);
087: ensureNot(ACTIVATE);
088: Test.ensure(name == null);
089: noneCalled();
090:
091: setReturn(true);
092: oc.activate(this , 3);
093: ensure(CAN_ACTIVATE);
094: ensure(ACTIVATE);
095: Test.ensure(name.equals("stored"));
096: noneCalled();
097:
098: setReturn(false);
099: name = "modified";
100: oc.set(this );
101: ensure(CAN_UPDATE);
102: ensureNot(UPDATE);
103: setReturn(true);
104: oc.ext().refresh(this , 3);
105: Test.ensure(name.equals("stored"));
106: noneCalled();
107:
108: setReturn(true);
109: name = "modified";
110: oc.set(this );
111: ensure(CAN_UPDATE);
112: ensure(UPDATE);
113: oc.ext().refresh(this , 3);
114: Test.ensure(name.equals("modified"));
115: noneCalled();
116:
117: // Test endless loops
118: helper = new CallbackHelper();
119: helper.name = "helper";
120: helper.parent = this ;
121: oc.set(this );
122: oc.activate(this , 3);
123: oc.deactivate(this , 3);
124:
125: oc.activate(this , 1);
126: oc.deactivate(this .helper, 1);
127: setReturn(false);
128: noneCalled();
129: oc.activate(this , 3);
130: ensureNot(ACTIVATE);
131:
132: setReturn(true);
133: noneCalled();
134: oc.delete(this );
135: oc.commit();
136:
137: Cool.sleepIgnoringInterruption(100);
138:
139: ensure(CAN_DELETE);
140: ensure(DELETE);
141:
142: noneCalled();
143: setReturn(true);
144: }
145:
146: static void setReturn(boolean val) {
147: synchronized (lock) {
148: returnValue = val;
149: }
150: }
151:
152: static boolean getReturn() {
153: synchronized (lock) {
154: return returnValue;
155: }
156: }
157:
158: public boolean objectCanActivate(ObjectContainer container) {
159: called[CAN_ACTIVATE] = true;
160: return getReturn();
161: }
162:
163: public boolean objectCanDeactivate(ObjectContainer container) {
164: called[CAN_DEACTIVATE] = true;
165: return getReturn();
166: }
167:
168: public boolean objectCanDelete(ObjectContainer container) {
169: called[CAN_DELETE] = true;
170: return getReturn();
171: }
172:
173: public boolean objectCanNew(ObjectContainer container) {
174: called[CAN_NEW] = true;
175: return getReturn();
176: }
177:
178: public boolean objectCanUpdate(ObjectContainer container) {
179: called[CAN_UPDATE] = true;
180: return getReturn();
181: }
182:
183: public void objectOnActivate(ObjectContainer container) {
184: called[ACTIVATE] = true;
185: if (helper != null) {
186: container.activate(helper, 3);
187: }
188: }
189:
190: public void objectOnDeactivate(ObjectContainer container) {
191: called[DEACTIVATE] = true;
192: if (helper != null) {
193: container.deactivate(helper, 3);
194: }
195: }
196:
197: public void objectOnDelete(ObjectContainer container) {
198: called[DELETE] = true;
199: if (helper != null) {
200: container.delete(helper);
201: }
202: }
203:
204: public void objectOnNew(ObjectContainer container) {
205: called[NEW] = true;
206: if (helper != null) {
207: container.set(helper);
208: }
209: }
210:
211: public void objectOnUpdate(ObjectContainer container) {
212: called[UPDATE] = true;
213: if (helper != null) {
214: container.set(helper);
215: }
216: }
217:
218: private void ensure(int eventPos) {
219: Test.ensure(called[eventPos]);
220: }
221:
222: private void ensureNot(int eventPos) {
223: Test.ensure(!called[eventPos]);
224: }
225:
226: private void noneCalled() {
227: for (int i = 0; i <= CAN_UPDATE; i++) {
228: called[i] = false;
229: }
230: }
231:
232: }
|