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 com.db4o.collections.*;
024: import com.db4o.db4ounit.common.ta.*;
025: import com.db4o.ext.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029: import db4ounit.extensions.fixtures.*;
030:
031: public class ArrayMap4TAMultiClientsTestCase extends
032: TransparentActivationTestCaseBase implements OptOutSolo {
033:
034: public static void main(String[] args) {
035: new ArrayMap4TAMultiClientsTestCase().runEmbeddedClientServer();
036: }
037:
038: private final ArrayMap4Operation<String, Integer> _clearOp = new ArrayMap4Operation<String, Integer>() {
039:
040: public void operate(ArrayMap4<String, Integer> map) {
041: map.clear();
042: }
043: };
044:
045: private final ArrayMap4Operation<String, Integer> _putOp = new ArrayMap4Operation<String, Integer>() {
046:
047: public void operate(ArrayMap4<String, Integer> map) {
048: map.put("10", Integer.valueOf(10 * 100));
049: }
050: };
051:
052: private final ArrayMap4Operation<String, Integer> _putAllOp = new ArrayMap4Operation<String, Integer>() {
053:
054: public void operate(ArrayMap4<String, Integer> map) {
055: for (int i = 10; i < 50; i++) {
056: map.put(String.valueOf(i), Integer.valueOf(i * 100));
057: }
058: }
059: };
060:
061: private final ArrayMap4Operation<String, Integer> _putAllOp2 = new ArrayMap4Operation<String, Integer>() {
062:
063: public void operate(ArrayMap4<String, Integer> map) {
064: for (int i = 50; i < 100; i++) {
065: map.put(String.valueOf(i), Integer.valueOf(i * 100));
066: }
067: }
068: };
069:
070: private final ArrayMap4Operation<String, Integer> _removeOp = new ArrayMap4Operation<String, Integer>() {
071:
072: public void operate(ArrayMap4<String, Integer> map) {
073: map.remove("0");
074: }
075: };
076:
077: protected void store() throws Exception {
078: ArrayMap4<String, Integer> map = new ArrayMap4<String, Integer>();
079: ArrayMap4Asserter.putData(map);
080: store(map);
081: }
082:
083: protected ExtObjectContainer openNewClient() {
084: return clientServerFixture().openNewClient();
085: }
086:
087: protected Db4oClientServerFixture clientServerFixture() {
088: return (Db4oClientServerFixture) fixture();
089: }
090:
091: private ArrayMap4<String, Integer> retrieveOnlyInstance(
092: ExtObjectContainer db) {
093: ArrayMap4<String, Integer> map = CollectionsUtil
094: .retrieveMapFromDB(db, reflector());
095: return map;
096: }
097:
098: private ArrayMap4<String, Integer> retrieveOnlyInstance() {
099: ArrayMap4<String, Integer> map = CollectionsUtil
100: .retrieveMapFromDB(db(), reflector());
101: return map;
102: }
103:
104: public void testClearClear() {
105: operate(_clearOp, _clearOp);
106:
107: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
108: Assert.areEqual(0, map.size());
109: }
110:
111: public void testClearPut() {
112: operate(_clearOp, _putOp);
113:
114: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
115: Assert.areEqual(11, map.size());
116: ArrayMap4Asserter.checkMap(map, 0, 11);
117: }
118:
119: public void testClearRemove() {
120: operate(_clearOp, _removeOp);
121:
122: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
123: Assert.areEqual(9, map.size());
124: ArrayMap4Asserter.checkMap(map, 1, 10);
125: }
126:
127: public void testClearGet() {
128: ExtObjectContainer client1 = openNewClient();
129: ExtObjectContainer client2 = openNewClient();
130: ExtObjectContainer client3 = openNewClient();
131: ExtObjectContainer client4 = openNewClient();
132: ExtObjectContainer client5 = openNewClient();
133: ExtObjectContainer client6 = openNewClient();
134:
135: ArrayMap4<String, Integer> map1 = retrieveOnlyInstance(client1);
136: ArrayMap4<String, Integer> map2 = retrieveOnlyInstance(client2);
137: ArrayMap4<String, Integer> map3 = retrieveOnlyInstance(client3);
138: ArrayMap4<String, Integer> map4 = retrieveOnlyInstance(client4);
139: ArrayMap4<String, Integer> map5 = retrieveOnlyInstance(client5);
140: ArrayMap4<String, Integer> map6 = retrieveOnlyInstance(client6);
141:
142: ArrayMap4Asserter.checkMap(map1, 0, 10);
143: ArrayMap4Asserter.checkMap(map2, 0, 10);
144: map1.clear();
145: ArrayMap4Asserter.assertContainsKey(map3);
146: ArrayMap4Asserter.assertContainsValue(map4);
147: ArrayMap4Asserter.assertEntrySet(map5);
148: ArrayMap4Asserter.assertKeySet(map6);
149:
150: client1.set(map1);
151: client2.set(map2);
152: client3.set(map3);
153: client4.set(map4);
154: client5.set(map5);
155: client6.set(map6);
156:
157: client1.close();
158: client2.close();
159: client3.close();
160: client4.close();
161: client5.close();
162: client6.close();
163:
164: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
165: Assert.areEqual(10, map.size());
166: ArrayMap4Asserter.checkMap(map, 0, 10);
167: }
168:
169: @SuppressWarnings("unchecked")
170: public void testClearClone() {
171: ExtObjectContainer client1 = openNewClient();
172: ExtObjectContainer client2 = openNewClient();
173:
174: ArrayMap4<String, Integer> map1 = retrieveOnlyInstance(client1);
175: ArrayMap4<String, Integer> map2 = retrieveOnlyInstance(client2);
176:
177: ArrayMap4Asserter.checkMap(map1, 0, 10);
178: ArrayMap4Asserter.checkMap(map2, 0, 10);
179: map1.clear();
180: ArrayMap4<String, Integer> clone = (ArrayMap4<String, Integer>) map2
181: .clone();
182: client1.set(map1);
183: client2.delete(map2);
184: client2.set(clone);
185: client1.close();
186: client2.close();
187:
188: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
189: Assert.areEqual(10, map.size());
190: ArrayMap4Asserter.checkMap(map, 0, 10);
191: }
192:
193: public void testPutPut() {
194: operate(_putOp, _putOp);
195:
196: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
197: ArrayMap4Asserter.checkMap(map, 0, 11);
198: Assert.isNull(map.get("11"));
199: }
200:
201: public void testPutPutAll() {
202: operate(_putOp, _putAllOp);
203:
204: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
205: ArrayMap4Asserter.checkMap(map, 0, 50);
206: Assert.isNull(map.get("100"));
207: }
208:
209: public void testPutRemove() {
210: operate(_putOp, _removeOp);
211:
212: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
213: ArrayMap4Asserter.checkMap(map, 1, 10);
214: Assert.isNull(map.get("0"));
215: }
216:
217: public void testPutClear() {
218: operate(_putOp, _clearOp);
219:
220: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
221: ArrayMap4Asserter.checkClear(map);
222: }
223:
224: public void testPutAllClear() {
225: operate(_putAllOp, _clearOp);
226:
227: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
228: ArrayMap4Asserter.checkClear(map);
229: }
230:
231: public void testPutAllPut() {
232: operate(_putAllOp, _putOp);
233:
234: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
235: ArrayMap4Asserter.checkMap(map, 0, 11);
236: }
237:
238: public void testPutAllPutAll() {
239: operate(_putAllOp2, _putAllOp);
240:
241: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
242: ArrayMap4Asserter.checkMap(map, 0, 50);
243: }
244:
245: public void testPutAllRemove() {
246: operate(_putAllOp, _removeOp);
247:
248: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
249: ArrayMap4Asserter.checkMap(map, 1, 10);
250: }
251:
252: public void testRemoveClear() {
253: operate(_removeOp, _clearOp);
254:
255: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
256: ArrayMap4Asserter.checkClear(map);
257: }
258:
259: public void testRemovePut() {
260: operate(_removeOp, _putOp);
261:
262: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
263: ArrayMap4Asserter.checkMap(map, 0, 11);
264: }
265:
266: public void testRemovePutAll() {
267: operate(_removeOp, _putAllOp);
268:
269: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
270: ArrayMap4Asserter.checkMap(map, 0, 50);
271: Assert.isNull(map.get("100"));
272: }
273:
274: public void testRemoveRemove() {
275: operate(_removeOp, _removeOp);
276:
277: ArrayMap4<String, Integer> map = retrieveOnlyInstance();
278: ArrayMap4Asserter.checkMap(map, 1, 10);
279: }
280:
281: private void operate(ArrayMap4Operation<String, Integer> op1,
282: ArrayMap4Operation<String, Integer> op2) {
283: ExtObjectContainer client1 = openNewClient();
284: ExtObjectContainer client2 = openNewClient();
285:
286: ArrayMap4<String, Integer> map1 = retrieveOnlyInstance(client1);
287: ArrayMap4<String, Integer> map2 = retrieveOnlyInstance(client2);
288:
289: ArrayMap4Asserter.checkMap(map1, 0, 10);
290: ArrayMap4Asserter.checkMap(map2, 0, 10);
291:
292: op1.operate(map1);
293: op2.operate(map2);
294:
295: client1.set(map1);
296: client2.set(map2);
297: client1.close();
298: client2.close();
299: }
300: }
|