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.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
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
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
71 instance.reload( FILE_CONFIG );
72 }
73
74
75
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
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 }