AppSettingsExpressionBuilderTest.cs 7.7 KB

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