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.io.BufferedWriter;
37  import java.io.File;
38  import java.io.FileInputStream;
39  import java.io.FileNotFoundException;
40  import java.io.FileWriter;
41  import java.io.IOException;
42  import java.io.InputStream;
43  import java.util.ArrayList;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import javax.servlet.ServletContext;
49  import javax.servlet.http.HttpServletRequest;
50  
51  import org.springframework.mock.web.MockHttpServletRequest;
52  import org.springframework.mock.web.MockServletContext;
53  
54  import fr.paris.lutece.portal.service.cache.CacheService;
55  import fr.paris.lutece.portal.service.cache.CacheableService;
56  import fr.paris.lutece.portal.service.content.PageData;
57  import fr.paris.lutece.portal.service.plugin.PluginService;
58  import fr.paris.lutece.portal.service.util.AppPathService;
59  import fr.paris.lutece.test.LuteceTestCase;
60  
61  public abstract class LinksIncludeTest extends LuteceTestCase
62  {
63      private static final String PLUGIN_NAME = "linksIncludeTestPlugin";
64      private boolean bOrigCacheEnabled;
65  
66      public abstract boolean enableCache( );
67  
68      @Override
69      protected void setUp( ) throws Exception
70      {
71          super.setUp( );
72          File dirPlugin = new File( AppPathService.getPath( "path.plugins" ) );
73          File testPluginFile = new File( dirPlugin, PLUGIN_NAME + ".xml" );
74          try ( BufferedWriter writer = new BufferedWriter( new FileWriter( testPluginFile ) ) )
75          {
76              writer.write( "<plug-in><name>" + PLUGIN_NAME + "</name><class>" + LinksIncludeTestPlugin.class.getName( ) + "</class>"
77                      + "<icon-url>../../images/admin/skin/plugins/myplugin/myplugin.gif</icon-url></plug-in>" );
78          }
79          PluginService.init( );
80          PluginService.getPlugin( PLUGIN_NAME ).install( );
81          List<CacheableService> caches = CacheService.getCacheableServicesList( );
82          for ( CacheableService cacheService : caches )
83          {
84              if ( LinksIncludeCacheService.SERVICE_NAME.equals( cacheService.getName( ) ) )
85              {
86                  bOrigCacheEnabled = cacheService.isCacheEnable( );
87                  cacheService.enableCache( enableCache( ) );
88                  break;
89              }
90          }
91      }
92  
93      @Override
94      protected void tearDown( ) throws Exception
95      {
96          List<CacheableService> caches = CacheService.getCacheableServicesList( );
97          for ( CacheableService cacheService : caches )
98          {
99              if ( LinksIncludeCacheService.SERVICE_NAME.equals( cacheService.getName( ) ) )
100             {
101                 cacheService.enableCache( bOrigCacheEnabled );
102                 break;
103             }
104         }
105         PluginService.getPlugin( PLUGIN_NAME ).uninstall( );
106         File dirPlugin = new File( AppPathService.getPath( "path.plugins" ) );
107         File testPluginFile = new File( dirPlugin, PLUGIN_NAME + ".xml" );
108         testPluginFile.delete( );
109         PluginService.init( );
110         super.tearDown( );
111     }
112 
113     public void testFillTemplateNull( )
114     {
115         try
116         {
117             LinksInclude include = new LinksInclude( );
118             include.fillTemplate( null, null, 0, null );
119             // did not throw
120         }
121         catch( Exception e )
122         {
123             fail( );
124         }
125     }
126 
127     public void testGetURICssAddPrefix( )
128     {
129         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
130         List<String> styleSheets = new ArrayList<>( );
131         styleSheets.add( PLUGIN_NAME + "/style.css" );
132         plugin.setCssStyleSheets( styleSheets );
133         plugin.setCssStyleSheetsScopePortal( true );
134         LinksInclude include = new LinksInclude( );
135         Map<String, Object> rootModel = new HashMap<>( );
136         PageData data = new PageData( );
137         int nMode = 0;
138         HttpServletRequest request = new MockHttpServletRequest( );
139         include.fillTemplate( rootModel, data, nMode, request );
140         String cssLinks = (String) rootModel.get( "plugins_css_links" );
141         assertEquals( "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/style.css\" type=\"text/css\"  media=\"screen\" />",
142                 cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
143     }
144 
145     public void testGetURICssAbsoluteHttp( )
146     {
147         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
148         List<String> styleSheets = new ArrayList<>( );
149         styleSheets.add( "http://example.com/style.css" );
150         plugin.setCssStyleSheets( styleSheets );
151         plugin.setCssStyleSheetsScopePortal( true );
152         LinksInclude include = new LinksInclude( );
153         Map<String, Object> rootModel = new HashMap<>( );
154         PageData data = new PageData( );
155         int nMode = 0;
156         HttpServletRequest request = new MockHttpServletRequest( );
157         include.fillTemplate( rootModel, data, nMode, request );
158         String cssLinks = (String) rootModel.get( "plugins_css_links" );
159         assertEquals( "<link rel=\"stylesheet\"  href=\"http://example.com/style.css\" type=\"text/css\"  media=\"screen\" />",
160                 cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
161     }
162 
163     public void testGetURICssAbsoluteHttps( )
164     {
165         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
166         List<String> styleSheets = new ArrayList<>( );
167         styleSheets.add( "https://example.com/style.css" );
168         plugin.setCssStyleSheets( styleSheets );
169         plugin.setCssStyleSheetsScopePortal( true );
170         LinksInclude include = new LinksInclude( );
171         Map<String, Object> rootModel = new HashMap<>( );
172         PageData data = new PageData( );
173         int nMode = 0;
174         HttpServletRequest request = new MockHttpServletRequest( );
175         include.fillTemplate( rootModel, data, nMode, request );
176         String cssLinks = (String) rootModel.get( "plugins_css_links" );
177         assertEquals( "<link rel=\"stylesheet\"  href=\"https://example.com/style.css\" type=\"text/css\"  media=\"screen\" />",
178                 cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
179     }
180 
181     public void testGetURICssProtocolRelative( )
182     {
183         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
184         List<String> styleSheets = new ArrayList<>( );
185         styleSheets.add( "//example.com/style.css" );
186         plugin.setCssStyleSheets( styleSheets );
187         plugin.setCssStyleSheetsScopePortal( true );
188         LinksInclude include = new LinksInclude( );
189         Map<String, Object> rootModel = new HashMap<>( );
190         PageData data = new PageData( );
191         int nMode = 0;
192         HttpServletRequest request = new MockHttpServletRequest( );
193         include.fillTemplate( rootModel, data, nMode, request );
194         String cssLinks = (String) rootModel.get( "plugins_css_links" );
195         assertEquals( "<link rel=\"stylesheet\"  href=\"//example.com/style.css\" type=\"text/css\"  media=\"screen\" />",
196                 cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
197     }
198 
199     public void testGetURICssHash( ) throws IOException
200     {
201         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
202         List<String> styleSheets = new ArrayList<>( );
203         styleSheets.add( "linksIncludeTestPlugin/junithashed.css" );
204         File hashedFile = new File( getResourcesDir( ), "css/plugins/linksIncludeTestPlugin/junithashed.css" );
205         System.out.println( hashedFile.toString( ) );
206         hashedFile.getParentFile( ).mkdirs( );
207         try ( FileWriter writer = new FileWriter( hashedFile ) )
208         {
209             writer.write( "abcd" );
210         }
211         try
212         {
213             plugin.setCssStyleSheets( styleSheets );
214             plugin.setCssStyleSheetsScopePortal( true );
215             LinksInclude include = new LinksInclude( );
216             Map<String, Object> rootModel = new HashMap<>( );
217             PageData data = new PageData( );
218             int nMode = 0;
219             ServletContext servletContext = new MockServletContext( )
220             {
221 
222                 @Override
223                 public InputStream getResourceAsStream( String path )
224                 {
225                     File file = new File( getResourcesDir( ), path );
226                     if ( file.exists( ) )
227                     {
228                         try
229                         {
230                             return new FileInputStream( file );
231                         }
232                         catch( FileNotFoundException e )
233                         {
234                             return null;
235                         }
236                     }
237                     return null;
238                 }
239 
240             };
241             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
242             include.fillTemplate( rootModel, data, nMode, request );
243             String cssLinks = (String) rootModel.get( "plugins_css_links" );
244             assertEquals(
245                     "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/junithashed.css?lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\" type=\"text/css\"  media=\"screen\" />",
246                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
247             try ( FileWriter writer = new FileWriter( hashedFile ) )
248             {
249                 writer.write( "bbcd" );
250             }
251             include.fillTemplate( rootModel, data, nMode, request );
252             String cssLinks2 = (String) rootModel.get( "plugins_css_links" );
253             assertNotNull( cssLinks2 );
254             if ( enableCache( ) )
255             {
256                 assertSame( cssLinks, cssLinks2 );
257             }
258             else
259             {
260                 assertFalse( cssLinks.equals( cssLinks2 ) );
261                 assertEquals(
262                         "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/junithashed.css?lutece_h=531ba794ef006cd3d69cf1acb33ddeccf8d6c655fb08f469335f8c2c32e2ab68\" type=\"text/css\"  media=\"screen\" />",
263                         cssLinks2.replace( "\n", "" ).replace( "\r", "" ) );
264             }
265         }
266         finally
267         {
268             hashedFile.delete( );
269         }
270     }
271 
272     public void testGetURICssHashQuery( ) throws IOException
273     {
274         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
275         List<String> styleSheets = new ArrayList<>( );
276         styleSheets.add( "linksIncludeTestPlugin/junithashed.css?arg=value" );
277         File hashedFile = new File( getResourcesDir( ), "css/plugins/linksIncludeTestPlugin/junithashed.css" );
278         System.out.println( hashedFile.toString( ) );
279         hashedFile.getParentFile( ).mkdirs( );
280         try ( FileWriter writer = new FileWriter( hashedFile ) )
281         {
282             writer.write( "abcd" );
283         }
284         try
285         {
286             plugin.setCssStyleSheets( styleSheets );
287             plugin.setCssStyleSheetsScopePortal( true );
288             LinksInclude include = new LinksInclude( );
289             Map<String, Object> rootModel = new HashMap<>( );
290             PageData data = new PageData( );
291             int nMode = 0;
292             ServletContext servletContext = new MockServletContext( )
293             {
294 
295                 @Override
296                 public InputStream getResourceAsStream( String path )
297                 {
298                     File file = new File( getResourcesDir( ), path );
299                     if ( file.exists( ) )
300                     {
301                         try
302                         {
303                             return new FileInputStream( file );
304                         }
305                         catch( FileNotFoundException e )
306                         {
307                             return null;
308                         }
309                     }
310                     return null;
311                 }
312 
313             };
314             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
315             include.fillTemplate( rootModel, data, nMode, request );
316             String cssLinks = (String) rootModel.get( "plugins_css_links" );
317             assertEquals(
318                     "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/junithashed.css?arg=value&lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\" type=\"text/css\"  media=\"screen\" />",
319                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
320         }
321         finally
322         {
323             hashedFile.delete( );
324         }
325     }
326 
327     public void testGetURICssHashDigestError( ) throws IOException
328     {
329         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
330         List<String> styleSheets = new ArrayList<>( );
331         styleSheets.add( "linksIncludeTestPlugin/junithashed.css" );
332         File hashedFile = new File( getResourcesDir( ), "css/plugins/linksIncludeTestPlugin/junithashed.css" );
333         System.out.println( hashedFile.toString( ) );
334         hashedFile.getParentFile( ).mkdirs( );
335         try ( FileWriter writer = new FileWriter( hashedFile ) )
336         {
337             writer.write( "abcd" );
338         }
339         try
340         {
341             plugin.setCssStyleSheets( styleSheets );
342             plugin.setCssStyleSheetsScopePortal( true );
343             LinksInclude include = new LinksInclude( );
344             Map<String, Object> rootModel = new HashMap<>( );
345             PageData data = new PageData( );
346             int nMode = 0;
347             ServletContext servletContext = new MockServletContext( )
348             {
349 
350                 @Override
351                 public InputStream getResourceAsStream( String path )
352                 {
353                     File file = new File( getResourcesDir( ), path );
354                     if ( file.exists( ) )
355                     {
356                         return new InputStream( )
357                         {
358 
359                             @Override
360                             public int read( ) throws IOException
361                             {
362                                 throw new IOException( "Mocking IO error" );
363                             }
364                         };
365                     }
366                     return null;
367                 }
368 
369             };
370             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
371             include.fillTemplate( rootModel, data, nMode, request );
372             String cssLinks = (String) rootModel.get( "plugins_css_links" );
373             assertEquals( "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/junithashed.css\" type=\"text/css\"  media=\"screen\" />",
374                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
375         }
376         finally
377         {
378             hashedFile.delete( );
379         }
380     }
381 
382     public void testGetURICssHashStreamCloseError( ) throws IOException
383     {
384         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
385         List<String> styleSheets = new ArrayList<>( );
386         styleSheets.add( "linksIncludeTestPlugin/junithashed.css" );
387         File hashedFile = new File( getResourcesDir( ), "css/plugins/linksIncludeTestPlugin/junithashed.css" );
388         System.out.println( hashedFile.toString( ) );
389         hashedFile.getParentFile( ).mkdirs( );
390         try ( FileWriter writer = new FileWriter( hashedFile ) )
391         {
392             writer.write( "abcd" );
393         }
394         try
395         {
396             plugin.setCssStyleSheets( styleSheets );
397             plugin.setCssStyleSheetsScopePortal( true );
398             LinksInclude include = new LinksInclude( );
399             Map<String, Object> rootModel = new HashMap<>( );
400             PageData data = new PageData( );
401             int nMode = 0;
402             ServletContext servletContext = new MockServletContext( )
403             {
404 
405                 @Override
406                 public InputStream getResourceAsStream( String path )
407                 {
408                     File file = new File( getResourcesDir( ), path );
409                     if ( file.exists( ) )
410                     {
411                         try
412                         {
413                             return new FileInputStream( file )
414                             {
415 
416                                 @Override
417                                 public void close( ) throws IOException
418                                 {
419                                     throw new IOException( "Mocking IO error on close" );
420                                 }
421 
422                             };
423                         }
424                         catch( FileNotFoundException e )
425                         {
426                             return null;
427                         }
428                     }
429                     return null;
430                 }
431 
432             };
433             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
434             include.fillTemplate( rootModel, data, nMode, request );
435             String cssLinks = (String) rootModel.get( "plugins_css_links" );
436             assertEquals(
437                     "<link rel=\"stylesheet\"  href=\"css/plugins/linksIncludeTestPlugin/junithashed.css?lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\" type=\"text/css\"  media=\"screen\" />",
438                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
439         }
440         finally
441         {
442             hashedFile.delete( );
443         }
444     }
445 
446     public void testGetURICssBadURI( )
447     {
448         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
449         List<String> styleSheets = new ArrayList<>( );
450         styleSheets.add( "://style.css" );
451         plugin.setCssStyleSheets( styleSheets );
452         plugin.setCssStyleSheetsScopePortal( true );
453         LinksInclude include = new LinksInclude( );
454         Map<String, Object> rootModel = new HashMap<>( );
455         PageData data = new PageData( );
456         int nMode = 0;
457         HttpServletRequest request = new MockHttpServletRequest( );
458         include.fillTemplate( rootModel, data, nMode, request );
459         String cssLinks = (String) rootModel.get( "plugins_css_links" );
460         assertEquals( "", cssLinks );
461     }
462 
463     // Javscript link
464 
465     public void testGetURIJavascriptAddPrefix( )
466     {
467         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
468         List<String> javascripts = new ArrayList<>( );
469         javascripts.add( PLUGIN_NAME + "/script.js" );
470         plugin.setJavascriptFiles( javascripts );
471         plugin.setJavascriptFilesScopePortal( true );
472         LinksInclude include = new LinksInclude( );
473         Map<String, Object> rootModel = new HashMap<>( );
474         PageData data = new PageData( );
475         int nMode = 0;
476         HttpServletRequest request = new MockHttpServletRequest( );
477         include.fillTemplate( rootModel, data, nMode, request );
478         String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
479         assertEquals( "<script src=\"js/plugins/linksIncludeTestPlugin/script.js\"></script>", cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
480     }
481 
482     public void testGetURIJavascriptAbsoluteHttp( )
483     {
484         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
485         List<String> javascripts = new ArrayList<>( );
486         javascripts.add( "http://example.com/script.js" );
487         plugin.setJavascriptFiles( javascripts );
488         plugin.setJavascriptFilesScopePortal( true );
489         LinksInclude include = new LinksInclude( );
490         Map<String, Object> rootModel = new HashMap<>( );
491         PageData data = new PageData( );
492         int nMode = 0;
493         HttpServletRequest request = new MockHttpServletRequest( );
494         include.fillTemplate( rootModel, data, nMode, request );
495         String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
496         assertEquals( "<script src=\"http://example.com/script.js\"></script>", cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
497     }
498 
499     public void testGetURIJavascriptAbsoluteHttps( )
500     {
501         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
502         List<String> javascripts = new ArrayList<>( );
503         javascripts.add( "https://example.com/script.js" );
504         plugin.setJavascriptFiles( javascripts );
505         plugin.setJavascriptFilesScopePortal( true );
506         LinksInclude include = new LinksInclude( );
507         Map<String, Object> rootModel = new HashMap<>( );
508         PageData data = new PageData( );
509         int nMode = 0;
510         HttpServletRequest request = new MockHttpServletRequest( );
511         include.fillTemplate( rootModel, data, nMode, request );
512         String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
513         assertEquals( "<script src=\"https://example.com/script.js\"></script>", cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
514     }
515 
516     public void testGetURIJavascriptProtocolRelative( )
517     {
518         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
519         List<String> javascripts = new ArrayList<>( );
520         javascripts.add( "//example.com/script.js" );
521         plugin.setJavascriptFiles( javascripts );
522         plugin.setJavascriptFilesScopePortal( true );
523         LinksInclude include = new LinksInclude( );
524         Map<String, Object> rootModel = new HashMap<>( );
525         PageData data = new PageData( );
526         int nMode = 0;
527         HttpServletRequest request = new MockHttpServletRequest( );
528         include.fillTemplate( rootModel, data, nMode, request );
529         String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
530         assertEquals( "<script src=\"//example.com/script.js\"></script>", cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
531     }
532 
533     public void testGetURIJavascriptHash( ) throws IOException
534     {
535         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
536         List<String> javascripts = new ArrayList<>( );
537         javascripts.add( "linksIncludeTestPlugin/scripthashed.js" );
538         File hashedFile = new File( getResourcesDir( ), "js/plugins/linksIncludeTestPlugin/scripthashed.js" );
539         hashedFile.getParentFile( ).mkdirs( );
540         try ( FileWriter writer = new FileWriter( hashedFile ) )
541         {
542             writer.write( "abcd" );
543         }
544         try
545         {
546             plugin.setJavascriptFiles( javascripts );
547             plugin.setJavascriptFilesScopePortal( true );
548             LinksInclude include = new LinksInclude( );
549             Map<String, Object> rootModel = new HashMap<>( );
550             PageData data = new PageData( );
551             int nMode = 0;
552             ServletContext servletContext = new MockServletContext( )
553             {
554 
555                 @Override
556                 public InputStream getResourceAsStream( String path )
557                 {
558                     File file = new File( getResourcesDir( ), path );
559                     if ( file.exists( ) )
560                     {
561                         try
562                         {
563                             return new FileInputStream( file );
564                         }
565                         catch( FileNotFoundException e )
566                         {
567                             return null;
568                         }
569                     }
570                     return null;
571                 }
572 
573             };
574             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
575             include.fillTemplate( rootModel, data, nMode, request );
576             String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
577             assertEquals(
578                     "<script src=\"js/plugins/linksIncludeTestPlugin/scripthashed.js?lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\"></script>",
579                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
580             try ( FileWriter writer = new FileWriter( hashedFile ) )
581             {
582                 writer.write( "bbcd" );
583             }
584             include.fillTemplate( rootModel, data, nMode, request );
585             String cssLinks2 = (String) rootModel.get( "plugins_javascript_links" );
586             assertNotNull( cssLinks2 );
587             if ( enableCache( ) )
588             {
589                 assertSame( cssLinks, cssLinks2 );
590             }
591             else
592             {
593                 assertFalse( cssLinks.equals( cssLinks2 ) );
594                 assertEquals(
595                         "<script src=\"js/plugins/linksIncludeTestPlugin/scripthashed.js?lutece_h=531ba794ef006cd3d69cf1acb33ddeccf8d6c655fb08f469335f8c2c32e2ab68\"></script>",
596                         cssLinks2.replace( "\n", "" ).replace( "\r", "" ) );
597             }
598         }
599         finally
600         {
601             hashedFile.delete( );
602         }
603     }
604 
605     public void testGetURIJavascriptHashQuery( ) throws IOException
606     {
607         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
608         List<String> javascripts = new ArrayList<>( );
609         javascripts.add( "linksIncludeTestPlugin/scripthashed.js?arg=value" );
610         File hashedFile = new File( getResourcesDir( ), "js/plugins/linksIncludeTestPlugin/scripthashed.js" );
611         hashedFile.getParentFile( ).mkdirs( );
612         try ( FileWriter writer = new FileWriter( hashedFile ) )
613         {
614             writer.write( "abcd" );
615         }
616         try
617         {
618             plugin.setJavascriptFiles( javascripts );
619             plugin.setJavascriptFilesScopePortal( true );
620             LinksInclude include = new LinksInclude( );
621             Map<String, Object> rootModel = new HashMap<>( );
622             PageData data = new PageData( );
623             int nMode = 0;
624             ServletContext servletContext = new MockServletContext( )
625             {
626 
627                 @Override
628                 public InputStream getResourceAsStream( String path )
629                 {
630                     File file = new File( getResourcesDir( ), path );
631                     if ( file.exists( ) )
632                     {
633                         try
634                         {
635                             return new FileInputStream( file );
636                         }
637                         catch( FileNotFoundException e )
638                         {
639                             return null;
640                         }
641                     }
642                     return null;
643                 }
644 
645             };
646             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
647             include.fillTemplate( rootModel, data, nMode, request );
648             String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
649             assertEquals(
650                     "<script src=\"js/plugins/linksIncludeTestPlugin/scripthashed.js?arg=value&lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\"></script>",
651                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
652         }
653         finally
654         {
655             hashedFile.delete( );
656         }
657     }
658 
659     public void testGetURIJavascriptHashDigestError( ) throws IOException
660     {
661         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
662         List<String> javascripts = new ArrayList<>( );
663         javascripts.add( "linksIncludeTestPlugin/scripthashed.js" );
664         File hashedFile = new File( getResourcesDir( ), "js/plugins/linksIncludeTestPlugin/scripthashed.js" );
665         hashedFile.getParentFile( ).mkdirs( );
666         try ( FileWriter writer = new FileWriter( hashedFile ) )
667         {
668             writer.write( "abcd" );
669         }
670         try
671         {
672             plugin.setJavascriptFiles( javascripts );
673             plugin.setJavascriptFilesScopePortal( true );
674             LinksInclude include = new LinksInclude( );
675             Map<String, Object> rootModel = new HashMap<>( );
676             PageData data = new PageData( );
677             int nMode = 0;
678             ServletContext servletContext = new MockServletContext( )
679             {
680 
681                 @Override
682                 public InputStream getResourceAsStream( String path )
683                 {
684                     File file = new File( getResourcesDir( ), path );
685                     if ( file.exists( ) )
686                     {
687                         return new InputStream( )
688                         {
689 
690                             @Override
691                             public int read( ) throws IOException
692                             {
693                                 throw new IOException( "Mocking IO error" );
694                             }
695                         };
696                     }
697                     return null;
698                 }
699 
700             };
701             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
702             include.fillTemplate( rootModel, data, nMode, request );
703             String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
704             assertEquals( "<script src=\"js/plugins/linksIncludeTestPlugin/scripthashed.js\"></script>", cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
705         }
706         finally
707         {
708             hashedFile.delete( );
709         }
710     }
711 
712     public void testGetURIJavascriptHashStreamCloseError( ) throws IOException
713     {
714         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
715         List<String> javascripts = new ArrayList<>( );
716         javascripts.add( "linksIncludeTestPlugin/scripthashed.js" );
717         File hashedFile = new File( getResourcesDir( ), "js/plugins/linksIncludeTestPlugin/scripthashed.js" );
718         hashedFile.getParentFile( ).mkdirs( );
719         try ( FileWriter writer = new FileWriter( hashedFile ) )
720         {
721             writer.write( "abcd" );
722         }
723         try
724         {
725             plugin.setJavascriptFiles( javascripts );
726             plugin.setJavascriptFilesScopePortal( true );
727             LinksInclude include = new LinksInclude( );
728             Map<String, Object> rootModel = new HashMap<>( );
729             PageData data = new PageData( );
730             int nMode = 0;
731             ServletContext servletContext = new MockServletContext( )
732             {
733 
734                 @Override
735                 public InputStream getResourceAsStream( String path )
736                 {
737                     File file = new File( getResourcesDir( ), path );
738                     if ( file.exists( ) )
739                     {
740                         try
741                         {
742                             return new FileInputStream( file )
743                             {
744 
745                                 @Override
746                                 public void close( ) throws IOException
747                                 {
748                                     throw new IOException( "Mocking IO error on close" );
749                                 }
750 
751                             };
752                         }
753                         catch( FileNotFoundException e )
754                         {
755                             return null;
756                         }
757                     }
758                     return null;
759                 }
760 
761             };
762             MockHttpServletRequest request = new MockHttpServletRequest( servletContext );
763             include.fillTemplate( rootModel, data, nMode, request );
764             String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
765             assertEquals(
766                     "<script src=\"js/plugins/linksIncludeTestPlugin/scripthashed.js?lutece_h=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589\"></script>",
767                     cssLinks.replace( "\n", "" ).replace( "\r", "" ) );
768         }
769         finally
770         {
771             hashedFile.delete( );
772         }
773     }
774 
775     public void testGetURIJavascriptBadURI( )
776     {
777         LinksIncludeTestPlugin./fr/paris/lutece/portal/web/includes/LinksIncludeTestPlugin.html#LinksIncludeTestPlugin">LinksIncludeTestPlugin plugin = (LinksIncludeTestPlugin) PluginService.getPlugin( PLUGIN_NAME );
778         List<String> javascripts = new ArrayList<>( );
779         javascripts.add( "://script.js" );
780         plugin.setJavascriptFiles( javascripts );
781         plugin.setJavascriptFilesScopePortal( true );
782         LinksInclude include = new LinksInclude( );
783         Map<String, Object> rootModel = new HashMap<>( );
784         PageData data = new PageData( );
785         int nMode = 0;
786         HttpServletRequest request = new MockHttpServletRequest( );
787         include.fillTemplate( rootModel, data, nMode, request );
788         String cssLinks = (String) rootModel.get( "plugins_javascript_links" );
789         assertEquals( "", cssLinks );
790     }
791 }