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.test;
022:
023: import java.io.*;
024: import java.util.*;
025:
026: import com.db4o.*;
027: import com.db4o.ext.*;
028: import com.db4o.foundation.io.*;
029: import com.db4o.internal.*;
030: import com.db4o.internal.btree.*;
031: import com.db4o.internal.classindex.*;
032: import com.db4o.query.*;
033:
034: public class UpdatingDb4oVersions {
035:
036: public static final String PATH = "./test/db4oVersions/";
037:
038: public static final String[] VERSIONS = { "db4o_3.0.3",
039: "db4o_4.0.004", "db4o_4.1.001", "db4o_4.6.003",
040: "db4o_4.6.004", "db4o_5.0.007", "db4o_5.1.001",
041: "db4o_5.2.001", "db4o_5.2.003", "db4o_5.2.008",
042: "db4o_5.3.001", "db4o_5.4.004", "db4o_5.5.2", "db4o_5.6.2",
043: "db4o_6.2.001", "db4o_6.2.301" };
044:
045: public List list;
046: public Map map;
047: public String name;
048:
049: public void configure() {
050: Db4o.configure().allowVersionUpdates(true);
051: Db4o.configure().objectClass(UpdatingDb4oVersions.class)
052: .objectField("name").indexed(true);
053: }
054:
055: public void store() {
056: if (debugModeOrClientServer()) {
057: return;
058: }
059: String file = PATH + fileName();
060: new File(file).mkdirs();
061: new File(file).delete();
062: ExtObjectContainer objectContainer = Db4o.openFile(file).ext();
063: try {
064: UpdatingDb4oVersions udv = new UpdatingDb4oVersions();
065: udv.name = "check";
066: udv.list = objectContainer.collections().newLinkedList();
067: udv.map = objectContainer.collections().newHashMap(1);
068: objectContainer.set(udv);
069: udv.list.add("check");
070: udv.map.put("check", "check");
071: } finally {
072: objectContainer.close();
073: }
074: }
075:
076: public void test() throws IOException {
077: if (debugModeOrClientServer()) {
078: return;
079: }
080: for (int i = 0; i < VERSIONS.length; i++) {
081: String oldFilePath = PATH + VERSIONS[i];
082: File oldFile = new File(oldFilePath);
083: if (oldFile.exists()) {
084:
085: String testFilePath = PATH + VERSIONS[i] + ".yap";
086: new File(testFilePath).delete();
087:
088: File4.copy(oldFilePath, testFilePath);
089:
090: checkDatabaseFile(testFilePath);
091:
092: // Twice, to ensure everything is fine after opening, converting and closing.
093: checkDatabaseFile(testFilePath);
094:
095: } else {
096: System.err
097: .println("Version upgrade check failed. File not found:");
098: System.err.println(oldFile);
099: }
100: }
101: }
102:
103: private boolean debugModeOrClientServer() {
104: return Deploy.debug || Test.isClientServer();
105: }
106:
107: private void checkDatabaseFile(String testFile) {
108: ExtObjectContainer objectContainer = Db4o.openFile(testFile)
109: .ext();
110: try {
111: checkStoredObjectsArePresent(objectContainer);
112: checkBTreeSize(objectContainer);
113: } finally {
114: objectContainer.close();
115: }
116: }
117:
118: private void checkBTreeSize(ExtObjectContainer objectContainer) {
119: ObjectContainerBase yapStream = (ObjectContainerBase) objectContainer;
120: StoredClass storedClass = objectContainer.storedClass(this
121: .getClass().getName());
122: ClassMetadata yc = (ClassMetadata) storedClass;
123: BTreeClassIndexStrategy btreeClassIndexStrategy = (BTreeClassIndexStrategy) yc
124: .index();
125: BTree btree = btreeClassIndexStrategy.btree();
126: Test.ensure(btree != null);
127: int size = btree.size(yapStream.transaction());
128: Test.ensure(size == 1);
129: }
130:
131: private void checkStoredObjectsArePresent(
132: ExtObjectContainer objectContainer) {
133: Query q = objectContainer.query();
134: q.constrain(UpdatingDb4oVersions.class);
135: ObjectSet objectSet = q.execute();
136: Test.ensure(objectSet.size() == 1);
137: UpdatingDb4oVersions udv = (UpdatingDb4oVersions) objectSet
138: .next();
139: Test.ensure(udv.name.equals("check"));
140: Test.ensure(udv.list.size() == 1);
141: Test.ensure(udv.list.get(0).equals("check"));
142: Test.ensure(udv.map.get("check").equals("check"));
143: }
144:
145: private static String fileName() {
146: return Db4o.version().replace(' ', '_') + ".yap";
147: }
148: }
|