1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.jcache.extras.writer;
20
21 import org.apache.commons.jcs.jcache.extras.InternalCacheRule;
22 import org.junit.Rule;
23 import org.junit.Test;
24
25 import javax.cache.Cache;
26 import javax.cache.configuration.Configuration;
27 import javax.cache.configuration.MutableConfiguration;
28 import javax.cache.integration.CacheWriterException;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32
33 import static java.util.Arrays.asList;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
37
38 public class CacheWriterAdapterTest
39 {
40 @Rule
41 public final InternalCacheRule rule = new InternalCacheRule(this);
42
43 private final Map<String, String> copy = new HashMap<String, String>();
44 private final Configuration<?, ?> config = new MutableConfiguration<String, String>()
45 .setStoreByValue(false).setReadThrough(true)
46 .setCacheWriterFactory(new CacheWriterAdapter<String, String>()
47 {
48 @Override
49 public void write(final Cache.Entry<? extends String, ? extends String> entry) throws CacheWriterException
50 {
51 copy.put(entry.getKey(), entry.getValue());
52 }
53
54 @Override
55 public void delete(final Object key) throws CacheWriterException
56 {
57 copy.remove(key);
58 }
59 });
60 private Cache<String, String> cache;
61
62 @Test
63 public void checkWriteAllAndDeleteAll()
64 {
65 assertTrue(copy.isEmpty());
66 assertFalse(cache.iterator().hasNext());
67 cache.put("foo", "bar");
68 assertEquals(1, copy.size());
69 cache.remove("foo");
70 assertTrue(copy.isEmpty());
71
72 cache.putAll(new HashMap<String, String>() {{
73 put("a", "b");
74 put("b", "c");
75 }});
76 assertEquals(2, copy.size());
77 cache.removeAll(new HashSet<String>(asList("a", "b")));
78 assertTrue(copy.isEmpty());
79 }
80 }