View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }