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 com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.db4ounit.util.*;
026: import com.db4o.ext.*;
027: import com.db4o.foundation.*;
028: import com.db4o.query.*;
029:
030: import db4ounit.*;
031:
032: public abstract class HandlerUpdateTestCaseBase extends
033: FormatMigrationTestCaseBase {
034:
035: public static class Holder {
036:
037: public Object[] _values;
038:
039: public Object _arrays;
040:
041: }
042:
043: protected int _handlerVersion;
044:
045: protected String fileNamePrefix() {
046: return "migrate_" + typeName() + "_";
047: }
048:
049: protected String[] versionNames() {
050: return new String[] { Db4o.version().substring(5) };
051: }
052:
053: protected void configure(Configuration config) {
054: // no special configuration
055: }
056:
057: protected void store(ExtObjectContainer objectContainer) {
058: Holder holder = new Holder();
059: holder._values = createValues();
060: holder._arrays = createArrays();
061: objectContainer.set(holder);
062: }
063:
064: protected void assertObjectsAreReadable(
065: ExtObjectContainer objectContainer) {
066: Query q = objectContainer.query();
067: q.constrain(Holder.class);
068: ObjectSet objectSet = q.execute();
069: Holder holder = (Holder) objectSet.next();
070:
071: investigateHandlerVersion(objectContainer, holder);
072:
073: assertValues(holder._values);
074: assertArrays(holder._arrays);
075: }
076:
077: private void investigateHandlerVersion(
078: ExtObjectContainer objectContainer, Object obj) {
079: _handlerVersion = VersionServices.slotHandlerVersion(
080: objectContainer, obj);
081: }
082:
083: protected abstract String typeName();
084:
085: protected abstract Object[] createValues();
086:
087: protected abstract Object createArrays();
088:
089: protected abstract void assertValues(Object[] values);
090:
091: protected abstract void assertArrays(Object obj);
092:
093: protected int[] castToIntArray(Object obj) {
094: ObjectByRef byRef = new ObjectByRef(obj);
095: castToIntArrayJavaOnly(byRef);
096: return (int[]) byRef.value;
097: }
098:
099: /**
100: * @sharpen.remove
101: */
102: private void castToIntArrayJavaOnly(ObjectByRef byRef) {
103: if (_db4oHeaderVersion != VersionServices.HEADER_30_40) {
104: return;
105: }
106:
107: // Bug in the oldest format:
108: // It accidentally converted int[] arrays to Integer[] arrays.
109:
110: Integer[] wrapperArray = (Integer[]) byRef.value;
111: int[] res = new int[wrapperArray.length];
112: for (int i = 0; i < wrapperArray.length; i++) {
113: if (wrapperArray[i] != null) {
114: res[i] = wrapperArray[i].intValue();
115: }
116: }
117: byRef.value = res;
118: }
119:
120: /**
121: * On .NET there are no primitive wrappers, so the primitives have
122: * their default value. Since default values are tested OK with the
123: * other values test, we don't have to test again, so it's safe to:
124: * @sharpen.remove
125: */
126: protected void assertPrimitiveWrapperIsNullJavaOnly(Object obj) {
127: Assert.isNull(obj);
128: }
129:
130: }
|