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.querying;
022:
023: import com.db4o.*;
024: import com.db4o.internal.*;
025: import com.db4o.query.Query;
026:
027: import db4ounit.Assert;
028: import db4ounit.extensions.AbstractDb4oTestCase;
029:
030: /**
031: * @exclude
032: */
033: public class ObjectSetTestCase extends AbstractDb4oTestCase {
034:
035: public static void main(String[] args) {
036: new ObjectSetTestCase().runSoloAndClientServer();
037: }
038:
039: public static class Item {
040: public String name;
041:
042: public Item() {
043: }
044:
045: public Item(String name_) {
046: name = name_;
047: }
048:
049: public String toString() {
050: return "Item(\"" + name + "\")";
051: }
052: }
053:
054: protected void store() throws Exception {
055: db().set(new Item("foo"));
056: db().set(new Item("bar"));
057: db().set(new Item("baz"));
058: }
059:
060: public void testObjectsCantBeSeenAfterDelete() {
061: final Transaction trans1 = newTransaction();
062: final Transaction trans2 = newTransaction();
063: final ObjectSet os = queryItems(trans1);
064: deleteItemAndCommit(trans2, "foo");
065: assertItems(new String[] { "bar", "baz" }, os);
066: }
067:
068: public void testAccessOrder() {
069: ObjectSet result = newQuery(Item.class).execute();
070: for (int i = 0; i < result.size(); ++i) {
071: Assert.isTrue(result.hasNext());
072: Assert.areSame(result.ext().get(i), result.next());
073: }
074: Assert.isFalse(result.hasNext());
075: }
076:
077: private void assertItems(String[] expectedNames, ObjectSet actual) {
078: for (int i = 0; i < expectedNames.length; i++) {
079: Assert.isTrue(actual.hasNext());
080: Assert.areEqual(expectedNames[i],
081: ((Item) actual.next()).name);
082: }
083: Assert.isFalse(actual.hasNext());
084: }
085:
086: private void deleteItemAndCommit(Transaction trans, String name) {
087: stream().delete(trans, queryItem(trans, name));
088: trans.commit();
089: }
090:
091: private Item queryItem(Transaction trans, String name) {
092: final Query q = newQuery(trans, Item.class);
093: q.descend("name").constrain(name);
094: return (Item) q.execute().next();
095: }
096:
097: private ObjectSet queryItems(final Transaction trans) {
098: final Query q = newQuery(trans, Item.class);
099: q.descend("name").orderAscending();
100: return q.execute();
101: }
102:
103: }
|