WebConfigurationManagerTest.cs 7.5 KB

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