WebConfigurationManagerTest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // WebConfigurationManagerTest.cs
  3. // - unit tests for System.Web.Configuration.WebConfigurationManager
  4. //
  5. // Author:
  6. // Chris Toshok <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.Configuration;
  33. using _Configuration = System.Configuration.Configuration;
  34. using System.IO;
  35. using System.Web.Configuration;
  36. using System.Web;
  37. using System.Web.Security;
  38. using MonoTests.SystemWeb.Framework;
  39. using System.Web.UI;
  40. namespace MonoTests.System.Web.Configuration {
  41. [TestFixture]
  42. public class WebConfigurationManagerTest {
  43. [TestFixtureTearDown]
  44. public void Unload ()
  45. {
  46. WebTest.Unload ();
  47. }
  48. [SetUp]
  49. public void TestSetUp ()
  50. {
  51. WebTest.CopyResource (GetType (), "CustomSectionEmptyCollection.aspx", "CustomSectionEmptyCollection.aspx");
  52. }
  53. [Test]
  54. [Category ("NotWorking")]
  55. public void OpenMachineConfiguration_1 ()
  56. {
  57. _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ();
  58. _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
  59. Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
  60. }
  61. [Test]
  62. [Category ("NotWorking")]
  63. public void OpenMachineConfiguration_2 ()
  64. {
  65. _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ("configTest");
  66. _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
  67. Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
  68. }
  69. [Test]
  70. [Category ("NotWorking")]
  71. public void OpenMachineConfiguration_serverNull ()
  72. {
  73. _Configuration c1 = WebConfigurationManager.OpenMachineConfiguration ("configTest", null);
  74. _Configuration c2 = ConfigurationManager.OpenMachineConfiguration ();
  75. Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
  76. }
  77. [Test]
  78. [Category ("NotWorking")]
  79. public void OpenWebConfiguration_null ()
  80. {
  81. _Configuration web = WebConfigurationManager.OpenWebConfiguration (null);
  82. _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
  83. Assert.AreEqual ("web.config", Path.GetFileName (web.FilePath), "A1");
  84. Assert.AreEqual (Path.GetDirectoryName (web.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
  85. }
  86. [Test]
  87. [Category ("NotWorking")]
  88. public void OpenWebConfiguration_empty ()
  89. {
  90. _Configuration web1 = WebConfigurationManager.OpenWebConfiguration (null);
  91. _Configuration web2 = WebConfigurationManager.OpenWebConfiguration ("");
  92. _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
  93. Assert.AreEqual (web1.FilePath, web2.FilePath, "A1");
  94. Assert.AreEqual (Path.GetDirectoryName (web2.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
  95. }
  96. [Test]
  97. [Category ("NotWorking")]
  98. public void OpenWebConfiguration_siteNull ()
  99. {
  100. _Configuration web = WebConfigurationManager.OpenWebConfiguration ("", null);
  101. _Configuration machine = ConfigurationManager.OpenMachineConfiguration ();
  102. Assert.AreEqual ("web.config", Path.GetFileName (web.FilePath), "A1");
  103. Assert.AreEqual (Path.GetDirectoryName (web.FilePath), Path.GetDirectoryName (machine.FilePath), "A2");
  104. }
  105. [Test]
  106. [Category ("NotWorking")]
  107. [ExpectedException (typeof (ConfigurationErrorsException))]
  108. public void OpenWebConfiguration_siteNull2_absolutePath ()
  109. {
  110. WebConfigurationManager.OpenWebConfiguration ("", null, "/clientTest");
  111. }
  112. [Test]
  113. [Category ("NotWorking")]
  114. public void OpenWebConfiguration_siteNull2 ()
  115. {
  116. _Configuration web = WebConfigurationManager.OpenWebConfiguration ("", null, "clientTest");
  117. _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");
  120. }
  121. [Test]
  122. [Category ("NotWorking")]
  123. public void GetWebApplicationSection_1 ()
  124. {
  125. Assert.IsNotNull (WebConfigurationManager.GetWebApplicationSection ("system.web/clientTarget"), "A1");
  126. }
  127. [Test]
  128. [Category ("NotWorking")]
  129. public void GetSection_1 ()
  130. {
  131. object sect1 = WebConfigurationManager.GetSection ("system.web/clientTarget");
  132. object sect2 = WebConfigurationManager.GetSection ("system.web/clientTarget");
  133. Assert.AreEqual (sect1, sect2, "A1");
  134. sect1 = WebConfigurationManager.GetSection ("foo");
  135. Assert.IsNull (sect1);
  136. sect1 = WebConfigurationManager.GetSection ("appSettings");
  137. Assert.IsNotNull (sect1, "A2");
  138. sect1 = WebConfigurationManager.GetSection ("connectionStrings");
  139. Assert.IsNotNull (sect1, "A3");
  140. }
  141. [Test]
  142. [Category ("NotWorking")]
  143. [ExpectedException (typeof (InvalidOperationException))]
  144. // InvalidOperationException (WebConfigurationManager.GetSection(sectionName,path) can only be called from within a web application.)
  145. // thrown from WebConfigurationManager.GetSection
  146. public void GetSection_2 ()
  147. {
  148. object sect1 = WebConfigurationManager.GetSection ("system.web/clientTarget", "/clientTest");
  149. Assert.IsNull (sect1, "A1");
  150. }
  151. [Test]
  152. [Category ("NunitWeb")]
  153. public void ClientTarget () {
  154. new WebTest (PageInvoker.CreateOnLoad (ClientTarget_load)).Run ();
  155. }
  156. public static void ClientTarget_load (Page p) {
  157. ClientTargetSection sec = (ClientTargetSection) WebConfigurationManager.GetSection ("system.web/clientTarget");
  158. ClientTarget clientTarget = sec.ClientTargets ["downlevel"];
  159. if (clientTarget == null)
  160. Assert.Fail ("ClientTarget Section: downlevel");
  161. }
  162. [Test]
  163. [Category ("NotWorking")]
  164. public void OpenMappedMachineConfiguration ()
  165. {
  166. ConfigurationFileMap map = new ConfigurationFileMap ();
  167. _Configuration c1 = WebConfigurationManager.OpenMappedMachineConfiguration (map, "clientTest");
  168. _Configuration c2 = ConfigurationManager.OpenMappedMachineConfiguration (map);
  169. Assert.AreEqual (c1.FilePath, c2.FilePath, "A1");
  170. }
  171. [Test]
  172. [Category ("NotWorking")]
  173. [ExpectedException (typeof (ConfigurationErrorsException))]
  174. // same stack trace for OpenWebConfiguration_siteNull2_absolutePath.
  175. public void OpenMappedMachineConfiguration_absolute ()
  176. {
  177. ConfigurationFileMap map = new ConfigurationFileMap ();
  178. WebConfigurationManager.OpenMappedMachineConfiguration (map, "/clientTest");
  179. }
  180. [Test]
  181. public void StaticProps ()
  182. {
  183. Assert.IsNotNull (WebConfigurationManager.AppSettings, "A1");
  184. Assert.IsNotNull (WebConfigurationManager.ConnectionStrings, "A2");
  185. }
  186. [Test]
  187. public void CustomSectionEmptyCollection ()
  188. {
  189. WebTest t = new WebTest ("CustomSectionEmptyCollection.aspx");
  190. t.Run ();
  191. }
  192. }
  193. }
  194. #endif