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.cs;
022:
023: import com.db4o.config.*;
024: import com.db4o.internal.cs.*;
025:
026: import db4ounit.*;
027:
028: public class SwitchingFilesFromMultipleClientsTestCase extends
029: StandaloneCSTestCaseBase implements TestLifeCycle {
030:
031: public static class Data {
032: public int _id;
033:
034: public Data(int id) {
035: this ._id = id;
036: }
037: }
038:
039: private int _counter;
040:
041: protected void configure(Configuration config) {
042: }
043:
044: protected void runTest() {
045: _counter = 0;
046: ClientObjectContainer clientA = openClient();
047: ClientObjectContainer clientB = openClient();
048: addData(clientA);
049: assertDataCount(clientA, clientB, 1, 0);
050: clientA.commit();
051: assertDataCount(clientA, clientB, 1, 1);
052:
053: clientA.switchToFile(SwitchingFilesFromClientUtil.FILENAME_A);
054: assertDataCount(clientA, clientB, 0, 1);
055: addData(clientA);
056: assertDataCount(clientA, clientB, 1, 1);
057: clientA.commit();
058: assertDataCount(clientA, clientB, 1, 1);
059:
060: clientB.switchToFile(SwitchingFilesFromClientUtil.FILENAME_B);
061: assertDataCount(clientA, clientB, 1, 0);
062: addData(clientA);
063: assertDataCount(clientA, clientB, 2, 0);
064: clientA.commit();
065: assertDataCount(clientA, clientB, 2, 0);
066: addData(clientB);
067: assertDataCount(clientA, clientB, 2, 1);
068:
069: clientA.switchToFile(SwitchingFilesFromClientUtil.FILENAME_B);
070: assertDataCount(clientA, clientB, 0, 1);
071: clientB.commit();
072: assertDataCount(clientA, clientB, 1, 1);
073: addData(clientA);
074: clientA.commit();
075: assertDataCount(clientA, clientB, 2, 2);
076: addData(clientB);
077: clientB.commit();
078: assertDataCount(clientA, clientB, 3, 3);
079:
080: clientB.switchToFile(SwitchingFilesFromClientUtil.FILENAME_A);
081: assertDataCount(clientA, clientB, 3, 2);
082:
083: clientA.switchToMainFile();
084: assertDataCount(clientA, clientB, 1, 2);
085:
086: clientB.switchToMainFile();
087: assertDataCount(clientA, clientB, 1, 1);
088:
089: clientA.close();
090: clientB.close();
091: }
092:
093: public void setUp() throws Exception {
094: SwitchingFilesFromClientUtil.deleteFiles();
095: }
096:
097: public void tearDown() throws Exception {
098: SwitchingFilesFromClientUtil.deleteFiles();
099: }
100:
101: private void assertDataCount(ClientObjectContainer clientA,
102: ClientObjectContainer clientB, int expectedA, int expectedB) {
103: assertDataCount(clientA, expectedA);
104: assertDataCount(clientB, expectedB);
105: }
106:
107: private void assertDataCount(ClientObjectContainer client,
108: int expected) {
109: Assert.areEqual(expected, client.query(Data.class).size());
110: }
111:
112: private void addData(ClientObjectContainer client) {
113: client.set(new Data(_counter++));
114: }
115: }
|