Beispiel #1
0
 public void testOne() {
   ExtObjectContainer oc = Test.objectContainer();
   StoredClass sc = oc.storedClass(this);
   StoredField[] sf = sc.getStoredFields();
   boolean[] cases = new boolean[3];
   for (int i = 0; i < sf.length; i++) {
     StoredField f = sf[i];
     if (f.getName().equals("foo")) {
       Test.ensure(f.get(this).equals("foo"));
       Test.ensure(f.getStoredType().getName().equals(String.class.getName()));
       cases[0] = true;
     }
     if (f.getName().equals("bar")) {
       Test.ensure(f.get(this).equals(new Integer(10)));
       Test.ensure(f.getStoredType().getName().equals(int.class.getName()));
       cases[1] = true;
     }
     if (f.getName().equals("atoms")) {
       Test.ensure(f.getStoredType().getName().equals(Atom.class.getName()));
       Test.ensure(f.isArray());
       Atom[] at = (Atom[]) f.get(this);
       Test.ensure(at[0].name.equals("one"));
       Test.ensure(at[1].name.equals("two"));
       cases[2] = true;
     }
   }
   for (int i = 0; i < cases.length; i++) {
     Test.ensure(cases[i]);
   }
 }
 public void conc2(ExtObjectContainer oc) {
   oc.store(new CreateIndexFor("d"));
   tQueryB(oc);
   tUpdateB(oc);
   oc.store(new CreateIndexFor("z"));
   oc.store(new CreateIndexFor("y"));
 }
 void checkHelper(Db4oHashMapHelper helper) {
   ExtObjectContainer con = db();
   if (con.isActive(helper)) {
     Assert.areEqual("hi", helper.i_childList.get(0));
     checkHelper(helper.i_child);
   }
 }
  /*
   * delete - set - commit set - commit delete
   */
  public void testSD() {
    ExtObjectContainer oc1 = openNewSession();
    ExtObjectContainer oc2 = openNewSession();
    ExtObjectContainer oc3 = openNewSession();
    try {
      SimpleObject o1 = (SimpleObject) retrieveOnlyInstance(oc1, SimpleObject.class);
      oc1.delete(o1);
      SimpleObject o2 = (SimpleObject) retrieveOnlyInstance(oc2, SimpleObject.class);
      Assert.areEqual("hello", o2.getS());
      o2.setS("o2");
      oc2.store(o2);

      oc2.commit();
      oc1.commit();

      assertOccurrences(oc1, SimpleObject.class, 0);
      assertOccurrences(oc2, SimpleObject.class, 0);
      assertOccurrences(oc3, SimpleObject.class, 0);

    } finally {
      oc1.close();
      oc2.close();
      oc3.close();
    }
  }
 public void conc(ExtObjectContainer oc) {
   oc.queryByExample((new QueryNonExistantTestCase(true)));
   assertOccurrences(oc, QueryNonExistantTestCase.class, 0);
   Query q = oc.query();
   q.constrain(new QueryNonExistantTestCase(true));
   Assert.areEqual(0, q.execute().size());
 }
 private MockActivatable retrieveMockFromNewClientAndClose() {
   final ExtObjectContainer client = openNewSession();
   try {
     return retrieveMock(client);
   } finally {
     client.close();
   }
 }
 public void concUpdateSameObject(ExtObjectContainer oc, int seq) throws Exception {
   Query query = oc.query();
   query.descend("_s").constrain(testString + COUNT / 2);
   ObjectSet result = query.execute();
   Assert.areEqual(1, result.size());
   SimpleObject o = (SimpleObject) result.next();
   o.setI(COUNT + seq);
   oc.store(o);
 }
  public void testSeparateSessions() {
    ExtObjectContainer oc1 = openNewSession();
    ExtObjectContainer oc2 = openNewSession();
    try {
      ObjectSet os1 = oc1.query(Item.class);
      ObjectSet os2 = oc2.query(Item.class);
      deleteObjectSet(oc1, os1);
      assertOccurrences(oc1, Atom.class, 0);
      assertOccurrences(oc2, Atom.class, 1);

      deleteObjectSet(oc2, os2);
      assertOccurrences(oc1, Atom.class, 0);
      assertOccurrences(oc2, Atom.class, 0);

      oc1.rollback();
      assertOccurrences(oc1, Atom.class, 1);
      assertOccurrences(oc2, Atom.class, 0);

      oc1.commit();
      assertOccurrences(oc1, Atom.class, 1);
      assertOccurrences(oc2, Atom.class, 0);

      deleteAll(oc2, Item.class);
      oc2.commit();
      assertOccurrences(oc1, Atom.class, 0);
      assertOccurrences(oc2, Atom.class, 0);

    } finally {
      oc1.close();
      oc2.close();
    }
  }
  public void conc(ExtObjectContainer oc) {
    Query q = oc.query();
    q.constrain(QueryForUnknownFieldTestCase.class);
    q.descend("_name").constrain("name");
    Assert.areEqual(1, q.execute().size());

    q = oc.query();
    q.constrain(QueryForUnknownFieldTestCase.class);
    q.descend("name").constrain("name");
    Assert.areEqual(0, q.execute().size());
  }
 public void testQuery() {
   ExtObjectContainer oc = fixture().db();
   initGenericObjects();
   ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
   Assert.isNotNull(rc);
   // now query to make sure there are none left
   Query q = oc.query();
   q.constrain(rc);
   q.descend("surname").constrain("John");
   ObjectSet results = q.execute();
   Assert.isTrue(results.size() == 1);
 }
 public void testUpdate() {
   ExtObjectContainer oc = fixture().db();
   initGenericObjects();
   // Db4oUtil.dump(oc);
   ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
   Assert.isNotNull(rc);
   Query q = oc.query();
   q.constrain(rc);
   ObjectSet results = q.execute();
   // Db4oUtil.dumpResults(oc, results);
   Assert.isTrue(results.size() == 1);
 }
 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 concUpdateDifferentObject(ExtObjectContainer oc, int seq) throws Exception {
   Query query = oc.query();
   query
       .descend("_s")
       .constrain(testString + seq)
       .and(query.descend("_i").constrain(new Integer(seq)));
   ObjectSet result = query.execute();
   Assert.areEqual(1, result.size());
   SimpleObject o = (SimpleObject) result.next();
   o.setI(seq + COUNT);
   oc.store(o);
 }
 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);
 }
  public void testCreate() throws Exception {

    initGenericObjects();
    // fixture().reopen();
    ExtObjectContainer oc = fixture().db();
    // now check to see if person was saved
    ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
    Assert.isNotNull(rc);
    Query q = oc.query();
    q.constrain(rc);
    ObjectSet results = q.execute();
    Assert.isTrue(results.size() == 1);
    // Db4oUtil.dumpResults(fixture().db(), results);

  }
  /**
   * ATTENTION: This function is duplicated in the Web Of Trust plugin, please backport any changes.
   */
  private void databaseIntegrityTest() {
    Logger.normal(this, "Testing database integrity...");
    synchronized (mIdentityManager) {
      synchronized (mMessageManager) {
        synchronized (mTaskManager) {
          final Query q = db.query();
          q.constrain(Persistent.class);

          for (final Persistent p : new Persistent.InitializingObjectSet<Persistent>(this, q)) {
            try {
              p.databaseIntegrityTest();
            } catch (Exception e) {
              try {
                Logger.error(this, "Integrity test failed for " + p, e);
              } catch (Exception toStringException) {
                Logger.error(
                    this,
                    "Integrity test failed for object and toString also failed, toString-Exception below",
                    toStringException);
                Logger.error(this, "Original integrity test failure below", e);
              }
            }
          }
        }
      }
    }
    Logger.normal(this, "Database integrity test finished.");
  }
 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);
   }
 }
  /**
   * ATTENTION: This function is duplicated in the Web Of Trust plugin, please backport any changes.
   */
  private void closeDatabase() {
    if (db == null) {
      Logger.warning(this, "Terminated already.");
      return;
    }

    synchronized (Persistent.transactionLock(db)) {
      try {
        System.gc();
        db.rollback();
        System.gc();
        db.close();
        db = null;
      } catch (RuntimeException e) {
        Logger.error(this, "Error while closing database", e);
      }
    }
  }
 public void checkUpdateSameObject(ExtObjectContainer oc) throws Exception {
   Query query = oc.query();
   query.descend("_s").constrain(testString + COUNT / 2);
   ObjectSet result = query.execute();
   Assert.areEqual(1, result.size());
   SimpleObject o = (SimpleObject) result.next();
   int i = o.getI();
   Assert.isTrue(COUNT <= i && i < COUNT + threadCount());
 }
  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 checkUpdateDifferentObject(ExtObjectContainer oc) throws Exception {

    ObjectSet result = oc.query(SimpleObject.class);
    Assert.areEqual(COUNT, result.size());
    while (result.hasNext()) {
      SimpleObject o = (SimpleObject) result.next();
      int i = o.getI();
      if (i >= COUNT) {
        i -= COUNT;
      }
      Assert.areEqual(testString + i, o.getS());
    }
  }
 private void operateOnClient1And2(
     ArrayList4Operation<Integer> op1, ArrayList4Operation<Integer> op2) throws Exception {
   ExtObjectContainer client1 = openNewSession();
   ExtObjectContainer client2 = openNewSession();
   ArrayList4<Integer> list1 = retrieveAndAssertNullArrayList4(client1);
   ArrayList4<Integer> list2 = retrieveAndAssertNullArrayList4(client2);
   op1.operate(list1);
   op2.operate(list2);
   client1.store(list1);
   client2.store(list2);
   client1.commit();
   client2.commit();
   client1.close();
   client2.close();
 }
