KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > jre12 > fieldindex > CollectionFieldIndexTestCase


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.jre12.fieldindex;
22
23 import java.util.*;
24
25 import com.db4o.ObjectSet;
26 import com.db4o.config.*;
27 import com.db4o.query.Query;
28
29 import db4ounit.Assert;
30 import db4ounit.extensions.AbstractDb4oTestCase;
31
32 /**
33  * @exclude
34  */

35 public class CollectionFieldIndexTestCase extends AbstractDb4oTestCase {
36     
37     public static void main(String JavaDoc[] args) {
38         new CollectionFieldIndexTestCase().runSolo();
39     }
40     
41     private static class Item {
42         private String JavaDoc _name;
43         
44         public Item(String JavaDoc name) {
45             _name = name;
46         }
47         
48         public String JavaDoc getName() {
49             return _name;
50         }
51     }
52     
53     private static class UntypedContainer {
54         private Object JavaDoc _set = new HashSet();
55         
56         public UntypedContainer(Object JavaDoc item) {
57             ((Set)_set).add(item);
58         }
59         
60         public Iterator iterator() {
61             return ((Set)_set).iterator();
62         }
63     }
64     
65     protected void configure(Configuration config) {
66         indexField(config,Item.class, "_name");
67         indexField(config,UntypedContainer.class, "_set");
68     }
69     
70     protected void store() throws Exception JavaDoc {
71         db().set(new UntypedContainer(new Item("foo")));
72         db().set(new UntypedContainer(new Item("bar")));
73     }
74     
75     public void testUntypedContainer() {
76         final Query q = db().query();
77         q.constrain(UntypedContainer.class);
78         q.descend("_set").descend("_name").constrain("foo");
79         
80         final ObjectSet result = q.execute();
81         Assert.areEqual(1, result.size());
82         
83         final UntypedContainer container = (UntypedContainer)result.next();
84         final Item item = (Item)container.iterator().next();
85         Assert.areEqual("foo", item.getName());
86     }
87
88 }
89
Popular Tags