Below are three test cases for a set as value in a map:
1) testTransactionMapWithSet = the map is TransactionMap<Integer,
Set<Integer>>
2) testMVMapWithSet = the map is MVMap<Integer, Set<Integer>>
3) testJavaMapWithSet = the map is Map<Integer,Set<Integer>>
Only test case 1) with TransactionMap<Integer, Set<Integer>> fails
because
the value returned from the map has type:
org.h2.mvstore.db.TransactionStore.VersionedValue
when the expected type is:
java.util.HashSet
The returned type org.h2.mvstore.db.TransactionStore.VersionedValue has a
field:
public Object value;
which is the correct object with the expected type but it is unaccessible
because class VersionedValue is not public.
What is the reason that test case 2) with MVMap works correctly but
exactly the same fails with TransactionMap? Finally, the test case 3)
shows
that Java Map is working as expected.
@Test
public void testTransactionMapWithSet() {
//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>> testMap = tx.openMap("testMap");
//testMap must be empty
assertNull(testMap.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
testMap.put(new Integer(0), set);
//Commit the transaction
tx.commit();
//Close TransactionStore
ts.close();
//Open testMap for reading outside the transaction
MVMap<Integer, Set<Integer>> auxMap = s.openMap("testMap");
//Read value on key=0
Object value = auxMap.get(0);
//Print value to console
System.out.println(value);
//Test the type of value that must be the same with the put value type
assertEquals(value.getClass().getCanonicalName(),set.getClass().getCanonicalName());
//Close MVStore
s.close();
}
@Test
public void testMVMapWithSet() {
//Create a new MVStore file
File file = new File(System.getProperty("java.io.tmpdir"),
"testMVMapWithSet.dat");
if (file.exists()) {
file.delete();
}
MVStore s = new
MVStore.Builder().fileName(file.getAbsolutePath()).open();
//Open testMap
MVMap<Integer, Set<Integer>> testMap = s.openMap("testMap");
//testMap must be empty
assertNull(testMap.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
testMap.put(new Integer(0), set);
//Read value on key=0
Object value = testMap.get(0);
//Print value to console
System.out.println(value);
//Test the type of value that must be the same with the put value type
assertEquals(value.getClass().getCanonicalName(),set.getClass().getCanonicalName());
//Close MVStore
s.close();
}
@Test
public void testJavaMapWithSet() {
//Create testMap
Map<Integer,Set<Integer>> testMap = new HashMap<Integer,Set<Integer>>();
//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
testMap.put(new Integer(0), set);
//Read value on key=0
Object value = testMap.get(0);
//Print value to console
System.out.println(value);
//Test the type of value that must be the same with the put value type
assertEquals(value.getClass().getCanonicalName(),set.getClass().getCanonicalName());
}
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.