1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.cache;
18
19 import java.io.CharArrayWriter;
20 import java.io.PrintWriter;
21 import java.security.Principal;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import junit.framework.TestCase;
26 import net.sf.ehcache.Cache;
27 import net.sf.ehcache.CacheManager;
28
29 import org.apache.jetspeed.aggregator.PortletContent;
30 import org.apache.jetspeed.aggregator.PortletRenderer;
31 import org.apache.jetspeed.cache.impl.EhPortletContentCacheImpl;
32 import org.apache.jetspeed.cache.impl.JetspeedCacheKeyGenerator;
33 import org.apache.jetspeed.mockobjects.request.MockRequestContext;
34
35 import com.mockrunner.mock.web.MockHttpServletRequest;
36 import com.mockrunner.mock.web.MockHttpServletResponse;
37 import com.mockrunner.mock.web.MockHttpSession;
38
39 /***
40 * <p>
41 * Test Content Cache
42 * </p>
43 * <p>
44 *
45 * </p>
46 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
47 * @version $Id: TestCachingInterceptors.java 516448 2007-03-09 16:25:47Z ate $
48 *
49 */
50 public class TestContentCache extends TestCase
51 {
52
53 public void testContentCacheByUser() throws Exception
54 {
55
56 CacheManager cacheManager = new CacheManager();
57 Cache ehContentCache = new Cache("ehPortletContentCache", 10000, false, false, 28800, 28800);
58 cacheManager.addCache(ehContentCache);
59 ehContentCache.setCacheManager(cacheManager);
60
61
62 List segments = new LinkedList();
63 segments.add("username");
64 segments.add("pipeline");
65 segments.add("windowid");
66 ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(segments);
67 JetspeedCache contentCache = new EhPortletContentCacheImpl(ehContentCache, generator);
68
69
70 MockHttpServletRequest request = new MockHttpServletRequest();
71 MockHttpServletResponse response = new MockHttpServletResponse();
72 request.setUserPrincipal(new MockPrincipal("david"));
73 MockRequestContext context = new MockRequestContext(request, response);
74
75
76 String window1 = "555-01";
77 ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
78 assertEquals(cckey1.getKey(), "david/portal/555-01");
79
80
81 String window2 = "555-02";
82 context.getParameterMap().put("encoder", "desktop");
83 ContentCacheKey cckey2 = contentCache.createCacheKey(context, window2);
84 assertEquals(cckey2.getKey(), "david/desktop/555-02");
85
86
87 PortletContent content1 = new MockPortletContent(cckey1, 100, "ContentOne", "content1content1content1content1");
88 PortletContent content2 = new MockPortletContent(cckey2, 200, "ContentTwo", "content2content2content2content2");
89
90
91 CacheElement element1 = contentCache.createElement(cckey1, content1);
92 contentCache.put(element1);
93 CacheElement element2 = contentCache.createElement(cckey2, content2);
94 contentCache.put(element2);
95
96
97 Object result1 = contentCache.get(cckey1);
98 assertNotNull(result1);
99 System.out.println("result 1 = " + result1);
100 Object result2 = contentCache.get(cckey2);
101 assertNotNull(result2);
102 System.out.println("result 2 = " + result2);
103
104
105 assertTrue(contentCache.isKeyInCache(cckey1));
106
107
108
109 contentCache.remove(cckey1);
110 assertFalse(contentCache.isKeyInCache(cckey1));
111 assertTrue(contentCache.isKeyInCache(cckey2));
112
113
114 request.setUserPrincipal(new MockPrincipal("sean"));
115
116 String window3 = "555-03";
117 ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
118 assertEquals(cckey3.getKey(), "sean/desktop/555-03");
119
120
121 String window4 = "555-04";
122 ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
123 assertEquals(cckey4.getKey(), "sean/desktop/555-04");
124
125
126 PortletContent content3 = new MockPortletContent(cckey3, 300, "ContentThree", "content3content3content3content3");
127 PortletContent content4 = new MockPortletContent(cckey4, 400, "ContentTwo", "content4content4content4content4");
128
129
130 CacheElement element3 = contentCache.createElement(cckey3, content3);
131 contentCache.put(element3);
132 CacheElement element4 = contentCache.createElement(cckey4, content4);
133 contentCache.put(element4);
134
135
136 assertTrue(contentCache.isKeyInCache(cckey3));
137 assertTrue(contentCache.isKeyInCache(cckey4));
138
139
140 contentCache.evictContentForUser("sean");
141 assertFalse(contentCache.isKeyInCache(cckey3));
142 assertFalse(contentCache.isKeyInCache(cckey4));
143 assertTrue(contentCache.isKeyInCache(cckey2));
144 }
145
146 public void testContentCacheBySession() throws Exception
147 {
148
149 CacheManager cacheManager = new CacheManager();
150 Cache ehContentCache = new Cache("ehPortletContentCache", 10000, false, false, 28800, 28800);
151 cacheManager.addCache(ehContentCache);
152 ehContentCache.setCacheManager(cacheManager);
153
154
155 List segments = new LinkedList();
156 segments.add("sessionid");
157 segments.add("pipeline");
158 segments.add("windowid");
159 ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(segments);
160 JetspeedCache contentCache = new EhPortletContentCacheImpl(ehContentCache, generator);
161
162
163 MockHttpServletRequest request = new MockHttpServletRequest();
164 MockHttpServletResponse response = new MockHttpServletResponse();
165 request.setUserPrincipal(new MockPrincipal("david"));
166 MockHttpSession session = new MockHttpSession();
167 request.setSession(session);
168 String sessionId = session.getId();
169
170 MockRequestContext context = new MockRequestContext(request, response);
171
172
173 String window1 = "555-01";
174 ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
175 assertEquals(cckey1.getKey(), sessionId + "/portal/555-01");
176
177
178 String window2 = "555-02";
179 context.getParameterMap().put("encoder", "desktop");
180 ContentCacheKey cckey2 = contentCache.createCacheKey(context, window2);
181 assertEquals(cckey2.getKey(), sessionId + "/desktop/555-02");
182
183
184 PortletContent content1 = new MockPortletContent(cckey1, 100, "ContentOne", "content1content1content1content1");
185 PortletContent content2 = new MockPortletContent(cckey2, 200, "ContentTwo", "content2content2content2content2");
186
187
188 CacheElement element1 = contentCache.createElement(cckey1, content1);
189 contentCache.put(element1);
190 CacheElement element2 = contentCache.createElement(cckey2, content2);
191 contentCache.put(element2);
192
193
194 Object result1 = contentCache.get(cckey1);
195 assertNotNull(result1);
196 System.out.println("result 1 = " + result1);
197 Object result2 = contentCache.get(cckey2);
198 assertNotNull(result2);
199 System.out.println("result 2 = " + result2);
200
201
202 assertTrue(contentCache.isKeyInCache(cckey1));
203
204
205
206 contentCache.remove(cckey1);
207 assertFalse(contentCache.isKeyInCache(cckey1));
208 assertTrue(contentCache.isKeyInCache(cckey2));
209
210
211 session = new MockHttpSession();
212 request.setSession(session);
213 sessionId = session.getId();
214 request.setUserPrincipal(new MockPrincipal("sean"));
215
216 String window3 = "555-03";
217 ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
218 assertEquals(cckey3.getKey(), sessionId + "/desktop/555-03");
219
220
221 String window4 = "555-04";
222 ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
223 assertEquals(cckey4.getKey(), sessionId + "/desktop/555-04");
224
225
226 PortletContent content3 = new MockPortletContent(cckey3, 300, "ContentThree", "content3content3content3content3");
227 PortletContent content4 = new MockPortletContent(cckey4, 400, "ContentTwo", "content4content4content4content4");
228
229
230 CacheElement element3 = contentCache.createElement(cckey3, content3);
231 contentCache.put(element3);
232 CacheElement element4 = contentCache.createElement(cckey4, content4);
233 contentCache.put(element4);
234
235
236 assertTrue(contentCache.isKeyInCache(cckey3));
237 assertTrue(contentCache.isKeyInCache(cckey4));
238
239
240 contentCache.evictContentForSession(sessionId);
241 assertFalse(contentCache.isKeyInCache(cckey3));
242 assertFalse(contentCache.isKeyInCache(cckey4));
243 assertTrue(contentCache.isKeyInCache(cckey2));
244 }
245
246 class MockPrincipal implements Principal
247 {
248 private String name;
249 public MockPrincipal(String name)
250 {
251 this.name = name;
252 }
253
254 public String getName()
255 {
256 return name;
257 }
258 }
259
260 class MockPortletContent implements PortletContent
261 {
262 private boolean complete = false;
263 private ContentCacheKey cacheKey;
264 private int expiration = 0;
265 private String title;
266 private String content;
267
268
269 MockPortletContent(ContentCacheKey cacheKey, int expiration, String title, String content)
270 {
271 this.cacheKey = cacheKey;
272 this.expiration = expiration;
273 this.title = title;
274 this.content = content;
275 }
276
277
278 public PrintWriter getWriter()
279 {
280 return null;
281 }
282
283 public void init()
284 {
285 }
286
287 public void release()
288 {
289 }
290
291 public String toString()
292 {
293 return content;
294 }
295
296 public void writeTo( java.io.Writer out ) throws java.io.IOException
297 {
298 }
299
300 public char[] toCharArray()
301 {
302 return content.toCharArray();
303 }
304
305 public boolean isComplete()
306 {
307 return complete;
308 }
309
310 void setComplete(boolean state, boolean notify)
311 {
312 this.complete = state;
313 }
314
315 public String getContent()
316 {
317 return toString();
318 }
319 /***
320 * <p>
321 * complete
322 * </p>
323 *
324 * @see org.apache.jetspeed.aggregator.PortletContent#complete()
325 *
326 */
327 public void complete()
328 {
329 setComplete(true, true);
330 }
331
332
333 public void completeWithError()
334 {
335 setComplete(true, false);
336 }
337
338 public ContentCacheKey getCacheKey()
339 {
340 return cacheKey;
341 }
342
343 public int getExpiration()
344 {
345 return expiration;
346 }
347
348 public void setExpiration(int expiration)
349 {
350 this.expiration = expiration;
351 }
352
353 public String getTitle()
354 {
355 return title;
356 }
357
358 public void setTitle(String title)
359 {
360 this.title = title;
361 }
362 }
363
364 }