ProviderBaseTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // System.Configuration.Provider.ProviderBaseTest.cs - Unit tests
  3. // for System.Configuration.Provider.ProviderBase.
  4. //
  5. // Author:
  6. // Gert Driesen <[email protected]>
  7. //
  8. // Copyright (C) 2007 Gert Driesen
  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 System;
  31. using System.Collections.Specialized;
  32. using System.Configuration;
  33. using System.Configuration.Provider;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Configuration.Provider
  36. {
  37. [TestFixture]
  38. public class ProviderBaseTest
  39. {
  40. [Test]
  41. public void Initialize ()
  42. {
  43. MockProvider provider = new MockProvider ();
  44. provider.Initialize ("Mono", (NameValueCollection) null);
  45. Assert.IsNotNull (provider.Description, "#A1");
  46. Assert.AreEqual ("Mono", provider.Description, "#A2");
  47. Assert.IsNotNull (provider.Name, "#A3");
  48. Assert.AreEqual ("Mono", provider.Name, "#A4");
  49. provider = new MockProvider ();
  50. provider.Initialize (" ", (NameValueCollection) null);
  51. Assert.IsNotNull (provider.Description, "#B1");
  52. Assert.AreEqual (" ", provider.Description, "#B2");
  53. Assert.IsNotNull (provider.Name, "#B3");
  54. Assert.AreEqual (" ", provider.Name, "#B4");
  55. NameValueCollection config = new NameValueCollection ();
  56. config ["name"] = "Novell";
  57. config ["description"] = "DESC";
  58. config ["foo"] = "FOO";
  59. provider = new MockProvider ();
  60. provider.Initialize ("Mono", config);
  61. Assert.IsNotNull (provider.Description, "#C1");
  62. Assert.AreEqual ("DESC", provider.Description, "#C2");
  63. Assert.IsNotNull (provider.Name, "#C3");
  64. Assert.AreEqual ("Mono", provider.Name, "#C4");
  65. Assert.IsTrue (ContainsKey (config, "name"), "#C5");
  66. Assert.IsFalse (ContainsKey (config, "description"), "#C6");
  67. Assert.IsTrue (ContainsKey (config, "foo"), "#C7");
  68. config = new NameValueCollection ();
  69. config ["description"] = null;
  70. provider = new MockProvider ();
  71. provider.Initialize ("Mono", config);
  72. Assert.IsNotNull (provider.Description, "#D1");
  73. Assert.AreEqual ("Mono", provider.Description, "#D2");
  74. Assert.IsNotNull (provider.Name, "#D3");
  75. Assert.AreEqual ("Mono", provider.Name, "#D4");
  76. Assert.IsFalse (ContainsKey (config, "description"), "#D5");
  77. config = new NameValueCollection ();
  78. config ["description"] = string.Empty;
  79. provider = new MockProvider ();
  80. provider.Initialize ("Mono", config);
  81. Assert.IsNotNull (provider.Description, "#E1");
  82. Assert.AreEqual ("Mono", provider.Description, "#E2");
  83. Assert.IsNotNull (provider.Name, "#E3");
  84. Assert.AreEqual ("Mono", provider.Name, "#E4");
  85. Assert.IsFalse (ContainsKey (config, "description"), "#E5");
  86. config = new NameValueCollection ();
  87. config ["description"] = " ";
  88. provider = new MockProvider ();
  89. provider.Initialize ("Mono", config);
  90. Assert.IsNotNull (provider.Description, "#F1");
  91. Assert.AreEqual (" ", provider.Description, "#F2");
  92. Assert.IsNotNull (provider.Name, "#F3");
  93. Assert.AreEqual ("Mono", provider.Name, "#F4");
  94. Assert.IsFalse (ContainsKey (config, "description"), "#F5");
  95. }
  96. [Test]
  97. public void Initialize_AlreadyInitialized ()
  98. {
  99. MockProvider provider = new MockProvider ();
  100. provider.Initialize ("Mono", (NameValueCollection) null);
  101. try {
  102. provider.Initialize ("Mono", (NameValueCollection) null);
  103. Assert.Fail ("#1");
  104. } catch (InvalidOperationException ex) {
  105. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  106. Assert.IsNull (ex.InnerException, "#3");
  107. Assert.IsNotNull (ex.Message, "#4");
  108. }
  109. }
  110. [Test]
  111. public void Initialize_Name_Null ()
  112. {
  113. MockProvider provider = new MockProvider ();
  114. try {
  115. provider.Initialize ((string) null, new NameValueCollection ());
  116. Assert.Fail ("#1");
  117. } catch (ArgumentNullException ex) {
  118. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  119. Assert.IsNull (ex.InnerException, "#3");
  120. Assert.IsNotNull (ex.Message, "#4");
  121. Assert.IsNotNull (ex.ParamName, "#5");
  122. Assert.AreEqual ("name", ex.ParamName, "#6");
  123. }
  124. }
  125. [Test]
  126. public void Initialize_Name_Empty ()
  127. {
  128. MockProvider provider = new MockProvider ();
  129. try {
  130. provider.Initialize (string.Empty, new NameValueCollection ());
  131. Assert.Fail ("#1");
  132. } catch (ArgumentException ex) {
  133. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  134. Assert.IsNull (ex.InnerException, "#3");
  135. Assert.IsNotNull (ex.Message, "#4");
  136. Assert.IsNotNull (ex.ParamName, "#5");
  137. Assert.AreEqual ("name", ex.ParamName, "#6");
  138. }
  139. }
  140. static bool ContainsKey (NameValueCollection collection, string searchKey)
  141. {
  142. foreach (string key in collection)
  143. if (key == searchKey)
  144. return true;
  145. return false;
  146. }
  147. class MockProvider : ProviderBase
  148. {
  149. }
  150. }
  151. }
  152. #endif