1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.net.URL;
20 import java.io.File;
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
31 {
32
33 FileConfiguration config = new PropertiesConfiguration();
34 config.setURL(new URL("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
41 config.setURL(new URL("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
47 {
48 PropertiesConfiguration config = new PropertiesConfiguration();
49
50 File directory = new File("conf");
51 File file = new File(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
70 {
71 File file = new File("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
87 {
88 File file = new File("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
105 {
106 File file = new File("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
128 {
129 File dir = new File("conf");
130 File file = new File(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(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
174 {
175 FileConfiguration config = new PropertiesConfiguration();
176 try
177 {
178 config.save(new URL("http://jakarta.apache.org"));
179 fail("Should throw a ConfigurationException!");
180 }
181 catch (ConfigurationException cex)
182 {
183
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
194 }
195 }
196 }