KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > ByteArray


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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