AppSettingsExpressionBuilderTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // Tests for System.Web.UI.WebControls.ListBoxTest.cs
  3. //
  4. // Author:
  5. // Vladimir Krasnov ([email protected])
  6. //
  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. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Drawing;
  33. using System.Collections.Specialized;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. using System.Data;
  39. using MonoTests.stand_alone.WebHarness;
  40. using MonoTests.SystemWeb.Framework;
  41. using System.Web.Compilation;
  42. namespace MonoTests.System.Web.Compilation
  43. {
  44. public class SettingTestingType
  45. {
  46. private string strProp;
  47. private int intProp;
  48. private DateTime dateTimeProp;
  49. private Type typeProp;
  50. public string StrProp {
  51. get { return strProp; }
  52. set { strProp = value; }
  53. }
  54. public int IntProp {
  55. get { return intProp; }
  56. set { intProp = value; }
  57. }
  58. public DateTime DateTimeProp {
  59. get { return dateTimeProp; }
  60. set { dateTimeProp = value; }
  61. }
  62. public Type TypeProp {
  63. get { return typeProp; }
  64. set { typeProp = value; }
  65. }
  66. }
  67. [TestFixture]
  68. public class AppSettingsExpressionBuilderTest
  69. {
  70. [Test] // GetAppSetting (String)
  71. [Category ("NunitWeb")]
  72. public void GetAppSetting1 ()
  73. {
  74. PageDelegates pd = new PageDelegates ();
  75. pd.Load = GetAppSetting1_Load;
  76. WebTest test = new WebTest (new PageInvoker (pd));
  77. test.Run ();
  78. }
  79. [Test] // GetAppSetting (String)
  80. public void GetAppSetting1_Key_DoesNotExist ()
  81. {
  82. try {
  83. AppSettingsExpressionBuilder.GetAppSetting ("DoesNotExist");
  84. Assert.Fail ("#1");
  85. } catch (InvalidOperationException ex) {
  86. // The application setting 'DoesNotExist' was
  87. // not found in the applications configuration
  88. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  89. Assert.IsNull (ex.InnerException, "#3");
  90. Assert.IsNotNull (ex.Message, "#4");
  91. Assert.IsTrue (ex.Message.IndexOf ("'DoesNotExist'") != -1, "#5");
  92. }
  93. }
  94. [Test] // GetAppSetting (String)
  95. public void GetAppSetting1_Key_Null ()
  96. {
  97. try {
  98. AppSettingsExpressionBuilder.GetAppSetting ((string) null);
  99. Assert.Fail ("#1");
  100. } catch (InvalidOperationException ex) {
  101. // The application setting '' was not found in
  102. // the applications configuration
  103. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  104. Assert.IsNull (ex.InnerException, "#3");
  105. Assert.IsNotNull (ex.Message, "#4");
  106. Assert.IsTrue (ex.Message.IndexOf ("''") != -1, "#5");
  107. }
  108. }
  109. [Test] // GetAppSetting (String, Type, String)
  110. [Category ("NunitWeb")]
  111. public void GetAppSetting2 ()
  112. {
  113. PageDelegates pd = new PageDelegates ();
  114. pd.Load = GetAppSetting2_Load;
  115. WebTest test = new WebTest (new PageInvoker (pd));
  116. test.Run ();
  117. }
  118. [Test] // GetAppSetting (String, Type, String)
  119. public void GetAppSetting2_Key_Null ()
  120. {
  121. try {
  122. AppSettingsExpressionBuilder.GetAppSetting (
  123. (string) null,
  124. typeof (SettingTestingType),
  125. "StrProp");
  126. Assert.Fail ("#1");
  127. } catch (InvalidOperationException ex) {
  128. // The application setting '' was not found in
  129. // the applications configuration
  130. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  131. Assert.IsNull (ex.InnerException, "#3");
  132. Assert.IsNotNull (ex.Message, "#4");
  133. Assert.IsTrue (ex.Message.IndexOf ("''") != -1, "#5");
  134. }
  135. }
  136. [Test]
  137. public void SupportsEvaluate ()
  138. {
  139. AppSettingsExpressionBuilder aseb = new AppSettingsExpressionBuilder ();
  140. Assert.IsTrue (aseb.SupportsEvaluate);
  141. }
  142. public static void GetAppSetting1_Load (Page p)
  143. {
  144. object o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue");
  145. Assert.AreEqual (typeof (string), o.GetType (), "#A1");
  146. Assert.AreEqual ("str", o, "#A2");
  147. o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue");
  148. Assert.AreEqual (typeof (string), o.GetType (), "#B1");
  149. Assert.AreEqual ("123", o, "#B2");
  150. }
  151. public static void GetAppSetting2_Load (Page p)
  152. {
  153. object o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue", typeof (SettingTestingType), "StrProp");
  154. Assert.AreEqual (typeof (string), o.GetType (), "#A1");
  155. Assert.AreEqual ("str", o, "#A2");
  156. // property does not exist
  157. o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue", typeof (SettingTestingType), "NotExistsProp");
  158. Assert.AreEqual (typeof (string), o.GetType (), "#B1");
  159. Assert.AreEqual ("str", o, "#B2");
  160. o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "IntProp");
  161. Assert.AreEqual (typeof (int), o.GetType (), "#C1");
  162. Assert.AreEqual (123, o, "#C2");
  163. // conversion
  164. o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "StrProp");
  165. Assert.AreEqual (typeof (string), o.GetType (), "#D1");
  166. Assert.AreEqual ("123", o, "#D2");
  167. // property does not exist
  168. o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "NotExistsProp");
  169. Assert.AreEqual (typeof (string), o.GetType (), "#E1");
  170. Assert.AreEqual ("123", o, "#E2");
  171. // targetType null
  172. o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", (Type) null, "NotExistsProp");
  173. Assert.AreEqual (typeof (string), o.GetType (), "#F1");
  174. Assert.AreEqual ("123", o, "#F2");
  175. // conversion failed
  176. try {
  177. AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
  178. typeof (SettingTestingType), "DateTimeProp");
  179. Assert.Fail ("#G1");
  180. } catch (FormatException ex) {
  181. // String was not recognized as a valid DateTime
  182. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
  183. Assert.IsNotNull (ex.Message, "#G3");
  184. }
  185. // conversion not supported
  186. try {
  187. AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
  188. typeof (SettingTestingType), "TypeProp");
  189. Assert.Fail ("#H1");
  190. } catch (InvalidOperationException ex) {
  191. // Could not convert the AppSetting '123' to the
  192. // type 'Type' on property 'TypeProp'
  193. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#H2");
  194. Assert.IsNull (ex.InnerException, "#H3");
  195. Assert.IsNotNull (ex.Message, "#H4");
  196. Assert.IsTrue (ex.Message.IndexOf ("'123'") != -1, "#H5");
  197. Assert.IsTrue (ex.Message.IndexOf ("'Type'") != -1, "#H6");
  198. Assert.IsTrue (ex.Message.IndexOf ("'TypeProp'") != -1, "#H7");
  199. }
  200. // propertyName null
  201. try {
  202. AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
  203. typeof (SettingTestingType), (string) null);
  204. Assert.Fail ("#I1");
  205. } catch (ArgumentNullException ex) {
  206. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#I2");
  207. Assert.IsNull (ex.InnerException, "#I3");
  208. Assert.IsNotNull (ex.Message, "#I4");
  209. //Assert.AreEqual ("key", ex.ParamName, "#I5");
  210. }
  211. }
  212. }
  213. }