SettingsPropertyValueTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.Specialized;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Configuration {
  37. [TestFixture]
  38. public class SettingsPropertyValueTest {
  39. [Test]
  40. public void Properties ()
  41. {
  42. SettingsProperty p = new SettingsProperty ("property",
  43. typeof (int),
  44. null,
  45. true,
  46. 10,
  47. SettingsSerializeAs.String,
  48. null,
  49. true,
  50. false);
  51. SettingsPropertyValue v = new SettingsPropertyValue (p);
  52. Assert.IsFalse (v.Deserialized, "A1");
  53. Assert.IsFalse (v.IsDirty, "A2");
  54. Assert.AreEqual ("property", v.Name, "A3");
  55. Assert.AreEqual (p, v.Property, "A4");
  56. Assert.AreEqual ((object)10, v.PropertyValue, "A5");
  57. Assert.AreEqual (null, v.SerializedValue, "A6");
  58. Assert.IsTrue (v.UsingDefaultValue, "A7");
  59. /* test that setting v.PropertyValue to
  60. * something else causes SerializedValue to
  61. * become not-null */
  62. v.PropertyValue = (object)5;
  63. Assert.AreEqual ("5", v.SerializedValue, "A9");
  64. /* test to see whether or not changing
  65. * SerializeAs causes SerializedValue to
  66. * change */
  67. p.SerializeAs = SettingsSerializeAs.Xml;
  68. Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
  69. /* only changing PropertyValue does */
  70. v.PropertyValue = (object)7;
  71. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>7</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A13");
  72. }
  73. [Test]
  74. public void Dirty ()
  75. {
  76. SettingsProperty p = new SettingsProperty ("property",
  77. typeof (int),
  78. null,
  79. true,
  80. 10,
  81. SettingsSerializeAs.String,
  82. null,
  83. true,
  84. false);
  85. SettingsPropertyValue v = new SettingsPropertyValue (p);
  86. Assert.AreEqual (10, v.PropertyValue, "A0");
  87. Assert.IsFalse (v.IsDirty, "A1");
  88. /* set PropertyValue to something else */
  89. v.PropertyValue = 5;
  90. Assert.IsTrue (v.IsDirty, "A2");
  91. v.IsDirty = false;
  92. /* set PropertyValue to the same thing */
  93. v.PropertyValue = 5;
  94. Assert.IsTrue (v.IsDirty, "A3");
  95. #if notyet
  96. /* the msdn docs say that
  97. * SettingsPropertyValue pessimistically sets
  98. * IsDirty to true when you use
  99. * v.PropertyValue on non-primitive types */
  100. /* try out a non-value type */
  101. p = new SettingsProperty ("property",
  102. typeof (StringWriter),
  103. null,
  104. true,
  105. null,
  106. SettingsSerializeAs.String,
  107. null,
  108. true,
  109. false);
  110. v = new SettingsPropertyValue (p);
  111. Assert.IsNull (v.PropertyValue, "A5");
  112. Console.WriteLine (v.PropertyValue);
  113. Assert.IsTrue (v.IsDirty, "A6");
  114. #endif
  115. }
  116. [Test]
  117. public void UsingDefaultValue ()
  118. {
  119. SettingsProperty p = new SettingsProperty ("property",
  120. typeof (int),
  121. null,
  122. true,
  123. 10,
  124. SettingsSerializeAs.String,
  125. null,
  126. true,
  127. false);
  128. SettingsPropertyValue v = new SettingsPropertyValue (p);
  129. /* this is wonky... ms returns false for all of the below cases */
  130. Assert.IsFalse (v.UsingDefaultValue, "A1");
  131. /* set PropertyValue to something else */
  132. v.PropertyValue = 5;
  133. Assert.IsFalse (v.UsingDefaultValue, "A2");
  134. /* set PropertyValue back to the default */
  135. v.PropertyValue = 10;
  136. Assert.IsFalse (v.UsingDefaultValue, "A3");
  137. /* let's try again */
  138. v = new SettingsPropertyValue (p);
  139. Assert.AreEqual (10, v.PropertyValue, "A4");
  140. Assert.IsTrue (v.UsingDefaultValue, "A5");
  141. }
  142. [Test]
  143. public void String_Xml_Serialize ()
  144. {
  145. SettingsProperty p = new SettingsProperty ("property",
  146. typeof (int),
  147. null,
  148. true,
  149. 10,
  150. SettingsSerializeAs.String,
  151. null,
  152. true,
  153. false);
  154. SettingsPropertyValue v = new SettingsPropertyValue (p);
  155. v.PropertyValue = 10;
  156. Assert.AreEqual (10, v.PropertyValue, "A1");
  157. Assert.AreEqual ("10", v.SerializedValue, "A2");
  158. v.PropertyValue = 10;
  159. p.SerializeAs = SettingsSerializeAs.Xml;
  160. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
  161. }
  162. [Test]
  163. public void Binary_Serialize ()
  164. {
  165. SettingsProperty p = new SettingsProperty ("property",
  166. typeof (int),
  167. null,
  168. true,
  169. 10,
  170. SettingsSerializeAs.Binary,
  171. null,
  172. true,
  173. false);
  174. SettingsPropertyValue v = new SettingsPropertyValue (p);
  175. byte[] foo;
  176. v.PropertyValue = 10;
  177. Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
  178. foo = (byte[])v.SerializedValue;
  179. v.PropertyValue = 5;
  180. Assert.AreEqual (5, v.PropertyValue, "A2");
  181. BinaryFormatter bf = new BinaryFormatter ();
  182. MemoryStream ms = new MemoryStream (foo);
  183. Assert.AreEqual (10, bf.Deserialize (ms), "A3");
  184. #if notyet
  185. v.Deserialized = false;
  186. v.SerializedValue = foo;
  187. Assert.AreEqual (10, v.PropertyValue, "A4");
  188. #endif
  189. }
  190. }
  191. }
  192. #endif