Beispiel #23
0
  /**
   * Loads an existing Config object from the database and adds any missing default values to it,
   * creates and stores a new one if none exists.
   *
   * @return The config object.
   */
  public static Config loadOrCreate(WoT myWoT) {
    ExtObjectContainer db = myWoT.getDB();
    synchronized (db.lock()) {
      Config config;
      ObjectSet<Config> result = db.queryByExample(Config.class);

      if (result.size() == 0) {
        Logger.debug(myWoT, "Creating new Config...");
        config = new Config(myWoT);
        config.storeAndCommit();
      } else {
        if (result.size() > 1) /* Do not throw, we do not want to prevent WoT from starting up. */
          Logger.error(myWoT, "Multiple config objects stored!");

        Logger.debug(myWoT, "Loaded config.");
        config = result.next();
        config.initializeTransient(myWoT);
        config.setDefaultValues(false);
      }

      return config;
    }
  }
  /**
   * This is to ensure that reflector.forObject(GenericArray) returns an instance of
   * GenericArrayClass instead of GenericClass http://tracker.db4o.com/jira/browse/COR-376
   */
  public void testGenericArrayClass() {
    ExtObjectContainer oc = fixture().db();
    initGenericObjects();
    ReflectClass elementType = oc.reflector().forName(PERSON_CLASSNAME);

    Object array = reflector().array().newInstance(elementType, 5);

    ReflectClass arrayClass = oc.reflector().forObject(array);
    Assert.isTrue(arrayClass.isArray());
    Assert.isTrue(arrayClass instanceof GenericArrayClass);

    arrayClass = oc.reflector().forName(array.getClass().getName());
    Assert.isTrue(arrayClass.isArray());
    Assert.isTrue(arrayClass instanceof GenericArrayClass);

    arrayClass = oc.reflector().forClass(array.getClass());
    Assert.isTrue(arrayClass.isArray());
    Assert.isTrue(arrayClass instanceof GenericArrayClass);

    Assert.areEqual(arrayClass.getName(), ReflectPlatform.fullyQualifiedName(array.getClass()));

    Assert.areEqual("(GA) " + elementType.getName(), array.toString());
  }
  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();
        }
      }
    }
  }
 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);
   }
 }
