KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25 import junitx.framework.ListAssert;
26
27 /**
28  * Tests the ConfigurationUtils class
29  *
30  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
31  */

32 public class TestConfigurationUtils extends TestCase
33 {
34     protected Configuration config = new BaseConfiguration();
35
36     public void testToString()
37     {
38         String JavaDoc lineSeparator = System.getProperty("line.separator");
39
40         assertEquals("String representation of an empty configuration", "", ConfigurationUtils.toString(config));
41
42         config.setProperty("one", "1");
43         assertEquals("String representation of a configuration", "one=1", ConfigurationUtils.toString(config));
44
45         config.setProperty("two", "2");
46         assertEquals("String representation of a configuration", "one=1" + lineSeparator + "two=2" , ConfigurationUtils.toString(config));
47         
48         config.clearProperty("one");
49         assertEquals("String representation of a configuration", "two=2" , ConfigurationUtils.toString(config));
50                 
51         config.setProperty("one","1");
52         assertEquals("String representation of a configuration", "two=2" + lineSeparator + "one=1" , ConfigurationUtils.toString(config));
53     }
54
55     public void testGetURL() throws Exception JavaDoc
56     {
57         assertEquals(
58             "http://localhost:8080/webapp/config/config.xml",
59             ConfigurationUtils
60                 .getURL(
61                     "http://localhost:8080/webapp/config/baseConfig.xml",
62                     "config.xml")
63                 .toString());
64         assertEquals(
65             "http://localhost:8080/webapp/config/config.xml",
66             ConfigurationUtils
67                 .getURL(
68                     "http://localhost:8080/webapp/baseConfig.xml",
69                     "config/config.xml")
70                 .toString());
71         URL JavaDoc url = ConfigurationUtils.getURL(null, "config.xml");
72         assertEquals("file", url.getProtocol());
73         assertEquals("", url.getHost());
74         
75         assertEquals(
76             "http://localhost:8080/webapp/config/config.xml",
77             ConfigurationUtils
78                 .getURL(
79                     "ftp://ftp.server.com/downloads/baseConfig.xml",
80                     "http://localhost:8080/webapp/config/config.xml")
81                 .toString());
82         assertEquals(
83             "http://localhost:8080/webapp/config/config.xml",
84             ConfigurationUtils
85                 .getURL(null, "http://localhost:8080/webapp/config/config.xml")
86                 .toString());
87         File JavaDoc absFile = new File JavaDoc("config.xml").getAbsoluteFile();
88         assertEquals(
89             absFile.toURL(),
90             ConfigurationUtils.getURL(
91                 "http://localhost:8080/webapp/config/baseConfig.xml",
92                 absFile.getAbsolutePath()));
93         assertEquals(
94             absFile.toURL(),
95             ConfigurationUtils.getURL(null, absFile.getAbsolutePath()));
96         
97         assertEquals(absFile.toURL(),
98         ConfigurationUtils.getURL(absFile.getParent(), "config.xml"));
99     }
100
101     public void testGetBasePath() throws Exception JavaDoc
102     {
103         URL JavaDoc url = new URL JavaDoc("http://xyz.net/foo/bar.xml");
104         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
105
106         url = new URL JavaDoc("http://xyz.net/foo/");
107         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
108
109         url = new URL JavaDoc("http://xyz.net/foo");
110         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
111
112         url = new URL JavaDoc("http://xyz.net/");
113         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
114
115         url = new URL JavaDoc("http://xyz.net");
116         assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
117     }
118
119     public void testGetFileName() throws Exception JavaDoc
120     {
121         assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
122
123         URL JavaDoc url = new URL JavaDoc("http://xyz.net/foo/");
124         assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
125
126         url = new URL JavaDoc("http://xyz.net/foo/bar.xml");
127         assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
128     }
129
130     public void testCopy()
131     {
132         // create the source configuration
133
Configuration conf1 = new BaseConfiguration();
134         conf1.addProperty("key1", "value1");
135         conf1.addProperty("key2", "value2");
136
137         // create the target configuration
138
Configuration conf2 = new BaseConfiguration();
139         conf2.addProperty("key1", "value3");
140         conf2.addProperty("key2", "value4");
141
142         // copy the source configuration into the target configuration
143
ConfigurationUtils.copy(conf1, conf2);
144
145         assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
146         assertEquals("'key2' property", "value2", conf2.getProperty("key2"));
147     }
148
149     public void testAppend()
150     {
151         // create the source configuration
152
Configuration conf1 = new BaseConfiguration();
153         conf1.addProperty("key1", "value1");
154         conf1.addProperty("key2", "value2");
155
156         // create the target configuration
157
Configuration conf2 = new BaseConfiguration();
158         conf2.addProperty("key1", "value3");
159         conf2.addProperty("key2", "value4");
160
161         // append the source configuration to the target configuration
162
ConfigurationUtils.append(conf1, conf2);
163
164         List JavaDoc expected = new ArrayList JavaDoc();
165         expected.add("value3");
166         expected.add("value1");
167         ListAssert.assertEquals("'key1' property", expected, conf2.getList("key1"));
168
169         expected = new ArrayList JavaDoc();
170         expected.add("value4");
171         expected.add("value2");
172         ListAssert.assertEquals("'key2' property", expected, conf2.getList("key2"));
173     }
174     
175     public void testGetFile() throws Exception JavaDoc
176     {
177         File JavaDoc directory = new File JavaDoc("target");
178         File JavaDoc reference = new File JavaDoc(directory, "test.txt").getAbsoluteFile();
179         
180         assertEquals(reference, ConfigurationUtils.getFile(null, reference.getAbsolutePath()));
181         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getAbsolutePath()));
182         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getName()));
183         assertEquals(reference, ConfigurationUtils.getFile(directory.toURL().toString(), reference.getName()));
184         assertEquals(reference, ConfigurationUtils.getFile("invalid", reference.toURL().toString()));
185     }
186 }
187
Popular Tags