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.jre11.concurrency.staging;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.db4ounit.common.persistent.*;
026: import com.db4o.ext.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class CascadeDeleteArrayTestCase extends
032: Db4oClientServerTestCase {
033:
034: public static void main(String[] args) {
035: new CascadeDeleteArrayTestCase().runConcurrency();
036: }
037:
038: public static class ArrayItem {
039: public SimpleObject[] elements;
040: }
041:
042: private int ELEMENT_COUNT = 10;
043:
044: protected void store() {
045: ArrayItem item = new ArrayItem();
046: item.elements = new SimpleObject[ELEMENT_COUNT];
047: for (int i = 0; i < ELEMENT_COUNT; ++i) {
048: item.elements[i] = new SimpleObject("testString" + i, i);
049: }
050: store(item);
051: }
052:
053: protected void configure(Configuration config) {
054: config.objectClass(ArrayItem.class).cascadeOnDelete(true);
055: }
056:
057: public void test() throws Exception {
058: int total = 10;
059: ExtObjectContainer[] containers = new ExtObjectContainer[total];
060: ExtObjectContainer container = null;
061: try {
062: for (int i = 0; i < total; i++) {
063: containers[i] = openNewClient();
064: Assert.areEqual(ELEMENT_COUNT, countOccurences(
065: containers[i], SimpleObject.class));
066: }
067:
068: for (int i = 0; i < total; i++) {
069: deleteAll(containers[i], SimpleObject.class);
070: }
071:
072: container = openNewClient();
073:
074: assertOccurrences(container, SimpleObject.class,
075: ELEMENT_COUNT);
076: // ocs[0] deletes all SimpleObject
077: containers[0].commit();
078: containers[0].close();
079: // FIXME: the following assertion fails
080: assertOccurrences(container, SimpleObject.class, 0);
081: for (int i = 1; i < total; i++) {
082: containers[i].close();
083: }
084: assertOccurrences(container, SimpleObject.class, 0);
085: } finally {
086: if (container != null) {
087: container.close();
088: }
089: for (int i = 0; i < total; i++) {
090: if (containers[i] != null) {
091: containers[i].close();
092: }
093: }
094: }
095: }
096:
097: public void concDelete(ExtObjectContainer oc) throws Exception {
098: int size = countOccurences(oc, SimpleObject.class);
099: if (size == 0) { // already deleted
100: return;
101: }
102: Assert.areEqual(ELEMENT_COUNT, size);
103:
104: ObjectSet os = oc.query(ArrayItem.class);
105: if (os.size() == 0) { // already deteled
106: return;
107: }
108: Assert.areEqual(1, os.size());
109: ArrayItem item = (ArrayItem) os.next();
110:
111: // waits for other threads
112: Thread.sleep(500);
113: oc.delete(item);
114:
115: oc.commit();
116:
117: assertOccurrences(oc, SimpleObject.class, 0);
118: }
119:
120: public void checkDelete(ExtObjectContainer oc) {
121: assertOccurrences(oc, SimpleObject.class, 0);
122: }
123:
124: }
|