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.db4ounit.util.*;
024: import com.db4o.foundation.*;
025:
026: import db4ounit.*;
027: import db4ounit.util.*;
028:
029: /**
030: * @exclude
031: */
032: public class MultiDimensionalArrayHandlerUpdateTestCase extends
033: HandlerUpdateTestCaseBase {
034:
035: // TODO: make asymmetrical once we support
036: public static final int[][] intData2D = new int[][] {
037: new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
038:
039: // TODO: make asymmetrical once we support
040: public static final String[][] stringData2D = new String[][] {
041: new String[] { "one", "two", },
042: new String[] { "three", "four", } };
043:
044: // TODO: make asymmetrical once we support
045: public static final Object[][] objectData2D = new Object[][] {
046: new Object[] { new Item("one"), null, new Item("two"), },
047: new Object[] { new Item("three"), new Item("four"), null } };
048:
049: // TODO: make asymmetrical once we support
050: public static final Object[][] stringObjectData2D = new Object[][] {
051: new Object[] { "one", "two", },
052: new Object[] { "three", "four", } };
053:
054: public static final byte[][] byteData2D = new byte[][] {
055: ByteHandlerUpdateTestCase.data,
056: ByteHandlerUpdateTestCase.data, };
057:
058: public static class ItemArrays {
059:
060: public int[][] _typedIntArray;
061:
062: public Object _untypedIntArray;
063:
064: public String[][] _typedStringArray;
065:
066: public Object _untypedStringArray;
067:
068: public Object[][] _objectArray;
069:
070: public Object[][] _stringObjectArray;
071:
072: public byte[][] _typedByteArray;
073:
074: }
075:
076: public static class Item {
077:
078: public String _name;
079:
080: public Item(String name) {
081: _name = name;
082: }
083:
084: public boolean equals(Object obj) {
085:
086: if (!(obj instanceof Item)) {
087: return false;
088: }
089:
090: Item other = (Item) obj;
091:
092: if (_name == null) {
093: return other._name == null;
094: }
095:
096: return _name.equals(other._name);
097: }
098:
099: }
100:
101: protected Object createArrays() {
102: ItemArrays item = new ItemArrays();
103: if (multiDimensionalArraysCantBeStored()) {
104: return item;
105: }
106: item._typedIntArray = intData2D;
107: item._untypedIntArray = intData2D;
108: item._typedStringArray = stringData2D;
109: item._untypedStringArray = stringData2D;
110: item._objectArray = objectData2D;
111: item._stringObjectArray = stringObjectData2D;
112: item._typedByteArray = byteData2D;
113: return item;
114: }
115:
116: protected void assertArrays(Object obj) {
117: if (multiDimensionalArraysCantBeStored()) {
118: return;
119: }
120: ItemArrays item = (ItemArrays) obj;
121: assertAreEqual(intData2D, item._typedIntArray);
122: assertAreEqual(intData2D,
123: castToIntArray2D(item._untypedIntArray));
124: assertAreEqual(stringData2D, item._typedStringArray);
125: assertAreEqual(stringData2D,
126: (String[][]) item._untypedStringArray);
127: assertAreEqual(objectData2D, item._objectArray);
128: assertAreEqual(objectData2D, item._objectArray);
129: assertAreEqual(byteData2D, item._typedByteArray);
130: }
131:
132: private boolean multiDimensionalArraysCantBeStored() {
133: return PlatformInformation.isDotNet()
134: && (db4oMajorVersion() < 6);
135: }
136:
137: public static void assertAreEqual(int[][] expected, int[][] actual) {
138: Assert.areEqual(expected.length, actual.length);
139: for (int i = 0; i < expected.length; i++) {
140: ArrayAssert.areEqual(expected[i], actual[i]);
141: }
142: }
143:
144: public static void assertAreEqual(String[][] expected,
145: String[][] actual) {
146: Assert.areEqual(expected.length, actual.length);
147: for (int i = 0; i < expected.length; i++) {
148: ArrayAssert.areEqual(expected[i], actual[i]);
149: }
150: }
151:
152: public static void assertAreEqual(Object[][] expected,
153: Object[][] actual) {
154: Assert.areEqual(expected.length, actual.length);
155: for (int i = 0; i < expected.length; i++) {
156: ArrayAssert.areEqual(expected[i], actual[i]);
157: }
158: }
159:
160: protected int[][] castToIntArray2D(Object obj) {
161: ObjectByRef byRef = new ObjectByRef(obj);
162: correctIntArray2DJavaOnly(byRef);
163: return (int[][]) byRef.value;
164: }
165:
166: public static void assertAreEqual(byte[][] expected, byte[][] actual) {
167: Assert.areEqual(expected.length, actual.length);
168: for (int i = 0; i < expected.length; i++) {
169: ArrayAssert.areEqual(expected[i], actual[i]);
170: }
171: }
172:
173: /**
174: * @sharpen.remove
175: */
176: protected void correctIntArray2DJavaOnly(ObjectByRef byRef) {
177: if (_db4oHeaderVersion == VersionServices.HEADER_30_40) {
178:
179: // Bug in the oldest format:
180: // It accidentally converted int[][] arrays to Integer[][] arrays.
181:
182: Integer[][] wrapperArray = (Integer[][]) byRef.value;
183: int[][] res = new int[wrapperArray.length][];
184: for (int i = 0; i < wrapperArray.length; i++) {
185: res[i] = castToIntArray(wrapperArray[i]);
186: }
187: byRef.value = res;
188: }
189: }
190:
191: protected Object[] createValues() {
192: // not used
193: return null;
194: }
195:
196: protected void assertValues(Object[] values) {
197: // not used
198: }
199:
200: protected String typeName() {
201: return "multidimensional_array";
202: }
203:
204: }
|