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.defragment;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.defragment.*;
028: import com.db4o.foundation.io.*;
029: import com.db4o.io.*;
030:
031: import db4ounit.*;
032:
033: public class COR775TestCase implements TestLifeCycle {
034:
035: private static final String ORIGINAL = "cor775.yap";
036:
037: private static final String DEFGARED = ORIGINAL + ".bk";
038:
039: Configuration db4oConfig;
040:
041: public void setUp() throws Exception {
042: cleanup();
043: }
044:
045: public void tearDown() throws Exception {
046: cleanup();
047: }
048:
049: private void cleanup() {
050: File4.delete(ORIGINAL);
051: File4.delete(DEFGARED);
052: }
053:
054: public static void main(String[] args) {
055: new TestRunner(COR775TestCase.class).run();
056: }
057:
058: public void testCOR775() throws Exception {
059: prepare();
060: verifyDB();
061:
062: DefragmentConfig config = new DefragmentConfig(ORIGINAL,
063: DEFGARED);
064: config.forceBackupDelete(true);
065: //config.storedClassFilter(new AvailableClassFilter());
066: config.db4oConfig(getConfiguration());
067: Defragment.defrag(config);
068:
069: verifyDB();
070: }
071:
072: private void prepare() {
073: File file = new File(ORIGINAL);
074: if (file.exists()) {
075: file.delete();
076: }
077:
078: ObjectContainer testDB = openDB();
079: Item item = new Item("richard", 100);
080: testDB.set(item);
081: testDB.close();
082: }
083:
084: private void verifyDB() {
085: ObjectContainer testDB = openDB();
086: ObjectSet result = testDB.get(Item.class);
087: if (result.hasNext()) {
088: Item retrievedItem = (Item) result.next();
089: Assert.areEqual("richard", retrievedItem.name);
090: Assert.areEqual(100, retrievedItem.value);
091: } else {
092: Assert.fail("Cannot retrieve the expected object.");
093: }
094: testDB.close();
095: }
096:
097: private ObjectContainer openDB() {
098: Configuration db4oConfig = getConfiguration();
099: ObjectContainer testDB = Db4o.openFile(db4oConfig, ORIGINAL);
100: return testDB;
101: }
102:
103: private Configuration getConfiguration() {
104: if (db4oConfig == null) {
105: db4oConfig = Db4o.newConfiguration();
106:
107: db4oConfig.activationDepth(Integer.MAX_VALUE);
108: db4oConfig.callConstructors(true);
109: IoAdapter ioAdapter = new MockIOAdapter(
110: new RandomAccessFileAdapter(), "db4o");
111: db4oConfig.io(ioAdapter);
112: }
113: return db4oConfig;
114: }
115:
116: public static class Item {
117: public String name;
118:
119: public int value;
120:
121: public Item(String name, int value) {
122: this .name = name;
123: this .value = value;
124: }
125: }
126:
127: public static class MockIOAdapter extends IoAdapter {
128:
129: private IoAdapter ioAdapter;
130:
131: private String password;
132:
133: private long pos;
134:
135: public MockIOAdapter(IoAdapter ioAdapter, String password) {
136: this .ioAdapter = ioAdapter;
137: this .password = password;
138: }
139:
140: public void close() throws Db4oIOException {
141: ioAdapter.close();
142: }
143:
144: public void delete(String path) {
145: ioAdapter.delete(path);
146: }
147:
148: public boolean exists(String path) {
149: return ioAdapter.exists(path);
150: }
151:
152: public long getLength() throws Db4oIOException {
153: return ioAdapter.getLength();
154: }
155:
156: public IoAdapter open(String path, boolean lockFile,
157: long initialLength, boolean readOnly)
158: throws Db4oIOException {
159: return new MockIOAdapter(ioAdapter.open(path, lockFile,
160: initialLength, readOnly), password);
161: }
162:
163: public int read(byte[] bytes, int length)
164: throws Db4oIOException {
165: ioAdapter.read(bytes);
166: for (int i = 0; i < length; i++) {
167: bytes[i] = (byte) (bytes[i] - password.hashCode());
168: }
169:
170: ioAdapter.seek(pos + length);
171: return length;
172: }
173:
174: public void seek(long pos) throws Db4oIOException {
175: this .pos = pos;
176: ioAdapter.seek(pos);
177: }
178:
179: public void sync() throws Db4oIOException {
180: ioAdapter.sync();
181: }
182:
183: public void write(byte[] buffer, int length)
184: throws Db4oIOException {
185: for (int i = 0; i < length; i++) {
186: buffer[i] = (byte) (buffer[i] + password.hashCode());
187: }
188: ioAdapter.write(buffer, length);
189: seek(pos + length);
190: }
191:
192: }
193:
194: }
|