1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // initialize ehCache
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          // initial Jetspeed caches
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          // create the mock request context
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          // create a simple key
73          String window1 = "/default-page.psml";
74          ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
75          assertEquals(cckey1.getKey(), "david/portal//default-page.psml");
76  
77          // create a another key for desktop
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          // create some PortletContent mock objects
84          MockTheme theme1 = new MockTheme("/default-page.psml");
85          MockTheme theme2 = new MockTheme("/about.psml");
86          
87          // put it in the cache
88          CacheElement element1 = contentCache.createElement(cckey1, theme1);
89          contentCache.put(element1);
90          CacheElement element2 = contentCache.createElement(cckey2, theme2);
91          contentCache.put(element2);
92          
93          // assert the gets
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         // assert isKey Apis        
102         assertTrue(contentCache.isKeyInCache(cckey1));
103 
104         
105         // test removes
106         contentCache.remove(cckey1);
107         assertFalse(contentCache.isKeyInCache(cckey1));        
108         assertTrue(contentCache.isKeyInCache(cckey2));
109         
110         // test user stuff
111         request.setUserPrincipal(new MockPrincipal("sean"));        
112         // create a simple key
113         String window3 = "/default-page.psml";
114         ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
115         assertEquals(cckey3.getKey(), "sean/desktop//default-page.psml");
116 
117         // create a another key for desktop
118         String window4 = "/about.psml";
119         ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
120         assertEquals(cckey4.getKey(), "sean/desktop//about.psml");
121         
122         // create some MockTheme objects
123         MockTheme theme3 = new MockTheme("/default-page.psml");
124         MockTheme theme4 = new MockTheme("/about.psml");
125         
126         // put it in the cache
127         CacheElement element3 = contentCache.createElement(cckey3, theme3);
128         contentCache.put(element3);
129         CacheElement element4 = contentCache.createElement(cckey4, theme4);
130         contentCache.put(element4);
131 
132         // assert 3 and 4
133         assertTrue(contentCache.isKeyInCache(cckey3));
134         assertTrue(contentCache.isKeyInCache(cckey4));
135         
136         // remove for user
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         // initialize ehCache
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         // initial Jetspeed caches
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         // create the mock request context
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         // create a simple key
170         String window1 = "/default-page.psml";
171         ContentCacheKey cckey1 = contentCache.createCacheKey(context, window1);
172         assertEquals(cckey1.getKey(), sessionId + "/portal//default-page.psml");
173 
174         // create a another key for desktop
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         // create some MockTheme objects
181         MockTheme theme1 = new MockTheme("/default-page.psml");
182         MockTheme theme2 = new MockTheme("/about.psml");
183         
184         // put it in the cache
185         CacheElement element1 = contentCache.createElement(cckey1, theme1);
186         contentCache.put(element1);
187         CacheElement element2 = contentCache.createElement(cckey2, theme2);
188         contentCache.put(element2);
189         
190         // assert the gets
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         // assert isKey Apis        
199         assertTrue(contentCache.isKeyInCache(cckey1));
200                 
201         
202         // test removes
203         contentCache.remove(cckey1);
204         assertFalse(contentCache.isKeyInCache(cckey1));        
205         assertTrue(contentCache.isKeyInCache(cckey2));
206         
207         // test user stuff
208         session = new MockHttpSession();
209         request.setSession(session);        
210         sessionId = session.getId();        
211         request.setUserPrincipal(new MockPrincipal("sean"));        
212         // create a simple key
213         String window3 = "/default-page.psml";
214         ContentCacheKey cckey3 = contentCache.createCacheKey(context, window3);
215         assertEquals(cckey3.getKey(), sessionId + "/desktop//default-page.psml");
216 
217         // create a another key for desktop
218         String window4 = "about.psml";
219         ContentCacheKey cckey4 = contentCache.createCacheKey(context, window4);
220         assertEquals(cckey4.getKey(), sessionId + "/desktop/about.psml");
221         
222         // create some PortletContent mock objects
223         MockTheme theme3 = new MockTheme("/default-page.psml");
224         MockTheme theme4 = new MockTheme("/about.psml");
225         
226         // put it in the cache
227         CacheElement element3 = contentCache.createElement(cckey3, theme3);
228         contentCache.put(element3);
229         CacheElement element4 = contentCache.createElement(cckey4, theme4);
230         contentCache.put(element4);
231 
232         // assert 3 and 4
233         assertTrue(contentCache.isKeyInCache(cckey3));
234         assertTrue(contentCache.isKeyInCache(cckey4));
235         
236         // remove for user
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 }