Beispiel #27
0
 /**
  * Stores the config object in the database. Please call this after any modifications to the
  * config, it is not done automatically because the user interface will usually change many values
  * at once.
  */
 public synchronized void storeAndCommit() {
   synchronized (mDB.lock()) {
     try {
       mDB.store(mStringParams, 3);
       mDB.store(mIntParams, 3);
       mDB.store(this);
       mDB.commit();
     } catch (RuntimeException e) {
       System.gc();
       mDB.rollback();
       Logger.error(this, "ROLLED BACK!", e);
       throw e;
     }
   }
 }
 public void conc1(ExtObjectContainer oc) throws Exception {
   ObjectSet os = oc.query(Item.class);
   Thread.sleep(500);
   deleteObjectSet(oc, os);
   oc.rollback();
 }
 public void conc2(ExtObjectContainer oc) throws Exception {
   ObjectSet os = oc.query(Item.class);
   Thread.sleep(500);
   deleteObjectSet(oc, os);
   assertOccurrences(oc, Atom.class, 0);
 }
 public void concQueryCaseInsenstive(ExtObjectContainer oc) {
   Query q = oc.query();
   q.constrain(CaseInsensitiveTestCase.class);
   q.constrain(new CaseInsensitiveEvaluation("helloworld"));
   Assert.areEqual(1, q.execute().size());
 }