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.handlers;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.db4ounit.util.*;
028: import com.db4o.ext.*;
029: import com.db4o.foundation.io.*;
030:
031: import db4ounit.*;
032: import db4ounit.extensions.fixtures.*;
033:
034: public abstract class FormatMigrationTestCaseBase implements
035: TestLifeCycle, OptOutNoFileSystemData {
036:
037: private String _db4oVersion;
038:
039: public void configure() {
040: Configuration config = Db4o.configure();
041: config.allowVersionUpdates(true);
042: configure(config);
043: }
044:
045: protected static final String PATH = Path4.combine(Path4
046: .getTempPath(), "test/db4oVersions");
047:
048: protected String fileName() {
049: _db4oVersion = Db4oVersion.NAME;
050: return fileName(_db4oVersion);
051: }
052:
053: protected String fileName(String versionName) {
054: return oldVersionFileName(versionName) + ".yap";
055: }
056:
057: protected String oldVersionFileName(String versionName) {
058: return Path4.combine(PATH, fileNamePrefix()
059: + versionName.replace(' ', '_'));
060: }
061:
062: public void createDatabase() {
063: createDatabase(fileName());
064: }
065:
066: public void createDatabaseFor(String versionName) {
067: _db4oVersion = versionName;
068: createDatabase(fileName(versionName));
069: }
070:
071: private void createDatabase(String file) {
072: File4.mkdirs(PATH);
073: if (File4.exists(file)) {
074: File4.delete(file);
075: }
076: ExtObjectContainer objectContainer = Db4o.openFile(file).ext();
077: try {
078: store(objectContainer);
079: } finally {
080: objectContainer.close();
081: }
082: }
083:
084: public void setUp() throws Exception {
085: configure();
086: createDatabase();
087: }
088:
089: public void test() throws IOException {
090: for (int i = 0; i < versionNames().length; i++) {
091: final String versionName = versionNames()[i];
092: test(versionName);
093: }
094: }
095:
096: public void test(final String versionName) throws IOException {
097: _db4oVersion = versionName;
098: String testFileName = fileName(versionName);
099: if (File4.exists(testFileName)) {
100: // System.out.println("Check database: " + testFileName);
101:
102: investigateFileHeaderVersion(testFileName);
103:
104: checkDatabaseFile(testFileName);
105: // Twice, to ensure everything is fine after opening, converting and closing.
106: checkDatabaseFile(testFileName);
107: } else {
108:
109: System.out
110: .println("Version upgrade check failed. File not found:"
111: + testFileName);
112:
113: // FIXME: The following fails the CC build since not all files are there on .NET.
114: // Change back when we have all files.
115: // Assert.fail("Version upgrade check failed. File not found:" + testFileName);
116: }
117: }
118:
119: public void tearDown() throws Exception {
120: // do nothing
121: }
122:
123: private void checkDatabaseFile(String testFile) {
124: configure();
125: ExtObjectContainer objectContainer = Db4o.openFile(testFile)
126: .ext();
127: try {
128: assertObjectsAreReadable(objectContainer);
129: } finally {
130: objectContainer.close();
131: }
132: }
133:
134: private void investigateFileHeaderVersion(String testFile)
135: throws IOException {
136: _db4oHeaderVersion = VersionServices
137: .fileHeaderVersion(testFile);
138: }
139:
140: protected int db4oMajorVersion() {
141: return new Integer(_db4oVersion.substring(0, 1)).intValue();
142: }
143:
144: protected byte _db4oHeaderVersion;
145:
146: protected abstract String[] versionNames();
147:
148: protected abstract String fileNamePrefix();
149:
150: protected abstract void configure(Configuration config);
151:
152: protected abstract void store(ExtObjectContainer objectContainer);
153:
154: protected abstract void assertObjectsAreReadable(
155: ExtObjectContainer objectContainer);
156:
157: }
|