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.jre11.migration;
022:
023: import java.io.*;
024:
025: import com.db4o.Db4o;
026: import com.db4o.ObjectContainer;
027: import com.db4o.db4ounit.util.IOServices;
028: import com.db4o.db4ounit.util.WorkspaceServices;
029: import com.db4o.foundation.io.File4;
030: import com.db4o.query.Query;
031:
032: import db4ounit.Assert;
033: import db4ounit.TestCase;
034: import db4ounit.TestLifeCycle;
035: import db4ounit.extensions.fixtures.*;
036:
037: public abstract class MigrationTestCaseBase implements TestCase,
038: TestLifeCycle, OptOutNoFileSystemData {
039:
040: private static final String NULL_NAME = "NULL";
041:
042: private static final String MIN_VALUE_NAME = "MIN_VALUE";
043:
044: private static final String MAX_VALUE_NAME = "MAX_VALUE";
045:
046: private static final String ORDINARY_NAME = "REGULAR";
047:
048: private ObjectContainer _container;
049:
050: private String _databaseFile;
051:
052: public void setUp() throws Exception {
053: Db4o.configure().allowVersionUpdates(true);
054: prepareDatabaseFile();
055: open();
056: }
057:
058: protected ObjectContainer db() {
059: return _container;
060: }
061:
062: private void prepareDatabaseFile() throws IOException {
063: _databaseFile = IOServices.buildTempPath(getDatabaseFileName());
064: File4.copy(WorkspaceServices.workspaceTestFilePath("migration/"
065: + getDatabaseFileName()), _databaseFile);
066: }
067:
068: protected void reopen() {
069: close();
070: open();
071: }
072:
073: private void open() {
074: _container = Db4o.openFile(_databaseFile);
075: }
076:
077: private void close() {
078: if (null != _container) {
079: _container.close();
080: _container = null;
081: }
082: }
083:
084: public void tearDown() throws Exception {
085: close();
086: Db4o.configure().allowVersionUpdates(false);
087: }
088:
089: protected MigrationItem getItem(String itemName) {
090: final Query q = db().query();
091: q.constrain(MigrationItem.class);
092: q.descend("name").constrain(itemName);
093: return (MigrationItem) q.execute().next();
094: }
095:
096: protected void updateItemDate(String itemName, Object newValue) {
097: final MigrationItem item = getItem(itemName);
098: item.setValue(newValue);
099: db().set(item);
100: }
101:
102: protected void assertItem(final Object expectedValue,
103: final String itemName) {
104: Assert
105: .areEqual(expectedValue, getItemValue(itemName),
106: itemName);
107: }
108:
109: private Object getItemValue(String itemName) {
110: return getItem(itemName).getValue();
111: }
112:
113: public void testValuesAreReadCorrectly() {
114: assertItem(getOrdinaryValue(), ORDINARY_NAME);
115: assertItem(getMaxValue(), MAX_VALUE_NAME);
116: assertItem(getMinValue(), MIN_VALUE_NAME);
117: assertItem(null, NULL_NAME);
118: }
119:
120: public void testValueCanBeUpdated() {
121: final Object updateValue = getUpdateValue();
122: updateItemDate(NULL_NAME, getOrdinaryValue());
123: updateItemDate(ORDINARY_NAME, updateValue);
124: updateItemDate(MAX_VALUE_NAME, null);
125:
126: for (int i = 0; i < 2; ++i) {
127: assertItem(null, MAX_VALUE_NAME);
128: assertItem(getOrdinaryValue(), NULL_NAME);
129: assertItem(updateValue, ORDINARY_NAME);
130: reopen();
131: }
132: }
133:
134: public void generateFile() {
135: new java.io.File(getDatabaseFileName()).delete();
136: final ObjectContainer container = Db4o
137: .openFile(getDatabaseFileName());
138: try {
139: container.set(newItem(NULL_NAME, null));
140: container.set(newItem(MAX_VALUE_NAME, getMaxValue()));
141: container.set(newItem(MIN_VALUE_NAME, getMinValue()));
142: container.set(newItem(ORDINARY_NAME, getOrdinaryValue()));
143: } finally {
144: container.close();
145: }
146: }
147:
148: protected abstract MigrationItem newItem(String name, Object value);
149:
150: protected abstract String getDatabaseFileName();
151:
152: protected abstract Object getMinValue();
153:
154: protected abstract Object getMaxValue();
155:
156: protected abstract Object getOrdinaryValue();
157:
158: protected abstract Object getUpdateValue();
159:
160: }
|