Repository navigation
Expand file tree
/
Copy pathMapListenerTest.java
More file actions
64 lines (47 loc) · 2.22 KB
/
Copy pathMapListenerTest.java
File metadata and controls
64 lines (47 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.mapdb;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@SuppressWarnings({"rawtypes","unchecked"})
public class MapListenerTest {
@Test public void hashMap(){
tt(DBMaker.memoryDB().transactionDisable().cacheHashTableEnable().make().hashMap("test"));
}
@Test public void treeMap(){
tt(DBMaker.memoryDB().transactionDisable().cacheHashTableEnable().make().treeMap("test"));
}
void tt(Bind.MapWithModificationListener m){
final AtomicReference key = new AtomicReference(null);
final AtomicReference newVal = new AtomicReference(null);
final AtomicReference oldVal = new AtomicReference(null);
final AtomicInteger counter = new AtomicInteger(0);
Bind.MapListener listener = new Bind.MapListener(){
@Override public void update(Object key2, Object oldVal2, Object newVal2) {
counter.incrementAndGet();
key.set(key2);
oldVal.set(oldVal2);
newVal.set(newVal2);
}
};
m.modificationListenerAdd(listener);
//check CRUD
m.put("aa","bb");
assertTrue(key.get()=="aa" && newVal.get()=="bb" && oldVal.get()==null && counter.get()==1);
m.put("aa","cc");
assertTrue(key.get()=="aa" && newVal.get()=="cc" && oldVal.get()=="bb" && counter.get()==2);
m.remove("aa");
assertTrue(key.get()=="aa" && newVal.get()==null && oldVal.get()=="cc" && counter.get()==3);
//check clear()
m.put("aa","bb");
assertTrue(key.get()=="aa" && newVal.get()=="bb" && oldVal.get()==null && counter.get()==4);
m.clear();
assertTrue(key.get()=="aa" && newVal.get()==null && oldVal.get()=="bb" && counter.get()==5);
//check it was unregistered
counter.set(0);
m.modificationListenerRemove(listener);
m.put("aa","bb");
assertEquals(0, counter.get());
}
}