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.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  }