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.page;
35
36 import fr.paris.lutece.portal.service.cache.CacheService;
37 import fr.paris.lutece.portal.service.cache.CacheableService;
38 import fr.paris.lutece.portal.service.message.SiteMessageException;
39 import fr.paris.lutece.portal.service.spring.SpringContextService;
40 import fr.paris.lutece.portal.web.LocalVariables;
41 import fr.paris.lutece.portal.web.constants.Parameters;
42 import fr.paris.lutece.test.LuteceTestCase;
43 import fr.paris.lutece.test.MokeHttpServletRequest;
44
45 import org.junit.Test;
46
47 import java.io.IOException;
48 import java.io.PrintWriter;
49
50 import java.util.List;
51 import java.util.Locale;
52
53 import javax.servlet.ServletOutputStream;
54 import javax.servlet.http.Cookie;
55 import javax.servlet.http.HttpServletResponse;
56
57
58 public class PageServiceTest extends LuteceTestCase
59 {
60 private boolean _pageCacheStatus;
61
62 @Override
63 public void setUp( ) throws Exception
64 {
65 super.setUp( );
66
67
68 List<CacheableService> services = CacheService.getCacheableServicesList( );
69
70 for ( CacheableService cs : services )
71 {
72 if ( cs instanceof PageCacheService )
73 {
74 _pageCacheStatus = cs.isCacheEnable( );
75 cs.enableCache( true );
76
77 break;
78 }
79 }
80 }
81
82 @Override
83 public void tearDown( ) throws Exception
84 {
85
86 List<CacheableService> services = CacheService.getCacheableServicesList( );
87
88 for ( CacheableService cs : services )
89 {
90 if ( cs instanceof PageCacheService )
91 {
92 cs.enableCache( _pageCacheStatus );
93
94 break;
95 }
96 }
97
98 super.tearDown( );
99 }
100
101
102
103
104
105
106 @Test
107 public void testBaseUrl( ) throws SiteMessageException
108 {
109 IPageService pageService = (IPageService) SpringContextService.getBean( "pageService" );
110 HttpServletResponse response = new MokeHttpServletResponse( );
111
112
113 MokeHttpServletRequest request = new MokeHttpServletRequest( )
114 {
115 @Override
116 public Object getAttribute( String string )
117 {
118 return null;
119 }
120 };
121
122 LocalVariables.setLocal( null, request, response );
123 request.addMokeParameters( Parameters.PAGE_ID, "1" );
124
125 String pageContent = pageService.getPage( request, 0 );
126 assertTrue( pageContent.contains( "<base href=\"://:0/\">" ) );
127
128
129 request = new MokeHttpServletRequest( )
130 {
131 @Override
132 public String getServerName( )
133 {
134 return "junit";
135 }
136
137
138 @Override
139 public Object getAttribute( String string )
140 {
141 return null;
142 }
143 };
144 LocalVariables.setLocal( null, request, response );
145 request.addMokeParameters( Parameters.PAGE_ID, "1" );
146 pageContent = pageService.getPage( request, 0 );
147 assertTrue( pageContent.contains( "<base href=\"://junit:0/\">" ) );
148 }
149
150
151 private static final class MokeHttpServletResponse implements HttpServletResponse
152 {
153 @Override
154 public void setLocale( Locale loc )
155 {
156
157 }
158
159 @Override
160 public void setContentType( String type )
161 {
162
163 }
164
165 @Override
166 public void setContentLength( int len )
167 {
168
169 }
170
171 @Override
172 public void setCharacterEncoding( String charset )
173 {
174
175 }
176
177 @Override
178 public void setBufferSize( int size )
179 {
180
181 }
182
183 @Override
184 public void resetBuffer( )
185 {
186
187 }
188
189 @Override
190 public void reset( )
191 {
192
193 }
194
195 @Override
196 public boolean isCommitted( )
197 {
198
199 return false;
200 }
201
202 @Override
203 public PrintWriter getWriter( ) throws IOException
204 {
205
206 return null;
207 }
208
209 @Override
210 public ServletOutputStream getOutputStream( )
211 throws IOException
212 {
213
214 return null;
215 }
216
217 @Override
218 public Locale getLocale( )
219 {
220
221 return null;
222 }
223
224 @Override
225 public String getContentType( )
226 {
227
228 return null;
229 }
230
231 @Override
232 public String getCharacterEncoding( )
233 {
234
235 return null;
236 }
237
238 @Override
239 public int getBufferSize( )
240 {
241
242 return 0;
243 }
244
245 @Override
246 public void flushBuffer( ) throws IOException
247 {
248
249 }
250
251 @Override
252 public void setStatus( int sc, String sm )
253 {
254
255 }
256
257 @Override
258 public void setStatus( int sc )
259 {
260
261 }
262
263 @Override
264 public void setIntHeader( String name, int value )
265 {
266
267 }
268
269 @Override
270 public void setHeader( String name, String value )
271 {
272
273 }
274
275 @Override
276 public void setDateHeader( String name, long date )
277 {
278
279 }
280
281 @Override
282 public void sendRedirect( String location ) throws IOException
283 {
284
285 }
286
287 @Override
288 public void sendError( int sc, String msg ) throws IOException
289 {
290
291 }
292
293 @Override
294 public void sendError( int sc ) throws IOException
295 {
296
297 }
298
299 @Override
300 public String encodeUrl( String url )
301 {
302
303 return null;
304 }
305
306 @Override
307 public String encodeURL( String url )
308 {
309
310 return null;
311 }
312
313 @Override
314 public String encodeRedirectUrl( String url )
315 {
316
317 return null;
318 }
319
320 @Override
321 public String encodeRedirectURL( String url )
322 {
323
324 return null;
325 }
326
327 @Override
328 public boolean containsHeader( String name )
329 {
330
331 return false;
332 }
333
334 @Override
335 public void addIntHeader( String name, int value )
336 {
337
338 }
339
340 @Override
341 public void addHeader( String name, String value )
342 {
343
344 }
345
346 @Override
347 public void addDateHeader( String name, long date )
348 {
349
350 }
351
352 @Override
353 public void addCookie( Cookie cookie )
354 {
355
356 }
357 }
358 }