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.util;
35  
36  import fr.paris.lutece.portal.service.util.AppPathService;
37  import fr.paris.lutece.test.LuteceTestCase;
38  
39  import java.io.File;
40  import java.io.FileNotFoundException;
41  import java.io.FileOutputStream;
42  import java.io.IOException;
43  import java.io.OutputStream;
44  
45  import java.util.Properties;
46  
47  /**
48   * PropertiesService Test Class
49   *
50   */
51  public class PropertiesServiceTest extends LuteceTestCase
52  {
53      private static final String PATH_CONF = "WEB-INF/conf";
54      private static final String PATH_CONF_PLUGINS = "WEB-INF/conf/plugins";
55      private static final String FILE_CONFIG = "config.properties";
56      private static final String PROPERTY_PROD_URL = "lutece.prod.url";
57  
58      /**
59       * Test of addPropertiesFile method, of class fr.paris.lutece.util.PropertiesService.
60       */
61      public void testAddPropertiesFile( ) throws Exception
62      {
63          System.out.println( "addPropertiesFile" );
64  
65          PropertiesService instance = new PropertiesService( AppPathService.getWebAppPath( ) );
66  
67          instance.addPropertiesFile( PATH_CONF, FILE_CONFIG );
68          instance.getProperty( PROPERTY_PROD_URL );
69  
70          // Test reloading
71          instance.reload( FILE_CONFIG );
72      }
73  
74      /**
75       * Test of addPropertiesDirectory method, of class fr.paris.lutece.util.PropertiesService.
76       */
77      public void testAddPropertiesDirectory( ) throws Exception
78      {
79          System.out.println( "addPropertiesDirectory" );
80  
81          String strRelativePath = PATH_CONF_PLUGINS;
82          PropertiesService instance = new PropertiesService( AppPathService.getWebAppPath( ) );
83  
84          instance.addPropertiesDirectory( strRelativePath );
85  
86          // Test reloading
87          instance.reloadAll( );
88      }
89  
90      public void testReloadAll( ) throws FileNotFoundException, IOException
91      {
92          File propsFile = File.createTempFile( "junit", ".properties" );
93          propsFile.deleteOnExit( );
94  
95          Properties props = new Properties( );
96          props.put( "test1", "test1" );
97          props.put( "test2", "test2" );
98  
99          OutputStream os = new FileOutputStream( propsFile );
100         props.store( os, this.getClass( ).getName( ) );
101         os.close( );
102 
103         PropertiesService instance = new PropertiesService( propsFile.getParent( ) );
104         instance.addPropertiesFile( "", propsFile.getName( ) );
105 
106         for ( String key : props.stringPropertyNames( ) )
107         {
108             assertNotNull( instance.getProperty( key ) );
109             assertEquals( props.getProperty( key ), instance.getProperty( key ) );
110         }
111 
112         props.setProperty( "test1", "test1_mod" );
113         props.remove( "test2" );
114         os = new FileOutputStream( propsFile );
115         props.store( os, this.getClass( ).getName( ) );
116         os.close( );
117 
118         instance.reloadAll( );
119         assertEquals( props.getProperty( "test1" ), instance.getProperty( "test1" ) );
120         assertNull( instance.getProperty( "test2" ) );
121     }
122 
123     public void testReloadAllOrder( ) throws IOException
124     {
125         File propsFile = File.createTempFile( "junit", ".properties" );
126         propsFile.deleteOnExit( );
127 
128         Properties props = new Properties( );
129         props.put( "key", "1" );
130 
131         OutputStream os = new FileOutputStream( propsFile );
132         props.store( os, this.getClass( ).getName( ) );
133         os.close( );
134 
135         PropertiesService instance = new PropertiesService( propsFile.getParent( ) );
136         instance.addPropertiesFile( "", propsFile.getName( ) );
137 
138         assertEquals( "1", instance.getProperty( "key" ) );
139 
140         for ( int i = 2; i < 10; i++ )
141         {
142             propsFile = File.createTempFile( "junit", ".properties" );
143             propsFile.deleteOnExit( );
144 
145             props = new Properties( );
146             props.put( "key", Integer.toString( i ) );
147             os = new FileOutputStream( propsFile );
148             props.store( os, this.getClass( ).getName( ) );
149             os.close( );
150 
151             instance.addPropertiesFile( "", propsFile.getName( ) );
152 
153             assertEquals( Integer.toString( i ), instance.getProperty( "key" ) );
154 
155             instance.reloadAll( );
156 
157             assertEquals( Integer.toString( i ), instance.getProperty( "key" ) );
158         }
159     }
160 }