View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.writer;
18  
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import net.sf.ehcache.CacheEntry;
25  import net.sf.ehcache.CacheException;
26  import net.sf.ehcache.Element;
27  
28  public class TestCacheWriter extends AbstractTestCacheWriter {
29      private final Properties properties;
30      private final Map<Object, Element> writtenElements = new HashMap<Object, Element>();
31      private final Map<Object, Element> deletedElements = new HashMap<Object, Element>();
32  
33      public TestCacheWriter(Properties properties) {
34          this.properties = properties;
35      }
36  
37      public Properties getProperties() {
38          return properties;
39      }
40  
41      public Map<Object, Element> getWrittenElements() {
42          return writtenElements;
43      }
44  
45      public Map<Object, Element> getDeletedElements() {
46          return deletedElements;
47      }
48  
49      private String getAdaptedKey(Object key) {
50          String keyPrefix = properties.getProperty("key.prefix", "");
51          String keySuffix = properties.getProperty("key.suffix", "");
52          return keyPrefix + key + keySuffix;
53      }
54  
55      @Override
56      public synchronized void write(Element element) throws CacheException {
57          writtenElements.put(getAdaptedKey(element.getObjectKey()), element);
58      }
59  
60      @Override
61      public synchronized void writeAll(Collection<Element> elements) throws CacheException {
62          for (Element element : elements) {
63              writtenElements.put(getAdaptedKey(element.getObjectKey()) + "-batched", element);
64          }
65      }
66  
67      @Override
68      public synchronized void delete(CacheEntry entry) throws CacheException {
69          deletedElements.put(getAdaptedKey(entry.getKey()), entry.getElement());
70      }
71  
72      @Override
73      public synchronized void deleteAll(Collection<CacheEntry> entries) throws CacheException {
74          for (CacheEntry entry : entries) {
75              deletedElements.put(getAdaptedKey(entry.getKey()) + "-batched", entry.getElement());
76          }
77      }
78  }