2 // WebConfigurationManagerTest.cs
3 // - unit tests for System.Web.Configuration.WebConfigurationManager
6 // Chris Toshok <toshok@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
35 using System.Configuration;
36 using _Configuration = System.Configuration.Configuration;
38 using System.Web.Configuration;
40 using System.Web.Security;
42 namespace MonoTests.System.Web.Configuration {
45 public class WebConfigurationManagerTest {
48 public void OpenMachineConfiguration_1 ()
50 _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ();
51 _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
53 Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
57 public void OpenMachineConfiguration_2 ()
59 _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ("configTest");
60 _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
62 Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
66 public void OpenMachineConfiguration_serverNull ()
68 _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ("configTest", null);
69 _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
71 Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
75 public void OpenWebConfiguration_null ()
77 _Configuration web = WebConfigurationManager.OpenWebConfiguration (null);
78 _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
80 Assert.AreEqual ("web.config", Path.GetFileName (web.FilePath), "A1");
81 Assert.AreEqual (Path.GetDirectoryName (web.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
85 public void OpenWebConfiguration_empty ()
87 _Configuration web1 = WebConfigurationManager.OpenWebConfiguration (null);
88 _Configuration web2 = WebConfigurationManager.OpenWebConfiguration ("");
89 _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
91 Assert.AreEqual (web1.FilePath, web2.FilePath, "A1");
92 Assert.AreEqual (Path.GetDirectoryName (web2.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
96 public void OpenWebConfiguration_siteNull ()
98 _Configuration web = WebConfigurationManager.OpenWebConfiguration ("", null);
99 _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
101 Assert.AreEqual ("web.config", Path.GetFileName (web.FilePath), "A1");
102 Assert.AreEqual (Path.GetDirectoryName (web.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
106 [ExpectedException (typeof (ConfigurationErrorsException))]
107 public void OpenWebConfiguration_siteNull2_absolutePath ()
109 WebConfigurationManager.OpenWebConfiguration ("", null, "/clientTest");
113 public void OpenWebConfiguration_siteNull2 ()
115 _Configuration web = WebConfigurationManager.OpenWebConfiguration ("", null, "clientTest");
116 _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
118 Assert.AreEqual ("web.config", Path.GetFileName (web.FilePath), "A1");
119 Assert.AreEqual (Path.GetDirectoryName (web.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
123 public void GetWebApplicationSection_1 ()
125 Assert.IsNotNull (WebConfigurationManager.GetWebApplicationSection ("system.web/clientTarget"), "A1");
129 public void GetSection_1 ()
131 object sect1 = WebConfigurationManager.GetSection ("system.web/clientTarget");
132 object sect2 = WebConfigurationManager.GetSection ("system.web/clientTarget");
133 Assert.AreEqual (sect1, sect2, "A1");
135 sect1 = WebConfigurationManager.GetSection ("foo");
136 Assert.IsNull (sect1);
138 sect1 = WebConfigurationManager.GetSection ("appSettings");
139 Assert.IsNotNull (sect1, "A2");
141 sect1 = WebConfigurationManager.GetSection ("connectionStrings");
142 Assert.IsNotNull (sect1, "A3");
146 [ExpectedException (typeof (InvalidOperationException))]
147 // InvalidOperationException (WebConfigurationManager.GetSection(sectionName,path) can only be called from within a web application.)
148 // thrown from WebConfigurationManager.GetSection
149 public void GetSection_2 ()
151 object sect1 = WebConfigurationManager.GetSection ("system.web/clientTarget", "/clientTest");
152 Assert.IsNull (sect1, "A1");
156 public void OpenMappedMachineConfiguration ()
158 ConfigurationFileMap map = new ConfigurationFileMap ();
160 _Configuration c1 = WebConfigurationManager.OpenMappedMachineConfiguration (map, "clientTest");
161 _Configuration c2 = ConfigurationManager.OpenMappedMachineConfiguration (map);
163 Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
167 [ExpectedException (typeof (ConfigurationErrorsException))]
168 // same stack trace for OpenWebConfiguration_siteNull2_absolutePath.
169 public void OpenMappedMachineConfiguration_absolute ()
171 ConfigurationFileMap map = new ConfigurationFileMap ();
173 WebConfigurationManager.OpenMappedMachineConfiguration (map, "/clientTest");
177 public void StaticProps ()
179 Assert.IsNotNull (WebConfigurationManager.AppSettings, "A1");
180 Assert.IsNotNull (WebConfigurationManager.ConnectionStrings, "A2");