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.internal.*;
024: import com.db4o.internal.handlers.*;
025:
026: import db4ounit.*;
027:
028: public class MultiDimensionalArrayHandlerTestCase extends
029: TypeHandlerTestCaseBase {
030:
031: public static void main(String[] args) {
032: new MultiDimensionalArrayHandlerTestCase().runSolo();
033: }
034:
035: public static class Item {
036:
037: public int[][] _int;
038:
039: public Item(int[][] int_) {
040: _int = int_;
041: }
042:
043: public boolean equals(Object obj) {
044: if (obj == this ) {
045: return true;
046: }
047: if (!(obj instanceof Item)) {
048: return false;
049: }
050: Item other = (Item) obj;
051:
052: if (_int.length != other._int.length) {
053: return false;
054: }
055:
056: for (int i = 0; i < _int.length; i++) {
057: if (_int[i].length != other._int[i].length) {
058: return false;
059: }
060: for (int j = 0; j < _int[i].length; j++) {
061: if (_int[i][j] != other._int[i][j]) {
062: return false;
063: }
064: }
065: }
066: return true;
067: }
068:
069: }
070:
071: private ArrayHandler intArrayHandler() {
072: return arrayHandler(int.class, true);
073: }
074:
075: // private ArrayHandler stringArrayHandler(){
076: // return arrayHandler(String.class, false);
077: // }
078:
079: private ArrayHandler arrayHandler(Class clazz, boolean isPrimitive) {
080: TypeHandler4 typeHandler = stream().handlers().handlerForClass(
081: stream(), reflector().forClass(clazz));
082: return new MultidimensionalArrayHandler(stream(), typeHandler,
083: isPrimitive);
084: }
085:
086: public void testReadWrite() {
087: MockWriteContext writeContext = new MockWriteContext(db());
088: Item expected = new Item(new int[][] { new int[] { 1, 2, 3 },
089: new int[] { 6, 5, 4 } });
090: intArrayHandler().write(writeContext, expected._int);
091:
092: MockReadContext readContext = new MockReadContext(writeContext);
093:
094: int[][] arr = (int[][]) intArrayHandler().read(readContext);
095: Item actualValue = new Item(arr);
096: Assert.areEqual(expected, actualValue);
097: }
098:
099: public void testStoreObject() throws Exception {
100: Item storedItem = new Item(new int[][] { new int[] { 1, 2, 3 },
101: new int[] { 6, 5, 4 } });
102: doTestStoreObject(storedItem);
103: }
104:
105: }
|