KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > querying > ObjectSetTestCase


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.querying;
22
23 import com.db4o.*;
24 import com.db4o.query.Query;
25
26 import db4ounit.Assert;
27 import db4ounit.extensions.AbstractDb4oTestCase;
28
29 /**
30  * @exclude
31  */

32 public class ObjectSetTestCase extends AbstractDb4oTestCase {
33     
34     public static void main(String JavaDoc[] args) {
35         new ObjectSetTestCase().runSoloAndClientServer();
36     }
37     
38     public static class Item {
39         public String JavaDoc name;
40         
41         public Item() {
42         }
43         
44         public Item(String JavaDoc name_) {
45             name = name_;
46         }
47         
48         public String JavaDoc toString() {
49             return "Item(\"" + name + "\")";
50         }
51     }
52     
53     protected void store() throws Exception JavaDoc {
54         db().set(new Item("foo"));
55         db().set(new Item("bar"));
56         db().set(new Item("baz"));
57     }
58     
59     public void testObjectsCantBeSeenAfterDelete() {
60         final Transaction trans1 = newTransaction();
61         final Transaction trans2 = newTransaction();
62         final ObjectSet os = queryItems(trans1);
63         deleteItemAndCommit(trans2, "foo");
64         assertItems(new String JavaDoc[] { "bar", "baz" }, os);
65     }
66     
67     public void testAccessOrder() {
68         ObjectSet result = newQuery(Item.class).execute();
69         for (int i=0; i < result.size(); ++i) {
70             Assert.isTrue(result.hasNext());
71             Assert.areSame(result.ext().get(i), result.next());
72         }
73         Assert.isFalse(result.hasNext());
74     }
75
76     private void assertItems(String JavaDoc[] expectedNames, ObjectSet actual) {
77         for (int i = 0; i < expectedNames.length; i++) {
78             Assert.isTrue(actual.hasNext());
79             Assert.areEqual(expectedNames[i], ((Item)actual.next()).name);
80         }
81         Assert.isFalse(actual.hasNext());
82     }
83
84     private void deleteItemAndCommit(Transaction trans, String JavaDoc name) {
85         stream().delete(trans, queryItem(trans, name));
86         trans.commit();
87     }
88
89     private Item queryItem(Transaction trans, String JavaDoc name) {
90         final Query q = newQuery(trans, Item.class);
91         q.descend("name").constrain(name);
92         return (Item) q.execute().next();
93     }
94
95     private ObjectSet queryItems(final Transaction trans) {
96         final Query q = newQuery(trans, Item.class);
97         q.descend("name").orderAscending();
98         return q.execute();
99     }
100
101 }
102
Popular Tags