1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@param |
40 |
|
|
41 |
|
@version |
42 |
|
@since |
43 |
|
|
|
|
| 52.1% |
Uncovered Elements: 23 (48) |
Complexity: 19 |
Complexity Density: 0.73 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
50 |
|
class TestCacheEntry implements CacheEntry<T> |
51 |
|
{ |
52 |
|
private final String key; |
53 |
|
private final T value; |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
55 |
0 |
TestCacheEntry(String key, T value)... |
56 |
|
{ |
57 |
0 |
this.key = key; |
58 |
0 |
this.value = value; |
59 |
|
} |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
@Override... |
62 |
|
public Cache<T> getCache() |
63 |
|
{ |
64 |
0 |
return TestCache.this; |
65 |
|
} |
66 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
0 |
@Override... |
68 |
|
public String getKey() |
69 |
|
{ |
70 |
0 |
return key; |
71 |
|
} |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0 |
@Override... |
74 |
|
public T getValue() |
75 |
|
{ |
76 |
0 |
return value; |
77 |
|
} |
78 |
|
} |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
0 |
private CacheEntryEvent<T> getEvent(final String key, final T value)... |
81 |
|
{ |
82 |
0 |
return new CacheEntryEvent<T>() |
83 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
0 |
@Override... |
85 |
|
public CacheEntry<T> getEntry() |
86 |
|
{ |
87 |
0 |
return new TestCacheEntry(key, value); |
88 |
|
} |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
0 |
@Override... |
91 |
|
public Cache<T> getCache() |
92 |
|
{ |
93 |
0 |
return TestCache.this; |
94 |
|
} |
95 |
|
}; |
96 |
|
} |
97 |
|
|
98 |
|
|
|
|
| 64.3% |
Uncovered Elements: 5 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
99 |
1117 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
8628 |
@Override... |
118 |
|
public T get(String key) |
119 |
|
{ |
120 |
8628 |
return cache.get(key); |
121 |
|
} |
122 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
123 |
337 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
0 |
@Override... |
134 |
|
public void removeAll() |
135 |
|
{ |
136 |
0 |
cache.clear(); |
137 |
|
} |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
0 |
@Override... |
140 |
|
public void addCacheEntryListener(CacheEntryListener<T> tCacheEntryListener) |
141 |
|
{ |
142 |
0 |
listener = tCacheEntryListener; |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
145 |
0 |
@Override... |
146 |
|
public void removeCacheEntryListener(CacheEntryListener<T> tCacheEntryListener) |
147 |
|
{ |
148 |
0 |
assertThat(tCacheEntryListener, equalTo(listener)); |
149 |
0 |
listener = null; |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
0 |
@Override... |
153 |
|
public void dispose() |
154 |
|
{ |
155 |
0 |
listener = null; |
156 |
|
} |
157 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
158 |
1454 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
169 |
765 |
public String getLastInsertedKey()... |
170 |
|
{ |
171 |
765 |
return lastInsertedKey; |
172 |
|
} |
173 |
|
} |