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.template;
35  
36  import fr.paris.lutece.portal.business.template.CommonsInclude;
37  import fr.paris.lutece.portal.service.template.CommonsService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.test.LuteceTestCase;
40  import fr.paris.lutece.util.ReferenceList;
41  import freemarker.template.TemplateException;
42  import java.io.File;
43  import java.io.FileFilter;
44  import java.io.IOException;
45  import java.util.HashMap;
46  import java.util.Map;
47  import org.apache.commons.io.FileUtils;
48  import org.junit.Test;
49  
50  /**
51   * CommonsTest
52   */
53  public class CommonsTest extends LuteceTestCase
54  {
55      private static final String RESSOURCES_PATH = "commons/templates/";
56      private static final String TEMPLATES_FOLDER = "/" + RESSOURCES_PATH;
57  
58      private static final String MARK_TEMPLATE = "template";
59      private static final String MARK_MOCK_OBJECT = "mockObject";
60      private static final String MARK_FOREIGN_KEYS_LIST = "id_foreigns_list";
61      private static final String [ ] CHARTERS_FOLDERS = {
62              "css", "fonts", "js", "themes"
63      };
64  
65      @Test
66      public void testCommonsTemplates( ) throws IOException, TemplateException
67      {
68          String strPath = getClass( ).getResource( TEMPLATES_FOLDER ).getPath( );
69          File fileTemplatesFolder = new File( strPath );
70  
71          copyCommonsFiles( strPath );
72  
73          String strOutput = strPath + "/output";
74          File fileOutputFolder = new File( strOutput );
75          fileOutputFolder.mkdir( );
76          boolean ko = false;
77  
78          for ( CommonsInclude ci : CommonsService.getCommonsIncludes( ) )
79          {
80              try
81              {
82                  CommonsService.activateCommons( ci.getKey( ) );
83                  String strCommonsOutput = strOutput + "/" + ci.getKey( );
84                  File fileOutputCommonsFolder = new File( strCommonsOutput );
85                  fileOutputCommonsFolder.mkdir( );
86  
87                  copyChartersFolders( strPath, strCommonsOutput );
88  
89                  MockFreemarkerTemplateServicelateService.html#MockFreemarkerTemplateService">MockFreemarkerTemplateService templateService = new MockFreemarkerTemplateService( strPath );
90                  templateService.init( "/" );
91                  for ( String strAutoIncludes : ci.getFiles( ) )
92                  {
93                      templateService.addAutoInclude( "commons/" + strAutoIncludes );
94                  }
95  
96                  for ( File file : fileTemplatesFolder.listFiles( ) )
97                  {
98                      if ( !file.isDirectory( ) )
99                      {
100                         System.out.println( file.getName( ) );
101                         Map<String, Object> model = new HashMap<>( );
102                         MockObjectplate/MockObject.html#MockObject">MockObject mockObject = new MockObject( );
103 
104                         model.put( MARK_TEMPLATE, file.getName( ) );
105                         model.put( MARK_MOCK_OBJECT, mockObject );
106                         model.put( MARK_FOREIGN_KEYS_LIST, getForeignKeysList( ) );
107 
108                         templateService.write( file.getName( ), fileOutputCommonsFolder.getPath( ), model );
109 
110                     }
111                 }
112             }
113             catch( Exception e )
114             {
115                 e.printStackTrace( );
116                 ko = true;
117             }
118         }
119         assertFalse( ko );
120     }
121 
122     private ReferenceList getForeignKeysList( )
123     {
124         ReferenceList listForeignKeys = new ReferenceList( );
125         listForeignKeys.addItem( 1, "Item 1" );
126         listForeignKeys.addItem( 2, "Item 2" );
127         return listForeignKeys;
128 
129     }
130 
131     private String getSourcePath( String strPath, String strFolder )
132     {
133         int nPos = strPath.indexOf( "target" );
134         return strPath.substring( 0, nPos ) + "target/lutece/" + strFolder;
135     }
136 
137     private void copyChartersFolders( String strRootPath, String strCommonsOutput ) throws IOException
138     {
139         for ( String strFolder : CHARTERS_FOLDERS )
140         {
141             String strSourcePath = getSourcePath( strRootPath, strFolder );
142             File fileSourceFolder = new File( strSourcePath );
143 
144             String strCommonsFolder = strCommonsOutput + "/" + strFolder;
145             File fileOutputCommonsFolder = new File( strCommonsFolder );
146             fileOutputCommonsFolder.mkdir( );
147             FileUtils.copyDirectory( fileSourceFolder, fileOutputCommonsFolder );
148         }
149     }
150 
151     private void copyCommonsFiles( String strRootPath ) throws IOException
152     {
153 
154         AppLogService.info( "Copying commons" );
155 
156         // commons
157         String strSourcePath = getSourcePath( strRootPath, "WEB-INF/templates" );
158         File fileSourceFolder = new File( strSourcePath );
159         String strDestPath = strRootPath + "/commons";
160         File fileCommonsFolder = new File( strDestPath );
161         fileCommonsFolder.mkdir( );
162         FileUtils.copyDirectory( fileSourceFolder, fileCommonsFolder, new CommonsFileFilter( ) );
163 
164         AppLogService.info( "Copying all files from {} to {}", fileSourceFolder.getAbsolutePath( ), fileCommonsFolder.getAbsolutePath( ) );
165 
166         // util macros
167         strSourcePath = getSourcePath( strRootPath, "WEB-INF/templates/admin/util/calendar" );
168         fileSourceFolder = new File( strSourcePath );
169         strDestPath = strRootPath + "/commons/admin/util/calendar";
170         File fileCommonsUtilFolder = new File( strDestPath );
171         fileCommonsUtilFolder.mkdir( );
172         AppLogService.info( fileCommonsUtilFolder.getAbsolutePath( ) + " created" );
173         FileUtils.copyDirectory( fileSourceFolder, fileCommonsUtilFolder );
174 
175         AppLogService.info( "Copying all files from {} to {}", fileSourceFolder.getAbsolutePath( ), fileCommonsUtilFolder.getAbsolutePath( ) );
176 
177         // themes
178         strSourcePath = getSourcePath( strRootPath, "WEB-INF/templates/admin/themes" );
179         fileSourceFolder = new File( strSourcePath );
180         strDestPath = strRootPath + "/commons/admin/themes";
181         File fileThemesFolder = new File( strDestPath );
182         fileThemesFolder.mkdir( );
183         FileUtils.copyDirectory( fileSourceFolder, fileThemesFolder );
184         AppLogService.info( "Copying all files from {} to {}", fileSourceFolder.getAbsolutePath( ), fileThemesFolder.getAbsolutePath( ) );
185     }
186 
187     class CommonsFileFilter implements FileFilter
188     {
189 
190         @Override
191         public boolean accept( File file )
192         {
193             if ( file.isFile( ) && file.getName( ).startsWith( "commons" ) )
194             {
195                 return true;
196             }
197             return false;
198         }
199 
200     }
201 
202 }