1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.cache.internal

File TestCache.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

10
31
16
2
173
122
23
0.74
1.94
8
1.44

Classes

Class Line # Actions
TestCache 44 26 0% 19 23
0.520833352.1%
TestCache.TestCacheEntry 50 5 0% 4 9
0.00%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package org.xwiki.security.authorization.cache.internal;
22   
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xwiki.cache.Cache;
27    import org.xwiki.cache.CacheEntry;
28    import org.xwiki.cache.DisposableCacheValue;
29    import org.xwiki.cache.event.CacheEntryEvent;
30    import org.xwiki.cache.event.CacheEntryListener;
31   
32    import static org.hamcrest.CoreMatchers.equalTo;
33    import static org.junit.Assert.assertThat;
34   
35    /**
36    * A Mock for the Cache<?> interface that ensure the same function than a cache without any
37    * unexpected eviction.
38    *
39    * @param <T> Type of values stored in the cache.
40    *
41    * @version $Id: 226864c7481d5fe3dc48797c68e64a22a59bce5e $
42    * @since 5.0M2
43    */
 
44    public class TestCache<T> implements Cache<T>
45    {
46    private Map<String, T> cache = new HashMap<String, T>();
47    private CacheEntryListener<T> listener;
48    private String lastInsertedKey;
49   
 
50    class TestCacheEntry implements CacheEntry<T>
51    {
52    private final String key;
53    private final T value;
54   
 
55  0 toggle TestCacheEntry(String key, T value)
56    {
57  0 this.key = key;
58  0 this.value = value;
59    }
60   
 
61  0 toggle @Override
62    public Cache<T> getCache()
63    {
64  0 return TestCache.this;
65    }
66   
 
67  0 toggle @Override
68    public String getKey()
69    {
70  0 return key;
71    }
72   
 
73  0 toggle @Override
74    public T getValue()
75    {
76  0 return value;
77    }
78    }
79   
 
80  0 toggle private CacheEntryEvent<T> getEvent(final String key, final T value)
81    {
82  0 return new CacheEntryEvent<T>()
83    {
 
84  0 toggle @Override
85    public CacheEntry<T> getEntry()
86    {
87  0 return new TestCacheEntry(key, value);
88    }
89   
 
90  0 toggle @Override
91    public Cache<T> getCache()
92    {
93  0 return TestCache.this;
94    }
95    };
96    }
97   
98   
 
99  1117 toggle @Override
100    public void set(String key, T value)
101    {
102  1117 T old = cache.put(key, value);
103  1117 if (listener != null && old == null) {
104  0 listener.cacheEntryAdded(getEvent(key, value));
105    } else {
106  1117 if (old != value) {
107  1117 disposeCacheValue(old);
108    }
109   
110  1117 if (listener != null) {
111  0 listener.cacheEntryModified(getEvent(key, value));
112    }
113    }
114  1117 lastInsertedKey = key;
115    }
116   
 
117  8628 toggle @Override
118    public T get(String key)
119    {
120  8628 return cache.get(key);
121    }
122   
 
123  337 toggle @Override
124    public void remove(String key)
125    {
126  337 T value = cache.remove(key);
127  337 if (listener != null) {
128  0 listener.cacheEntryRemoved(getEvent(key, value));
129    }
130  337 disposeCacheValue(value);
131    }
132   
 
133  0 toggle @Override
134    public void removeAll()
135    {
136  0 cache.clear();
137    }
138   
 
139  0 toggle @Override
140    public void addCacheEntryListener(CacheEntryListener<T> tCacheEntryListener)
141    {
142  0 listener = tCacheEntryListener;
143    }
144   
 
145  0 toggle @Override
146    public void removeCacheEntryListener(CacheEntryListener<T> tCacheEntryListener)
147    {
148  0 assertThat(tCacheEntryListener, equalTo(listener));
149  0 listener = null;
150    }
151   
 
152  0 toggle @Override
153    public void dispose()
154    {
155  0 listener = null;
156    }
157   
 
158  1454 toggle private void disposeCacheValue(T value)
159    {
160  1454 if (value instanceof DisposableCacheValue) {
161  337 try {
162  337 ((DisposableCacheValue) value).dispose();
163    } catch (Exception e) {
164  0 throw new RuntimeException(e);
165    }
166    }
167    }
168   
 
169  765 toggle public String getLastInsertedKey()
170    {
171  765 return lastInsertedKey;
172    }
173    }