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.jre5.collections;
022:
023: import java.util.*;
024:
025: import com.db4o.collections.*;
026: import com.db4o.ext.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.fixtures.*;
030:
031: /**
032: * @exclude
033: */
034: public class ArrayList4TAMultiClientsTestCase extends
035: ArrayList4TATestCaseBase implements OptOutSolo {
036: public static void main(String[] args) {
037: new ArrayList4TAMultiClientsTestCase()
038: .runEmbeddedClientServer();
039: }
040:
041: private static final ArrayList4Operation<Integer> _addOp = new ArrayList4Operation<Integer>() {
042: public void operate(ArrayList4<Integer> list) {
043: list.add(new Integer(ArrayList4Asserter.CAPACITY));
044: }
045: };
046:
047: private static final ArrayList4Operation<Integer> _removeOp = new ArrayList4Operation<Integer>() {
048:
049: public void operate(ArrayList4<Integer> list) {
050: list.remove(0);
051: }
052: };
053:
054: private static final ArrayList4Operation<Integer> _setOp = new ArrayList4Operation<Integer>() {
055: public void operate(ArrayList4<Integer> list) {
056: list.set(0, new Integer(1));
057: }
058: };
059:
060: private static final ArrayList4Operation<Integer> _clearOp = new ArrayList4Operation<Integer>() {
061: public void operate(ArrayList4<Integer> list) {
062: list.clear();
063: }
064: };
065:
066: private static final ArrayList4Operation<Integer> _containsOp = new ArrayList4Operation<Integer>() {
067: public void operate(ArrayList4<Integer> list) {
068: Assert.isFalse(list.contains(new Integer(
069: ArrayList4Asserter.CAPACITY)));
070: }
071: };
072:
073: private static final ArrayList4Operation<Integer> _addAllOp = new ArrayList4Operation<Integer>() {
074: public void operate(ArrayList4<Integer> list) {
075: final Vector<Integer> v = new Vector<Integer>();
076: for (int i = 0; i < ArrayList4Asserter.CAPACITY; ++i) {
077: v.add(new Integer(ArrayList4Asserter.CAPACITY + i));
078: }
079: list.addAll(v);
080: }
081: };
082:
083: private static final ArrayList4Operation<Integer> _removeRangeOp = new ArrayList4Operation<Integer>() {
084: public void operate(ArrayList4<Integer> list) {
085: list.subList(ArrayList4Asserter.CAPACITY - 10,
086: ArrayList4Asserter.CAPACITY).clear();
087: }
088: };
089:
090: public void testAddAdd() throws Exception {
091: ArrayList4Operation<Integer> anotherAddOp = new ArrayList4Operation<Integer>() {
092: public void operate(ArrayList4<Integer> list) {
093: list.add(new Integer(ArrayList4Asserter.CAPACITY + 42));
094: }
095: };
096: operate(anotherAddOp, _addOp);
097: checkAdd();
098: }
099:
100: public void testSetAdd() throws Exception {
101: operate(_setOp, _addOp);
102: checkAdd();
103: }
104:
105: public void testRemoveAdd() throws Exception {
106: operate(_removeOp, _addOp);
107: checkAdd();
108: }
109:
110: private void checkAdd() throws Exception {
111: checkListSizeAndContents(ArrayList4Asserter.CAPACITY + 1);
112: }
113:
114: private void checkNotModified() throws Exception {
115: checkListSizeAndContents(ArrayList4Asserter.CAPACITY);
116: }
117:
118: private void checkListSizeAndContents(int expectedSize)
119: throws Exception {
120: ArrayList4<Integer> list = retrieveAndAssertNullArrayList4();
121: Assert.areEqual(expectedSize, list.size());
122: for (int i = 0; i < expectedSize; ++i) {
123: Assert.areEqual(new Integer(i), list.get(i));
124: }
125: }
126:
127: public void testAddRemove() throws Exception {
128: operate(_addOp, _removeOp);
129: checkRemove();
130: }
131:
132: public void testsetRemove() throws Exception {
133: operate(_setOp, _removeOp);
134: checkRemove();
135: }
136:
137: public void testRemoveRemove() throws Exception {
138: ArrayList4Operation<Integer> anotherRemoveOp = new ArrayList4Operation<Integer>() {
139: public void operate(ArrayList4<Integer> list) {
140: list.remove(1);
141: }
142: };
143: operate(anotherRemoveOp, _removeOp);
144: checkRemove();
145: }
146:
147: private void checkRemove() throws Exception {
148: ArrayList4<Integer> list = retrieveAndAssertNullArrayList4();
149: Assert.areEqual(ArrayList4Asserter.CAPACITY - 1, list.size());
150: for (int i = 0; i < ArrayList4Asserter.CAPACITY - 1; ++i) {
151: Assert.areEqual(new Integer(i + 1), list.get(i));
152: }
153: }
154:
155: public void testAddSet() throws Exception {
156: operate(_addOp, _setOp);
157: checkSet();
158: }
159:
160: public void testRemoveSet() throws Exception {
161: operate(_removeOp, _setOp);
162: checkSet();
163: }
164:
165: public void testSetSet() throws Exception {
166: ArrayList4Operation<Integer> anotherSetOp = new ArrayList4Operation<Integer>() {
167: public void operate(ArrayList4<Integer> list) {
168: list.set(0, new Integer(2));
169: }
170: };
171: operate(anotherSetOp, _setOp);
172: checkSet();
173: }
174:
175: public void testClearSet() throws Exception {
176: operate(_clearOp, _setOp);
177: checkSet();
178: }
179:
180: public void testSetClear() throws Exception {
181: operate(_setOp, _clearOp);
182: checkClear();
183: }
184:
185: public void testClearRemove() throws Exception {
186: operate(_clearOp, _removeOp);
187: checkRemove();
188: }
189:
190: public void testRemoveClear() throws Exception {
191: operate(_removeOp, _clearOp);
192: checkClear();
193: }
194:
195: public void testContainsClear() throws Exception {
196: operate(_containsOp, _clearOp);
197: checkClear();
198: }
199:
200: public void testContainsSet() throws Exception {
201: operate(_containsOp, _setOp);
202: checkSet();
203: }
204:
205: public void testContainsRemove() throws Exception {
206: operate(_containsOp, _removeOp);
207: checkRemove();
208: }
209:
210: public void testContainsAdd() throws Exception {
211: operate(_containsOp, _addOp);
212: checkAdd();
213: }
214:
215: public void testContainsRemoveRange() throws Exception {
216: operate(_containsOp, _removeRangeOp);
217: checkRemoveRange();
218: }
219:
220: public void testAddContains() throws Exception {
221: operate(_addOp, _containsOp);
222: checkNotModified();
223: }
224:
225: public void testSetContains() throws Exception {
226: operate(_setOp, _containsOp);
227: checkNotModified();
228: }
229:
230: public void testRemoveContains() throws Exception {
231: operate(_removeOp, _containsOp);
232: checkNotModified();
233: }
234:
235: public void testClearContains() throws Exception {
236: operate(_clearOp, _containsOp);
237: checkNotModified();
238: }
239:
240: public void testRemoveRangeContains() throws Exception {
241: operate(_removeRangeOp, _containsOp);
242: checkNotModified();
243: }
244:
245: public void testAddAllSet() throws Exception {
246: operate(_addAllOp, _setOp);
247: checkSet();
248: }
249:
250: public void testAddAllClear() throws Exception {
251: operate(_addAllOp, _clearOp);
252: checkClear();
253: }
254:
255: public void testAddAllRemove() throws Exception {
256: operate(_addAllOp, _removeOp);
257: checkRemove();
258: }
259:
260: public void testAddAllAdd() throws Exception {
261: operate(_addAllOp, _addOp);
262: checkAdd();
263: }
264:
265: public void testSetAddAll() throws Exception {
266: operate(_setOp, _addAllOp);
267: checkAddAll();
268: }
269:
270: public void testClearAddAll() throws Exception {
271: operate(_clearOp, _addAllOp);
272: checkAddAll();
273: }
274:
275: public void testRemoveAddAll() throws Exception {
276: operate(_removeOp, _addAllOp);
277: checkAddAll();
278: }
279:
280: public void testAddAddAll() throws Exception {
281: operate(_addOp, _addAllOp);
282: checkAddAll();
283: }
284:
285: public void testRemoveRangeSet() throws Exception {
286: operate(_removeRangeOp, _setOp);
287: checkSet();
288: }
289:
290: public void testRemoveRangeAdd() throws Exception {
291: operate(_removeRangeOp, _addOp);
292: checkAdd();
293: }
294:
295: public void testRemoveRangeClear() throws Exception {
296: operate(_removeRangeOp, _clearOp);
297: checkClear();
298: }
299:
300: public void testRemoveRangeAddAll() throws Exception {
301: operate(_removeRangeOp, _addAllOp);
302: checkAddAll();
303: }
304:
305: public void testRemoveRangeRemove() throws Exception {
306: operate(_removeRangeOp, _removeOp);
307: checkRemove();
308: }
309:
310: public void testSetRemoveRange() throws Exception {
311: operate(_setOp, _removeRangeOp);
312: checkRemoveRange();
313: }
314:
315: public void testAddRemoveRange() throws Exception {
316: operate(_addOp, _removeRangeOp);
317: checkRemoveRange();
318: }
319:
320: public void testClearRemoveRange() throws Exception {
321: operate(_clearOp, _removeRangeOp);
322: checkRemoveRange();
323: }
324:
325: public void testAddAllRemoveRange() throws Exception {
326: operate(_addAllOp, _removeRangeOp);
327: checkRemoveRange();
328: }
329:
330: public void testRemoveRemoveRange() throws Exception {
331: operate(_removeOp, _removeRangeOp);
332: checkRemoveRange();
333: }
334:
335: private void checkRemoveRange() throws Exception {
336: checkListSizeAndContents(ArrayList4Asserter.CAPACITY - 10);
337: }
338:
339: private void checkAddAll() throws Exception {
340: ArrayList4<Integer> list = retrieveAndAssertNullArrayList4();
341: for (int i = 0; i < ArrayList4Asserter.CAPACITY * 2; ++i) {
342: Assert.areEqual(new Integer(i), list.get(i));
343: }
344: }
345:
346: private void checkClear() throws Exception {
347: ArrayList4<Integer> list = retrieveAndAssertNullArrayList4();
348: Assert.areEqual(0, list.size());
349: }
350:
351: private void checkSet() throws Exception {
352: ArrayList4<Integer> list = retrieveAndAssertNullArrayList4();
353: Assert.areEqual(ArrayList4Asserter.CAPACITY, list.size());
354: Assert.areEqual(new Integer(1), list.get(0));
355: for (int i = 1; i < ArrayList4Asserter.CAPACITY; ++i) {
356: Assert.areEqual(new Integer(i), list.get(i));
357: }
358: }
359:
360: private void operate(ArrayList4Operation<Integer> op1,
361: ArrayList4Operation<Integer> op2) throws Exception {
362: ExtObjectContainer client1 = openNewClient();
363: ExtObjectContainer client2 = openNewClient();
364: ArrayList4<Integer> list1 = retrieveAndAssertNullArrayList4(client1);
365: ArrayList4<Integer> list2 = retrieveAndAssertNullArrayList4(client2);
366: op1.operate(list1);
367: op2.operate(list2);
368: client1.set(list1);
369: client2.set(list2);
370: client1.commit();
371: client2.commit();
372: client1.close();
373: client2.close();
374: }
375:
376: }
|