1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.cachepolicy; |
35 |
|
|
36 |
|
import static org.hamcrest.Matchers.equalTo; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
import static org.mockito.Matchers.anyObject; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
import info.magnolia.cms.core.AggregationState; |
42 |
|
import info.magnolia.cms.core.Channel; |
43 |
|
import info.magnolia.cms.security.User; |
44 |
|
import info.magnolia.context.MgnlContext; |
45 |
|
import info.magnolia.context.WebContext; |
46 |
|
import info.magnolia.module.cache.AbstractCacheTest; |
47 |
|
import info.magnolia.module.cache.Cache; |
48 |
|
import info.magnolia.module.cache.CachePolicyResult; |
49 |
|
import info.magnolia.module.cache.FlushPolicy; |
50 |
|
import info.magnolia.module.cache.cachekey.CacheKeyGenerator; |
51 |
|
import info.magnolia.module.cache.cachekey.DefaultCacheKey; |
52 |
|
import info.magnolia.module.cache.cachepolicy.instructor.CacheInstructor; |
53 |
|
import info.magnolia.module.cache.filter.UncacheableEntry; |
54 |
|
import info.magnolia.objectfactory.guice.GuiceUtils; |
55 |
|
import info.magnolia.test.ComponentsTestUtil; |
56 |
|
|
57 |
|
import java.util.Arrays; |
58 |
|
import java.util.HashMap; |
59 |
|
import java.util.Locale; |
60 |
|
import java.util.Map; |
61 |
|
|
62 |
|
import javax.servlet.http.HttpServletRequest; |
63 |
|
|
64 |
|
import org.junit.After; |
65 |
|
import org.junit.Before; |
66 |
|
import org.junit.Test; |
67 |
|
|
|
|
| 95.3% |
Uncovered Elements: 5 (107) |
Complexity: 13 |
Complexity Density: 0.14 |
|
68 |
|
public class DefaultTest extends AbstractCacheTest { |
69 |
|
|
70 |
|
private Default policy; |
71 |
|
private AggregationState aggregationState; |
72 |
|
private WebContext webCtx; |
73 |
|
private HttpServletRequest request; |
74 |
|
private Map<String, String[]> params; |
75 |
|
private final CacheInstructor cacheInstructor = new CacheInstructor(); |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
|
77 |
10 |
@Before... |
78 |
|
public void setUp() throws Exception { |
79 |
10 |
super.setUp(); |
80 |
10 |
webCtx = mock(WebContext.class); |
81 |
10 |
request = mock(HttpServletRequest.class); |
82 |
10 |
aggregationState = new AggregationState(); |
83 |
10 |
MgnlContext.setInstance(webCtx); |
84 |
|
|
85 |
10 |
Default defaultPolicy = new Default(null, GuiceUtils.providerForInstance(cacheInstructor)); |
86 |
10 |
defaultPolicy.setRefreshOnNoCacheRequests(true); |
87 |
10 |
policy = defaultPolicy; |
88 |
|
|
89 |
10 |
Boolean isSecure = false; |
90 |
10 |
String uri = "localhost"; |
91 |
10 |
String serverName = "test"; |
92 |
10 |
params = new HashMap(); |
93 |
|
|
94 |
10 |
when(webCtx.getContextPath()).thenReturn(uri); |
95 |
10 |
when(webCtx.getRequest()).thenReturn(request); |
96 |
10 |
when(webCtx.getRequest()).thenReturn(request); |
97 |
10 |
User user = mock(User.class); |
98 |
10 |
when(webCtx.getUser()).thenReturn(user); |
99 |
|
|
100 |
10 |
when(request.getServerName()).thenReturn(serverName); |
101 |
10 |
when(request.isSecure()).thenReturn(isSecure); |
102 |
10 |
when(request.getMethod()).thenReturn("GET"); |
103 |
10 |
when(request.getParameterMap()).thenReturn(params); |
104 |
|
|
105 |
10 |
aggregationState.setCharacterEncoding("UTF-8"); |
106 |
10 |
aggregationState.setLocale(Locale.ENGLISH); |
107 |
|
|
108 |
10 |
final Channel channel = new Channel(); |
109 |
10 |
channel.setName("mobile"); |
110 |
10 |
aggregationState.setChannel(channel); |
111 |
10 |
aggregationState.setOriginalURI(uri); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
114 |
1 |
@Test... |
115 |
|
public void testRetrieveDefaultCacheKey() { |
116 |
|
|
117 |
1 |
params.put("testkey", new String[]{"testvalue"}); |
118 |
|
|
119 |
|
|
120 |
1 |
final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState); |
121 |
|
|
122 |
|
|
123 |
1 |
assertThat(cacheKey.getUri(), equalTo("localhost")); |
124 |
1 |
assertThat(cacheKey.getServerName(), equalTo("test")); |
125 |
1 |
assertThat(cacheKey.getLocale(), equalTo("en")); |
126 |
1 |
assertThat(cacheKey.getChannel(), equalTo("mobile")); |
127 |
1 |
assertThat(cacheKey.getParams().toString(), equalTo("{testkey=testvalue}")); |
128 |
1 |
assertThat(cacheKey.getIsSecured(), equalTo(false)); |
129 |
1 |
assertThat(cacheKey.getMethod(), equalTo("get")); |
130 |
|
|
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
public void testRetrieveDefaultCacheKeyWhenRequestParamHasMoreValuesDefined() { |
135 |
|
|
136 |
1 |
params.put("bTestKey", new String[]{"bTestValue1", "bTestValue2", "bTestValue3"}); |
137 |
1 |
params.put("aTestKey", new String[]{"aTestValue"}); |
138 |
|
|
139 |
|
|
140 |
1 |
final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState); |
141 |
|
|
142 |
|
|
143 |
1 |
assertThat(cacheKey.getUri(), equalTo("localhost")); |
144 |
1 |
assertThat(cacheKey.getServerName(), equalTo("test")); |
145 |
1 |
assertThat(cacheKey.getLocale(), equalTo("en")); |
146 |
1 |
assertThat(cacheKey.getChannel(), equalTo("mobile")); |
147 |
1 |
assertThat(cacheKey.getParams().toString(), equalTo("{aTestKey=aTestValue, bTestKey=bTestValue1;bTestValue2;bTestValue3}")); |
148 |
1 |
assertThat(cacheKey.getIsSecured(), equalTo(false)); |
149 |
1 |
assertThat(cacheKey.getMethod(), equalTo("get")); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void testSemicolonIsEscapedInParameterValues() { |
154 |
|
|
155 |
1 |
params.put("bTestKey", new String[]{";bTestValue1;", "bTestVal;ue2", "bTestValue3"}); |
156 |
1 |
params.put("aTestKey", new String[]{"aTestValue"}); |
157 |
|
|
158 |
|
|
159 |
1 |
final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState); |
160 |
|
|
161 |
|
|
162 |
1 |
assertThat(cacheKey.getParams().toString(), equalTo("{aTestKey=aTestValue, bTestKey=;;bTestValue1;;;bTestVal;;ue2;bTestValue3}")); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
165 |
1 |
@Test... |
166 |
|
public void testSemicolonIsEscapedInParameterValues1() { |
167 |
|
|
168 |
1 |
params.put("bTestKey", new String[]{";bTestValue1;", "bTestVal;ue2", "bTestValue3"}); |
169 |
1 |
params.put("aTestKey", new String[]{"aTestValue"}); |
170 |
|
|
171 |
|
|
172 |
1 |
policy.retrieveCacheKey(aggregationState); |
173 |
|
|
174 |
|
|
175 |
1 |
assertEquals("[aTestValue]", Arrays.asList(params.get("aTestKey")).toString()); |
176 |
1 |
assertEquals("[;bTestValue1;, bTestVal;ue2, bTestValue3]", Arrays.asList(params.get("bTestKey")).toString()); |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
179 |
1 |
@Test... |
180 |
|
public void testDontSortParameterValuesInPlace() { |
181 |
|
|
182 |
1 |
params.put("key", new String[]{"1", "3", "2"}); |
183 |
|
|
184 |
|
|
185 |
1 |
policy.retrieveCacheKey(aggregationState); |
186 |
|
|
187 |
|
|
188 |
1 |
assertEquals("Params values in request should remain unchanged", "[1, 3, 2]", Arrays.asList(params.get("key")).toString()); |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
191 |
1 |
@Test... |
192 |
|
public void testRefreshWithCacheControlHeader() { |
193 |
|
|
194 |
1 |
Cache cache = mock(Cache.class); |
195 |
1 |
FlushPolicy flushPolicy = mock(FlushPolicy.class); |
196 |
|
|
197 |
1 |
when(request.getHeader("Cache-Control")).thenReturn("no-cache"); |
198 |
1 |
when(cache.get((Object) anyObject())).thenReturn("cached-key"); |
199 |
|
|
200 |
|
|
201 |
1 |
CachePolicyResult result = policy.shouldCache(cache, aggregationState, flushPolicy); |
202 |
|
|
203 |
|
|
204 |
1 |
assertEquals("store", result.getBehaviour().toString()); |
205 |
1 |
assertEquals(null, result.getCachedEntry()); |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
208 |
1 |
@Test... |
209 |
|
public void testRefreshWithPragmaHeader() { |
210 |
|
|
211 |
1 |
Cache cache = mock(Cache.class); |
212 |
1 |
FlushPolicy flushPolicy = mock(FlushPolicy.class); |
213 |
|
|
214 |
1 |
when(request.getHeader("Pragma")).thenReturn("no-cache"); |
215 |
1 |
when(cache.get((Object) anyObject())).thenReturn("cached-key"); |
216 |
|
|
217 |
|
|
218 |
1 |
CachePolicyResult result = policy.shouldCache(cache, aggregationState, flushPolicy); |
219 |
|
|
220 |
|
|
221 |
1 |
assertEquals("store", result.getBehaviour().toString()); |
222 |
1 |
assertEquals(null, result.getCachedEntry()); |
223 |
|
} |
224 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
225 |
0 |
public void testStore() {... |
226 |
|
|
227 |
0 |
Cache cache = mock(Cache.class); |
228 |
|
|
229 |
|
|
230 |
0 |
CachePolicyResult result = policy.shouldCache(cache, aggregationState, null); |
231 |
|
|
232 |
|
|
233 |
0 |
assertEquals(CachePolicyResult.store, result.getBehaviour()); |
234 |
0 |
assertNull(result.getCachedEntry()); |
235 |
|
} |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
237 |
1 |
@Test... |
238 |
|
public void testUseCache() { |
239 |
|
|
240 |
1 |
Cache cache = mock(Cache.class); |
241 |
1 |
Object value = "someValue"; |
242 |
1 |
when(cache.get(anyObject())).thenReturn(value); |
243 |
|
|
244 |
1 |
CachePolicyResult result = policy.shouldCache(cache, aggregationState, null); |
245 |
|
|
246 |
|
|
247 |
1 |
assertEquals(CachePolicyResult.useCache, result.getBehaviour()); |
248 |
1 |
assertNotNull(result.getCachedEntry()); |
249 |
|
} |
250 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
251 |
1 |
@Test... |
252 |
|
public void testBypass() { |
253 |
|
|
254 |
1 |
Cache cache = mock(Cache.class); |
255 |
1 |
Object value = mock(UncacheableEntry.class); |
256 |
1 |
when(cache.get(anyObject())).thenReturn(value); |
257 |
|
|
258 |
1 |
CachePolicyResult result = policy.shouldCache(cache, aggregationState, null); |
259 |
|
|
260 |
|
|
261 |
1 |
assertEquals(CachePolicyResult.bypass, result.getBehaviour()); |
262 |
1 |
assertNull(result.getCachedEntry()); |
263 |
|
} |
264 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
265 |
1 |
@Test... |
266 |
|
public void testCustomCacheKeyGenerator() { |
267 |
|
|
268 |
1 |
cacheInstructor.setKeyGenerator(new DummyCacheKeyGererator()); |
269 |
|
|
270 |
|
|
271 |
1 |
Object key = policy.retrieveCacheKey(aggregationState); |
272 |
|
|
273 |
|
|
274 |
1 |
assertThat(key.toString(), equalTo("key")); |
275 |
|
|
276 |
|
|
277 |
1 |
ComponentsTestUtil.clear(); |
278 |
|
} |
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
280 |
|
public static class DummyCacheKeyGererator implements CacheKeyGenerator<String> { |
281 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
282 |
1 |
@Override... |
283 |
|
public String generateKey(AggregationState aggregationState) { |
284 |
1 |
return "key"; |
285 |
|
} |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
288 |
10 |
@After... |
289 |
|
public void tearDown() throws Exception { |
290 |
10 |
super.tearDown(); |
291 |
10 |
MgnlContext.setInstance(null); |
292 |
|
} |
293 |
|
} |