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.db4ounit.common.concurrency;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.db4ounit.common.persistent.*;
028: import com.db4o.ext.*;
029:
030: import db4ounit.*;
031: import db4ounit.extensions.*;
032:
033: public class CascadeToVectorTestCase extends Db4oClientServerTestCase {
034:
035: public static void main(String[] args) {
036: new CascadeToVectorTestCase().runConcurrency();
037: }
038:
039: public Vector vec;
040:
041: protected void configure(Configuration config) {
042: config.objectClass(this ).cascadeOnUpdate(true);
043: config.objectClass(this ).cascadeOnDelete(true);
044: config.objectClass(Atom.class).cascadeOnDelete(false);
045: }
046:
047: protected void store() {
048: CascadeToVectorTestCase ctv = new CascadeToVectorTestCase();
049: ctv.vec = new Vector();
050: ctv.vec.addElement(new Atom("stored1"));
051: ctv.vec
052: .addElement(new Atom(new Atom("storedChild1"),
053: "stored2"));
054: store(ctv);
055: }
056:
057: public void conc(ExtObjectContainer oc) {
058: CascadeToVectorTestCase ctv = (CascadeToVectorTestCase) retrieveOnlyInstance(
059: oc, CascadeToVectorTestCase.class);
060: Enumeration i = ctv.vec.elements();
061: while (i.hasMoreElements()) {
062: Atom atom = (Atom) i.nextElement();
063: atom.name = "updated";
064: if (atom.child != null) {
065: // This one should NOT cascade
066: atom.child.name = "updated";
067: }
068: }
069: oc.set(ctv);
070: }
071:
072: public void check(ExtObjectContainer oc) {
073: CascadeToVectorTestCase ctv = (CascadeToVectorTestCase) retrieveOnlyInstance(
074: oc, CascadeToVectorTestCase.class);
075: Enumeration i = ctv.vec.elements();
076: while (i.hasMoreElements()) {
077: Atom atom = (Atom) i.nextElement();
078: Assert.areEqual("updated", atom.name);
079: if (atom.child != null) {
080: Assert.areEqual("storedChild1", atom.child.name);
081: }
082: }
083: }
084:
085: public void concDelete(ExtObjectContainer oc, int seq)
086: throws Exception {
087: ObjectSet os = oc.query(CascadeToVectorTestCase.class);
088: if (os.size() == 0) { // already deleted
089: return;
090: }
091: Assert.areEqual(1, os.size());
092: CascadeToVectorTestCase ctv = (CascadeToVectorTestCase) os
093: .next();
094: // wait for other threads
095: Thread.sleep(500);
096: oc.delete(ctv);
097: }
098:
099: public void checkDelete(ExtObjectContainer oc) {
100: // Cascade-On-Delete Test: We only want one atom to remain.
101: assertOccurrences(oc, Atom.class, 1);
102: }
103: }
|