SettingsBaseTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #if TARGET_JVM
  44. class CustomerException : Exception { }
  45. #endif
  46. class MySettings : SettingsBase
  47. {
  48. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  49. public int Foo {
  50. get { return (int) this ["Foo"]; }
  51. set { this ["Foo"] = value; }
  52. }
  53. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  54. [DefaultSettingValue ("20")]
  55. public int Bar {
  56. get { return (int) this ["Bar"]; }
  57. set { this ["Bar"] = value; }
  58. }
  59. }
  60. class MySettings2 : SettingsBase
  61. {
  62. int foo;
  63. [UserScopedSetting] // ignored in non-ApplicationSettingsBase
  64. public int Foo {
  65. get { return (int) this ["Foo"]; }
  66. set { this ["Foo"] = value; }
  67. }
  68. public override SettingsPropertyCollection Properties {
  69. get { return null; }
  70. }
  71. public SettingsPropertyCollection BaseProperties {
  72. get { return base.Properties; }
  73. }
  74. }
  75. [Test]
  76. public void PropertyDefaults ()
  77. {
  78. MySettings s = new MySettings ();
  79. Assert.IsNull (s.Properties, "#1");
  80. Assert.IsNull (s.Providers, "#2");
  81. Assert.IsNull (s.Context, "#3");
  82. Assert.AreEqual (0, s.PropertyValues.Count, "#4");
  83. Assert.IsNull (s.Properties, "#5");
  84. Assert.IsNull (s.Providers, "#6");
  85. Assert.IsNull (s.Context, "#7");
  86. s.Initialize (s.Context, s.Properties, s.Providers);
  87. }
  88. [Test]
  89. public void PropertiesOverriden ()
  90. {
  91. MySettings2 s = new MySettings2 ();
  92. s.Initialize (s.Context, new SettingsPropertyCollection (), s.Providers);
  93. Assert.IsNull (s.Properties, "#1");
  94. Assert.IsNotNull (s.BaseProperties, "#2");
  95. Assert.AreEqual (0, s.PropertyValues.Count, "#3");
  96. }
  97. [Test]
  98. public void PropertyValuesInstance ()
  99. {
  100. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  101. SettingsProviderCollection provs = new SettingsProviderCollection ();
  102. MyProvider p = new MyProvider ();
  103. MySettings s = new MySettings ();
  104. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  105. provs.Add (p);
  106. s.Initialize (new SettingsContext (), props, provs);
  107. Assert.AreEqual (s.PropertyValues, s.PropertyValues);
  108. }
  109. [Test]
  110. public void PropertyValuesUninitialized ()
  111. {
  112. MySettings s = new MySettings ();
  113. s.Initialize (new SettingsContext (), new SettingsPropertyCollection (), new SettingsProviderCollection ());
  114. s.Properties.Add (new SettingsProperty ("Foo"));
  115. // values are filled only at initialization phase.
  116. Assert.AreEqual (0, s.PropertyValues.Count, "#1");
  117. }
  118. [Test]
  119. public void PropertyValuesInitialized ()
  120. {
  121. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  122. SettingsProviderCollection provs = new SettingsProviderCollection ();
  123. MyProvider p = new MyProvider ();
  124. MySettings s = new MySettings ();
  125. int i;
  126. try {
  127. i = s.Foo;
  128. Assert.Fail ("#1-2");
  129. } catch (SettingsPropertyNotFoundException) {
  130. }
  131. s.Initialize (new SettingsContext (), props, provs);
  132. Assert.AreEqual (0, s.PropertyValues.Count, "#2-1");
  133. Assert.AreEqual (0, s.Context.Count, "#2-2");
  134. props.Add (new SettingsProperty ("Foo", typeof (int), p, false, 10, SettingsSerializeAs.String, null, true, true));
  135. // initialize w/o the provider
  136. s.Initialize (new SettingsContext (), props, provs);
  137. Assert.AreEqual (0, s.PropertyValues.Count, "#3-0");
  138. Assert.AreEqual (100, s.Foo, "#3-1");
  139. // ... !!!
  140. Assert.AreEqual (1, s.PropertyValues.Count, "#3-2");
  141. SettingsPropertyValue v = s.PropertyValues ["Foo"];
  142. Assert.AreEqual (100, v.PropertyValue, "#3-3");
  143. Assert.AreEqual (0, s.Context.Count, "#3-4");
  144. // initialize w/ the provider
  145. provs.Add (p);
  146. provs.Add (new MyProvider2 ("Bar", 25));
  147. props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider2"], false, 10, SettingsSerializeAs.String, null, true, true));
  148. s.Initialize (new SettingsContext (), props, provs);
  149. Assert.AreEqual (1, s.PropertyValues.Count, "#4-1");
  150. Assert.AreEqual (100, s.Foo, "#4-2");
  151. Assert.AreEqual (25, s.Bar, "#4-3");
  152. // ... !!!
  153. Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2");
  154. Assert.AreEqual (0, s.Context.Count, "#4-4");
  155. // wrong provider
  156. props.Remove ("Bar");
  157. props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider"], false, 10, SettingsSerializeAs.String, null, true, true));
  158. s = new MySettings ();
  159. s.Initialize (new SettingsContext (), props, provs);
  160. Assert.AreEqual (0, s.PropertyValues.Count, "#5-1");
  161. Assert.AreEqual (100, s.Foo, "#5-2");
  162. Assert.AreEqual (10, s.Bar, "#5-3");
  163. }
  164. [Test]
  165. public void AddPropertyTypeMismatch ()
  166. {
  167. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  168. SettingsProviderCollection provs = new SettingsProviderCollection ();
  169. MyProvider p = new MyProvider ();
  170. MySettings s = new MySettings ();
  171. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  172. provs.Add (p);
  173. s.Initialize (new SettingsContext (), props, provs);
  174. int i = s.Foo; // it still works as int, regardless of the settings property type...
  175. }
  176. [Test]
  177. [Ignore (".NET throws NRE, which means that it is not well designed.")]
  178. public void AddPropertyNoProviderButInProviders ()
  179. {
  180. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  181. SettingsProviderCollection provs = new SettingsProviderCollection ();
  182. MyProvider p = new MyProvider ();
  183. MySettings s = new MySettings ();
  184. props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true));
  185. provs.Add (p);
  186. s.Initialize (new SettingsContext (), props, provs);
  187. Assert.AreEqual (100, s.Foo);
  188. }
  189. [Test]
  190. public void ExceptionalGetPropertyValues ()
  191. {
  192. SettingsPropertyCollection props = new SettingsPropertyCollection ();
  193. SettingsProviderCollection provs = new SettingsProviderCollection ();
  194. MyProvider3 p = new MyProvider3 ();
  195. MySettings s = new MySettings ();
  196. props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
  197. provs.Add (p);
  198. s.Initialize (new SettingsContext (), props, provs);
  199. Assert.AreEqual (0, s.Context.Count, "#0");
  200. try {
  201. Assert.AreEqual (100, s.Foo, "#1");
  202. Assert.Fail ("#2");
  203. #if !TARGET_JVM
  204. } catch (Win32Exception) {
  205. #else
  206. } catch (CustomerException) {
  207. #endif
  208. }
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentException))]
  212. public void ProviderCollectionAddNameless ()
  213. {
  214. new SettingsProviderCollection ().Add (
  215. new MyProvider (true));
  216. }
  217. [Test]
  218. [ExpectedException (typeof (ArgumentException))]
  219. public void ProviderCollectionAddDuplicate ()
  220. {
  221. SettingsProviderCollection c = new SettingsProviderCollection ();
  222. c.Add (new MyProvider ());
  223. c.Add (new MyProvider ());
  224. }
  225. class MyProvider3 : MyProvider
  226. {
  227. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
  228. {
  229. #if !TARGET_JVM
  230. throw new Win32Exception (); // unlikely thrown otherwise.
  231. #else
  232. throw new CustomerException (); // unlikely thrown otherwise.
  233. #endif
  234. }
  235. }
  236. class MyProvider2 : MyProvider
  237. {
  238. public MyProvider2 (string item, object value)
  239. : base (item, value)
  240. {
  241. }
  242. public override string Name {
  243. get { return "MyProvider2"; }
  244. }
  245. }
  246. class MyProvider : SettingsProvider
  247. {
  248. bool bogus;
  249. string item;
  250. object default_value;
  251. public MyProvider ()
  252. : this (false)
  253. {
  254. }
  255. public MyProvider (bool bogus)
  256. {
  257. this.item = "Foo";
  258. default_value = 100;
  259. this.bogus = bogus;
  260. }
  261. public MyProvider (string item, object value)
  262. {
  263. this.item = item;
  264. this.default_value = value;
  265. }
  266. public override string Name {
  267. get { return bogus ? null : "MyProvider"; }
  268. }
  269. string app;
  270. public override string ApplicationName {
  271. get { return app; }
  272. set { app = value; }
  273. }
  274. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
  275. {
  276. SettingsPropertyValueCollection vals =
  277. new SettingsPropertyValueCollection ();
  278. foreach (SettingsProperty p in props)
  279. if (p.Provider == this) {
  280. SettingsPropertyValue pv = new SettingsPropertyValue (p);
  281. if (pv.Name == item)
  282. pv.PropertyValue = default_value;
  283. vals.Add (pv);
  284. }
  285. return vals;
  286. }
  287. public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
  288. {
  289. throw new Exception ();
  290. }
  291. }
  292. }
  293. }
  294. #endif