mono-2.6.7-for-amiga /mcs/class/System.Web.DynamicData/Test/System.Web.DynamicData/FieldTemplateUserControlTest.cs

Language C# Lines 241
MD5 Hash 0a3ea4a087f0b148bba6977bf3312c1a Estimated Cost $3,403 (why?)
Repository https://bitbucket.org/steenlund/mono-2.6.7-for-amiga View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// FieldTemplateUserControlTest.cs
//
// Authors:
//      Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2009 Novell Inc. http://novell.com
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.DynamicData;
using System.Web.DynamicData.ModelProviders;
using System.Web.Routing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;

using NUnit.Framework;
using NUnit.Mocks;
using MonoTests.stand_alone.WebHarness;
using MonoTests.SystemWeb.Framework;
using MonoTests.Common;
using MonoTests.DataSource;
using MonoTests.DataObjects;
using MonoTests.ModelProviders;

using MetaModel = System.Web.DynamicData.MetaModel;
using MetaTable = System.Web.DynamicData.MetaTable;

namespace MonoTests.System.Web.DynamicData
{
	[TestFixture]
	public class FieldTemplateUserControlTest
	{
		DynamicDataContainerModelProvider<TestDataContext> dynamicModelProvider;

		[TestFixtureSetUp]
		public void SetUp ()
		{
			Type type = GetType ();
			WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_01.aspx", "ListView_DynamicControl_01.aspx");
			WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_01.aspx.cs", "ListView_DynamicControl_01.aspx.cs");
			WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_10.aspx", "ListView_DynamicControl_10.aspx");
			WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_10.aspx.cs", "ListView_DynamicControl_10.aspx.cs");

			dynamicModelProvider = new DynamicDataContainerModelProvider<TestDataContext> ();
			Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
		}

		[TestFixtureTearDown]
		public void TearDown ()
		{
			WebTest.Unload ();
		}

		[Test]
		public void Defaults ()
		{
			var field = new PokerFieldTemplateUserControl ();

			// And for another time - not a single null check in the code...
			//
			// All the asserts commented out below throw a NREX
			//

			//Assert.IsNull (field.ChildrenColumn, "#A1");
			//Assert.IsNull (field.GetChildrenPath (), "#A2");
			//Assert.IsNull (field.Column, "#A3");
			Assert.IsNull (field.DataControl, "#A4");
			//Assert.IsNull (field.FieldValue, "#A5");
			//Assert.IsNull (field.FieldValueEditString, "#A6");
			//Assert.IsNull (field.FieldValueString, "#A7");
			//Assert.IsNull (field.ForeignKeyColumn, "#A8");
			//Assert.IsNull (field.GetForeignKeyPath (), "#A9");
			Assert.IsNull (field.Host, "#A10");
			//Assert.IsNull (field.MetadataAttributes, "#A11");
			//Assert.IsNull (field.Mode, "#A12");
			//Assert.IsNull (field.Row, "#A13");
			//Assert.IsNull (field.Table, "#A14");
		}

		[Test]
		public void Defaults_WithHost ()
		{
			var test = new WebTest ("ListView_DynamicControl_01.aspx");
			test.Invoker = PageInvoker.CreateOnLoad (Defaults_WithHost_OnLoad);
			var p = test.Run ();
			Assert.IsNotNull (test.Response, "#X1");
			Assert.AreNotEqual (HttpStatusCode.NotFound, test.Response.StatusCode, "#X1-1{0}Returned HTML:{0}{1}", Environment.NewLine, p);
			Assert.AreNotEqual (HttpStatusCode.InternalServerError, test.Response.StatusCode, "#X1-2{0}Returned HTML:{0}{1}", Environment.NewLine, p);
			Assert.IsFalse (String.IsNullOrEmpty (p), "#X1-3");
		}

