001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.cache;
018:
019: import java.io.Serializable;
020: import java.security.Principal;
021: import java.util.LinkedList;
022: import java.util.List;
023:
024: import junit.framework.TestCase;
025: import net.sf.ehcache.Cache;
026: import net.sf.ehcache.CacheManager;
027:
028: import org.apache.jetspeed.cache.impl.EhDecorationContentCacheImpl;
029: import org.apache.jetspeed.cache.impl.JetspeedCacheKeyGenerator;
030: import org.apache.jetspeed.mockobjects.request.MockRequestContext;
031:
032: import com.mockrunner.mock.web.MockHttpServletRequest;
033: import com.mockrunner.mock.web.MockHttpServletResponse;
034: import com.mockrunner.mock.web.MockHttpSession;
035:
036: /**
037: * <p>
038: * Test Content Cache
039: * </p>
040: * <p>
041: *
042: * </p>
043: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
044: * @version $Id: TestCachingInterceptors.java 516448 2007-03-09 16:25:47Z ate $
045: *
046: */
047: public class TestDecorationContentCache extends TestCase {
048:
049: public void testContentCacheByUser() throws Exception {
050: // initialize ehCache
051: CacheManager cacheManager = new CacheManager();
052: Cache ehContentCache = new Cache("ehDecorationContentCache",
053: 10000, false, false, 28800, 28800);
054: cacheManager.addCache(ehContentCache);
055: ehContentCache.setCacheManager(cacheManager);
056:
057: // initial Jetspeed caches
058: List segments = new LinkedList();
059: segments.add("username");
060: segments.add("pipeline");
061: segments.add("windowid");
062: ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(
063: segments);
064: JetspeedCache contentCache = new EhDecorationContentCacheImpl(
065: ehContentCache, generator);
066:
067: // create the mock request context
068: MockHttpServletRequest request = new MockHttpServletRequest();
069: MockHttpServletResponse response = new MockHttpServletResponse();
070: request.setUserPrincipal(new MockPrincipal("david"));
071: MockRequestContext context = new MockRequestContext(request,
072: response);
073:
074: // create a simple key
075: String window1 = "/default-page.psml";
076: ContentCacheKey cckey1 = contentCache.createCacheKey(context,
077: window1);
078: assertEquals(cckey1.getKey(), "david/portal//default-page.psml");
079:
080: // create a another key for desktop
081: String window2 = "/about.psml";
082: context.getParameterMap().put("encoder", "desktop");
083: ContentCacheKey cckey2 = contentCache.createCacheKey(context,
084: window2);
085: assertEquals(cckey2.getKey(), "david/desktop//about.psml");
086:
087: // create some PortletContent mock objects
088: MockTheme theme1 = new MockTheme("/default-page.psml");
089: MockTheme theme2 = new MockTheme("/about.psml");
090:
091: // put it in the cache
092: CacheElement element1 = contentCache.createElement(cckey1,
093: theme1);
094: contentCache.put(element1);
095: CacheElement element2 = contentCache.createElement(cckey2,
096: theme2);
097: contentCache.put(element2);
098:
099: // assert the gets
100: Object result1 = contentCache.get(cckey1);
101: assertNotNull(result1);
102: System.out.println("result 1 = " + result1);
103: Object result2 = contentCache.get(cckey2);
104: assertNotNull(result2);
105: System.out.println("result 2 = " + result2);
106:
107: // assert isKey Apis
108: assertTrue(contentCache.isKeyInCache(cckey1));
109:
110: // test removes
111: contentCache.remove(cckey1);
112: assertFalse(contentCache.isKeyInCache(cckey1));
113: assertTrue(contentCache.isKeyInCache(cckey2));
114:
115: // test user stuff
116: request.setUserPrincipal(new MockPrincipal("sean"));
117: // create a simple key
118: String window3 = "/default-page.psml";
119: ContentCacheKey cckey3 = contentCache.createCacheKey(context,
120: window3);
121: assertEquals(cckey3.getKey(), "sean/desktop//default-page.psml");
122:
123: // create a another key for desktop
124: String window4 = "/about.psml";
125: ContentCacheKey cckey4 = contentCache.createCacheKey(context,
126: window4);
127: assertEquals(cckey4.getKey(), "sean/desktop//about.psml");
128:
129: // create some MockTheme objects
130: MockTheme theme3 = new MockTheme("/default-page.psml");
131: MockTheme theme4 = new MockTheme("/about.psml");
132:
133: // put it in the cache
134: CacheElement element3 = contentCache.createElement(cckey3,
135: theme3);
136: contentCache.put(element3);
137: CacheElement element4 = contentCache.createElement(cckey4,
138: theme4);
139: contentCache.put(element4);
140:
141: // assert 3 and 4
142: assertTrue(contentCache.isKeyInCache(cckey3));
143: assertTrue(contentCache.isKeyInCache(cckey4));
144:
145: // remove for user
146: contentCache.evictContentForUser("sean");
147: assertFalse(contentCache.isKeyInCache(cckey3));
148: assertFalse(contentCache.isKeyInCache(cckey4));
149: assertTrue(contentCache.isKeyInCache(cckey2));
150: }
151:
152: public void testContentCacheBySession() throws Exception {
153: // initialize ehCache
154: CacheManager cacheManager = new CacheManager();
155: Cache ehContentCache = new Cache("ehDecorationContentCache",
156: 10000, false, false, 28800, 28800);
157: cacheManager.addCache(ehContentCache);
158: ehContentCache.setCacheManager(cacheManager);
159:
160: // initial Jetspeed caches
161: List segments = new LinkedList();
162: segments.add("sessionid");
163: segments.add("pipeline");
164: segments.add("windowid");
165: ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(
166: segments);
167: JetspeedCache contentCache = new EhDecorationContentCacheImpl(
168: ehContentCache, generator);
169:
170: // create the mock request context
171: MockHttpServletRequest request = new MockHttpServletRequest();
172: MockHttpServletResponse response = new MockHttpServletResponse();
173: request.setUserPrincipal(new MockPrincipal("david"));
174: MockHttpSession session = new MockHttpSession();
175: request.setSession(session);
176: String sessionId = session.getId();
177:
178: MockRequestContext context = new MockRequestContext(request,
179: response);
180:
181: // create a simple key
182: String window1 = "/default-page.psml";
183: ContentCacheKey cckey1 = contentCache.createCacheKey(context,
184: window1);
185: assertEquals(cckey1.getKey(), sessionId
186: + "/portal//default-page.psml");
187:
188: // create a another key for desktop
189: String window2 = "/about.psml";
190: context.getParameterMap().put("encoder", "desktop");
191: ContentCacheKey cckey2 = contentCache.createCacheKey(context,
192: window2);
193: assertEquals(cckey2.getKey(), sessionId
194: + "/desktop//about.psml");
195:
196: // create some MockTheme objects
197: MockTheme theme1 = new MockTheme("/default-page.psml");
198: MockTheme theme2 = new MockTheme("/about.psml");
199:
200: // put it in the cache
201: CacheElement element1 = contentCache.createElement(cckey1,
202: theme1);
203: contentCache.put(element1);
204: CacheElement element2 = contentCache.createElement(cckey2,
205: theme2);
206: contentCache.put(element2);
207:
208: // assert the gets
209: Object result1 = contentCache.get(cckey1);
210: assertNotNull(result1);
211: System.out.println("result 1 = " + result1);
212: Object result2 = contentCache.get(cckey2);
213: assertNotNull(result2);
214: System.out.println("result 2 = " + result2);
215:
216: // assert isKey Apis
217: assertTrue(contentCache.isKeyInCache(cckey1));
218:
219: // test removes
220: contentCache.remove(cckey1);
221: assertFalse(contentCache.isKeyInCache(cckey1));
222: assertTrue(contentCache.isKeyInCache(cckey2));
223:
224: // test user stuff
225: session = new MockHttpSession();
226: request.setSession(session);
227: sessionId = session.getId();
228: request.setUserPrincipal(new MockPrincipal("sean"));
229: // create a simple key
230: String window3 = "/default-page.psml";
231: ContentCacheKey cckey3 = contentCache.createCacheKey(context,
232: window3);
233: assertEquals(cckey3.getKey(), sessionId
234: + "/desktop//default-page.psml");
235:
236: // create a another key for desktop
237: String window4 = "about.psml";
238: ContentCacheKey cckey4 = contentCache.createCacheKey(context,
239: window4);
240: assertEquals(cckey4.getKey(), sessionId + "/desktop/about.psml");
241:
242: // create some PortletContent mock objects
243: MockTheme theme3 = new MockTheme("/default-page.psml");
244: MockTheme theme4 = new MockTheme("/about.psml");
245:
246: // put it in the cache
247: CacheElement element3 = contentCache.createElement(cckey3,
248: theme3);
249: contentCache.put(element3);
250: CacheElement element4 = contentCache.createElement(cckey4,
251: theme4);
252: contentCache.put(element4);
253:
254: // assert 3 and 4
255: assertTrue(contentCache.isKeyInCache(cckey3));
256: assertTrue(contentCache.isKeyInCache(cckey4));
257:
258: // remove for user
259: contentCache.evictContentForSession(sessionId);
260: assertFalse(contentCache.isKeyInCache(cckey3));
261: assertFalse(contentCache.isKeyInCache(cckey4));
262: assertTrue(contentCache.isKeyInCache(cckey2));
263: }
264:
265: class MockPrincipal implements Principal {
266: private String name;
267:
268: public MockPrincipal(String name) {
269: this .name = name;
270: }
271:
272: public String getName() {
273: return name;
274: }
275: }
276:
277: class MockTheme implements Serializable {
278: private String pageId;
279:
280: public MockTheme(String pageId) {
281: this .pageId = pageId;
282: }
283:
284: public String toString() {
285: return this.pageId;
286: }
287: }
288:
289: }
|