View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.web.includes;
35  
36  import java.util.HashSet;
37  import java.util.Locale;
38  import java.util.Set;
39  
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.plugin.PluginEvent;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.test.LuteceTestCase;
45  
46  public class LinksIncludeCacheServiceTest extends LuteceTestCase
47  {
48      private LinksIncludeCacheService service;
49      private boolean bOrigCacheStatus;
50  
51      @Override
52      protected void setUp( ) throws Exception
53      {
54          super.setUp( );
55          service = SpringContextService.getBean( LinksIncludeCacheService.SERVICE_NAME );
56          bOrigCacheStatus = service.isCacheEnable( );
57          service.enableCache( true );
58      }
59  
60      @Override
61      protected void tearDown( ) throws Exception
62      {
63          service.enableCache( bOrigCacheStatus );
64          super.tearDown( );
65      }
66  
67      public void testGetCacheKey( )
68      {
69          Set<String> keys = new HashSet<>( );
70          for ( int nMode : new int [ ] {
71                  0, 1
72          } )
73          {
74              for ( String strPage : new String [ ] {
75                      null, "test", "test2"
76              } )
77              {
78                  for ( Locale locale : new Locale [ ] {
79                          null, Locale.FRANCE, Locale.FRENCH
80                  } )
81                  {
82                      String key = service.getCacheKey( nMode, strPage, locale );
83                      assertTrue( "key " + key + " was already generated", keys.add( key ) );
84                  }
85              }
86          }
87      }
88  
89      public void testProcessPluginEvent( )
90      {
91          service.putInCache( "key", "value" );
92          assertEquals( "value", service.getFromCache( "key" ) );
93          service.processPluginEvent( null );
94          assertNull( service.getFromCache( "key" ) );
95          for ( Plugin plugin : new Plugin [ ] {
96                  null, PluginService.getCore( )
97          } )
98          {
99              for ( int eventType : new int [ ] {
100                     PluginEvent.PLUGIN_INSTALLED, PluginEvent.PLUGIN_POOL_CHANGED, PluginEvent.PLUGIN_UNINSTALLED
101             } )
102             {
103                 service.putInCache( "key", "value" );
104                 assertEquals( "value", service.getFromCache( "key" ) );
105                 PluginEvent event = new PluginEvent( plugin, eventType );
106                 service.processPluginEvent( event );
107                 assertNull( service.getFromCache( "key" ) );
108             }
109         }
110     }
111 
112 }