View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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          // activate the page cache
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          // restore the previous page cache status
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      * Tests that the base URL is correct when the page is cached
103      * and the URL used to access the site changes
104      * @throws SiteMessageException
105      */
106     @Test
107     public void testBaseUrl(  ) throws SiteMessageException
108     {
109         IPageService pageService = (IPageService) SpringContextService.getBean( "pageService" );
110         HttpServletResponse response = new MokeHttpServletResponse(  );
111 
112         // FIXME : rework when LUTECE-1837 is resolved
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         // change server name and thus base_url
129         request = new MokeHttpServletRequest(  )
130                 {
131                     @Override
132                     public String getServerName(  )
133                     {
134                         return "junit";
135                     }
136 
137                     // FIXME : rework when LUTECE-1837 is resolved
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     // FIXME : remove when LUTECE-1838 is resolved
151     private static final class MokeHttpServletResponse implements HttpServletResponse
152     {
153         @Override
154         public void setLocale( Locale loc )
155         {
156             // TODO Auto-generated method stub
157         }
158 
159         @Override
160         public void setContentType( String type )
161         {
162             // TODO Auto-generated method stub
163         }
164 
165         @Override
166         public void setContentLength( int len )
167         {
168             // TODO Auto-generated method stub
169         }
170 
171         @Override
172         public void setCharacterEncoding( String charset )
173         {
174             // TODO Auto-generated method stub
175         }
176 
177         @Override
178         public void setBufferSize( int size )
179         {
180             // TODO Auto-generated method stub
181         }
182 
183         @Override
184         public void resetBuffer(  )
185         {
186             // TODO Auto-generated method stub
187         }
188 
189         @Override
190         public void reset(  )
191         {
192             // TODO Auto-generated method stub
193         }
194 
195         @Override
196         public boolean isCommitted(  )
197         {
198             // TODO Auto-generated method stub
199             return false;
200         }
201 
202         @Override
203         public PrintWriter getWriter(  ) throws IOException
204         {
205             // TODO Auto-generated method stub
206             return null;
207         }
208 
209         @Override
210         public ServletOutputStream getOutputStream(  )
211             throws IOException
212         {
213             // TODO Auto-generated method stub
214             return null;
215         }
216 
217         @Override
218         public Locale getLocale(  )
219         {
220             // TODO Auto-generated method stub
221             return null;
222         }
223 
224         @Override
225         public String getContentType(  )
226         {
227             // TODO Auto-generated method stub
228             return null;
229         }
230 
231         @Override
232         public String getCharacterEncoding(  )
233         {
234             // TODO Auto-generated method stub
235             return null;
236         }
237 
238         @Override
239         public int getBufferSize(  )
240         {
241             // TODO Auto-generated method stub
242             return 0;
243         }
244 
245         @Override
246         public void flushBuffer(  ) throws IOException
247         {
248             // TODO Auto-generated method stub
249         }
250 
251         @Override
252         public void setStatus( int sc, String sm )
253         {
254             // TODO Auto-generated method stub
255         }
256 
257         @Override
258         public void setStatus( int sc )
259         {
260             // TODO Auto-generated method stub
261         }
262 
263         @Override
264         public void setIntHeader( String name, int value )
265         {
266             // TODO Auto-generated method stub
267         }
268 
269         @Override
270         public void setHeader( String name, String value )
271         {
272             // TODO Auto-generated method stub
273         }
274 
275         @Override
276         public void setDateHeader( String name, long date )
277         {
278             // TODO Auto-generated method stub
279         }
280 
281         @Override
282         public void sendRedirect( String location ) throws IOException
283         {
284             // TODO Auto-generated method stub
285         }
286 
287         @Override
288         public void sendError( int sc, String msg ) throws IOException
289         {
290             // TODO Auto-generated method stub
291         }
292 
293         @Override
294         public void sendError( int sc ) throws IOException
295         {
296             // TODO Auto-generated method stub
297         }
298 
299         @Override
300         public String encodeUrl( String url )
301         {
302             // TODO Auto-generated method stub
303             return null;
304         }
305 
306         @Override
307         public String encodeURL( String url )
308         {
309             // TODO Auto-generated method stub
310             return null;
311         }
312 
313         @Override
314         public String encodeRedirectUrl( String url )
315         {
316             // TODO Auto-generated method stub
317             return null;
318         }
319 
320         @Override
321         public String encodeRedirectURL( String url )
322         {
323             // TODO Auto-generated method stub
324             return null;
325         }
326 
327         @Override
328         public boolean containsHeader( String name )
329         {
330             // TODO Auto-generated method stub
331             return false;
332         }
333 
334         @Override
335         public void addIntHeader( String name, int value )
336         {
337             // TODO Auto-generated method stub
338         }
339 
340         @Override
341         public void addHeader( String name, String value )
342         {
343             // TODO Auto-generated method stub
344         }
345 
346         @Override
347         public void addDateHeader( String name, long date )
348         {
349             // TODO Auto-generated method stub
350         }
351 
352         @Override
353         public void addCookie( Cookie cookie )
354         {
355             // TODO Auto-generated method stub
356         }
357     }
358 }