KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > fieldindex > StringIndexTestCase


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.fieldindex;
22
23 import com.db4o.*;
24 import com.db4o.config.*;
25 import com.db4o.db4ounit.common.btree.ExpectingVisitor;
26 import com.db4o.foundation.Visitor4;
27 import com.db4o.inside.freespace.*;
28 import com.db4o.inside.slots.Slot;
29 import com.db4o.query.Query;
30
31 import db4ounit.Assert;
32 import db4ounit.extensions.AbstractDb4oTestCase;
33 import db4ounit.extensions.fixtures.*;
34
35 /**
36  * @exclude
37  */

38 public class StringIndexTestCase extends AbstractDb4oTestCase implements OptOutCS{
39     
40     public static void main(String JavaDoc[] args) {
41         new StringIndexTestCase().runSolo();
42     }
43     
44     public static class Item {
45         public String JavaDoc name;
46         
47         public Item() {
48         }
49
50         public Item(String JavaDoc name_) {
51             name = name_;
52         }
53     }
54     
55     protected void configure(Configuration config){
56         indexField(config,Item.class, "name");
57     }
58     
59     public void testNotEquals() {
60         add("foo");
61         add("bar");
62         add("baz");
63         add(null);
64         
65         final Query query = newQuery(Item.class);
66         query.descend("name").constrain("bar").not();
67         assertItems(new String JavaDoc[] { "foo", "baz", null }, query.execute());
68     }
69
70     private void assertItems(final String JavaDoc[] expected, final ObjectSet result) {
71         final ExpectingVisitor expectingVisitor = new ExpectingVisitor(toObjectArray(expected));
72         while (result.hasNext()) {
73             expectingVisitor.visit(((Item)result.next()).name);
74         }
75         expectingVisitor.assertExpectations();
76     }
77     
78     private Object JavaDoc[] toObjectArray(String JavaDoc[] source) {
79         Object JavaDoc[] array = new Object JavaDoc[source.length];
80         System.arraycopy(source, 0, array, 0, source.length);
81         return array;
82     }
83
84     public void testCancelRemovalRollback() throws Exception JavaDoc {
85         
86         prepareCancelRemoval(trans(), "original");
87         rename("original", "updated");
88         db().rollback();
89         grafittiFreeSpace();
90         reopen();
91         
92         assertExists("original");
93     }
94     
95     public void testCancelRemovalRollbackForMultipleTransactions() throws Exception JavaDoc {
96         final Transaction trans1 = newTransaction();
97         final Transaction trans2 = newTransaction();
98         
99         prepareCancelRemoval(trans1, "original");
100         assertExists(trans2, "original");
101         
102         trans1.rollback();
103         assertExists(trans2, "original");
104         
105         add(trans2, "second");
106         assertExists(trans2, "original");
107         
108         trans2.commit();
109         assertExists(trans2, "original");
110         
111         grafittiFreeSpace();
112         reopen();
113         assertExists("original");
114     }
115     
116     public void testCancelRemoval() throws Exception JavaDoc {
117         prepareCancelRemoval(trans(), "original");
118         db().commit();
119         grafittiFreeSpace();
120         reopen();
121         
122         assertExists("original");
123     }
124
125     private void prepareCancelRemoval(Transaction transaction, String JavaDoc itemName) {
126         add(itemName);
127         db().commit();
128         
129         rename(transaction, itemName, "updated");
130         assertExists(transaction, "updated");
131         
132         rename(transaction, "updated", itemName);
133         assertExists(transaction, itemName);
134     }
135     
136     public void testCancelRemovalForMultipleTransactions() throws Exception JavaDoc {
137         final Transaction trans1 = newTransaction();
138         final Transaction trans2 = newTransaction();
139         
140         prepareCancelRemoval(trans1, "original");
141         rename(trans2, "original", "updated");
142         trans1.commit();
143         grafittiFreeSpace();
144         reopen();
145         
146         assertExists("original");
147     }
148     
149     private void grafittiFreeSpace() {
150         final YapRandomAccessFile file = ((YapRandomAccessFile)db());
151         final FreespaceManagerRam fm = (FreespaceManagerRam) file.freespaceManager();
152         fm.traverseFreeSlots(new Visitor4() {
153             public void visit(Object JavaDoc obj) {
154                 Slot slot = (Slot) obj;
155                 file.writeXBytes(slot.getAddress(), slot.getLength());
156             }
157         });
158     }
159
160     public void testDeletingAndReaddingMember() throws Exception JavaDoc{
161         add("original");
162         assertExists("original");
163         rename("original", "updated");
164         assertExists("updated");
165         Assert.isNull(query("original"));
166         reopen();
167         assertExists("updated");
168         Assert.isNull(query("original"));
169     }
170
171     private void assertExists(String JavaDoc itemName) {
172         assertExists(trans(), itemName);
173     }
174
175     private void add(final String JavaDoc itemName) {
176         add(trans(), itemName);
177     }
178     
179     private void add(Transaction transaction, String JavaDoc itemName) {
180         stream().set(transaction, new Item(itemName));
181     }
182     
183     private void assertExists(Transaction transaction, String JavaDoc itemName) {
184         Assert.isNotNull(query(transaction, itemName));
185     }
186
187     private void rename(Transaction transaction, String JavaDoc from, String JavaDoc to) {
188         final Item item = query(transaction, from);
189         Assert.isNotNull(item);
190         item.name = to;
191         stream().set(transaction, item);
192     }
193
194     private void rename(String JavaDoc from, String JavaDoc to) {
195         rename(trans(), from, to);
196     }
197
198     private Item query(String JavaDoc name){
199         return query(trans(), name);
200     }
201     
202     private Item query(Transaction transaction, String JavaDoc name) {
203         ObjectSet objectSet = newQuery(transaction, name).execute();
204         if (!objectSet.hasNext()) {
205             return null;
206         }
207         return (Item) objectSet.next();
208     }
209
210     private Query newQuery(Transaction transaction, String JavaDoc itemName) {
211         final Query query = stream().query(transaction);
212         query.constrain(Item.class);
213         query.descend("name").constrain(itemName);
214         return query;
215     }
216 }
217
Popular Tags