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.types.arrays;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.query.*;
028:
029: import db4ounit.*;
030: import db4ounit.extensions.*;
031:
032: public class ByteArrayTestCase extends AbstractDb4oTestCase {
033:
034: public static void main(String[] args) {
035: new ByteArrayTestCase().runAll();
036: }
037:
038: public static interface IByteArrayHolder {
039: byte[] getBytes();
040: }
041:
042: public static class SerializableByteArrayHolder implements
043: Serializable, IByteArrayHolder {
044:
045: private static final long serialVersionUID = 1L;
046:
047: byte[] _bytes;
048:
049: public SerializableByteArrayHolder(byte[] bytes) {
050: this ._bytes = bytes;
051: }
052:
053: public byte[] getBytes() {
054: return _bytes;
055: }
056: }
057:
058: public static class ByteArrayHolder implements IByteArrayHolder {
059:
060: public byte[] _bytes;
061:
062: public ByteArrayHolder(byte[] bytes) {
063: this ._bytes = bytes;
064: }
065:
066: public byte[] getBytes() {
067: return _bytes;
068: }
069: }
070:
071: static final int INSTANCES = 2;
072: static final int ARRAY_LENGTH = 1024;
073:
074: /**
075: * @sharpen.if !CF_1_0 && !CF_2_0
076: */
077: protected void configure(Configuration config) {
078: config.objectClass(SerializableByteArrayHolder.class)
079: .translate(new TSerializable());
080: }
081:
082: protected void store() {
083: for (int i = 0; i < INSTANCES; ++i) {
084: db().set(new ByteArrayHolder(createByteArray()));
085: db()
086: .set(
087: new SerializableByteArrayHolder(
088: createByteArray()));
089: }
090: }
091:
092: /**
093: * @sharpen.if !CF_1_0 && !CF_2_0
094: */
095: public void testByteArrayHolder() throws Exception {
096: timeQueryLoop("raw byte array", ByteArrayHolder.class);
097: }
098:
099: /**
100: * @sharpen.if !CF_1_0 && !CF_2_0
101: */
102: public void testSerializableByteArrayHolder() throws Exception {
103: timeQueryLoop("TSerializable",
104: SerializableByteArrayHolder.class);
105: }
106:
107: private void timeQueryLoop(String label, final Class clazz)
108: throws Exception {
109: Query query = newQuery(clazz);
110: ObjectSet os = query.execute();
111: Assert.areEqual(INSTANCES, os.size());
112:
113: while (os.hasNext()) {
114: Assert.areEqual(ARRAY_LENGTH,
115: ((IByteArrayHolder) os.next()).getBytes().length,
116: label);
117: }
118: }
119:
120: byte[] createByteArray() {
121: byte[] bytes = new byte[ARRAY_LENGTH];
122: for (int i = 0; i < bytes.length; ++i) {
123: bytes[i] = (byte) (i % 256);
124: }
125: return bytes;
126: }
127: }
|