1   package org.apache.commons.configuration;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * Test for loading and saving properties files.
28   *
29   * @version $Id: TestPropertiesConfiguration.java 156237 2005-03-05 10:26:22Z oheger $
30   */
31  public class TestPropertiesConfiguration extends TestCase
32  {
33      private PropertiesConfiguration conf;
34  
35      /*** The File that we test with */
36      private String testProperties = new File("conf/test.properties").getAbsolutePath();
37  
38      private String testBasePath = new File("conf").getAbsolutePath();
39      private String testBasePath2 = new File("conf").getAbsoluteFile().getParentFile().getAbsolutePath();
40      private File testSavePropertiesFile = new File("target/testsave.properties");
41  
42      protected void setUp() throws Exception
43      {
44          conf = new PropertiesConfiguration(testProperties);
45      }
46  
47      public void testLoad() throws Exception
48      {
49          String loaded = conf.getString("configuration.loaded");
50          assertEquals("true", loaded);
51      }
52      
53      /***
54       * Tests if properties can be appended by simply calling load() another
55       * time.
56       */
57      public void testAppend() throws Exception
58      {
59          File file2 = new File("conf/threesome.properties");
60          conf.load(file2);
61          assertEquals("aaa", conf.getString("test.threesome.one"));
62          assertEquals("true", conf.getString("configuration.loaded"));
63      }
64  
65      /***
66       * Tests that empty properties are treated as the empty string
67       * (rather than as null).
68       */
69      public void testEmpty() throws Exception
70      {
71          String empty = conf.getString("test.empty");
72          assertNotNull(empty);
73          assertEquals("", empty);
74      }
75  
76      /***
77       * Tests that references to other properties work
78       */
79      public void testReference() throws Exception
80      {
81          assertEquals("baseextra", conf.getString("base.reference"));
82      }
83  
84      /***
85       * test if includes properties get loaded too
86       */
87      public void testLoadInclude() throws Exception
88      {
89          String loaded = conf.getString("include.loaded");
90          assertEquals("true", loaded);
91      }
92  
93      public void testSetInclude() throws Exception
94      {
95          // change the include key
96          PropertiesConfiguration.setInclude("import");
97  
98          // load the configuration
99          PropertiesConfiguration conf = new PropertiesConfiguration();
100         conf.load("conf/test.properties");
101 
102         // restore the previous value for the other tests
103         PropertiesConfiguration.setInclude("include");
104 
105         assertNull(conf.getString("include.loaded"));
106     }
107 
108     /***
109      * Tests <code>List</code> parsing.
110      */
111     public void testList() throws Exception
112     {
113         List packages = conf.getList("packages");
114         // we should get 3 packages here
115         assertEquals(3, packages.size());
116     }
117 
118     public void testSave() throws Exception
119     {
120         // remove the file previously saved if necessary
121         if (testSavePropertiesFile.exists())
122         {
123             assertTrue(testSavePropertiesFile.delete());
124         }
125 
126         // add an array of strings to the configuration
127         conf.addProperty("string", "value1");
128         List list = new ArrayList();
129         for (int i = 1; i < 5; i++)
130         {
131             list.add("value" + i);
132         }
133         conf.addProperty("array", list);
134 
135         // save the configuration
136         String filename = testSavePropertiesFile.getAbsolutePath();
137         conf.save(filename);
138 
139         assertTrue("The saved file doesn't exist", testSavePropertiesFile.exists());
140 
141         // read the configuration and compare the properties
142         PropertiesConfiguration checkConfig = new PropertiesConfiguration(filename);
143         for (Iterator i = conf.getKeys(); i.hasNext();)
144         {
145             String key = (String) i.next();
146             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
147             assertEquals("Value of the '" + key + "' property", conf.getProperty(key), checkConfig.getProperty(key));
148         }
149 
150         // Save it again, verifing a save with a filename works.
151         checkConfig.save();
152     }
153 
154     public void testSaveMissingFilename()
155     {
156         PropertiesConfiguration pc = new PropertiesConfiguration();
157         try
158         {
159             pc.save();
160             fail("Should have throw ConfigurationException");
161         }
162         catch (ConfigurationException ce)
163         {
164             //good
165         }
166     }
167     
168     /***
169      * Tests if the base path is taken into account by the save() method.
170      * @throws Exception if an error occurs
171      */
172     public void testSaveWithBasePath() throws Exception
173     {
174         // remove the file previously saved if necessary
175         if (testSavePropertiesFile.exists())
176         {
177             assertTrue(testSavePropertiesFile.delete());
178         }
179         
180         conf.setProperty("test", "true");
181         conf.setBasePath(testSavePropertiesFile.getParentFile().toURL().toString());
182         conf.setFileName(testSavePropertiesFile.getName());
183         conf.save();
184         assertTrue(testSavePropertiesFile.exists());
185     }
186 
187     public void testLoadViaProperty() throws Exception
188     {
189         PropertiesConfiguration pc = new PropertiesConfiguration();
190         pc.setFileName(testProperties);
191         pc.load();
192 
193         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
194     }
195 
196     public void testLoadViaPropertyWithBasePath() throws Exception
197     {
198         PropertiesConfiguration pc = new PropertiesConfiguration();
199         pc.setBasePath(testBasePath);
200         pc.setFileName("test.properties");
201         pc.load();
202 
203         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
204     }
205 
206     public void testLoadViaPropertyWithBasePath2() throws Exception
207     {
208         PropertiesConfiguration pc = new PropertiesConfiguration();
209         pc.setBasePath(testBasePath2);
210         pc.setFileName("conf/test.properties");
211         pc.load();
212 
213         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
214 
215         pc = new PropertiesConfiguration();
216         pc.setBasePath(testBasePath2);
217         pc.setFileName("conf/test.properties");
218         pc.load();
219 
220         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
221     }
222 
223     public void testLoadFromJAR() throws Exception
224     {
225         conf = new PropertiesConfiguration();
226         conf.setIncludesAllowed(true);
227         conf.setFileName("test-jar.properties");
228         conf.load();
229 
230         assertEquals("jar", conf.getProperty("configuration.location"));
231         assertEquals("property in an included file", "jar", conf.getProperty("include.location"));
232     }
233 
234     public void testLoadFromFile() throws Exception
235     {
236         File file = new File("conf/test.properties");
237         conf = new PropertiesConfiguration(file);
238 
239         assertEquals("true", conf.getString("configuration.loaded"));
240     }
241     
242     public void testLoadUnexistingFile()
243     {
244         try
245         {
246             conf = new PropertiesConfiguration("Unexisting file");
247             fail("Unexisting file was loaded.");
248         }
249         catch(ConfigurationException cex)
250         {
251             // fine
252         }
253     }
254 
255     public void testGetStringWithEscapedChars()
256     {
257         String property = conf.getString("test.unescape");
258         assertEquals("String with escaped characters", "This \n string \t contains \" escaped // characters", property);
259     }
260 
261     public void testGetStringWithEscapedComma()
262     {
263         String property = conf.getString("test.unescape.list-separator");
264         assertEquals("String with an escaped list separator", "This string contains , an escaped list separator", property);
265     }
266 
267     public void testUnescapeJava()
268     {
269         assertEquals("test//,test", PropertiesConfiguration.unescapeJava("test//,test", ','));
270     }
271 
272     public void testMixedArray()
273     {
274         String[] array = conf.getStringArray("test.mixed.array");
275 
276         assertEquals("array length", 4, array.length);
277         assertEquals("1st element", "a", array[0]);
278         assertEquals("2nd element", "b", array[1]);
279         assertEquals("3rd element", "c", array[2]);
280         assertEquals("4th element", "d", array[3]);
281     }
282 
283     public void testMultilines()
284     {
285         String property = "This is a value spread out across several adjacent "
286                 + "natural lines by escaping the line terminator with "
287                 + "a backslash character.";
288 
289         assertEquals("'test.multilines' property", property, conf.getString("test.multilines"));
290     }
291 
292     public void testChangingDelimiter() throws Exception
293     {
294         PropertiesConfiguration pc = new PropertiesConfiguration(testProperties);
295         assertEquals(4, pc.getList("test.mixed.array").size());
296 
297         char delimiter = PropertiesConfiguration.getDelimiter();
298         PropertiesConfiguration.setDelimiter('^');
299         pc = new PropertiesConfiguration(testProperties);
300         assertEquals(2, pc.getList("test.mixed.array").size());
301         PropertiesConfiguration.setDelimiter(delimiter);
302     }
303 
304 }