Ejemplo n.º 1
0
  @Test
  public void mustAllowDoubleCommitAndRollback() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      tbl.addColumn(RealmFieldType.STRING, "name");
      tbl.addColumn(RealmFieldType.INTEGER, "number");

      // allow commit before any changes
      assertEquals(0, tbl.size());
      tbl.add("Hello", 1);
      trans.commit();
    }
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      // allow double rollback
      tbl.add("Hello", 2);
      assertEquals(2, tbl.size());
      trans.rollback();
      trans.rollback();
      trans.rollback();
      trans.rollback();
    }
    {
      ReadTransaction trans = db.beginRead();
      Table tbl = trans.getTable("EmployeeTable");
      assertEquals(1, tbl.size());
      trans.endRead();
    }
  }
Ejemplo n.º 2
0
 protected void checkRead(SharedGroup db, int rows) {
   // Read transaction
   ReadTransaction trans = db.beginRead();
   Table tbl = trans.getTable("EmployeeTable");
   assertEquals(true, tbl.isValid());
   assertEquals(rows, tbl.size());
   trans.endRead();
 }
Ejemplo n.º 3
0
  @Test
  public void shouldThrowExceptionWhenWritingInReadTrans() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    ReadTransaction rt = db.beginRead();

    try {
      try {
        rt.getTable(
            "newTable"); // Should throw exception, as this method creates a new table, if the table
                         // does not exists, thereby making it a mutable operation
        fail();
      } catch (IllegalStateException e) {
        assertNotNull(e);
      }
    } finally {
      rt.endRead();
    }
  }
Ejemplo n.º 4
0
  @Test
  public void shouldThrowExceptionAfterClosedReadTransaction() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    writeOneTransaction(db, 10);
    ReadTransaction rt = db.beginRead();

    try {
      Table tbl = rt.getTable("EmployeeTable");
      rt.endRead();
      try {
        tbl
            .getColumnCount(); // Should throw exception, the table is invalid when transaction has
                               // been closed
        fail();
      } catch (IllegalStateException ignored) {
      }
    } finally {
      rt.endRead();
    }
  }
Ejemplo n.º 5
0
  @Test
  public void shouldThrowExceptionAfterClosedReadTransactionWhenWriting() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    writeOneTransaction(db, 10);
    ReadTransaction rt = db.beginRead();

    try {
      Table tbl = rt.getTable("EmployeeTable");
      rt.endRead();
      try {
        tbl.addColumn(
            RealmFieldType.STRING,
            "newString"); // Should throw exception, as adding a column is not allowed in read
                          // transaction
        fail();
      } catch (IllegalStateException e) {
        // assertNotNull(e);
      }
    } finally {
      rt.endRead();
    }
  }
Ejemplo n.º 6
0
  @Test
  public void mustFailOnWriteInReadTransactions() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);

    writeOneTransaction(db, 1);

    ReadTransaction t = db.beginRead();
    Table table = t.getTable("EmployeeTable");

    try {
      table.addEmptyRow();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.addEmptyRows(1);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.clear();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.optimize();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.remove(0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.removeLast();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setBinaryByteArray(0, 0, null);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setBoolean(0, 0, false);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setDate(0, 0, new Date(0));
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.addSearchIndex(0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setLong(0, 0, 0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setMixed(0, 0, null);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      table.setString(0, 0, "");
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }

    TableQuery q = table.where();
    try {
      q.remove();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      q.remove(0, 0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }

    TableView v = q.findAll();
    try {
      v.clear();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.remove(0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.removeLast();
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setBinaryByteArray(0, 0, null);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setBoolean(0, 0, false);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setDate(0, 0, new Date());
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setLong(0, 0, 0);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setString(0, 0, "");
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
    try {
      v.setMixed(0, 0, null);
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }

    t.endRead();
  }