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: import db4ounit.extensions.*;
028:
029: public class ArrayHandlerTestCase extends AbstractDb4oTestCase {
030:
031: public static void main(String[] args) {
032: new ArrayHandlerTestCase().runSolo();
033: }
034:
035: public static class IntArrayHolder {
036: public int[] _ints;
037:
038: public IntArrayHolder(int[] ints) {
039: _ints = ints;
040: }
041: }
042:
043: public static class StringArrayHolder {
044: public String[] _strings;
045:
046: public StringArrayHolder(String[] strings) {
047: _strings = strings;
048: }
049: }
050:
051: private ArrayHandler intArrayHandler() {
052: return arrayHandler(int.class, true);
053: }
054:
055: private ArrayHandler stringArrayHandler() {
056: return arrayHandler(String.class, false);
057: }
058:
059: private ArrayHandler arrayHandler(Class clazz, boolean isPrimitive) {
060: ClassMetadata classMetadata = stream().produceClassMetadata(
061: reflector().forClass(clazz));
062: return new ArrayHandler(stream(), classMetadata.typeHandler(),
063: isPrimitive);
064: }
065:
066: public void testIntArrayReadWrite() {
067: MockWriteContext writeContext = new MockWriteContext(db());
068: int[] expected = new int[] { 7, 8, 9 };
069: intArrayHandler().write(writeContext, expected);
070: MockReadContext readContext = new MockReadContext(writeContext);
071: int[] actual = (int[]) intArrayHandler().read(readContext);
072: ArrayAssert.areEqual(expected, actual);
073: }
074:
075: public void testIntArrayStoreObject() throws Exception {
076: IntArrayHolder expectedItem = new IntArrayHolder(new int[] { 1,
077: 2, 3 });
078: db().set(expectedItem);
079: db().purge(expectedItem);
080: IntArrayHolder readItem = (IntArrayHolder) retrieveOnlyInstance(IntArrayHolder.class);
081: Assert.areNotSame(expectedItem, readItem);
082: ArrayAssert.areEqual(expectedItem._ints, readItem._ints);
083: }
084:
085: public void testStringArrayReadWrite() {
086: MockWriteContext writeContext = new MockWriteContext(db());
087: String[] expected = new String[] { "one", "two", "three" };
088: stringArrayHandler().write(writeContext, expected);
089: MockReadContext readContext = new MockReadContext(writeContext);
090: String[] actual = (String[]) stringArrayHandler().read(
091: readContext);
092: ArrayAssert.areEqual(expected, actual);
093: }
094:
095: public void testStringArrayStoreObject() throws Exception {
096: StringArrayHolder expectedItem = new StringArrayHolder(
097: new String[] { "one", "two", "three" });
098: db().set(expectedItem);
099: db().purge(expectedItem);
100: StringArrayHolder readItem = (StringArrayHolder) retrieveOnlyInstance(StringArrayHolder.class);
101: Assert.areNotSame(expectedItem, readItem);
102: ArrayAssert.areEqual(expectedItem._strings, readItem._strings);
103: }
104:
105: }
|