01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.jre12.assorted;
22:
23: import com.db4o.*;
24: import com.db4o.config.*;
25: import com.db4o.foundation.io.*;
26: import com.db4o.query.*;
27: import com.db4o.reflect.*;
28: import com.db4o.reflect.generic.*;
29: import com.db4o.reflect.jdk.*;
30: import com.db4o.test.util.*;
31:
32: import db4ounit.*;
33:
34: public class GenericPrimitiveArrayTestCase implements TestCase {
35:
36: private static final byte[] BYTES = new byte[] { 1, 2 };
37:
38: public static class Data {
39: public byte[] _bytes;
40:
41: public Data(byte[] bytes) {
42: _bytes = bytes;
43: }
44: }
45:
46: public void testGenericPrimitiveArray() {
47: final String filePath = Path4.combine(Path4.getTempPath(),
48: "generic.db4o");
49: store(filePath);
50: ClassLoader loader = new ExcludingClassLoader(Data.class
51: .getClassLoader(), new Class[] { Data.class });
52: Configuration config = Db4o.newConfiguration();
53: config.reflectWith(new JdkReflector(loader));
54: ObjectContainer db = Db4o.openFile(config, filePath);
55: GenericReflector reflector = db.ext().reflector();
56: ReflectClass clazz = reflector.forName(Data.class.getName());
57: ReflectField field = clazz.getDeclaredField("_bytes");
58: Assert.isTrue(field.getFieldType().isArray());
59: Query query = db.query();
60: query.constrain(clazz);
61: ObjectSet result = query.execute();
62: Assert.areEqual(1, result.size());
63: Object retrieved = result.next();
64: Assert.areEqual(clazz, reflector.forObject(retrieved));
65: byte[] bytes = (byte[]) field.get(retrieved);
66: ArrayAssert.areEqual(BYTES, bytes);
67: db.close();
68: }
69:
70: private void store(final String filePath) {
71: File4.delete(filePath);
72: ObjectContainer db = Db4o.openFile(Db4o.newConfiguration(),
73: filePath);
74: db.set(new Data(BYTES));
75: db.close();
76: }
77:
78: public static void main(String[] args) {
79: new TestRunner(GenericPrimitiveArrayTestCase.class).run();
80: }
81: }
|