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.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
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
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
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
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 }