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.CharArrayWriter;
020: import java.io.PrintWriter;
021: import java.security.Principal;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026: import net.sf.ehcache.Cache;
027: import net.sf.ehcache.CacheManager;
028:
029: import org.apache.jetspeed.aggregator.PortletContent;
030: import org.apache.jetspeed.aggregator.PortletRenderer;
031: import org.apache.jetspeed.cache.impl.EhPortletContentCacheImpl;
032: import org.apache.jetspeed.cache.impl.JetspeedCacheKeyGenerator;
033: import org.apache.jetspeed.mockobjects.request.MockRequestContext;
034:
035: import com.mockrunner.mock.web.MockHttpServletRequest;
036: import com.mockrunner.mock.web.MockHttpServletResponse;
037: import com.mockrunner.mock.web.MockHttpSession;
038:
039: /**
040: * <p>
041: * Test Content Cache
042: * </p>
043: * <p>
044: *
045: * </p>
046: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
047: * @version $Id: TestCachingInterceptors.java 516448 2007-03-09 16:25:47Z ate $
048: *
049: */
050: public class TestContentCache extends TestCase {
051:
052: public void testContentCacheByUser() throws Exception {
053: // initialize ehCache
054: CacheManager cacheManager = new CacheManager();
055: Cache ehContentCache = new Cache("ehPortletContentCache",
056: 10000, false, false, 28800, 28800);
057: cacheManager.addCache(ehContentCache);
058: ehContentCache.setCacheManager(cacheManager);
059:
060: // initial Jetspeed caches
061: List segments = new LinkedList();
062: segments.add("username");
063: segments.add("pipeline");
064: segments.add("windowid");
065: ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(
066: segments);
067: JetspeedCache contentCache = new EhPortletContentCacheImpl(
068: ehContentCache, generator);
069:
070: // create the mock request context
071: MockHttpServletRequest request = new MockHttpServletRequest();
072: MockHttpServletResponse response = new MockHttpServletResponse();
073: request.setUserPrincipal(new MockPrincipal("david"));
074: MockRequestContext context = new MockRequestContext(request,
075: response);
076:
077: // create a simple key
078: String window1 = "555-01";
079: ContentCacheKey cckey1 = contentCache.createCacheKey(context,
080: window1);
081: assertEquals(cckey1.getKey(), "david/portal/555-01");
082:
083: // create a another key for desktop
084: String window2 = "555-02";
085: context.getParameterMap().put("encoder", "desktop");
086: ContentCacheKey cckey2 = contentCache.createCacheKey(context,
087: window2);
088: assertEquals(cckey2.getKey(), "david/desktop/555-02");
089:
090: // create some PortletContent mock objects
091: PortletContent content1 = new MockPortletContent(cckey1, 100,
092: "ContentOne", "content1content1content1content1");
093: PortletContent content2 = new MockPortletContent(cckey2, 200,
094: "ContentTwo", "content2content2content2content2");
095:
096: // put it in the cache
097: CacheElement element1 = contentCache.createElement(cckey1,
098: content1);
099: contentCache.put(element1);
100: CacheElement element2 = contentCache.createElement(cckey2,
101: content2);
102: contentCache.put(element2);
103:
104: // assert the gets
105: Object result1 = contentCache.get(cckey1);
106: assertNotNull(result1);
107: System.out.println("result 1 = " + result1);
108: Object result2 = contentCache.get(cckey2);
109: assertNotNull(result2);
110: System.out.println("result 2 = " + result2);
111:
112: // assert isKey Apis
113: assertTrue(contentCache.isKeyInCache(cckey1));
114:
115: // test removes
116: contentCache.remove(cckey1);
117: assertFalse(contentCache.isKeyInCache(cckey1));
118: assertTrue(contentCache.isKeyInCache(cckey2));
119:
120: // test user stuff
121: request.setUserPrincipal(new MockPrincipal("sean"));
122: // create a simple key
123: String window3 = "555-03";
124: ContentCacheKey cckey3 = contentCache.createCacheKey(context,
125: window3);
126: assertEquals(cckey3.getKey(), "sean/desktop/555-03");
127:
128: // create a another key for desktop
129: String window4 = "555-04";
130: ContentCacheKey cckey4 = contentCache.createCacheKey(context,
131: window4);
132: assertEquals(cckey4.getKey(), "sean/desktop/555-04");
133:
134: // create some PortletContent mock objects
135: PortletContent content3 = new MockPortletContent(cckey3, 300,
136: "ContentThree", "content3content3content3content3");
137: PortletContent content4 = new MockPortletContent(cckey4, 400,
138: "ContentTwo", "content4content4content4content4");
139:
140: // put it in the cache
141: CacheElement element3 = contentCache.createElement(cckey3,
142: content3);
143: contentCache.put(element3);
144: CacheElement element4 = contentCache.createElement(cckey4,
145: content4);
146: contentCache.put(element4);
147:
148: // assert 3 and 4
149: assertTrue(contentCache.isKeyInCache(cckey3));
150: assertTrue(contentCache.isKeyInCache(cckey4));
151:
152: // remove for user
153: contentCache.evictContentForUser("sean");
154: assertFalse(contentCache.isKeyInCache(cckey3));
155: assertFalse(contentCache.isKeyInCache(cckey4));
156: assertTrue(contentCache.isKeyInCache(cckey2));
157: }
158:
159: public void testContentCacheBySession() throws Exception {
160: // initialize ehCache
161: CacheManager cacheManager = new CacheManager();
162: Cache ehContentCache = new Cache("ehPortletContentCache",
163: 10000, false, false, 28800, 28800);
164: cacheManager.addCache(ehContentCache);
165: ehContentCache.setCacheManager(cacheManager);
166:
167: // initial Jetspeed caches
168: List segments = new LinkedList();
169: segments.add("sessionid");
170: segments.add("pipeline");
171: segments.add("windowid");
172: ContentCacheKeyGenerator generator = new JetspeedCacheKeyGenerator(
173: segments);
174: JetspeedCache contentCache = new EhPortletContentCacheImpl(
175: ehContentCache, generator);
176:
177: // create the mock request context
178: MockHttpServletRequest request = new MockHttpServletRequest();
179: MockHttpServletResponse response = new MockHttpServletResponse();
180: request.setUserPrincipal(new MockPrincipal("david"));
181: MockHttpSession session = new MockHttpSession();
182: request.setSession(session);
183: String sessionId = session.getId();
184:
185: MockRequestContext context = new MockRequestContext(request,
186: response);
187:
188: // create a simple key
189: String window1 = "555-01";
190: ContentCacheKey cckey1 = contentCache.createCacheKey(context,
191: window1);
192: assertEquals(cckey1.getKey(), sessionId + "/portal/555-01");
193:
194: // create a another key for desktop
195: String window2 = "555-02";
196: context.getParameterMap().put("encoder", "desktop");
197: ContentCacheKey cckey2 = contentCache.createCacheKey(context,
198: window2);
199: assertEquals(cckey2.getKey(), sessionId + "/desktop/555-02");
200:
201: // create some PortletContent mock objects
202: PortletContent content1 = new MockPortletContent(cckey1, 100,
203: "ContentOne", "content1content1content1content1");
204: PortletContent content2 = new MockPortletContent(cckey2, 200,
205: "ContentTwo", "content2content2content2content2");
206:
207: // put it in the cache
208: CacheElement element1 = contentCache.createElement(cckey1,
209: content1);
210: contentCache.put(element1);
211: CacheElement element2 = contentCache.createElement(cckey2,
212: content2);
213: contentCache.put(element2);
214:
215: // assert the gets
216: Object result1 = contentCache.get(cckey1);
217: assertNotNull(result1);
218: System.out.println("result 1 = " + result1);
219: Object result2 = contentCache.get(cckey2);
220: assertNotNull(result2);
221: System.out.println("result 2 = " + result2);
222:
223: // assert isKey Apis
224: assertTrue(contentCache.isKeyInCache(cckey1));
225:
226: // test removes
227: contentCache.remove(cckey1);
228: assertFalse(contentCache.isKeyInCache(cckey1));
229: assertTrue(contentCache.isKeyInCache(cckey2));
230:
231: // test user stuff
232: session = new MockHttpSession();
233: request.setSession(session);
234: sessionId = session.getId();
235: request.setUserPrincipal(new MockPrincipal("sean"));
236: // create a simple key
237: String window3 = "555-03";
238: ContentCacheKey cckey3 = contentCache.createCacheKey(context,
239: window3);
240: assertEquals(cckey3.getKey(), sessionId + "/desktop/555-03");
241:
242: // create a another key for desktop
243: String window4 = "555-04";
244: ContentCacheKey cckey4 = contentCache.createCacheKey(context,
245: window4);
246: assertEquals(cckey4.getKey(), sessionId + "/desktop/555-04");
247:
248: // create some PortletContent mock objects
249: PortletContent content3 = new MockPortletContent(cckey3, 300,
250: "ContentThree", "content3content3content3content3");
251: PortletContent content4 = new MockPortletContent(cckey4, 400,
252: "ContentTwo", "content4content4content4content4");
253:
254: // put it in the cache
255: CacheElement element3 = contentCache.createElement(cckey3,
256: content3);
257: contentCache.put(element3);
258: CacheElement element4 = contentCache.createElement(cckey4,
259: content4);
260: contentCache.put(element4);
261:
262: // assert 3 and 4
263: assertTrue(contentCache.isKeyInCache(cckey3));
264: assertTrue(contentCache.isKeyInCache(cckey4));
265:
266: // remove for user
267: contentCache.evictContentForSession(sessionId);
268: assertFalse(contentCache.isKeyInCache(cckey3));
269: assertFalse(contentCache.isKeyInCache(cckey4));
270: assertTrue(contentCache.isKeyInCache(cckey2));
271: }
272:
273: class MockPrincipal implements Principal {
274: private String name;
275:
276: public MockPrincipal(String name) {
277: this .name = name;
278: }
279:
280: public String getName() {
281: return name;
282: }
283: }
284:
285: class MockPortletContent implements PortletContent {
286: private boolean complete = false;
287: private ContentCacheKey cacheKey;
288: private int expiration = 0;
289: private String title;
290: private String content;
291:
292: MockPortletContent(ContentCacheKey cacheKey, int expiration,
293: String title, String content) {
294: this .cacheKey = cacheKey;
295: this .expiration = expiration;
296: this .title = title;
297: this .content = content;
298: }
299:
300: public PrintWriter getWriter() {
301: return null;
302: }
303:
304: public void init() {
305: }
306:
307: public void release() {
308: }
309:
310: public String toString() {
311: return content;
312: }
313:
314: public void writeTo(java.io.Writer out)
315: throws java.io.IOException {
316: }
317:
318: public char[] toCharArray() {
319: return content.toCharArray();
320: }
321:
322: public boolean isComplete() {
323: return complete;
324: }
325:
326: void setComplete(boolean state, boolean notify) {
327: this .complete = state;
328: }
329:
330: public String getContent() {
331: return toString();
332: }
333:
334: /**
335: * <p>
336: * complete
337: * </p>
338: *
339: * @see org.apache.jetspeed.aggregator.PortletContent#complete()
340: *
341: */
342: public void complete() {
343: setComplete(true, true);
344: }
345:
346: // error case, don't notify
347: public void completeWithError() {
348: setComplete(true, false);
349: }
350:
351: public ContentCacheKey getCacheKey() {
352: return cacheKey;
353: }
354:
355: public int getExpiration() {
356: return expiration;
357: }
358:
359: public void setExpiration(int expiration) {
360: this .expiration = expiration;
361: }
362:
363: public String getTitle() {
364: return title;
365: }
366:
367: public void setTitle(String title) {
368: this.title = title;
369: }
370: }
371:
372: }
|