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.db4ounit.common.persistent.*;
027: import com.db4o.ext.*;
028:
029: import db4ounit.*;
030: import db4ounit.extensions.*;
031:
032: public class UpdateCollectionTestCase extends Db4oClientServerTestCase {
033:
034: public static void main(String[] args) {
035: new UpdateCollectionTestCase().runConcurrency();
036: }
037:
038: private static String testString = "simple test string";
039:
040: public List list = new ArrayList();
041:
042: private static int LIST_SIZE = 100;
043:
044: protected void store() throws Exception {
045:
046: for (int i = 0; i < LIST_SIZE; i++) {
047: SimpleObject o = new SimpleObject(testString + i, i);
048: list.add(o);
049: }
050: store(list);
051: }
052:
053: public void concUpdateSameElement(ExtObjectContainer oc, int seq)
054: throws Exception {
055:
056: ObjectSet result = oc.get(List.class);
057: Assert.areEqual(1, result.size());
058: List l = (ArrayList) result.next();
059: Assert.areEqual(LIST_SIZE, l.size());
060: boolean found = false;
061: Iterator iter = l.iterator();
062: while (iter.hasNext()) {
063: SimpleObject o = (SimpleObject) iter.next();
064: // find the middle element, by comparing SimpleObject.s
065: if ((testString + LIST_SIZE / 2).equals(o.getS())) {
066: o.setI(LIST_SIZE + seq);
067: found = true;
068: break;
069: }
070: }
071: Assert.isTrue(found);
072: oc.set(l);
073: }
074:
075: public void checkUpdateSameElement(ExtObjectContainer oc)
076: throws Exception {
077:
078: ObjectSet result = oc.get(List.class);
079: Assert.areEqual(1, result.size());
080: List l = (ArrayList) result.next();
081: Assert.areEqual(LIST_SIZE, l.size());
082: boolean found = false;
083: Iterator iter = l.iterator();
084: while (iter.hasNext()) {
085: SimpleObject o = (SimpleObject) iter.next();
086: // find the middle element, by comparing SimpleObject.s
087: if ((testString + LIST_SIZE / 2).equals(o.getS())) {
088: int i = o.getI();
089: Assert.isTrue(LIST_SIZE <= i
090: && i < LIST_SIZE + threadCount());
091: found = true;
092: break;
093: }
094: }
095: Assert.isTrue(found);
096:
097: }
098:
099: public void concUpdateDifferentElement(ExtObjectContainer oc,
100: int seq) throws Exception {
101:
102: ObjectSet result = oc.get(List.class);
103: Assert.areEqual(1, result.size());
104: List l = (ArrayList) result.next();
105: Assert.areEqual(LIST_SIZE, l.size());
106: boolean found = false;
107: Iterator iter = l.iterator();
108: while (iter.hasNext()) {
109: SimpleObject o = (SimpleObject) iter.next();
110: if ((testString + seq).equals(o.getS())) {
111: o.setI(LIST_SIZE + seq);
112: oc.set(o);
113: found = true;
114: break;
115: }
116: }
117: Assert.isTrue(found);
118:
119: }
120:
121: public void checkUpdateDifferentElement(ExtObjectContainer oc)
122: throws Exception {
123:
124: ObjectSet result = oc.get(List.class);
125: Assert.areEqual(1, result.size());
126: List l = (ArrayList) result.next();
127: Assert.areEqual(LIST_SIZE, l.size());
128: Iterator iter = l.iterator();
129: while (iter.hasNext()) {
130: SimpleObject o = (SimpleObject) iter.next();
131: int i = o.getI();
132: if (i >= LIST_SIZE) {
133: i = i - LIST_SIZE;
134: }
135: Assert.areEqual(testString + i, o.getS());
136: }
137:
138: }
139:
140: public void concUpdateList(ExtObjectContainer oc, int seq)
141: throws Exception {
142:
143: ObjectSet result = oc.get(List.class);
144: Assert.areEqual(1, result.size());
145: List l = (ArrayList) result.next();
146: Assert.areEqual(LIST_SIZE, l.size());
147: Iterator iter = l.iterator();
148: while (iter.hasNext()) {
149: SimpleObject o = (SimpleObject) iter.next();
150: // set all SimpleObject.i as thread sequence.
151: o.setI(seq);
152: }
153: oc.set(l);
154:
155: }
156:
157: public void checkUpdateList(ExtObjectContainer oc) throws Exception {
158:
159: ObjectSet result = oc.get(List.class);
160: Assert.areEqual(1, result.size());
161: List l = (ArrayList) result.next();
162: Assert.areEqual(LIST_SIZE, l.size());
163: Iterator iter = l.iterator();
164: SimpleObject firstElement = (SimpleObject) iter.next();
165: int expectedI = firstElement.getI();
166: // assert all SimpleObject.i have the same value.
167: while (iter.hasNext()) {
168: SimpleObject o = (SimpleObject) iter.next();
169: Assert.areEqual(expectedI, o.getI());
170: }
171:
172: }
173:
174: }
|