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.Map;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertTrue;
34
35 public class CompositeCacheWriterTest
36 {
37 @Rule
38 public final InternalCacheRule rule = new InternalCacheRule(this);
39
40
41 private final Map<String, String> copy1 = new HashMap<String, String>();
42 private final Map<String, String> copy2 = new HashMap<String, String>();
43
44 private final CacheWriterAdapter<String, String> writer1 = new CacheWriterAdapter<String, String>()
45 {
46 @Override
47 public void write(final Cache.Entry<? extends String, ? extends String> entry) throws CacheWriterException
48 {
49 copy1.put(entry.getKey(), entry.getValue());
50 }
51
52 @Override
53 public void delete(final Object key) throws CacheWriterException
54 {
55 copy1.remove(key);
56 }
57 };
58 private final CacheWriterAdapter<String, String> writer2 = new CacheWriterAdapter<String, String>()
59 {
60 @Override
61 public void write(final Cache.Entry<? extends String, ? extends String> entry) throws CacheWriterException
62 {
63 copy2.put(entry.getKey(), entry.getValue());
64 }
65
66 @Override
67 public void delete(final Object key) throws CacheWriterException
68 {
69 copy2.remove(key);
70 }
71 };
72 private final Configuration<?, ?> config = new MutableConfiguration<String, String>()
73 .setStoreByValue(false)
74 .setWriteThrough(true)
75 .setCacheWriterFactory(new CompositeCacheWriter<String, String>(writer1, writer2));
76 private Cache<String, String> cache;
77
78 @Test
79 public void checkComposite()
80 {
81 cache.put("a", "b");
82 assertEquals("b", copy1.get("a"));
83 assertEquals("b", copy2.get("a"));
84 assertEquals(1, copy1.size());
85 assertEquals(1, copy2.size());
86 cache.remove("a");
87 assertTrue(copy1.isEmpty());
88 assertTrue(copy2.isEmpty());
89 }
90 }