		static void Defaults_WithHost_OnLoad (Page p)
		{
			// Not many "defaults" here

			var lc = p.FindControl ("ListView1") as ListView;
			Assert.IsNotNull (lc, "#A1");

			var dc = lc.FindChild<DynamicControl> ("FirstName");
			Assert.IsNotNull (dc, "#B1-1");
			Assert.AreEqual ("FirstName", dc.ID, "#B1-2");

			var field = new PokerFieldTemplateUserControl ();

			// Without it Row NREXes... However, with Page set, we still get an exception
			// this time from Page.GetDataItem () as we're not bound to data. So, there is
			// no way to test the "defaults" as if we used a field control retrieved from 
			// DataControl it would already be initialized
			field.Page = p;
			((IFieldTemplate) field).SetHost (dc);

			AssertExtensions.Throws<Exception> (() => {
				var f = field.ChildrenColumn;
			}, "#C1");

			// The FirstName column is not a children one
			AssertExtensions.Throws<Exception> (() => {
				field.GetChildrenPath ();
			}, "#C2");
			Assert.IsNotNull (field.Column, "#C3");
			Assert.AreEqual (dc.Column, field.Column, "#C3-1");
			Assert.IsNull (field.DataControl, "#C4");

			// The tests below fail with exception:
			//
			// MonoTests.System.Web.DynamicData.FieldTemplateUserControlTest.Defaults_WithHost:
			// System.InvalidOperationException : System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
			//   at System.Web.UI.Page.GetDataItem()
			//   at System.Web.DynamicData.FieldTemplateUserControl.get_Row()
			//   at System.Web.DynamicData.FieldTemplateUserControl.GetColumnValue(MetaColumn column)
			//   at System.Web.DynamicData.FieldTemplateUserControl.get_FieldValue()
			//   at System.Web.DynamicData.FieldTemplateUserControl.get_FieldValueEditString()
			//   at MonoTests.System.Web.DynamicData.FieldTemplateUserControlTest.Defaults_WithHost_OnLoad(Page p)
			//   at MonoTests.SystemWeb.Framework.PageInvoker.Invoke(PageDelegate callback)
			//   at MonoTests.SystemWeb.Framework.PageInvoker.OnLoad(Object sender, EventArgs a)
			//   at System.EventHandler.Invoke(Object sender, EventArgs e)
			//   at System.Web.UI.Control.OnLoad(EventArgs e)
			//   at System.Web.UI.Control.LoadRecursive()
			//   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
			//  ----> System.InvalidOperationException : Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

			//Assert.IsNull (field.FieldValue, "#C5");
			//Assert.IsNull (field.FieldValueEditString, "#C6");
			//Assert.IsNull (field.FieldValueString, "#C7");

			// The FirstName column is not a foreign key one
			AssertExtensions.Throws<Exception> (() => {
				var f = field.ForeignKeyColumn;
			}, "#C8");
			AssertExtensions.Throws<Exception> (() => {
				var f = field.GetForeignKeyPath ();
			}, "#C9");
			Assert.IsNotNull (field.Host, "#C10");
			Assert.AreEqual (dc, field.Host, "#C10-1");
			Assert.IsNotNull (field.MetadataAttributes, "#C11");
			Assert.AreEqual (dc.Column.Attributes, field.MetadataAttributes, "#C11-1");
			Assert.AreEqual (DataBoundControlMode.ReadOnly, field.Mode, "#C12");
			Assert.AreEqual (field.Host.Mode, field.Mode, "#C12-1");

			// Failure with the same exception as above
			//Assert.IsNull (field.Row, "#C13");

			Assert.IsNotNull (field.Table, "#C14");
			Assert.AreEqual (dc.Table, field.Table, "#C14-1");
		}

		[Test]
		public void ChildrenPath ()
		{
			var test = new WebTest ("ListView_DynamicControl_10.aspx");
			test.Invoker = PageInvoker.CreateOnInit (ChildrenPath_OnInit);
			var p = test.Run ();
			Assert.IsNotNull (test.Response, "#X1");
			Assert.AreNotEqual (HttpStatusCode.NotFound, test.Response.StatusCode, "#X1-1{0}Returned HTML:{0}{1}", Environment.NewLine, p);
			Assert.AreNotEqual (HttpStatusCode.InternalServerError, test.Response.StatusCode, "#X1-2{0}Returned HTML:{0}{1}", Environment.NewLine, p);
			Assert.IsFalse (String.IsNullOrEmpty (p), "#X1-3");
		}

		static void ChildrenPath_OnInit (Page p)
		{
			var lc = p.FindControl ("ListView1") as ListView;
			Assert.IsNotNull (lc, "#A1");

			var page = p as TestsBasePage<TestDataContext3>;
			Assert.IsNotNull (p, "#A1-1");

			page.ItemDataBinding += new EventHandler(ChildrenPath_ListControl_OnDataBound);
		}

		static void ChildrenPath_ListControl_OnDataBound (object sender, EventArgs e)
		{
			var dc = sender as DynamicControl;
			Assert.IsNotNull (dc, "#B1");
			Assert.AreEqual ("PrimaryKeyColumn2", dc.ID, "#B1-1");
			Assert.AreEqual (typeof (MetaChildrenColumn), dc.Column.GetType (), "#B1-2");

			var field = dc.FieldTemplate as PokerFieldTemplateUserControl;
			Assert.IsNotNull (field, "#C1");
			Assert.AreEqual ("/NunitWeb/AssociatedBarTable/List.aspx", field.GetChildrenPath (), "#C1-1");
		}
	}
}
Back to Top