@Test
  public void testSavepointsAreActive() throws Exception {
    Txn parent = control.beginTransaction(DESTINATION_TABLE);
    TransactionImpl transaction = new TransactionImpl("user", parent, false, control);

    int res = transaction.setSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);

    transaction.elevate(DESTINATION_TABLE);

    Txn parent2 = control.beginTransaction();

    long[] ids = txnStore.getActiveTransactionIds(parent2, DESTINATION_TABLE);
    Assert.assertEquals("Incorrect size", 2, ids.length);
    Assert.assertArrayEquals(
        "Incorrect values", new long[] {parent.getTxnId(), transaction.getTxn().getTxnId()}, ids);

    res = transaction.rollbackToSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);

    ids = txnStore.getActiveTransactionIds(parent2, DESTINATION_TABLE);
    Assert.assertEquals("Incorrect size", 1, ids.length);
    Assert.assertArrayEquals("Incorrect values", new long[] {parent.getTxnId()}, ids);
  }
  @Test
  public void testElevateWholeStack() throws Exception {
    Txn parent = control.beginTransaction();
    TransactionImpl transaction = new TransactionImpl("user", parent, false, control);
    Assert.assertFalse(transaction.getTxn().allowsWrites());

    int res = transaction.setSavePoint("first", null);
    Assert.assertEquals("Wrong txn stack size", 2, res);
    Assert.assertFalse(transaction.getTxn().allowsWrites());

    res = transaction.setSavePoint("second", null);
    Assert.assertEquals("Wrong txn stack size", 3, res);
    Assert.assertFalse(transaction.getTxn().allowsWrites());

    transaction.elevate(DESTINATION_TABLE);
    Assert.assertTrue(transaction.getTxn().allowsWrites());

    res = transaction.releaseSavePoint("second", null);
    Assert.assertEquals("Wrong txn stack size", 2, res);
    Assert.assertTrue(transaction.getTxn().allowsWrites());

    res = transaction.releaseSavePoint("first", null);
    Assert.assertEquals("Wrong txn stack size", 1, res);
    Assert.assertTrue(transaction.getTxn().allowsWrites());

    transaction.commit();
  }
  @Test
  public void testRollbackSavepoint() throws Exception {
    Txn parent = control.beginTransaction(DESTINATION_TABLE);
    TransactionImpl transaction = new TransactionImpl("user", parent, false, control);

    int res = transaction.setSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);
    transaction.elevate(DESTINATION_TABLE);
    TxnView first = transaction.getTxn();

    res = transaction.setSavePoint("second", null);

    Assert.assertEquals("Wrong txn stack size", 3, res);
    transaction.elevate(DESTINATION_TABLE);
    Txn second = transaction.getTxn();

    // rollback to first, should rollback second
    res = transaction.rollbackToSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);

    Assert.assertEquals(Txn.State.ROLLEDBACK, first.getState());
    Assert.assertEquals(Txn.State.ROLLEDBACK, second.getState());
  }
  @Test
  public void testReleaseSavepoint() throws Exception {
    Txn parent = control.beginTransaction(DESTINATION_TABLE);
    TransactionImpl transaction = new TransactionImpl("user", parent, false, control);

    int res = transaction.setSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);
    transaction.elevate(DESTINATION_TABLE);
    TxnView first = transaction.getTxn();

    res = transaction.setSavePoint("second", null);

    Assert.assertEquals("Wrong txn stack size", 3, res);
    transaction.elevate(DESTINATION_TABLE);
    Txn second = transaction.getTxn();

    // release first, should also commit second
    res = transaction.releaseSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 1, res);

    Assert.assertEquals(Txn.State.COMMITTED, first.getState());
    Assert.assertEquals(Txn.State.COMMITTED, second.getState());
  }
  @Test(expected = SavePointNotFoundException.class)
  public void testSavepointNotFound() throws Exception {
    Txn parent = control.beginTransaction(DESTINATION_TABLE);
    TransactionImpl transaction = new TransactionImpl("user", parent, false, control);

    int res = transaction.setSavePoint("first", null);

    Assert.assertEquals("Wrong txn stack size", 2, res);

    transaction.rollbackToSavePoint("second", null);
  }