DataBoundControlTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Tests for System.Web.UI.WebControls.DataBoundControl.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([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.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using System.Text;
  38. using System.Collections;
  39. using System.Data;
  40. using MonoTests.SystemWeb.Framework;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. [TestFixture]
  44. public class DataBoundControlTest {
  45. class Poker : DataBoundControl {
  46. protected override void PerformSelect ()
  47. {
  48. Console.WriteLine ("PerformSelect\n{0}", Environment.StackTrace);
  49. }
  50. public void DoValidateDataSource (object dataSource)
  51. {
  52. ValidateDataSource (dataSource);
  53. }
  54. }
  55. class MyDataBoundControl : DataBoundControl
  56. {
  57. public int CreateDataSourceSelectArgumentsCalled;
  58. public DataSourceSelectArguments CreatedDataSourceSelectArguments;
  59. private StringBuilder dataBindTrace = new StringBuilder ();
  60. public string DataBindTrace {
  61. get { return dataBindTrace.ToString (); }
  62. }
  63. public override void DataBind () {
  64. dataBindTrace = new StringBuilder ();
  65. dataBindTrace.Append ("[Start DataBind]");
  66. base.DataBind ();
  67. dataBindTrace.Append ("[End DataBind]");
  68. }
  69. protected override void PerformSelect () {
  70. dataBindTrace.Append ("[Start PerformSelect]");
  71. base.PerformSelect ();
  72. dataBindTrace.Append ("[End PerformSelect]");
  73. }
  74. protected override void PerformDataBinding (IEnumerable data) {
  75. dataBindTrace.Append ("[Start PerformDataBinding]");
  76. base.PerformDataBinding (data);
  77. dataBindTrace.Append ("[End PerformDataBinding]");
  78. }
  79. protected override void OnDataBinding (EventArgs e) {
  80. dataBindTrace.Append ("[Start OnDataBinding]");
  81. base.OnDataBinding (e);
  82. dataBindTrace.Append ("[End OnDataBinding]");
  83. }
  84. protected override void OnDataBound (EventArgs e) {
  85. dataBindTrace.Append ("[Start OnDataBound]");
  86. base.OnDataBound (e);
  87. dataBindTrace.Append ("[End OnDataBound]");
  88. }
  89. protected override DataSourceView GetData () {
  90. dataBindTrace.Append ("[Start GetData]");
  91. DataSourceView d = base.GetData ();
  92. dataBindTrace.Append ("[End GetData]");
  93. return d;
  94. }
  95. public DataSourceView DoGetData () {
  96. return GetData ();
  97. }
  98. public IDataSource DoGetDataSource () {
  99. return GetDataSource ();
  100. }
  101. public void DoEnsureDataBound () {
  102. EnsureDataBound();
  103. }
  104. protected override DataSourceSelectArguments CreateDataSourceSelectArguments () {
  105. CreateDataSourceSelectArgumentsCalled++;
  106. CreatedDataSourceSelectArguments = base.CreateDataSourceSelectArguments ();
  107. return CreatedDataSourceSelectArguments;
  108. }
  109. public DataSourceSelectArguments GetSelectArguments () {
  110. return SelectArguments;
  111. }
  112. }
  113. [Test]
  114. public void DataBoundControl_GetData () {
  115. Page p = new Page ();
  116. MyDataBoundControl dc = new MyDataBoundControl ();
  117. p.Controls.Add (dc);
  118. DataSourceView data = dc.DoGetData ();
  119. Assert.IsNotNull (data, "GetData");
  120. IDataSource dataSource = dc.DoGetDataSource ();
  121. Assert.IsNotNull (dataSource, "GetDataSource");
  122. }
  123. [Test]
  124. public void DataBoundControl_DataBindFlow () {
  125. Page p = new Page ();
  126. MyDataBoundControl dc = new MyDataBoundControl ();
  127. p.Controls.Add (dc);
  128. dc.DataBind ();
  129. string expected = "[Start DataBind][Start PerformSelect][Start OnDataBinding][End OnDataBinding][Start GetData][End GetData][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
  130. Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
  131. }
  132. [Test]
  133. [Category ("NunitWeb")]
  134. public void DataBoundControl_DataBindFlow2 () {
  135. new WebTest (PageInvoker.CreateOnLoad (DataBoundControl_DataBindFlow2_Load)).Run ();
  136. }
  137. public static void DataBoundControl_DataBindFlow2_Load(Page p){
  138. MyDataBoundControl dc = new MyDataBoundControl ();
  139. p.Controls.Add (dc);
  140. dc.DataSourceID = "ObjectDataSource1";
  141. ObjectDataSource ods = new ObjectDataSource (typeof(Control).FullName, "ToString");
  142. ods.ID = "ObjectDataSource1";
  143. p.Controls.Add (ods);
  144. dc.DataBind ();
  145. string expected = "[Start DataBind][Start PerformSelect][Start GetData][End GetData][Start OnDataBinding][End OnDataBinding][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
  146. Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
  147. }
  148. [Test]
  149. public void DataBoundControl_DataBindFlow3 () {
  150. Page p = new Page ();
  151. MyDataBoundControl dc = new MyDataBoundControl ();
  152. p.Controls.Add (dc);
  153. DataSourceSelectArguments arg1 = dc.GetSelectArguments ();
  154. Assert.AreEqual (1, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#1");
  155. dc.DataBind ();
  156. DataSourceSelectArguments argCreated2 = dc.CreatedDataSourceSelectArguments;
  157. DataSourceSelectArguments arg2 = dc.GetSelectArguments ();
  158. Assert.AreEqual (2, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#2");
  159. dc.DataBind ();
  160. DataSourceSelectArguments argCreated3 = dc.CreatedDataSourceSelectArguments;
  161. Assert.AreEqual (3, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#3");
  162. Assert.IsTrue (object.ReferenceEquals (argCreated2, arg2), "CreateDataSourceSelectArgumentsCalled#4");
  163. }
  164. [Test]
  165. public void Defaults ()
  166. {
  167. Poker p = new Poker ();
  168. Assert.AreEqual ("", p.DataMember, "A1");
  169. Assert.AreEqual ("", p.DataSourceID, "A2");
  170. }
  171. [Test]
  172. public void ValidateDataSource () {
  173. Poker p = new Poker ();
  174. // Allows null
  175. p.DoValidateDataSource (null);
  176. }
  177. }
  178. }
  179. #endif