public void testTransactionConsistentVersion() throws Exception {

    store(new Item(NEWER));
    db().commit();

    Item newer = itemByName(NEWER);
    Item original = itemByName(ORIGINAL);

    Assert.isGreater(version(original), version(newer));

    newer._name += " modified";
    original._name += " modified";

    store(newer);
    store(original);

    db().commit();

    Assert.areEqual(version(newer), version(original));

    reopen();

    newer = itemByName(newer._name);
    original = itemByName(original._name);

    Assert.areEqual(version(newer), version(original));
  }
  public void test() throws Exception {

    store(new Original("original"));

    db().commit();

    Assert.areEqual(1, countOccurences(Original.class));

    // Rename messages are visible at level 1
    // fixture().config().messageLevel(1);

    ObjectClass oc = fixture().config().objectClass(Original.class);

    // allways rename fields first
    oc.objectField("originalName").rename("changedName");
    // we must use ReflectPlatform here as the string must include
    // the assembly name in .net
    oc.rename(CrossPlatformServices.fullyQualifiedName(Changed.class));

    reopen();

    Assert.areEqual(0, countOccurences(Original.class));
    Assert.areEqual(1, countOccurences(Changed.class));

    Changed changed = (Changed) retrieveOnlyInstance(Changed.class);

    Assert.areEqual("original", changed.changedName);
  }
  public void testUpdateAndQuery() {

    Item item1 = new Item();
    store(item1);
    Item item2 = new Item();
    store(item2);
    commit();

    long initialCommitTimestamp1 = db().getObjectInfo(item1).getCommitTimestamp();
    long initialCommitTimestamp2 = db().getObjectInfo(item2).getCommitTimestamp();

    Assert.areEqual(initialCommitTimestamp1, initialCommitTimestamp2);

    store(item2);
    commit();

    long secondCommitTimestamp1 = db().getObjectInfo(item1).getCommitTimestamp();
    long secondCommitTimestamp2 = db().getObjectInfo(item2).getCommitTimestamp();

    Assert.areEqual(initialCommitTimestamp1, secondCommitTimestamp1);
    Assert.areNotEqual(initialCommitTimestamp2, secondCommitTimestamp2);

    assertQueryForTimestamp(item1, initialCommitTimestamp1);
    assertQueryForTimestamp(item2, secondCommitTimestamp2);
  }
 public void testAllThere() throws Exception {
   for (int i = 0; i < FOOS.length; i++) {
     Query q = createQuery(FOOS[i]);
     ObjectSet objectSet = q.execute();
     Assert.areEqual(1, objectSet.size());
     FieldIndexItem fii = (FieldIndexItem) objectSet.next();
     Assert.areEqual(FOOS[i], fii.foo);
   }
 }
 public void testObjectReferenceCount() {
   int objectCount = 10;
   Item[] items = new Item[objectCount];
   for (int i = 0; i < objectCount; i++) {
     Assert.areEqual(referenceCountForDb4oDatabase() + i, objectReferenceCount());
     items[i] = new Item();
     store(items[i]);
   }
   db().purge(items[0]);
   Assert.areEqual(referenceCountForDb4oDatabase() + objectCount - 1, objectReferenceCount());
 }
 private void tUpdateB(ExtObjectContainer oc) {
   ObjectSet res = query(oc, "b");
   CreateIndexFor ci = (CreateIndexFor) res.next();
   ci.i_name = "j";
   oc.store(ci);
   res = query(oc, "b");
   Assert.areEqual(0, res.size());
   res = query(oc, "j");
   Assert.areEqual(1, res.size());
   ci.i_name = "b";
   oc.store(ci);
   tQueryB(oc);
 }
 private void tExpectInts(Query q, int[] ints, int zeroSize) {
   ObjectSet res = q.execute();
   Assert.areEqual(ints.length + zeroSize, res.size());
   while (res.hasNext()) {
     CreateIndexFor ci = (CreateIndexFor) res.next();
     for (int i = 0; i < ints.length; i++) {
       if (ints[i] == ci.i_int) {
         ints[i] = 0;
         break;
       }
     }
   }
   for (int i = 0; i < ints.length; i++) {
     Assert.areEqual(0, ints[i]);
   }
 }
 public void checkUpdate(ExtObjectContainer oc) {
   Query q = oc.query();
   q.constrain(IndexedByIdentityTestCase.class);
   ObjectSet os = q.execute();
   Assert.areEqual(COUNT, os.size());
   String expected = null;
   while (os.hasNext()) {
     IndexedByIdentityTestCase idi = (IndexedByIdentityTestCase) os.next();
     if (expected == null) {
       expected = idi.atom.name;
       Assert.isTrue(expected.startsWith("updated"));
       Assert.isTrue(expected.length() > "updated".length());
     }
     Assert.areEqual(expected, idi.atom.name);
   }
 }
 public void test() {
   SubClass subClass = retrieveOnlyInstance(SubClass.class);
   db().delete(subClass);
   db().commit();
   ObjectSet objectSet = db().query(Member.class);
   Assert.areEqual(0, objectSet.size());
 }
 public void concRead(ExtObjectContainer oc) {
   for (int i = 0; i < COUNT; i++) {
     Query q = oc.query();
     q.constrain(Atom.class);
     q.descend("name").constrain("ibi" + i);
     ObjectSet objectSet = q.execute();
     Assert.areEqual(1, objectSet.size());
     Atom child = (Atom) objectSet.next();
     q = oc.query();
     q.constrain(IndexedByIdentityTestCase.class);
     q.descend("atom").constrain(child).identity();
     objectSet = q.execute();
     Assert.areEqual(1, objectSet.size());
     IndexedByIdentityTestCase ibi = (IndexedByIdentityTestCase) objectSet.next();
     Assert.areSame(child, ibi.atom);
   }
 }
