1   package org.apache.commons.configuration;
2   
3   /*
4    * Copyright 2001-2004 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.net.URL;
21  
22  import junit.framework.TestCase;
23  
24  /***
25   * Tests the ConfigurationUtils class
26   *
27   */
28  public class TestConfigurationUtils extends TestCase
29  {
30      protected Configuration config = new BaseConfiguration();
31  
32      public void testToString()
33      {
34          String lineSeparator = System.getProperty("line.separator");
35  
36          assertEquals("String representation of an empty configuration", "", ConfigurationUtils.toString(config));
37  
38          config.setProperty("one", "1");
39          assertEquals("String representation of a configuration", "one=1", ConfigurationUtils.toString(config));
40  
41          config.setProperty("two", "2");
42          assertEquals("String representation of a configuration", "one=1" + lineSeparator + "two=2" , ConfigurationUtils.toString(config));
43          
44          config.clearProperty("one");
45          assertEquals("String representation of a configuration", "two=2" , ConfigurationUtils.toString(config));
46                  
47          config.setProperty("one","1");
48          assertEquals("String representation of a configuration", "two=2" + lineSeparator + "one=1" , ConfigurationUtils.toString(config));
49      }
50  
51      public void testGetURL() throws Exception
52      {
53          assertEquals(
54              "http://localhost:8080/webapp/config/config.xml",
55              ConfigurationUtils
56                  .getURL(
57                      "http://localhost:8080/webapp/config/baseConfig.xml",
58                      "config.xml")
59                  .toString());
60          assertEquals(
61              "http://localhost:8080/webapp/config/config.xml",
62              ConfigurationUtils
63                  .getURL(
64                      "http://localhost:8080/webapp/baseConfig.xml",
65                      "config/config.xml")
66                  .toString());
67          URL url = ConfigurationUtils.getURL(null, "config.xml");
68          assertEquals("file", url.getProtocol());
69          assertEquals("", url.getHost());
70          
71          assertEquals(
72              "http://localhost:8080/webapp/config/config.xml",
73              ConfigurationUtils
74                  .getURL(
75                      "ftp://ftp.server.com/downloads/baseConfig.xml",
76                      "http://localhost:8080/webapp/config/config.xml")
77                  .toString());
78          assertEquals(
79              "http://localhost:8080/webapp/config/config.xml",
80              ConfigurationUtils
81                  .getURL(null, "http://localhost:8080/webapp/config/config.xml")
82                  .toString());
83          File absFile = new File("config.xml").getAbsoluteFile();
84          assertEquals(
85              absFile.toURL(),
86              ConfigurationUtils.getURL(
87                  "http://localhost:8080/webapp/config/baseConfig.xml",
88                  absFile.getAbsolutePath()));
89          assertEquals(
90              absFile.toURL(),
91              ConfigurationUtils.getURL(null, absFile.getAbsolutePath()));
92          
93  		assertEquals(absFile.toURL(),
94  		ConfigurationUtils.getURL(absFile.getParent(), "config.xml"));
95      }
96  
97      public void testGetBasePath() throws Exception
98      {
99          URL url = new URL("http://xyz.net/foo/bar.xml");
100         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
101 
102         url = new URL("http://xyz.net/foo/");
103         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
104 
105         url = new URL("http://xyz.net/foo");
106         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
107 
108         url = new URL("http://xyz.net/");
109         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
110 
111         url = new URL("http://xyz.net");
112         assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
113     }
114 
115     public void testGetFileName() throws Exception
116     {
117         assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
118 
119         URL url = new URL("http://xyz.net/foo/");
120         assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
121 
122         url = new URL("http://xyz.net/foo/bar.xml");
123         assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
124     }
125 
126 }