SettingsPropertyValueTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // System.Configuration.SettingsPropertyValueTest.cs - Unit tests for
  3. // System.Configuration.SettingsPropertyValue.
  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. #if NET_2_0
  30. using System;
  31. using System.IO;
  32. using System.Configuration;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.Runtime.Serialization.Formatters.Binary;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Configuration {
  38. [TestFixture]
  39. public class SettingsPropertyValueTest {
  40. [Test]
  41. public void Properties ()
  42. {
  43. SettingsProperty p = new SettingsProperty ("property",
  44. typeof (int),
  45. null,
  46. true,
  47. 10,
  48. SettingsSerializeAs.String,
  49. null,
  50. true,
  51. false);
  52. SettingsPropertyValue v = new SettingsPropertyValue (p);
  53. Assert.IsFalse (v.Deserialized, "A1");
  54. Assert.IsFalse (v.IsDirty, "A2");
  55. Assert.AreEqual ("property", v.Name, "A3");
  56. Assert.AreEqual (p, v.Property, "A4");
  57. Assert.AreEqual ((object)10, v.PropertyValue, "A5");
  58. Assert.AreEqual (null, v.SerializedValue, "A6");
  59. Assert.IsTrue (v.UsingDefaultValue, "A7");
  60. /* test that setting v.PropertyValue to
  61. * something else causes SerializedValue to
  62. * become not-null */
  63. v.PropertyValue = (object)5;
  64. Assert.AreEqual ("5", v.SerializedValue, "A9");
  65. /* test to see whether or not changing
  66. * SerializeAs causes SerializedValue to
  67. * change */
  68. p.SerializeAs = SettingsSerializeAs.Xml;
  69. Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
  70. /* only changing PropertyValue does */
  71. v.PropertyValue = (object)7;
  72. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>7</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A13");
  73. }
  74. [Test]
  75. public void Dirty ()
  76. {
  77. SettingsProperty p = new SettingsProperty ("property",
  78. typeof (int),
  79. null,
  80. true,
  81. 10,
  82. SettingsSerializeAs.String,
  83. null,
  84. true,
  85. false);
  86. SettingsPropertyValue v = new SettingsPropertyValue (p);
  87. Assert.AreEqual (10, v.PropertyValue, "A0");
  88. Assert.IsFalse (v.IsDirty, "A1");
  89. /* set PropertyValue to something else */
  90. v.PropertyValue = 5;
  91. Assert.IsTrue (v.IsDirty, "A2");
  92. v.IsDirty = false;
  93. /* set PropertyValue to the same thing */
  94. v.PropertyValue = 5;
  95. Assert.IsTrue (v.IsDirty, "A3");
  96. /* try out a non-value type */
  97. p = new SettingsProperty ("property",
  98. typeof (StringWriter),
  99. null,
  100. true,
  101. "",
  102. SettingsSerializeAs.String,
  103. null,
  104. true,
  105. false);
  106. v = new SettingsPropertyValue (p);
  107. Assert.IsNotNull (v.PropertyValue, "A5");
  108. Console.WriteLine (v.PropertyValue);
  109. Assert.IsTrue (v.IsDirty, "A6");
  110. }
  111. [Test]
  112. public void UsingDefaultValue ()
  113. {
  114. SettingsProperty p = new SettingsProperty ("property",
  115. typeof (int),
  116. null,
  117. true,
  118. 10,
  119. SettingsSerializeAs.String,
  120. null,
  121. true,
  122. false);
  123. SettingsPropertyValue v = new SettingsPropertyValue (p);
  124. Assert.AreEqual (10, v.PropertyValue, "A1");
  125. Assert.IsTrue (v.UsingDefaultValue, "A2");
  126. /* set PropertyValue to something else */
  127. v.PropertyValue = 5;
  128. Assert.IsFalse (v.UsingDefaultValue, "A3");
  129. /* set PropertyValue back to the default */
  130. v.PropertyValue = 10;
  131. Assert.IsFalse (v.UsingDefaultValue, "A4");
  132. }
  133. [Test]
  134. public void String_Deserialize ()
  135. {
  136. SettingsProperty p = new SettingsProperty ("property",
  137. typeof (int),
  138. null,
  139. true,
  140. 10,
  141. SettingsSerializeAs.String,
  142. null,
  143. true,
  144. false);
  145. SettingsPropertyValue v = new SettingsPropertyValue (p);
  146. v.SerializedValue = "123";
  147. Assert.AreEqual (123, v.PropertyValue, "A1");
  148. Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
  149. Assert.AreEqual (false, v.UsingDefaultValue, "A3");
  150. }
  151. [Test]
  152. public void Xml_Deserialize ()
  153. {
  154. SettingsProperty p = new SettingsProperty ("property",
  155. typeof (int),
  156. null,
  157. true,
  158. 10,
  159. SettingsSerializeAs.Xml,
  160. null,
  161. true,
  162. false);
  163. SettingsPropertyValue v = new SettingsPropertyValue (p);
  164. v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
  165. Assert.AreEqual (123, v.PropertyValue, "A1");
  166. Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
  167. Assert.AreEqual (false, v.UsingDefaultValue, "A3");
  168. }
  169. [Test]
  170. public void String_Xml_Serialize ()
  171. {
  172. SettingsProperty p = new SettingsProperty ("property",
  173. typeof (int),
  174. null,
  175. true,
  176. 10,
  177. SettingsSerializeAs.String,
  178. null,
  179. true,
  180. false);
  181. SettingsPropertyValue v = new SettingsPropertyValue (p);
  182. v.PropertyValue = 10;
  183. Assert.AreEqual (10, v.PropertyValue, "A1");
  184. Assert.AreEqual ("10", v.SerializedValue, "A2");
  185. v.PropertyValue = 10;
  186. p.SerializeAs = SettingsSerializeAs.Xml;
  187. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
  188. }
  189. [Test]
  190. public void Binary_Serialize ()
  191. {
  192. SettingsProperty p = new SettingsProperty ("property",
  193. typeof (int),
  194. null,
  195. true,
  196. 10,
  197. SettingsSerializeAs.Binary,
  198. null,
  199. true,
  200. false);
  201. SettingsPropertyValue v = new SettingsPropertyValue (p);
  202. byte[] foo;
  203. v.PropertyValue = 10;
  204. Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
  205. foo = (byte[])v.SerializedValue;
  206. v.PropertyValue = 5;
  207. Assert.AreEqual (5, v.PropertyValue, "A2");
  208. BinaryFormatter bf = new BinaryFormatter ();
  209. MemoryStream ms = new MemoryStream (foo);
  210. Assert.AreEqual (10, bf.Deserialize (ms), "A3");
  211. v.Deserialized = false;
  212. v.SerializedValue = foo;
  213. Assert.AreEqual (10, v.PropertyValue, "A4");
  214. }
  215. [Test]
  216. public void DefaultValueType ()
  217. {
  218. SettingsProperty p1 = new SettingsProperty ("property",
  219. typeof (int),
  220. null,
  221. true,
  222. (int) 10,
  223. SettingsSerializeAs.String,
  224. null,
  225. true,
  226. false);
  227. SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
  228. Assert.AreEqual (typeof (int), v1.PropertyValue.GetType (), "A1");
  229. Assert.AreEqual (10, v1.PropertyValue, "A2");
  230. SettingsProperty p2 = new SettingsProperty ("property",
  231. typeof (int),
  232. null,
  233. true,
  234. "10",
  235. SettingsSerializeAs.String,
  236. null,
  237. true,
  238. false);
  239. SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
  240. Assert.AreEqual (typeof (int), v2.PropertyValue.GetType (), "A3");
  241. Assert.AreEqual (10, v2.PropertyValue, "A4");
  242. }
  243. [Serializable]
  244. public class MyData
  245. {
  246. private int intProp = 777;
  247. public int IntProp
  248. {
  249. get { return intProp; }
  250. set { intProp = value; }
  251. }
  252. }
  253. [Test]
  254. public void DefaultValueCompexTypeEmpty ()
  255. {
  256. SettingsProperty p1 = new SettingsProperty ("property",
  257. typeof (MyData),
  258. null,
  259. true,
  260. "",
  261. SettingsSerializeAs.String,
  262. null,
  263. true,
  264. false);
  265. SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
  266. Assert.IsNotNull (v1.PropertyValue, "A1");
  267. Assert.AreEqual (typeof (MyData), v1.PropertyValue.GetType (), "A2");
  268. MyData h = (MyData) v1.PropertyValue;
  269. Assert.AreEqual (777, h.IntProp, "A3");
  270. }
  271. [Test]
  272. public void DefaultValueCompexType ()
  273. {
  274. SettingsProperty p2 = new SettingsProperty ("property",
  275. typeof (MyData),
  276. null,
  277. true,
  278. @"<?xml version=""1.0"" encoding=""utf-16""?><MyData xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><IntProp>5</IntProp></MyData>",
  279. SettingsSerializeAs.Xml,
  280. null,
  281. true,
  282. false);
  283. SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
  284. Assert.IsNotNull (v2.PropertyValue, "A1");
  285. Assert.AreEqual (typeof (MyData), v2.PropertyValue.GetType (), "A2");
  286. MyData h = (MyData) v2.PropertyValue;
  287. Assert.AreEqual (5, h.IntProp, "A3");
  288. }
  289. [Test]
  290. public void IsDirtyAndValueDateTime ()
  291. {
  292. SettingsProperty sp = new SettingsProperty ("heh");
  293. sp.PropertyType = typeof (DateTime);
  294. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  295. Assert.IsFalse (spv.IsDirty, "A1");
  296. Assert.IsNotNull (spv.PropertyValue, "A2");
  297. Assert.AreEqual (typeof (DateTime), spv.PropertyValue.GetType (), "A3");
  298. Assert.IsFalse (spv.IsDirty, "A4");
  299. }
  300. [Test]
  301. public void IsDirtyAndValuePrimitive ()
  302. {
  303. SettingsProperty sp = new SettingsProperty ("heh");
  304. sp.PropertyType = typeof (int);
  305. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  306. Assert.IsFalse (spv.IsDirty, "A1");
  307. Assert.AreEqual (0, spv.PropertyValue, "A2");
  308. Assert.AreEqual (typeof (int), spv.PropertyValue.GetType (), "A3");
  309. Assert.IsFalse (spv.IsDirty, "A4");
  310. }
  311. [Test]
  312. public void IsDirtyAndValueDecimal ()
  313. {
  314. SettingsProperty sp = new SettingsProperty ("heh");
  315. sp.PropertyType = typeof (decimal);
  316. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  317. Assert.IsFalse (spv.IsDirty, "A1");
  318. Assert.AreEqual (0, spv.PropertyValue, "A2");
  319. Assert.AreEqual (typeof (decimal), spv.PropertyValue.GetType (), "A3");
  320. Assert.IsTrue (spv.IsDirty, "A4");
  321. }
  322. [Test]
  323. public void IsDirtyAndValueString ()
  324. {
  325. SettingsProperty sp = new SettingsProperty ("heh");
  326. sp.PropertyType = typeof (string);
  327. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  328. Assert.IsFalse (spv.IsDirty, "A1");
  329. Assert.IsNull (spv.PropertyValue, "A2");
  330. Assert.IsFalse (spv.IsDirty, "A3");
  331. SettingsProperty sp2 = new SettingsProperty ("heh");
  332. sp2.PropertyType = typeof (string);
  333. sp2.DefaultValue = "";
  334. SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
  335. Assert.IsFalse (spv2.IsDirty, "A4");
  336. Assert.IsNotNull (spv2.PropertyValue, "A5");
  337. Assert.IsFalse (spv2.IsDirty, "A6");
  338. }
  339. [Serializable]
  340. public struct MyData2
  341. {
  342. public int intProp;
  343. }
  344. [Test]
  345. public void IsDirtyAndValueMyData2 ()
  346. {
  347. SettingsProperty sp = new SettingsProperty ("heh");
  348. sp.PropertyType = typeof (MyData2);
  349. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  350. Assert.IsFalse (spv.IsDirty, "A1");
  351. Assert.IsNotNull (spv.PropertyValue, "A2");
  352. Assert.IsTrue (spv.IsDirty, "A3");
  353. }
  354. [Test]
  355. public void IsDirtyAndValueArrayList ()
  356. {
  357. SettingsProperty sp = new SettingsProperty ("heh");
  358. sp.PropertyType = typeof (ArrayList);
  359. SettingsPropertyValue spv = new SettingsPropertyValue (sp);
  360. Assert.IsFalse (spv.IsDirty, "A1");
  361. Assert.IsNull (spv.PropertyValue, "A2");
  362. Assert.IsFalse (spv.IsDirty, "A3");
  363. SettingsProperty sp2 = new SettingsProperty ("heh");
  364. sp2.PropertyType = typeof (ArrayList);
  365. sp2.DefaultValue = "";
  366. SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
  367. Assert.IsFalse (spv2.IsDirty, "A5");
  368. Assert.IsNotNull (spv2.PropertyValue, "A6");
  369. Assert.AreEqual (typeof (ArrayList), spv2.PropertyValue.GetType (), "A7");
  370. Assert.IsTrue (spv2.IsDirty, "A8");
  371. }
  372. }
  373. }
  374. #endif