Beispiel #11
0
 public void _test() throws Exception {
   Query query = newQuery();
   query.constrain(new TestEvaluation());
   query.constrain(Item.class);
   query.descend("name").orderDescending();
   ObjectSet set = query.execute();
   Assert.areEqual(3, set.size());
 }
 private void assertQueryForTimestamp(Item expected, long timestamp) {
   Query query = db().query();
   query.constrain(Item.class);
   query.descend(VirtualField.COMMIT_TIMESTAMP).constrain(timestamp);
   ObjectSet<Object> objectSet = query.execute();
   Assert.areEqual(1, objectSet.size());
   Item actual = (Item) objectSet.next();
   Assert.areSame(expected, actual);
 }
Beispiel #13
0
  private void assertStoredField(
      Class objectClass,
      String fieldName,
      final Object expectedFieldValue,
      Class expectedFieldType,
      boolean hasIndex,
      boolean isArray) {
    StoredClass storedClass = storedClass(objectClass);
    StoredField[] storedFields = storedClass.getStoredFields();
    Assert.areEqual(1, storedFields.length);
    final StoredField storedField = storedFields[0];
    Assert.areEqual(fieldName, storedField.getName());
    StoredField storedFieldByName = storedClass.storedField(fieldName, expectedFieldType);
    Assert.areEqual(storedField, storedFieldByName);

    Object item = retrieveOnlyInstance(objectClass);
    Assert.areEqual(expectedFieldValue, storedField.get(item));
    ReflectClass fieldType = storedField.getStoredType();
    Assert.areEqual(reflector().forClass(expectedFieldType), fieldType);
    Assert.areEqual(isArray, storedField.isArray());

    if (isMultiSession()) {
      return;
    }

    Assert.areEqual(hasIndex, storedField.hasIndex());

    // FIXME: test rename

    if (!hasIndex) {
      Assert.expect(
          RuntimeException.class,
          new CodeBlock() {
            public void run() throws Throwable {
              storedField.traverseValues(
                  new Visitor4() {
                    public void visit(Object obj) {}
                  });
            }
          });
    } else {
      final IntByRef count = new IntByRef();
      storedField.traverseValues(
          new Visitor4() {
            public void visit(Object obj) {
              count.value++;
              Assert.areEqual(expectedFieldValue, obj);
            }
          });
      Assert.areEqual(1, count.value);
    }
  }
 public void concUpdate(ExtObjectContainer oc, int seq) throws Exception {
   Query q = oc.query();
   q.constrain(IndexedByIdentityTestCase.class);
   ObjectSet os = q.execute();
   Assert.areEqual(COUNT, os.size());
   while (os.hasNext()) {
     IndexedByIdentityTestCase idi = (IndexedByIdentityTestCase) os.next();
     idi.atom.name = "updated" + seq;
     oc.store(idi);
     Thread.sleep(100);
   }
 }
  public void concDelete(ExtObjectContainer oc) throws Exception {
    int size = countOccurences(oc, SimpleObject.class);
    if (size == 0) { // already deleted
      return;
    }
    Assert.areEqual(ELEMENT_COUNT, size);

    ObjectSet os = oc.query(ArrayItem.class);
    if (os.size() == 0) { // already deteled
      return;
    }
    Assert.areEqual(1, os.size());
    ArrayItem item = (ArrayItem) os.next();

    // waits for other threads
    Thread.sleep(500);
    oc.delete(item);

    oc.commit();

    assertOccurrences(oc, SimpleObject.class, 0);
  }
  public void testQueryForVersionNumber() {
    store(new Item(NEWER));
    db().commit();

    Item newer = itemByName(NEWER);

    long version = version(newer);

    Query query = db().query();
    query.descend(VirtualField.COMMIT_TIMESTAMP).constrain(new Long(version)).smaller().not();
    ObjectSet<Item> set = query.execute();
    Assert.areEqual(1, set.size());
    Assert.areSame(newer, set.next());
  }
  public <T, T1> void querydb(
      EmbeddedConfiguration config, int[] count, Class<T> class1, Class<T1> class2) {
    EmbeddedObjectContainer database = Db4oEmbedded.openFile(config, tempFile());
    try {
      List<T> list = database.query(class1);
      Assert.areEqual(
          count[0], list.size(), "Unexpected result querying for " + class1.getSimpleName());
      if (count[0] > 0) {
        // System.out.println("Querying for " + class1.getSimpleName() + " getting " +
        // list.get(0).getClass().getSimpleName());
      }

      List<T1> list1 = database.query(class2);
      Assert.areEqual(
          count[1], list1.size(), "Unexpected result querying for " + class2.getSimpleName());
      if (count[1] > 0) {
        // System.out.println("Querying for " + class2.getSimpleName() + " getting " +
        // list1.get(0).getClass().getSimpleName());
      }

    } finally {
      database.close();
    }
  }
  public void test() throws Exception {
    int total = 10;
    ExtObjectContainer[] containers = new ExtObjectContainer[total];
    ExtObjectContainer container = null;
    try {
      for (int i = 0; i < total; i++) {
        containers[i] = openNewSession();
        Assert.areEqual(ELEMENT_COUNT, countOccurences(containers[i], SimpleObject.class));
      }

      for (int i = 0; i < total; i++) {
        deleteAll(containers[i], SimpleObject.class);
      }

      container = openNewSession();

      assertOccurrences(container, SimpleObject.class, ELEMENT_COUNT);
      // ocs[0] deletes all SimpleObject
      containers[0].commit();
      containers[0].close();
      // FIXME: the following assertion fails
      assertOccurrences(container, SimpleObject.class, 0);
      for (int i = 1; i < total; i++) {
        containers[i].close();
      }
      assertOccurrences(container, SimpleObject.class, 0);
    } finally {
      if (container != null) {
        container.close();
      }
      for (int i = 0; i < total; i++) {
        if (containers[i] != null) {
          containers[i].close();
        }
      }
    }
  }
 private void assertData() {
   Holder holder = (Holder) retrieveOnlyInstance(Holder.class);
   Assert.isNotNull(holder._item);
   Assert.areEqual(NAME, holder._item._name);
 }
 private void assertNoItemFoundByQuery() {
   Query q = db().query();
   q.constrain(Item.class);
   Assert.areEqual(0, q.execute().size());
 }
 public void conc(ExtObjectContainer oc) {
   ht = (Hashtable) retrieveOnlyInstance(oc, Hashtable.class);
   C2C c2c = (C2C) ht.get("test");
   Assert.areEqual("parent", c2c.parent.name);
 }
 private void assertSingleElementQueryResult(Query query, Object element) {
   ObjectSet objectSet = query.execute();
   Assert.areEqual(1, objectSet.size());
   Item item = (Item) objectSet.next();
   Assert.areEqual(element, item.list.get(0));
 }
 private void assertInstanceOK(ABar bar) {
   Assert.areEqual("foo", bar.foo);
   Assert.areEqual("bar", bar.bar);
 }
 private void assertName(EmbeddedConfiguration config, String expected) {
   EmbeddedObjectContainer db = Db4oEmbedded.openFile(config, FILE_NAME);
   Assert.areEqual(expected, db.toString());
   db.close();
 }
  private void tQueryInts(ExtObjectContainer oc, int expectedZeroSize) {
    Query q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(0));
    int zeroSize = q.execute().size();
    Assert.areEqual(expectedZeroSize, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(4)).greater().equal();
    tExpectInts(q, new int[] {5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(4)).greater();
    tExpectInts(q, new int[] {5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(3)).greater();
    tExpectInts(q, new int[] {5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(3)).greater().equal();
    tExpectInts(q, new int[] {3, 3, 5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(2)).greater().equal();
    tExpectInts(q, new int[] {2, 3, 3, 5, 7});
    q = oc.query();

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(2)).greater();
    tExpectInts(q, new int[] {3, 3, 5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(1)).greater().equal();
    tExpectInts(q, new int[] {1, 2, 3, 3, 5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(1)).greater();
    tExpectInts(q, new int[] {2, 3, 3, 5, 7});

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(4)).smaller();
    tExpectInts(q, new int[] {1, 2, 3, 3}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(4)).smaller().equal();
    tExpectInts(q, new int[] {1, 2, 3, 3}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(3)).smaller();
    tExpectInts(q, new int[] {1, 2}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(3)).smaller().equal();
    tExpectInts(q, new int[] {1, 2, 3, 3}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(2)).smaller().equal();
    tExpectInts(q, new int[] {1, 2}, zeroSize);
    q = oc.query();

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(2)).smaller();
    tExpectInts(q, new int[] {1}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(1)).smaller().equal();
    tExpectInts(q, new int[] {1}, zeroSize);

    q = oc.query();
    q.constrain(CreateIndexFor.class);
    q.descend("i_int").constrain(new Integer(1)).smaller();
    tExpectInts(q, new int[] {}, zeroSize);
  }
 private void tQueryB(ExtObjectContainer oc) {
   ObjectSet res = query(oc, "b");
   Assert.areEqual(1, res.size());
   CreateIndexFor ci = (CreateIndexFor) res.next();
   Assert.areEqual("b", ci.i_name);
 }
Beispiel #27
0
 public void testGetIds() {
   StoredClass itemClass = itemStoredClass();
   long[] ids = itemClass.getIDs();
   Assert.areEqual(1, ids.length);
   Assert.areEqual(_id, ids[0]);
 }
Beispiel #28
0
 public void testGetName() {
   StoredClass itemClass = itemStoredClass();
   Assert.areEqual(reflector().forClass(Item.class).getName(), itemClass.getName());
 }
Beispiel #29
0
 public void testGetParentStoredClass() {
   StoredClass itemClass = itemStoredClass();
   StoredClass parentStoredClass = itemClass.getParentStoredClass();
   Assert.areEqual(reflector().forClass(ItemParent.class).getName(), parentStoredClass.getName());
   Assert.areEqual(parentStoredClass, db().storedClass(ItemParent.class));
 }