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.service.i18n;
35
36 import fr.paris.lutece.test.LuteceTestCase;
37 import fr.paris.lutece.util.ReferenceList;
38
39 import org.springframework.core.io.ClassPathResource;
40
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.OutputStream;
47
48 import java.security.SecureRandom;
49
50 import java.text.DateFormat;
51
52 import java.util.Date;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Properties;
56
57
58
59
60
61 public class I18nServiceTest extends LuteceTestCase
62 {
63
64
65
66 public void testLocalize( )
67 {
68 System.out.println( "localize" );
69
70 String strSource = "#i18n{portal.util.labelCancel}";
71 Locale locale = Locale.FRENCH;
72
73 String expResult = "Annuler";
74 String result = fr.paris.lutece.portal.service.i18n.I18nService.localize( strSource, locale );
75 assertEquals( expResult, result );
76 }
77
78
79
80
81 public void testGetLocalizedString( )
82 {
83 System.out.println( "getLocalizedString" );
84
85 String strKey = "portal.util.labelCancel";
86 Locale locale = Locale.FRENCH;
87
88 String expResult = "Annuler";
89 String result = fr.paris.lutece.portal.service.i18n.I18nService.getLocalizedString( strKey, locale );
90 assertEquals( expResult, result );
91 }
92
93
94
95
96 public void testGetLocalizedDate( )
97 {
98 System.out.println( "getLocalizedDate" );
99
100 Date date = new Date( 0 );
101 Locale locale = Locale.FRENCH;
102 int nDateFormat = DateFormat.SHORT;
103
104 String expResultJava8 = "01/01/70";
105 String expResultJava10 = "01/01/1970";
106 String result = fr.paris.lutece.portal.service.i18n.I18nService.getLocalizedDate( date, locale, nDateFormat );
107 assertTrue( expResultJava8.equals( result ) || expResultJava10.equals( result ) );
108 }
109
110
111
112
113 public void testGetLocalizedDateTime( )
114 {
115 System.out.println( "getLocalizedDateTime" );
116
117 Date date = new Date( 0 );
118 Locale locale = Locale.FRENCH;
119 int nDateFormat = DateFormat.SHORT;
120 int nTimeFormat = DateFormat.SHORT;
121
122 String expResultJava8 = "01/01/70 01:00";
123 String expResultJava10 = "01/01/1970 01:00";
124 String result = fr.paris.lutece.portal.service.i18n.I18nService.getLocalizedDateTime( date, locale, nDateFormat, nTimeFormat );
125 assertTrue( expResultJava8.equals( result ) || expResultJava10.equals( result ) );
126 }
127
128
129
130
131 public void testGetAdminAvailableLocales( )
132 {
133 System.out.println( "getAdminAvailableLocales" );
134
135 List<Locale> result = fr.paris.lutece.portal.service.i18n.I18nService.getAdminAvailableLocales( );
136 assertTrue( result.size( ) >= 2 );
137 }
138
139
140
141
142 public void testGetAdminLocales( )
143 {
144 System.out.println( "getAdminLocales" );
145
146 Locale locale = Locale.FRENCH;
147
148 ReferenceList result = fr.paris.lutece.portal.service.i18n.I18nService.getAdminLocales( locale );
149 assertTrue( result.size( ) >= 2 );
150 }
151
152
153
154
155
156
157 public void testResetCache( ) throws IOException
158 {
159
160 String message = I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH );
161
162
163 File propertiesFile = new ClassPathResource( "fr/paris/lutece/portal/resources/admin_messages_fr.properties" ).getFile( );
164 Properties resources = new Properties( );
165 InputStream is = new FileInputStream( propertiesFile );
166 resources.load( is );
167 is.close( );
168
169 String newValue = "junit" + new SecureRandom( ).nextLong( );
170 resources.setProperty( "admin_home.password", newValue );
171
172 OutputStream os = new FileOutputStream( propertiesFile );
173 resources.store( os, null );
174 os.close( );
175
176 assertEquals( message, I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH ) );
177
178 I18nService.resetCache( );
179 assertEquals( newValue, I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH ) );
180
181 resources.setProperty( "admin_home.password", message );
182 os = new FileOutputStream( propertiesFile );
183 resources.store( os, null );
184 os.close( );
185 I18nService.resetCache( );
186 }
187 }