1 21 package com.db4o.db4ounit.common.types.arrays; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.config.*; 27 import com.db4o.query.*; 28 29 import db4ounit.*; 30 import db4ounit.extensions.*; 31 32 public class ByteArrayTestCase extends AbstractDb4oTestCase { 33 34 public static interface IByteArrayHolder { 35 byte[] getBytes(); 36 } 37 38 public static class SerializableByteArrayHolder implements Serializable, IByteArrayHolder { 39 40 private static final long serialVersionUID = 1L; 41 42 byte[] _bytes; 43 44 public SerializableByteArrayHolder(byte[] bytes) { 45 this._bytes = bytes; 46 } 47 48 public byte[] getBytes() { 49 return _bytes; 50 } 51 } 52 53 public static class ByteArrayHolder implements IByteArrayHolder { 54 55 public byte[] _bytes; 56 57 public ByteArrayHolder(byte[] bytes) { 58 this._bytes = bytes; 59 } 60 61 public byte[] getBytes() { 62 return _bytes; 63 } 64 } 65 66 static final int INSTANCES = 2; 67 static final int ARRAY_LENGTH = 1024*512; 68 69 72 protected void configure(Configuration config) { 73 config.objectClass(SerializableByteArrayHolder.class).translate(new TSerializable()); 74 } 75 76 protected void store() { 77 for (int i=0; i<INSTANCES; ++i) { 78 db().set(new ByteArrayHolder(createByteArray())); 79 db().set(new SerializableByteArrayHolder(createByteArray())); 80 } 81 } 82 83 86 public void testByteArrayHolder() throws Exception { 87 timeQueryLoop("raw byte array", ByteArrayHolder.class); 88 } 89 90 93 public void testSerializableByteArrayHolder() throws Exception { 94 timeQueryLoop("TSerializable", SerializableByteArrayHolder.class); 95 } 96 97 private void timeQueryLoop(String label, final Class clazz) throws Exception { 98 Query query = newQuery(clazz); 99 ObjectSet os = query.execute(); 100 Assert.areEqual(INSTANCES, os.size()); 101 102 while (os.hasNext()) { 103 Assert.areEqual(ARRAY_LENGTH, ((IByteArrayHolder) os.next()) 104 .getBytes().length, label); 105 } 106 } 107 108 byte[] createByteArray() { 109 byte[] bytes = new byte[ARRAY_LENGTH]; 110 for (int i=0; i<bytes.length; ++i) { 111 bytes[i] = (byte)(i % 256); 112 } 113 return bytes; 114 } 115 } 116 | Popular Tags |