KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > types > arrays > ByteArrayTestCase


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.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     /**
70      * @sharpen.if !CF_1_0 && !CF_2_0
71      */

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     /**
84      * @sharpen.if !CF_1_0 && !CF_2_0
85      */

86     public void testByteArrayHolder() throws Exception JavaDoc {
87         timeQueryLoop("raw byte array", ByteArrayHolder.class);
88     }
89     
90     /**
91      * @sharpen.if !CF_1_0 && !CF_2_0
92      */

93     public void testSerializableByteArrayHolder() throws Exception JavaDoc {
94         timeQueryLoop("TSerializable", SerializableByteArrayHolder.class);
95     }
96
97     private void timeQueryLoop(String JavaDoc label, final Class JavaDoc clazz) throws Exception JavaDoc {
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