SessionParameterTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Tests for System.Web.UI.WebControls.FormView.cs
  3. //
  4. // Author:
  5. // Merav Sudri ([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 System;
  31. using System.IO;
  32. using System.Collections.Generic;
  33. using System.Text;
  34. using NUnit.Framework;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. using MonoTests.SystemWeb.Framework;
  39. using MonoTests.stand_alone.WebHarness;
  40. namespace MonoTests.System.Web.UI.WebControls
  41. {
  42. public class SessionParameterPoker : SessionParameter
  43. {
  44. public SessionParameterPoker()
  45. {
  46. TrackViewState();
  47. }
  48. public SessionParameterPoker(SessionParameter param)
  49. : base(param)
  50. {
  51. }
  52. public SessionParameterPoker(string name, string sessionField)
  53. : base(name, sessionField)
  54. {
  55. }
  56. public SessionParameterPoker(string name, TypeCode type, string sessionField)
  57. : base(name, type, sessionField)
  58. {
  59. }
  60. public object DoEvaluate(HttpContext context, Control control)
  61. {
  62. return base.Evaluate(context, control);
  63. }
  64. public Parameter DoClone()
  65. {
  66. return base.Clone();
  67. }
  68. public object SaveState()
  69. {
  70. return SaveViewState();
  71. }
  72. public void LoadState(object o)
  73. {
  74. LoadViewState(o);
  75. }
  76. public StateBag StateBag
  77. {
  78. get { return base.ViewState; }
  79. }
  80. }
  81. [TestFixture]
  82. public class SessionParameterTest
  83. {
  84. [Test]
  85. public void SessionParameter_DefaultProperties()
  86. {
  87. SessionParameterPoker sessionParam1 = new SessionParameterPoker();
  88. Assert.AreEqual("", sessionParam1.SessionField, "DefaultSessionField");
  89. SessionParameterPoker sessionParam2 = new SessionParameterPoker("Name", "id");
  90. Assert.AreEqual("Name", sessionParam2.Name, "OverloadConstructorName1");
  91. Assert.AreEqual("id", sessionParam2.SessionField, "OverloadConstructorSessionField1");
  92. SessionParameterPoker sessionParam3 = new SessionParameterPoker("Name", TypeCode.Int64, "id");
  93. Assert.AreEqual("Name", sessionParam3.Name, "OverloadConstructorName2");
  94. Assert.AreEqual("id", sessionParam3.SessionField, "OverloadConstructorsessionField2");
  95. Assert.AreEqual(TypeCode.Int64, sessionParam3.Type, "OverloadConstructorType2");
  96. SessionParameterPoker sessionParam4 = new SessionParameterPoker(sessionParam3);
  97. Assert.AreEqual("Name", sessionParam4.Name, "OverloadConstructorName3");
  98. Assert.AreEqual("id", sessionParam4.SessionField, "OverloadConstructorSessionField3");
  99. Assert.AreEqual(TypeCode.Int64, sessionParam4.Type, "OverloadConstructorType3");
  100. }
  101. [Test]
  102. public void SessionParameter_AssignToDefaultProperties()
  103. {
  104. SessionParameterPoker sessionParam = new SessionParameterPoker();
  105. sessionParam.SessionField = "Test";
  106. Assert.AreEqual("Test", sessionParam.SessionField, "AssignToSessionField");
  107. }
  108. //Protected Methods
  109. [Test]
  110. public void SessionParameter_Clone()
  111. {
  112. SessionParameterPoker sessionParam = new SessionParameterPoker("EmployeeName", TypeCode.String, "Name");
  113. SessionParameter clonedParam = (SessionParameter)sessionParam.DoClone();
  114. Assert.AreEqual("EmployeeName", clonedParam.Name, "SessionParameterCloneName");
  115. Assert.AreEqual(TypeCode.String, clonedParam.Type, "SessionParameterCloneType");
  116. }
  117. [Test]
  118. [Category("NunitWeb")]
  119. public void SessionParameter_Evaluate()
  120. {
  121. SessionParameterPoker sessionParam = new SessionParameterPoker("employee",TypeCode.String ,"id") ;
  122. Button b = new Button();
  123. string value = (string)sessionParam.DoEvaluate(null, b);
  124. Assert.AreEqual(null, value, "EvaluateSessionWhenNullContext");
  125. WebTest t = new WebTest();
  126. PageDelegates pd = new PageDelegates();
  127. pd.Init = InitSesssion;
  128. pd.Load = EvaluateSession;
  129. t.Invoker = new PageInvoker(pd);
  130. string html = t.Run();
  131. WebTest.Unload();
  132. }
  133. public static void InitSesssion(Page p)
  134. {
  135. p.Session["key"] = "Key1";
  136. }
  137. public static void EvaluateSession(Page p)
  138. {
  139. SessionParameterPoker sessionParam = new SessionParameterPoker();
  140. sessionParam.SessionField = "key";
  141. sessionParam.Type = TypeCode.String;
  142. TextBox tb = new TextBox();
  143. p.Controls.Add(tb);
  144. string value = (string)sessionParam.DoEvaluate(HttpContext.Current, tb);
  145. Assert.AreEqual("Key1", value, "EvaluateSessionParameter");
  146. }
  147. }
  148. }
  149. #endif