1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.cache;
35
36 import java.math.BigInteger;
37 import java.security.SecureRandom;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Random;
42 import java.util.Set;
43
44 import org.springframework.mock.web.MockHttpServletRequest;
45
46 import fr.paris.lutece.portal.business.page.Page;
47 import fr.paris.lutece.portal.service.page.PageEvent;
48 import fr.paris.lutece.portal.service.page.PageService;
49 import fr.paris.lutece.portal.service.portal.PortalService;
50 import fr.paris.lutece.portal.service.spring.SpringContextService;
51 import fr.paris.lutece.portal.web.constants.Parameters;
52 import fr.paris.lutece.test.LuteceTestCase;
53
54 public class PathCacheServiceEnabledTest extends LuteceTestCase
55 {
56
57 private static final String BEAN_PAGE_SERVICE = "pageService";
58
59 IPathCacheService service;
60 boolean bEnabled;
61
62 @Override
63 protected void setUp( ) throws Exception
64 {
65 super.setUp( );
66 service = null;
67 List<CacheableService> serviceList = CacheService.getCacheableServicesList( );
68 for ( CacheableService aService : serviceList )
69 {
70 if ( aService instanceof IPathCacheService )
71 {
72 service = (IPathCacheService) aService;
73 bEnabled = aService.isCacheEnable( );
74 aService.enableCache( true );
75 aService.resetCache( );
76 break;
77 }
78 }
79 assertNotNull( service );
80 }
81
82 @Override
83 protected void tearDown( ) throws Exception
84 {
85 List<CacheableService> serviceList = CacheService.getCacheableServicesList( );
86 for ( CacheableService aService : serviceList )
87 {
88 if ( aService == service )
89 {
90 aService.resetCache( );
91 aService.enableCache( bEnabled );
92 break;
93 }
94 }
95 service = null;
96 super.tearDown( );
97 }
98
99 public void testGetKey( )
100 {
101 Set<String> keys = new HashSet<>( );
102 for ( String xpageName : new String [ ] {
103 "name", "name2"
104 } )
105 {
106 for ( int mode : new int [ ] {
107 0, 1, 2
108 } )
109 {
110 String key = service.getKey( xpageName, mode, null );
111 checkKey( keys, key );
112 MockHttpServletRequest request = new MockHttpServletRequest( );
113 request.addPreferredLocale( Locale.FRANCE );
114 key = service.getKey( xpageName, mode, request );
115 checkKey( keys, key );
116 request = new MockHttpServletRequest( );
117 request.addPreferredLocale( Locale.ITALY );
118 key = service.getKey( xpageName, mode, request );
119 checkKey( keys, key );
120 request = new MockHttpServletRequest( );
121 request.addParameter( Parameters.PAGE_ID, "1" );
122 key = service.getKey( xpageName, mode, request );
123 checkKey( keys, key );
124 request = new MockHttpServletRequest( );
125 request.addParameter( Parameters.PAGE_ID, "2" );
126 key = service.getKey( xpageName, mode, request );
127 checkKey( keys, key );
128 request = new MockHttpServletRequest( );
129 request.addParameter( Parameters.PORTLET_ID, "1" );
130 key = service.getKey( xpageName, mode, request );
131 checkKey( keys, key );
132 request = new MockHttpServletRequest( );
133 request.addParameter( Parameters.PORTLET_ID, "2" );
134 key = service.getKey( xpageName, mode, request );
135 checkKey( keys, key );
136 }
137 }
138 }
139
140 public void testGetKeyWithTitleUrls( )
141 {
142 Set<String> keys = new HashSet<>( );
143 for ( String xpageName : new String [ ] {
144 "name", "name2"
145 } )
146 {
147 for ( int mode : new int [ ] {
148 0, 1, 2
149 } )
150 {
151 for ( String titleUrls : new String [ ] {
152 null, "title1", "title2"
153 } )
154 {
155 String key = service.getKey( xpageName, mode, titleUrls, null );
156 checkKey( keys, key );
157 MockHttpServletRequest request = new MockHttpServletRequest( );
158 request.addPreferredLocale( Locale.FRANCE );
159 key = service.getKey( xpageName, mode, titleUrls, request );
160 checkKey( keys, key );
161 request = new MockHttpServletRequest( );
162 request.addPreferredLocale( Locale.ITALY );
163 key = service.getKey( xpageName, mode, titleUrls, request );
164 checkKey( keys, key );
165 request = new MockHttpServletRequest( );
166 request.addParameter( Parameters.PAGE_ID, "1" );
167 key = service.getKey( xpageName, mode, titleUrls, request );
168 checkKey( keys, key );
169 request = new MockHttpServletRequest( );
170 request.addParameter( Parameters.PAGE_ID, "2" );
171 key = service.getKey( xpageName, mode, titleUrls, request );
172 checkKey( keys, key );
173 request = new MockHttpServletRequest( );
174 request.addParameter( Parameters.PORTLET_ID, "1" );
175 key = service.getKey( xpageName, mode, titleUrls, request );
176 checkKey( keys, key );
177 request = new MockHttpServletRequest( );
178 request.addParameter( Parameters.PORTLET_ID, "2" );
179 key = service.getKey( xpageName, mode, titleUrls, request );
180 checkKey( keys, key );
181 }
182 }
183 }
184 }
185
186 public void testPutAndGetFromCache( )
187 {
188 service.putInCache( null, "junit" );
189 String key = service.getKey( "junit", 0, null );
190 service.putInCache( key, "junit" );
191 assertEquals( "junit", service.getFromCache( key ) );
192 assertNull( service.getFromCache( null ) );
193 assertNull( service.getFromCache( "NotInCache" ) );
194 }
195
196 public void testProcessPageEvent( )
197 {
198 String key = service.getKey( "junit", 0, null );
199 service.putInCache( key, "junit" );
200 PageEvent event = new PageEvent( new Page( ), PageEvent.PAGE_CREATED );
201 ( (PathCacheService) service ).processPageEvent( event );
202 assertEquals( "junit", service.getFromCache( key ) );
203 for ( int nEventType : new int [ ] {
204 PageEvent.PAGE_CONTENT_MODIFIED, PageEvent.PAGE_DELETED, PageEvent.PAGE_MOVED, PageEvent.PAGE_STATE_CHANGED
205 } )
206 {
207 service.putInCache( key, "junit" );
208 event = new PageEvent( new Page( ), nEventType );
209 ( (PathCacheService) service ).processPageEvent( event );
210 assertNull( service.getFromCache( key ) );
211 }
212 }
213
214 public void testRegisteredPageEventListener( )
215 {
216 String key = service.getKey( "junit", 0, null );
217 service.putInCache( key, "junit" );
218 PageService pageService = SpringContextService.getBean( BEAN_PAGE_SERVICE );
219 Page page = new Page( );
220 page.setName( getRandomName( ) );
221 page.setDescription( page.getName( ) );
222 page.setParentPageId( PortalService.getRootPageId( ) );
223 pageService.createPage( page );
224 try
225 {
226
227
228
229 assertNull( service.getFromCache( key ) );
230 service.putInCache( key, "junit" );
231 page.setDescription( page.getName( ) + page.getName( ) );
232 pageService.updatePage( page );
233 assertNull( service.getFromCache( key ) );
234 service.putInCache( key, "junit" );
235 }
236 finally
237 {
238 pageService.removePage( page.getId( ) );
239 assertNull( service.getFromCache( key ) );
240 }
241 }
242
243 private String getRandomName( )
244 {
245 Random rand = new SecureRandom( );
246 BigInteger bigInt = new BigInteger( 128, rand );
247 return "junit" + bigInt.toString( 36 );
248 }
249
250 private void checkKey( Set<String> keys, String key )
251 {
252 assertNotNull( key );
253 assertFalse( keys.contains( key ) );
254 keys.add( key );
255 }
256 }