RangeValidatorTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // Tests for System.Web.UI.WebControls.RangeValidator
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([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.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using WebSpace = System.Web;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. [TestFixture]
  42. public class RangeValidatorTest : ValidatorTest {
  43. public class RangeValidatorTestClass : RangeValidator {
  44. public RangeValidatorTestClass () {
  45. TrackViewState();
  46. }
  47. public object SaveState () {
  48. return SaveViewState ();
  49. }
  50. public void LoadState (object o) {
  51. LoadViewState (o);
  52. }
  53. public void SetTrackingVS () {
  54. TrackViewState ();
  55. }
  56. public void CheckProperties () {
  57. ControlPropertiesValid ();
  58. }
  59. public bool DoEvaluateIsValid () {
  60. return EvaluateIsValid ();
  61. }
  62. public void CallInit() {
  63. base.OnInit(EventArgs.Empty);
  64. }
  65. public string Render () {
  66. HtmlTextWriter writer;
  67. writer = RangeValidatorTest.GetWriter();
  68. base.Render (writer);
  69. return writer.InnerWriter.ToString ();
  70. }
  71. public bool UpRender () {
  72. return base.RenderUplevel;
  73. }
  74. }
  75. private static HtmlTextWriter GetWriter () {
  76. StringWriter sw = new StringWriter ();
  77. sw.NewLine = "\n";
  78. return new HtmlTextWriter (sw);
  79. }
  80. [Test]
  81. public void State () {
  82. RangeValidatorTestClass p;
  83. RangeValidatorTestClass p_copy;
  84. object state;
  85. p = new RangeValidatorTestClass();
  86. Assert.AreEqual (p.ControlToValidate, String.Empty, "S1");
  87. Assert.AreEqual (p.MinimumValue, String.Empty, "S2");
  88. Assert.AreEqual (p.MaximumValue, String.Empty, "S3");
  89. p.ControlToValidate = "TextBox";
  90. Assert.AreEqual ("TextBox", p.ControlToValidate, "S4");
  91. p.MinimumValue = "123";
  92. Assert.AreEqual ("123", p.MinimumValue, "S5");
  93. p.MaximumValue = "456";
  94. Assert.AreEqual ("456", p.MaximumValue, "S6");
  95. state = p.SaveState ();
  96. p_copy = new RangeValidatorTestClass ();
  97. p_copy.LoadState (state);
  98. Assert.AreEqual ("TextBox", p.ControlToValidate, "S7");
  99. Assert.AreEqual ("123", p.MinimumValue, "S8");
  100. Assert.AreEqual ("456", p.MaximumValue, "S9");
  101. }
  102. [Test]
  103. public void Defaults ()
  104. {
  105. RangeValidatorTestClass p;
  106. p = new RangeValidatorTestClass();
  107. Assert.AreEqual (String.Empty, p.ControlToValidate, "D1");
  108. Assert.AreEqual (String.Empty, p.MinimumValue, "D2");
  109. Assert.AreEqual (String.Empty, p.MaximumValue, "D3");
  110. Assert.AreEqual (true, p.EnableClientScript, "D4");
  111. Assert.AreEqual (false, p.UpRender(), "D5");
  112. }
  113. [Test]
  114. public void Render () {
  115. RangeValidatorTestClass p;
  116. TextBox t;
  117. p = new RangeValidatorTestClass();
  118. StartValidationTest(p);
  119. p.Type = ValidationDataType.Integer;
  120. Assert.AreEqual (false, p.UpRender(), "R0");
  121. t = SetValidationTextBox("textbox", "3");
  122. p.MinimumValue = "1";
  123. p.MaximumValue = "2";
  124. p.Validate();
  125. p.ErrorMessage = "aw shucks";
  126. p.Display = ValidatorDisplay.Static;
  127. p.Enabled = true;
  128. p.EnableViewState = true;
  129. Assert.AreEqual("<span style=\"color:Red;\">aw shucks</span>", p.Render(), "R1");
  130. }
  131. [Test]
  132. public void ValidateRangeTest () {
  133. RangeValidatorTestClass p;
  134. TextBox t;
  135. p = new RangeValidatorTestClass();
  136. StartValidationTest(p);
  137. p.Type = ValidationDataType.Integer;
  138. p.MinimumValue = "2";
  139. p.MaximumValue = "4";
  140. t = SetValidationTextBox("textbox", "1");
  141. Assert.AreEqual(false, p.DoEvaluateIsValid(), "V1");
  142. t.Text = "2";
  143. Assert.AreEqual(true, p.DoEvaluateIsValid(), "V2");
  144. t.Text = "3";
  145. Assert.AreEqual(true, p.DoEvaluateIsValid(), "V3");
  146. t.Text = "4";
  147. Assert.AreEqual(true, p.DoEvaluateIsValid(), "V4");
  148. t.Text = "5";
  149. Assert.AreEqual(false, p.DoEvaluateIsValid(), "V5");
  150. StopValidationTest();
  151. }
  152. [Test]
  153. [ExpectedException(typeof(WebSpace.HttpException))]
  154. public void Exception1Test () {
  155. RangeValidatorTestClass p;
  156. TextBox t;
  157. p = new RangeValidatorTestClass();
  158. p.CheckProperties();
  159. }
  160. [Test]
  161. [ExpectedException(typeof(WebSpace.HttpException))]
  162. public void Exception2Test () {
  163. RangeValidatorTestClass p;
  164. TextBox t;
  165. p = new RangeValidatorTestClass();
  166. StartValidationTest(p);
  167. t = SetValidationTextBox("textbox", "1");
  168. p.Type = ValidationDataType.Integer;
  169. p.MaximumValue = "1";
  170. p.CheckProperties();
  171. }
  172. [Test]
  173. [ExpectedException(typeof(WebSpace.HttpException))]
  174. public void Exception3Test () {
  175. RangeValidatorTestClass p;
  176. TextBox t;
  177. p = new RangeValidatorTestClass();
  178. StartValidationTest(p);
  179. t = SetValidationTextBox("textbox", "1");
  180. p.Type = ValidationDataType.Integer;
  181. p.MinimumValue = "1";
  182. p.CheckProperties();
  183. }
  184. [Test]
  185. [ExpectedException(typeof(WebSpace.HttpException))]
  186. public void Exception4Test () {
  187. RangeValidatorTestClass p;
  188. TextBox t;
  189. p = new RangeValidatorTestClass();
  190. StartValidationTest(p);
  191. t = SetValidationTextBox("textbox", "1");
  192. p.Type = ValidationDataType.Date;
  193. p.MaximumValue = "1";
  194. p.CheckProperties();
  195. }
  196. [Test]
  197. [ExpectedException(typeof(WebSpace.HttpException))]
  198. public void Exception5Test () {
  199. RangeValidatorTestClass p;
  200. TextBox t;
  201. p = new RangeValidatorTestClass();
  202. StartValidationTest(p);
  203. t = SetValidationTextBox("textbox", "1");
  204. p.Type = ValidationDataType.Integer;
  205. p.MinimumValue = "3";
  206. p.MaximumValue = "1";
  207. p.CheckProperties();
  208. }
  209. [Test]
  210. [ExpectedException(typeof(WebSpace.HttpException))]
  211. public void Exception6Test () {
  212. RangeValidatorTestClass p;
  213. TextBox t;
  214. p = new RangeValidatorTestClass();
  215. StartValidationTest(p);
  216. t = SetValidationTextBox("textbox", "1");
  217. p.Type = ValidationDataType.Date;
  218. p.MinimumValue = "01/01/02";
  219. p.MaximumValue = "01/01/01";
  220. p.CheckProperties();
  221. }
  222. [Test]
  223. //[ExpectedException(typeof(WebSpace.HttpException))]
  224. public void NoException7Test () {
  225. RangeValidatorTestClass p;
  226. TextBox t;
  227. p = new RangeValidatorTestClass();
  228. StartValidationTest(p);
  229. t = SetValidationTextBox("textbox", "1");
  230. p.Type = ValidationDataType.String;
  231. p.MinimumValue = "lasdjflk jasldfj ";
  232. p.MaximumValue = "s.dfjalsd fl;asdf";
  233. p.CheckProperties();
  234. }
  235. [Test]
  236. [ExpectedException(typeof(WebSpace.HttpException))]
  237. public void Exception8Test () {
  238. RangeValidatorTestClass p;
  239. TextBox t;
  240. p = new RangeValidatorTestClass();
  241. StartValidationTest(p);
  242. p.Type = ValidationDataType.Integer;
  243. p.MinimumValue = "2";
  244. p.MaximumValue = "4";
  245. t = SetValidationTextBox("textbox", "1");
  246. t = SetValidationTextBox("textbox", "2");
  247. p.DoEvaluateIsValid();
  248. }
  249. }
  250. }