KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestFileConfiguration


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.net.URL JavaDoc;
20 import java.io.File JavaDoc;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @author Emmanuel Bourg
26  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
27  */

28 public class TestFileConfiguration extends TestCase
29 {
30     public void testSetURL() throws Exception JavaDoc
31     {
32         // http URL
33
FileConfiguration config = new PropertiesConfiguration();
34         config.setURL(new URL JavaDoc("http://jakarta.apache.org/commons/configuration/index.html"));
35
36         assertEquals("base path", "http://jakarta.apache.org/commons/configuration/", config
37                 .getBasePath());
38         assertEquals("file name", "index.html", config.getFileName());
39
40         // file URL
41
config.setURL(new URL JavaDoc("file:/temp/test.properties"));
42         assertEquals("base path", "file:/temp/", config.getBasePath());
43         assertEquals("file name", "test.properties", config.getFileName());
44     }
45
46     public void testLocations() throws Exception JavaDoc
47     {
48         PropertiesConfiguration config = new PropertiesConfiguration();
49
50         File JavaDoc directory = new File JavaDoc("conf");
51         File JavaDoc file = new File JavaDoc(directory, "test.properties");
52         config.setFile(file);
53         assertEquals(directory.getAbsolutePath(), config.getBasePath());
54         assertEquals("test.properties", config.getFileName());
55         assertEquals(file.getAbsolutePath(), config.getPath());
56
57         config.setPath("conf" + File.separator + "test.properties");
58         assertEquals("test.properties", config.getFileName());
59         assertEquals(directory.getAbsolutePath(), config.getBasePath());
60         assertEquals(file.getAbsolutePath(), config.getPath());
61         assertEquals(file.toURL(), config.getURL());
62
63         config.setBasePath(null);
64         config.setFileName("test.properties");
65         assertNull(config.getBasePath());
66         assertEquals("test.properties", config.getFileName());
67     }
68
69     public void testCreateFile1() throws Exception JavaDoc
70     {
71         File JavaDoc file = new File JavaDoc("target/test-resources/foo/bar/test.properties");
72         if (file.exists())
73         {
74             file.delete();
75             file.getParentFile().delete();
76         }
77
78         assertFalse("The file should not exist", file.exists());
79
80         FileConfiguration config = new PropertiesConfiguration(file);
81         config.save();
82
83         assertTrue("The file doesn't exist", file.exists());
84     }
85
86     public void testCreateFile2() throws Exception JavaDoc
87     {
88         File JavaDoc file = new File JavaDoc("target/test-resources/foo/bar/test.properties");
89         if (file.exists())
90         {
91             file.delete();
92             file.getParentFile().delete();
93         }
94
95         assertFalse("The file should not exist", file.exists());
96
97         FileConfiguration config = new PropertiesConfiguration();
98         config.setFile(file);
99         config.save();
100
101         assertTrue("The file doesn't exist", file.exists());
102     }
103
104     public void testCreateFile3() throws Exception JavaDoc
105     {
106         File JavaDoc file = new File JavaDoc("target/test-resources/foo/bar/test.properties");
107         if (file.exists())
108         {
109             file.delete();
110             file.getParentFile().delete();
111         }
112
113         assertFalse("The file should not exist", file.exists());
114
115         FileConfiguration config = new PropertiesConfiguration();
116         config.save(file);
117
118         assertTrue("The file doesn't exist", file.exists());
119     }
120
121     /**
122      * Tests collaboration with ConfigurationFactory: Is the base path set on
123      * loading is valid in file based configurations?
124      *
125      * @throws Exception if an error occurs
126      */

127     public void testWithConfigurationFactory() throws Exception JavaDoc
128     {
129         File JavaDoc dir = new File JavaDoc("conf");
130         File JavaDoc file = new File JavaDoc(dir, "testFileConfiguration.properties");
131
132         if (file.exists())
133         {
134             assertTrue("File cannot be deleted", file.delete());
135         }
136
137         try
138         {
139             ConfigurationFactory factory = new ConfigurationFactory();
140             factory.setConfigurationURL(new File JavaDoc(dir, "testDigesterConfiguration2.xml").toURL());
141             CompositeConfiguration cc = (CompositeConfiguration) factory.getConfiguration();
142             PropertiesConfiguration config = null;
143             for (int i = 0; config == null; i++)
144             {
145                 if (cc.getConfiguration(i) instanceof PropertiesConfiguration)
146                 {
147                     config = (PropertiesConfiguration) cc.getConfiguration(i);
148                 }
149             }
150
151             config.setProperty("test", "yes");
152             config.save(file.getName());
153             assertTrue(file.exists());
154             config = new PropertiesConfiguration();
155             config.setFile(file);
156             config.load();
157
158             assertEquals("yes", config.getProperty("test"));
159             assertEquals("masterOfPost", config.getProperty("mail.account.user"));
160         }
161         finally
162         {
163             if (file.exists())
164             {
165                 assertTrue("File could not be deleted", file.delete());
166             }
167         }
168     }
169     
170     /**
171      * Tests if invalid URLs cause an exception.
172      */

173     public void testSaveInvalidURL() throws Exception JavaDoc
174     {
175         FileConfiguration config = new PropertiesConfiguration();
176         try
177         {
178             config.save(new URL JavaDoc("http://jakarta.apache.org"));
179             fail("Should throw a ConfigurationException!");
180         }
181         catch (ConfigurationException cex)
182         {
183             //fine
184
}
185
186         try
187         {
188             config.save("http://www.apache.org");
189             fail("Should throw a ConfigurationException!");
190         }
191         catch (ConfigurationException cex)
192         {
193             //fine
194
}
195     }
196 }
Popular Tags