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.migration;
022:
023: import java.io.*;
024:
025: import com.db4o.db4ounit.common.handlers.*;
026:
027: import db4ounit.*;
028:
029: public class Db4oMigrationSuiteBuilder extends
030: ReflectionTestSuiteBuilder {
031:
032: /**
033: * Runs the tests against all archived libraries + the current one
034: */
035: public static final String[] ALL = null;
036:
037: /**
038: * Runs the tests against the current version only.
039: */
040: public static final String[] CURRENT = new String[0];
041:
042: private final Db4oLibraryEnvironmentProvider _environmentProvider = new Db4oLibraryEnvironmentProvider(
043: PathProvider.testCasePath());
044: private final String[] _specificLibraries;
045:
046: /**
047: * Creates a suite builder for the specific FormatMigrationTestCaseBase derived classes
048: * and specific db4o libraries. If no libraries are specified (either null or empty array)
049: * {@link Db4oLibrarian#libraries} is used to find archived libraries.
050: *
051: * @param classes
052: * @param specificLibraries
053: */
054: public Db4oMigrationSuiteBuilder(Class[] classes,
055: String[] specificLibraries) {
056: super (classes);
057: _specificLibraries = specificLibraries;
058: }
059:
060: protected TestSuite fromClass(Class clazz) {
061: assertMigrationTestCase(clazz);
062: final TestSuite defaultTestSuite = super .fromClass(clazz);
063: try {
064: final TestSuite migrationTestSuite = migrationTestSuite(
065: clazz, db4oLibraries());
066: return new TestSuite(new Test[] { migrationTestSuite,
067: defaultTestSuite });
068: } catch (Exception e) {
069: return new TestSuite(new Test[] {
070: new FailingTest(clazz.getName(), e),
071: defaultTestSuite });
072: }
073: }
074:
075: private TestSuite migrationTestSuite(Class clazz,
076: Db4oLibrary[] libraries) throws Exception {
077: Test[] migrationTests = new Test[libraries.length];
078: for (int i = 0; i < libraries.length; i++) {
079: migrationTests[i] = migrationTest(libraries[i], clazz);
080: }
081: return new TestSuite(migrationTests);
082: }
083:
084: private Db4oMigrationTest migrationTest(final Db4oLibrary library,
085: Class clazz) throws Exception {
086: final FormatMigrationTestCaseBase instance = (FormatMigrationTestCaseBase) newInstance(clazz);
087: return new Db4oMigrationTest(instance, library);
088: }
089:
090: private Db4oLibrary[] db4oLibraries() throws Exception {
091: if (hasSpecificLibraries()) {
092: return specificLibraries();
093: }
094: return librarian().libraries();
095: }
096:
097: private Db4oLibrary[] specificLibraries() throws Exception {
098: Db4oLibrary[] libraries = new Db4oLibrary[_specificLibraries.length];
099: for (int i = 0; i < libraries.length; i++) {
100: libraries[i] = librarian().forFile(_specificLibraries[i]);
101: }
102: return libraries;
103: }
104:
105: private boolean hasSpecificLibraries() {
106: return null != _specificLibraries;
107: }
108:
109: private Db4oLibrarian librarian() {
110: return new Db4oLibrarian(_environmentProvider);
111: }
112:
113: private void assertMigrationTestCase(Class clazz) {
114: if (!FormatMigrationTestCaseBase.class.isAssignableFrom(clazz)) {
115: throw new IllegalArgumentException();
116: }
117: }
118:
119: private static final class Db4oMigrationTest extends TestAdapter {
120:
121: private final FormatMigrationTestCaseBase _test;
122: private final Db4oLibrary _library;
123: private final String _version;
124:
125: public Db4oMigrationTest(FormatMigrationTestCaseBase test,
126: Db4oLibrary library) throws Exception {
127: _library = library;
128: _test = test;
129: _version = environment().version();
130: }
131:
132: public String getLabel() {
133: return "[" + _version + "] " + _test.getClass().getName();
134: }
135:
136: protected void runTest() throws Exception {
137: createDatabase();
138: test();
139: }
140:
141: private void test() throws IOException {
142: _test.test(_version);
143: }
144:
145: private void createDatabase() throws Exception {
146: environment().invokeInstanceMethod(_test.getClass(),
147: "createDatabaseFor", new Object[] { _version });
148: }
149:
150: private Db4oLibraryEnvironment environment() {
151: return _library.environment;
152: }
153: }
154: }
|