001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.fieldindex;
022:
023: import com.db4o.ObjectSet;
024: import com.db4o.config.Configuration;
025: import com.db4o.db4ounit.common.btree.ExpectingVisitor;
026: import com.db4o.foundation.Visitor4;
027: import com.db4o.internal.*;
028: import com.db4o.internal.freespace.*;
029: import com.db4o.internal.slots.Slot;
030: import com.db4o.query.Query;
031:
032: import db4ounit.Assert;
033: import db4ounit.extensions.AbstractDb4oTestCase;
034:
035: /**
036: * @exclude
037: */
038: public abstract class StringIndexTestCaseBase extends
039: AbstractDb4oTestCase {
040:
041: public static class Item {
042: public String name;
043:
044: public Item() {
045: }
046:
047: public Item(String name_) {
048: name = name_;
049: }
050: }
051:
052: public StringIndexTestCaseBase() {
053: super ();
054: }
055:
056: protected void configure(Configuration config) {
057: indexField(config, Item.class, "name");
058: }
059:
060: protected void assertItems(final String[] expected,
061: final ObjectSet result) {
062: final ExpectingVisitor expectingVisitor = new ExpectingVisitor(
063: toObjectArray(expected));
064: while (result.hasNext()) {
065: expectingVisitor.visit(((Item) result.next()).name);
066: }
067: expectingVisitor.assertExpectations();
068: }
069:
070: protected Object[] toObjectArray(String[] source) {
071: Object[] array = new Object[source.length];
072: System.arraycopy(source, 0, array, 0, source.length);
073: return array;
074: }
075:
076: protected void grafittiFreeSpace() {
077: final IoAdaptedObjectContainer file = ((IoAdaptedObjectContainer) db());
078: final FreespaceManager fm = file.freespaceManager();
079: fm.traverse(new Visitor4() {
080: public void visit(Object obj) {
081: Slot slot = (Slot) obj;
082: file.overwriteDeletedBlockedSlot(slot);
083: }
084: });
085: }
086:
087: protected void assertExists(String itemName) {
088: assertExists(trans(), itemName);
089: }
090:
091: protected void add(final String itemName) {
092: add(trans(), itemName);
093: }
094:
095: protected void add(Transaction transaction, String itemName) {
096: stream().set(transaction, new Item(itemName));
097: }
098:
099: protected void assertExists(Transaction transaction, String itemName) {
100: Assert.isNotNull(query(transaction, itemName));
101: }
102:
103: protected void rename(Transaction transaction, String from,
104: String to) {
105: final Item item = query(transaction, from);
106: Assert.isNotNull(item);
107: item.name = to;
108: stream().set(transaction, item);
109: }
110:
111: protected void rename(String from, String to) {
112: rename(trans(), from, to);
113: }
114:
115: protected Item query(String name) {
116: return query(trans(), name);
117: }
118:
119: protected Item query(Transaction transaction, String name) {
120: ObjectSet objectSet = newQuery(transaction, name).execute();
121: if (!objectSet.hasNext()) {
122: return null;
123: }
124: return (Item) objectSet.next();
125: }
126:
127: protected Query newQuery(Transaction transaction, String itemName) {
128: final Query query = stream().query(transaction);
129: query.constrain(Item.class);
130: query.descend("name").constrain(itemName);
131: return query;
132: }
133:
134: }
|