Below are some unit tests for Set as a value in a TransactionMap:
1) testTransactionMapWithSetRollback1 = one item is added to a set which is 
get from TransactionMap and rollback is called for this change.
2) testTransactionMapWithSetRollback2 = the same rollback test as 1) but 
the set is first copied to a new set and the new set with the added value 
is put to TransactionMap
3) testTransactionMapWithSetCommit1 = the same as 1) but rollback is 
replaced by commit()

The interesting result is that 1) fails and both 2) and 3) succeed.
Apparently rollback is not working correctly when the content of Set is 
changed.
However, rollback works if the whole Set is replaced by a new set with 
changed contents.

@Test
    public void testTransactionMapWithSetRollback1() {
    //Create a new MVStore file
    File file = new File(System.getProperty("java.io.tmpdir"), 
"testTransactionMapWithSet.dat");
if (file.exists()) {
file.delete();
}
    MVStore s = new 
MVStore.Builder().fileName(file.getAbsolutePath()).open();

    //Open testMap in a transaction
    TransactionStore ts = new TransactionStore(s);
        ts.init();
        Transaction tx = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap = tx.openMap("testMap");
//testMap must be empty 
assertNull(setMap.lastKey());
//Create set of Integer values
Set<Integer> set = new HashSet<Integer>();
set.add(new Integer(0));
set.add(new Integer(1));
set.add(new Integer(2));
//Put set to testMap with key=0
setMap.put(new Integer(0), set);
//Commit the transaction
        tx.commit();

        //Open another transaction, change the set and rollback
        Transaction tx2 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap2 = tx2.openMap("testMap");

//Read set on key=0
Set<Integer> set2 = setMap2.get(0);
set2.add(new Integer(3));
    //Assert size of sets
    assertEquals(set2.size(),4);
    
    tx2.rollback();

        //Open third transaction, read set and compare to the original set
        Transaction tx3 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap3 = tx3.openMap("testMap");
Set<Integer> set3 = setMap3.get(0);
    //Assert size of sets - THIS FAILS!
    assertEquals(set3.size(),3);
    
    tx3.rollback();
    
    //Close all
        ts.close();
        s.close();
}

@Test
    public void testTransactionMapWithSetRollback2() {
    //Create a new MVStore file
    File file = new File(System.getProperty("java.io.tmpdir"), 
"testTransactionMapWithSet.dat");
if (file.exists()) {
file.delete();
}
    MVStore s = new 
MVStore.Builder().fileName(file.getAbsolutePath()).open();

    //Open testMap in a transaction
    TransactionStore ts = new TransactionStore(s);
        ts.init();
        Transaction tx = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap = tx.openMap("testMap");
//testMap must be empty 
assertNull(setMap.lastKey());
//Create set of Integer values
Set<Integer> set = new HashSet<Integer>();
set.add(new Integer(0));
set.add(new Integer(1));
set.add(new Integer(2));
//Put set to testMap with key=0
setMap.put(new Integer(0), set);
//Commit the transaction
        tx.commit();

        //Open another transaction, change the set and rollback
        Transaction tx2 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap2 = tx2.openMap("testMap");

//Read set on key=0
Set<Integer> set2 = setMap2.get(0);
//Make a copy and add a new item to it
HashSet<Integer> setAux = new HashSet<Integer>();
setAux.addAll(set2);
setAux.add(new Integer(3));
    //Assert size of set
    assertEquals(setAux.size(),4);
    //Replace the old set by the new set
setMap2.put(new Integer(0), setAux);
//Rollback
tx2.rollback();

        //Open third transaction and read the set
        Transaction tx3 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap3 = tx3.openMap("testMap");
Set<Integer> set3 = setMap3.get(0);
    //Assert size of set - THIS SUCCEEDS
    assertEquals(set3.size(),3);
    
    tx3.rollback();
    
    //Close all
        ts.close();
        s.close();
}

@Test
    public void testTransactionMapWithSetCommit1() {
    //Create a new MVStore file
    File file = new File(System.getProperty("java.io.tmpdir"), 
"testTransactionMapWithSet.dat");
if (file.exists()) {
file.delete();
}
    MVStore s = new 
MVStore.Builder().fileName(file.getAbsolutePath()).open();

    //Open testMap in a transaction
    TransactionStore ts = new TransactionStore(s);
        ts.init();
        Transaction tx = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap = tx.openMap("testMap");
//testMap must be empty 
assertNull(setMap.lastKey());
//Create set of Integer values
Set<Integer> set = new HashSet<Integer>();
set.add(new Integer(0));
set.add(new Integer(1));
set.add(new Integer(2));
//Put set to testMap with key=0
setMap.put(new Integer(0), set);
//Commit the transaction
        tx.commit();

        //Open another transaction, change the set and rollback
        Transaction tx2 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap2 = tx2.openMap("testMap");

//Read set on key=0
Set<Integer> set2 = setMap2.get(0);
set2.add(new Integer(3));
    //Assert size of sets
    assertEquals(set2.size(),4);
    
    tx2.commit();

        //Open third transaction, read set and compare to the original set
        Transaction tx3 = ts.begin();
TransactionMap<Integer, Set<Integer>> setMap3 = tx3.openMap("testMap");
Set<Integer> set3 = setMap3.get(0);
    //Assert size of sets
    assertEquals(set3.size(),4);
    
    tx3.rollback();
    
    //Close all
        ts.close();
        s.close();
}

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to h2-database+unsubscr...@googlegroups.com.
To post to this group, send email to h2-database@googlegroups.com.
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to