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.collections;
22:
23: import java.util.*;
24:
25: import com.db4o.*;
26: import com.db4o.config.*;
27: import com.db4o.io.*;
28: import com.db4o.query.*;
29:
30: import db4ounit.*;
31:
32: // see COR-714
33: public class ArrayListCandidatesTestCase implements TestCase {
34:
35: private static final String DB_ID = "in_memory";
36:
37: private static class DataHolder {
38: public ArrayList _data;
39:
40: private DataHolder() {
41: }
42:
43: public DataHolder(Object[] data) {
44: _data = new ArrayList(data.length);
45: for (int dataIdx = 0; dataIdx < data.length; dataIdx++) {
46: _data.add(data[dataIdx]);
47: }
48: }
49: }
50:
51: private static class DataA {
52: private String _a;
53:
54: public DataA(String a) {
55: _a = a;
56: }
57: }
58:
59: private static class DataB {
60: private String _b;
61:
62: public DataB(String b) {
63: _b = b;
64: }
65: }
66:
67: public void test() {
68: Configuration config = Db4o.newConfiguration();
69: config.io(new MemoryIoAdapter());
70: ObjectContainer db = Db4o.openFile(config, DB_ID);
71:
72: try {
73: storeObjects(db);
74: retrieveObjectByUsingConstraints(db);
75:
76: } finally {
77: db.close();
78: }
79: }
80:
81: private void storeObjects(ObjectContainer db) {
82: Object[] data = new Object[] { new DataA("A"), new DataB("B") };
83: DataHolder holder = new DataHolder(data);
84: db.set(holder);
85: }
86:
87: private void retrieveObjectByUsingConstraints(ObjectContainer db) {
88: Query query = db.query();
89: Constraint extentConstraint = query.constrain(DataHolder.class);
90: Constraint aConstraint = query.descend("_data").descend("_a")
91: .constrain("A");
92: Constraint bConstraint = query.descend("_data").descend("_b")
93: .constrain("B");
94: extentConstraint.and(aConstraint);
95: extentConstraint.and(bConstraint);
96:
97: query.execute();
98: }
99: }
|