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.Serializable;
20 import java.security.Principal;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import junit.framework.TestCase;
25 import net.sf.ehcache.Cache;
26 import net.sf.ehcache.CacheManager;
27
28 import org.apache.jetspeed.cache.impl.EhDecorationContentCacheImpl;
29 import org.apache.jetspeed.cache.impl.JetspeedCacheKeyGenerator;
30 import org.apache.jetspeed.mockobjects.request.MockRequestContext;
31
32 import com.mockrunner.mock.web.MockHttpServletRequest;
33 import com.mockrunner.mock.web.MockHttpServletResponse;
34 import com.mockrunner.mock.web.MockHttpSession;
35
36 /***
37 * <p>
38 * Test Content Cache
39 * </p>
40 * <p>
41 *
42 * </p>
43 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
44 * @version $Id: TestCachingInterceptors.java 516448 2007-03-09 16:25:47Z ate $
45 *
46 */
47 public class TestDecorationContentCache extends TestCase
48 {
49
50 public void testContentCacheByUser() throws Exception
51 {
52
53 CacheManager cacheManager = new CacheManager();
54 Cache ehContentCache = new Cache("ehDecorationContentCache", 10000, false, false, 28800, 28800);
55 cacheManager.addCache(ehContentCache);
56 ehContentCache.setCacheManager(cacheManager);
57
58
59 List segments = new LinkedList();
60 segments.add("username");
61 segments.add("pipeline");
62 segments.add("windowid");
63 ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(segments);
64 JetspeedCache contentCache = new EhDecorationContentCacheImpl(ehContentCache, generator);
65
66
67 MockHttpServletRequest request = new MockHttpServletRequest();
68 MockHttpServletResponse response = new MockHttpServletResponse();
69 request.setUserPrincipal(new MockPrincipal("david"));
70 MockRequestContext context = new MockRequestContext(request, response);
71
72
73 String window1 = "/default-page.psml";
74 ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
75 assertEquals(cckey1.getKey(), "david/portal//default-page.psml");
76
77
78 String window2 = "/about.psml";
79 context.getParameterMap().put("encoder", "desktop");
80 ContentCacheKey cckey2 = contentCache.createCacheKey(context, window2);
81 assertEquals(cckey2.getKey(), "david/desktop//about.psml");
82
83
84 MockTheme theme1 = new MockTheme("/default-page.psml");
85 MockTheme theme2 = new MockTheme("/about.psml");
86
87
88 CacheElement element1 = contentCache.createElement(cckey1, theme1);
89 contentCache.put(element1);
90 CacheElement element2 = contentCache.createElement(cckey2, theme2);
91 contentCache.put(element2);
92
93
94 Object result1 = contentCache.get(cckey1);
95 assertNotNull(result1);
96 System.out.println("result 1 = " + result1);
97 Object result2 = contentCache.get(cckey2);
98 assertNotNull(result2);
99 System.out.println("result 2 = " + result2);
100
101
102 assertTrue(contentCache.isKeyInCache(cckey1));
103
104
105
106 contentCache.remove(cckey1);
107 assertFalse(contentCache.isKeyInCache(cckey1));
108 assertTrue(contentCache.isKeyInCache(cckey2));
109
110
111 request.setUserPrincipal(new MockPrincipal("sean"));
112
113 String window3 = "/default-page.psml";
114 ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
115 assertEquals(cckey3.getKey(), "sean/desktop//default-page.psml");
116
117
118 String window4 = "/about.psml";
119 ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
120 assertEquals(cckey4.getKey(), "sean/desktop//about.psml");
121
122
123 MockTheme theme3 = new MockTheme("/default-page.psml");
124 MockTheme theme4 = new MockTheme("/about.psml");
125
126
127 CacheElement element3 = contentCache.createElement(cckey3, theme3);
128 contentCache.put(element3);
129 CacheElement element4 = contentCache.createElement(cckey4, theme4);
130 contentCache.put(element4);
131
132
133 assertTrue(contentCache.isKeyInCache(cckey3));
134 assertTrue(contentCache.isKeyInCache(cckey4));
135
136
137 contentCache.evictContentForUser("sean");
138 assertFalse(contentCache.isKeyInCache(cckey3));
139 assertFalse(contentCache.isKeyInCache(cckey4));
140 assertTrue(contentCache.isKeyInCache(cckey2));
141 }
142
143 public void testContentCacheBySession() throws Exception
144 {
145
146 CacheManager cacheManager = new CacheManager();
147 Cache ehContentCache = new Cache("ehDecorationContentCache", 10000, false, false, 28800, 28800);
148 cacheManager.addCache(ehContentCache);
149 ehContentCache.setCacheManager(cacheManager);
150
151
152 List segments = new LinkedList();
153 segments.add("sessionid");
154 segments.add("pipeline");
155 segments.add("windowid");
156 ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(segments);
157 JetspeedCache contentCache = new EhDecorationContentCacheImpl(ehContentCache, generator);
158
159
160 MockHttpServletRequest request = new MockHttpServletRequest();
161 MockHttpServletResponse response = new MockHttpServletResponse();
162 request.setUserPrincipal(new MockPrincipal("david"));
163 MockHttpSession session = new MockHttpSession();
164 request.setSession(session);
165 String sessionId = session.getId();
166
167 MockRequestContext context = new MockRequestContext(request, response);
168
169
170 String window1 = "/default-page.psml";
171 ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
172 assertEquals(cckey1.getKey(), sessionId + "/portal//default-page.psml");
173
174
175 String window2 = "/about.psml";
176 context.getParameterMap().put("encoder", "desktop");
177 ContentCacheKey cckey2 = contentCache.createCacheKey(context, window2);
178 assertEquals(cckey2.getKey(), sessionId + "/desktop//about.psml");
179
180
181 MockTheme theme1 = new MockTheme("/default-page.psml");
182 MockTheme theme2 = new MockTheme("/about.psml");
183
184
185 CacheElement element1 = contentCache.createElement(cckey1, theme1);
186 contentCache.put(element1);
187 CacheElement element2 = contentCache.createElement(cckey2, theme2);
188 contentCache.put(element2);
189
190
191 Object result1 = contentCache.get(cckey1);
192 assertNotNull(result1);
193 System.out.println("result 1 = " + result1);
194 Object result2 = contentCache.get(cckey2);
195 assertNotNull(result2);
196 System.out.println("result 2 = " + result2);
197
198
199 assertTrue(contentCache.isKeyInCache(cckey1));
200
201
202
203 contentCache.remove(cckey1);
204 assertFalse(contentCache.isKeyInCache(cckey1));
205 assertTrue(contentCache.isKeyInCache(cckey2));
206
207
208 session = new MockHttpSession();
209 request.setSession(session);
210 sessionId = session.getId();
211 request.setUserPrincipal(new MockPrincipal("sean"));
212
213 String window3 = "/default-page.psml";
214 ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
215 assertEquals(cckey3.getKey(), sessionId + "/desktop//default-page.psml");
216
217
218 String window4 = "about.psml";
219 ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
220 assertEquals(cckey4.getKey(), sessionId + "/desktop/about.psml");
221
222
223 MockTheme theme3 = new MockTheme("/default-page.psml");
224 MockTheme theme4 = new MockTheme("/about.psml");
225
226
227 CacheElement element3 = contentCache.createElement(cckey3, theme3);
228 contentCache.put(element3);
229 CacheElement element4 = contentCache.createElement(cckey4, theme4);
230 contentCache.put(element4);
231
232
233 assertTrue(contentCache.isKeyInCache(cckey3));
234 assertTrue(contentCache.isKeyInCache(cckey4));
235
236
237 contentCache.evictContentForSession(sessionId);
238 assertFalse(contentCache.isKeyInCache(cckey3));
239 assertFalse(contentCache.isKeyInCache(cckey4));
240 assertTrue(contentCache.isKeyInCache(cckey2));
241 }
242
243 class MockPrincipal implements Principal
244 {
245 private String name;
246 public MockPrincipal(String name)
247 {
248 this.name = name;
249 }
250
251 public String getName()
252 {
253 return name;
254 }
255 }
256
257 class MockTheme implements Serializable
258 {
259 private String pageId;
260
261 public MockTheme(String pageId)
262 {
263 this.pageId = pageId;
264 }
265
266 public String toString()
267 {
268 return this.pageId;
269 }
270 }
271
272 }