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.db4ounit.common.migration.*;
027: import com.db4o.defragment.*;
028: import com.db4o.foundation.io.*;
029: import com.db4o.query.*;
030:
031: import db4ounit.*;
032:
033: /**
034: * test case for COR-785
035: */
036: public class LegacyDatabaseDefragTestCase implements TestCase {
037:
038: private static final int ITEM_COUNT = 50;
039:
040: public static final class Item {
041:
042: public int value;
043:
044: public Item() {
045: }
046:
047: public Item(int value) {
048: this .value = value;
049: }
050: }
051:
052: // FIXME: solve the workspacePath issue and uncomment this
053: public void _test() throws Exception {
054: final String dbFile = getTempFile();
055: createLegacyDatabase(dbFile);
056: defrag(dbFile);
057: assertContents(dbFile);
058: }
059:
060: private void assertContents(String dbFile) {
061: final ObjectContainer container = Db4o.openFile(dbFile);
062: try {
063: final ObjectSet found = queryItems(container);
064: for (int i = 1; i < ITEM_COUNT; i += 2) {
065: Assert.isTrue(found.hasNext());
066: Assert.areEqual(i, ((Item) found.next()).value);
067: }
068: } finally {
069: container.close();
070: }
071: }
072:
073: private ObjectSet queryItems(final ObjectContainer container) {
074: final Query q = container.query();
075: q.constrain(Item.class);
076: q.descend("value").orderAscending();
077: final ObjectSet found = q.execute();
078: return found;
079: }
080:
081: public void createDatabase(String fname) {
082: final ObjectContainer container = Db4o.openFile(fname);
083: try {
084: fragmentDatabase(container);
085: } finally {
086: container.close();
087: }
088: }
089:
090: private void fragmentDatabase(final ObjectContainer container) {
091: Item[] items = createItems();
092: for (int i = 0; i < items.length; ++i) {
093: container.set(items[i]);
094: }
095: for (int i = 0; i < items.length; i += 2) {
096: container.delete(items[i]);
097: }
098: }
099:
100: private Item[] createItems() {
101: Item[] items = new Item[LegacyDatabaseDefragTestCase.ITEM_COUNT];
102: for (int i = 0; i < items.length; ++i) {
103: items[i] = new Item(i);
104: }
105: return items;
106: }
107:
108: private String getTempFile() throws IOException {
109: return Path4.getTempFileName();
110: }
111:
112: private void defrag(String dbFile) throws IOException {
113: final DefragmentConfig config = new DefragmentConfig(dbFile);
114: config.upgradeFile(dbFile + ".upgraded");
115: Defragment.defrag(config);
116: }
117:
118: private void createLegacyDatabase(String dbFile) throws Exception {
119: Db4oLibrary library = librarian().forVersion("6.1");
120: library.environment.invokeInstanceMethod(getClass(),
121: "createDatabase", new Object[] { dbFile });
122: }
123:
124: private Db4oLibrarian librarian() {
125: return new Db4oLibrarian(new Db4oLibraryEnvironmentProvider(
126: PathProvider.testCasePath()));
127: }
128:
129: }
|