ProviderBaseTest.cs 5.7 KB

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