SettingsBaseTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // SettingsBaseTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //#define SPEW
  29. #if NET_2_0
  30. using System;
  31. using System.Text;
  32. using System.Configuration;
  33. using System.ComponentModel;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using NUnit.Framework;
  37. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  38. namespace MonoTests.System.Configuration
  39. {
  40. [TestFixture]
  41. public class SettingsBaseTest
  42. {
  43. class MySettings : SettingsBase
  44. {
  45. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  46. public int Foo {
  47. get { return (int) this ["Foo"]; }
  48. set { this ["Foo"] = value; }
  49. }
  50. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  51. [DefaultSettingValue ("20")]
  52. public int Bar {
  53. get { return (int) this ["Bar"]; }
  54. set { this ["Bar"] = value; }
  55. }
  56. }
  57. class MySettings2 : SettingsBase
  58. {
  59. int foo;
  60. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  61. public int Foo {
  62. get { return (int) this ["Foo"]; }
  63. set { this ["Foo"] = value; }
  64. }
  65. public override SettingsPropertyCollection Properties {
  66. get { return null; }
  67. }
  68. public SettingsPropertyCollection BaseProperties {
  69. get { return base.Properties; }
  70. }
  71. }
  72. [Test]
  73. public void PropertyDefaults ()
  74. {
  75. MySettings s = new MySettings ();
  76. Assert.IsNull (s.Properties, "#1");
  77. Assert.IsNull (s.Providers, "#2");
  78. Assert.IsNull (s.Context, "#3");
  79. Assert.AreEqual (0, s.PropertyValues.Count, "#4");
  80. Assert.IsNull (s.Properties, "#5");
  81. Assert.IsNull (s.Providers, "#6");
  82. Assert.IsNull (s.Context, "#7");
  83. s.Initialize (s.Context, s.Properties, s.Providers);
  84. }
  85. [Test]
  86. public void PropertiesOverriden ()
  87. {
  88. MySettings2 s = new MySettings2 ();
  89. s.Initialize (s.Context, new SettingsPropertyCollection (), s.Providers);
  90. Assert.IsNull (s.Properties, "#1");
  91. Assert.IsNotNull (s.BaseProperties, "#2");
  92. Assert.AreEqual (0, s.PropertyValues.Count, "#3");
  93. }
  94. [Test]
  95. public void PropertyValuesInstance ()
  96. {
  97. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  98. SettingsProviderCollection provs = new SettingsProviderCollection ();
  99. MyProvider p = new MyProvider ();
  100. MySettings s = new MySettings ();
  101. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  102. provs.Add (p);
  103. s.Initialize (new SettingsContext (), props, provs);
  104. Assert.AreEqual (s.PropertyValues, s.PropertyValues);
  105. }
  106. [Test]
  107. public void PropertyValuesUninitialized ()
  108. {
  109. MySettings s = new MySettings ();
  110. s.Initialize (new SettingsContext (), new SettingsPropertyCollection (), new SettingsProviderCollection ());
  111. s.Properties.Add (new SettingsProperty ("Foo"));
  112. // values are filled only at initialization phase.
  113. Assert.AreEqual (0, s.PropertyValues.Count, "#1");
  114. }
  115. [Test]
  116. public void PropertyValuesInitialized ()
  117. {
  118. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  119. SettingsProviderCollection provs = new SettingsProviderCollection ();
  120. MyProvider p = new MyProvider ();
  121. MySettings s = new MySettings ();
  122. int i;
  123. try {
  124. i = s.Foo;
  125. Assert.Fail ("#1-2");
  126. } catch (SettingsPropertyNotFoundException) {
  127. }
  128. s.Initialize (new SettingsContext (), props, provs);
  129. Assert.AreEqual (0, s.PropertyValues.Count, "#2-1");
  130. Assert.AreEqual (0, s.Context.Count, "#2-2");
  131. props.Add (new SettingsProperty ("Foo", typeof (int), p, false, 10, SettingsSerializeAs.String, null, true, true));
  132. // initialize w/o the provider
  133. s.Initialize (new SettingsContext (), props, provs);
  134. Assert.AreEqual (0, s.PropertyValues.Count, "#3-0");
  135. Assert.AreEqual (100, s.Foo, "#3-1");
  136. // ... !!!
  137. Assert.AreEqual (1, s.PropertyValues.Count, "#3-2");
  138. SettingsPropertyValue v = s.PropertyValues ["Foo"];
  139. Assert.AreEqual (100, v.PropertyValue, "#3-3");
  140. Assert.AreEqual (0, s.Context.Count, "#3-4");
  141. // initialize w/ the provider
  142. provs.Add (p);
  143. provs.Add (new MyProvider2 ("Bar", 25));
  144. props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider2"], false, 10, SettingsSerializeAs.String, null, true, true));
  145. s.Initialize (new SettingsContext (), props, provs);
  146. Assert.AreEqual (1, s.PropertyValues.Count, "#4-1");
  147. Assert.AreEqual (100, s.Foo, "#4-2");
  148. Assert.AreEqual (25, s.Bar, "#4-3");
  149. // ... !!!
  150. Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2");
  151. Assert.AreEqual (0, s.Context.Count, "#4-4");
  152. // wrong provider
  153. props.Remove ("Bar");
  154. props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider"], false, 10, SettingsSerializeAs.String, null, true, true));
  155. s = new MySettings ();
  156. s.Initialize (new SettingsContext (), props, provs);
  157. Assert.AreEqual (0, s.PropertyValues.Count, "#5-1");
  158. Assert.AreEqual (100, s.Foo, "#5-2");
  159. Assert.AreEqual (10, s.Bar, "#5-3");
  160. }
  161. [Test]
  162. public void AddPropertyTypeMismatch ()
  163. {
  164. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  165. SettingsProviderCollection provs = new SettingsProviderCollection ();
  166. MyProvider p = new MyProvider ();
  167. MySettings s = new MySettings ();
  168. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  169. provs.Add (p);
  170. s.Initialize (new SettingsContext (), props, provs);
  171. int i = s.Foo; // it still works as int, regardless of the settings property type...
  172. }
  173. [Test]
  174. [Ignore (".NET throws NRE, which means that it is not well designed.")]
  175. public void AddPropertyNoProviderButInProviders ()
  176. {
  177. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  178. SettingsProviderCollection provs = new SettingsProviderCollection ();
  179. MyProvider p = new MyProvider ();
  180. MySettings s = new MySettings ();
  181. props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true));
  182. provs.Add (p);
  183. s.Initialize (new SettingsContext (), props, provs);
  184. Assert.AreEqual (100, s.Foo);
  185. }
  186. [Test]
  187. public void ExceptionalGetPropertyValues ()
  188. {
  189. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  190. SettingsProviderCollection provs = new SettingsProviderCollection ();
  191. MyProvider3 p = new MyProvider3 ();
  192. MySettings s = new MySettings ();
  193. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  194. provs.Add (p);
  195. s.Initialize (new SettingsContext (), props, provs);
  196. Assert.AreEqual (0, s.Context.Count, "#0");
  197. try {
  198. Assert.AreEqual (100, s.Foo, "#1");
  199. Assert.Fail ("#2");
  200. } catch (Win32Exception) {
  201. }
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void ProviderCollectionAddNameless ()
  206. {
  207. new SettingsProviderCollection ().Add (
  208. new MyProvider (true));
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentException))]
  212. public void ProviderCollectionAddDuplicate ()
  213. {
  214. SettingsProviderCollection c = new SettingsProviderCollection ();
  215. c.Add (new MyProvider ());
  216. c.Add (new MyProvider ());
  217. }
  218. class MyProvider3 : MyProvider
  219. {
  220. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
  221. {
  222. throw new Win32Exception (); // unlikely thrown otherwise.
  223. }
  224. }
  225. class MyProvider2 : MyProvider
  226. {
  227. public MyProvider2 (string item, object value)
  228. : base (item, value)
  229. {
  230. }
  231. public override string Name {
  232. get { return "MyProvider2"; }
  233. }
  234. }
  235. class MyProvider : SettingsProvider
  236. {
  237. bool bogus;
  238. string item;
  239. object default_value;
  240. public MyProvider ()
  241. : this (false)
  242. {
  243. }
  244. public MyProvider (bool bogus)
  245. {
  246. this.item = "Foo";
  247. default_value = 100;
  248. this.bogus = bogus;
  249. }
  250. public MyProvider (string item, object value)
  251. {
  252. this.item = item;
  253. this.default_value = value;
  254. }
  255. public override string Name {
  256. get { return bogus ? null : "MyProvider"; }
  257. }
  258. string app;
  259. public override string ApplicationName {
  260. get { return app; }
  261. set { app = value; }
  262. }
  263. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
  264. {
  265. SettingsPropertyValueCollection vals =
  266. new SettingsPropertyValueCollection ();
  267. foreach (SettingsProperty p in props)
  268. if (p.Provider == this) {
  269. SettingsPropertyValue pv = new SettingsPropertyValue (p);
  270. if (pv.Name == item)
  271. pv.PropertyValue = default_value;
  272. vals.Add (pv);
  273. }
  274. return vals;
  275. }
  276. public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
  277. {
  278. throw new Exception ();
  279. }
  280. }
  281. }
  282. }
  283. #endif