1 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 32 public class ObjectSetTestCase extends AbstractDb4oTestCase { 33 34 public static void main(String [] args) { 35 new ObjectSetTestCase().runSoloAndClientServer(); 36 } 37 38 public static class Item { 39 public String name; 40 41 public Item() { 42 } 43 44 public Item(String name_) { 45 name = name_; 46 } 47 48 public String toString() { 49 return "Item(\"" + name + "\")"; 50 } 51 } 52 53 protected void store() throws Exception { 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 [] { "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 [] 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 name) { 85 stream().delete(trans, queryItem(trans, name)); 86 trans.commit(); 87 } 88 89 private Item queryItem(Transaction trans, String 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 |