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.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   * I18nService Test Class
59   *
60   */
61  public class I18nServiceTest extends LuteceTestCase
62  {
63      /**
64       * Test of localize method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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       * Test of getLocalizedString method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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       * Test of getLocalizedDate method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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      * Test of getLocalizedDateTime method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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      * Test of getAdminAvailableLocales method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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      * Test of getAdminLocales method, of class fr.paris.lutece.portal.service.i18n.I18nService.
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      * Test of resetCache method, of class fr.paris.lutece.portal.service.i18n.I18nService.
154      * 
155      * @throws IOException
156      */
157     public void testResetCache( ) throws IOException
158     {
159         // get a message
160         String message = I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH );
161 
162         // change the message
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         // read the value again
176         assertEquals( message, I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH ) );
177         // clear the cache and read again
178         I18nService.resetCache( );
179         assertEquals( newValue, I18nService.getLocalizedString( "portal.admin.admin_home.password", Locale.FRENCH ) );
180         // Restore default property value
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 }