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.test.legacy;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.query.*;
028: import com.db4o.test.*;
029:
030: public class ByteArray {
031:
032: public static interface IByteArrayHolder {
033: byte[] getBytes();
034: }
035:
036: public static class ByteArrayHolder implements IByteArrayHolder {
037:
038: public byte[] _bytes;
039:
040: public ByteArrayHolder(byte[] bytes) {
041: this ._bytes = bytes;
042: }
043:
044: public byte[] getBytes() {
045: return _bytes;
046: }
047: }
048:
049: public static class SerializableByteArrayHolder implements
050: Serializable, IByteArrayHolder {
051:
052: private static final long serialVersionUID = 1L;
053:
054: public byte[] _bytes;
055:
056: public SerializableByteArrayHolder(byte[] bytes) {
057: this ._bytes = bytes;
058: }
059:
060: public byte[] getBytes() {
061: return _bytes;
062: }
063: }
064:
065: static final int INSTANCES = 2;
066:
067: static final int ARRAY_LENGTH = 1024 * 512;
068:
069: public void store() {
070: Test.close();
071:
072: com.db4o.Db4o.configure().objectClass(
073: SerializableByteArrayHolder.class).translate(
074: new TSerializable());
075: Test.open();
076:
077: for (int i = 0; i < INSTANCES; ++i) {
078: Test.store(new ByteArrayHolder(createByteArray()));
079: Test.store(new SerializableByteArrayHolder(
080: createByteArray()));
081: }
082: }
083:
084: public void testByteArrayHolder() {
085: timeQueryLoop("raw byte array", ByteArrayHolder.class);
086: }
087:
088: public void testSerializableByteArrayHolder() {
089: timeQueryLoop("TSerializable",
090: SerializableByteArrayHolder.class);
091: }
092:
093: private void timeQueryLoop(String label, final Class clazz) {
094:
095: Test.close();
096: Test.open();
097:
098: Query query = Test.query();
099: query.constrain(clazz);
100:
101: ObjectSet os = query.execute();
102: Test.ensure(INSTANCES == os.size());
103:
104: while (os.hasNext()) {
105: Test.ensure(ARRAY_LENGTH == ((IByteArrayHolder) os.next())
106: .getBytes().length);
107: }
108: }
109:
110: byte[] createByteArray() {
111: byte[] bytes = new byte[ARRAY_LENGTH];
112: for (int i = 0; i < bytes.length; ++i) {
113: bytes[i] = (byte) (i % 256);
114: }
115: return bytes;
116: }
117: }
|