CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla.test/Basic/BasicTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BasicTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11using Csla.TestHelpers;
12
13#if !NUNIT
14using Microsoft.VisualStudio.TestTools.UnitTesting;
15#else
16using NUnit.Framework;
17using TestClass = NUnit.Framework.TestFixtureAttribute;
18using TestInitialize = NUnit.Framework.SetUpAttribute;
19using TestCleanup = NUnit.Framework.TearDownAttribute;
20using TestMethod = NUnit.Framework.TestAttribute;
21#endif
22
23namespace Csla.Test.Basic
24{
25 [TestClass]
26 public class BasicTests
27 {
28 private static TestDIContext _testDIContext;
29
31 public static void ClassInitialize(TestContext context)
32 {
33 _testDIContext = TestDIContextFactory.CreateDefaultContext();
34 }
35
36 [TestMethod]
38 {
39 IDataPortal<DataBinding.ParentEntity> dataPortal = _testDIContext.CreateDataPortal<DataBinding.ParentEntity>();
40
42 Csla.Test.DataBinding.ParentEntity p = DataBinding.ParentEntity.NewParentEntity(dataPortal);
43
44 p.NotUndoable = "something";
45 p.Data = "data";
46 p.BeginEdit();
47 p.NotUndoable = "something else";
48 p.Data = "new data";
49 p.CancelEdit();
50 //NotUndoable property points to a private field marked with [NotUndoable()]
51 //so its state is never copied when BeginEdit() is called
52 Assert.AreEqual("something else", p.NotUndoable);
53 //the Data property points to a field that is undoable, so it reverts
54 Assert.AreEqual("data", p.Data);
55 }
56
57 [TestMethod]
58 public void TestReadOnlyList()
59 {
60 //ReadOnlyList list = ReadOnlyList.GetReadOnlyList();
61 //Assert.AreEqual("Fetched", TestResults.GetResult("ReadOnlyList"));
62 }
63
64 [TestMethod]
65 public void TestNameValueList()
66 {
67 IDataPortal<NameValueListObj> dataPortal = _testDIContext.CreateDataPortal<NameValueListObj>();
68
69 NameValueListObj nvList = dataPortal.Fetch();
70 Assert.AreEqual("Fetched", TestResults.GetResult("NameValueListObj"));
71
72 Assert.AreEqual("element_1", nvList[1].Value);
73
74 //won't work, because IsReadOnly is set to true after object is populated in the for
75 //loop in DataPortal_Fetch
76 //NameValueListObj.NameValuePair newPair = new NameValueListObj.NameValuePair(45, "something");
77
78 //nvList.Add(newPair);
79
80 //Assert.AreEqual("something", nvList[45].Value);
81 }
82
83 [TestMethod]
84 public void TestCommandBase()
85 {
86 IDataPortal<CommandObject> dataPortal = _testDIContext.CreateDataPortal<CommandObject>();
87
89 CommandObject obj = dataPortal.Create();
90 obj = dataPortal.Execute(obj);
91 Assert.AreEqual("Executed", obj.AProperty);
92 }
93
94 [TestMethod]
95 public void CreateGenRoot()
96 {
98 GenRoot root;
99 root = NewGenRoot();
100 Assert.IsNotNull(root);
101 Assert.AreEqual("<new>", root.Data);
102 Assert.AreEqual("Created", TestResults.GetResult("GenRoot"));
103 Assert.AreEqual(true, root.IsNew);
104 Assert.AreEqual(false, root.IsDeleted);
105 Assert.AreEqual(true, root.IsDirty);
106 }
107
108 [TestMethod]
109 public void InheritanceUndo()
110 {
112 GenRoot root;
113 root = NewGenRoot();
114 root.BeginEdit();
115 root.Data = "abc";
116 root.CancelEdit();
117
119 root = NewGenRoot();
120 root.BeginEdit();
121 root.Data = "abc";
122 root.ApplyEdit();
123 }
124
125 [TestMethod]
126 public void CreateRoot()
127 {
129 Root root;
130 root = NewRoot();
131 Assert.IsNotNull(root);
132 Assert.AreEqual("<new>", root.Data);
133 Assert.AreEqual("Created", TestResults.GetResult("Root"));
134 Assert.AreEqual(true, root.IsNew);
135 Assert.AreEqual(false, root.IsDeleted);
136 Assert.AreEqual(true, root.IsDirty);
137 }
138
139 [TestMethod]
140 public void AddChild()
141 {
142 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
143
145 Root root = NewRoot();
146 root.Children.Add(childDataPortal, "1");
147 Assert.AreEqual(1, root.Children.Count);
148 Assert.AreEqual("1", root.Children[0].Data);
149 }
150
151 [TestMethod]
152 public void AddRemoveChild()
153 {
154 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
155
157 Root root = NewRoot();
158 root.Children.Add(childDataPortal, "1");
159 root.Children.Remove(root.Children[0]);
160 Assert.AreEqual(0, root.Children.Count);
161 }
162
163 [TestMethod]
164 public void AddRemoveAddChild()
165 {
166 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
167
169 Root root = NewRoot();
170 root.Children.Add(childDataPortal, "1");
171 root.BeginEdit();
172 root.Children.Remove(root.Children[0]);
173
174 root.Children.Add(childDataPortal, "2");
175 root.CancelEdit();
176
177 Assert.AreEqual(1, root.Children.Count);
178 Assert.AreEqual("1", root.Children[0].Data);
179 }
180
181 [TestMethod]
182 public void AddGrandChild()
183 {
184 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
185
187 Root root = NewRoot();
188 root.Children.Add(childDataPortal, "1");
189 Child child = root.Children[0];
190 child.GrandChildren.Add("1");
191 Assert.AreEqual(1, child.GrandChildren.Count);
192 Assert.AreEqual("1", child.GrandChildren[0].Data);
193 }
194
195 [TestMethod]
197 {
198 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
199
201 Root root = NewRoot();
202 root.Children.Add(childDataPortal, "1");
203 Child child = root.Children[0];
204 child.GrandChildren.Add("1");
205 child.GrandChildren.Remove(child.GrandChildren[0]);
206 Assert.AreEqual(0, child.GrandChildren.Count);
207 }
208
212 //[TestMethod]
213 //public void CloneGraph()
214 //{
215 // TestResults.Reinitialise();
216 // Root root = NewRoot();
217 // FormSimulator form = new FormSimulator(root);
218 // SerializableListener listener = new SerializableListener(root);
219 // root.Children.Add("1");
220 // Child child = root.Children[0];
221 // Child.GrandChildren.Add("1");
222 // Assert.AreEqual<int>(1, child.GrandChildren.Count);
223 // Assert.AreEqual<string>("1", child.GrandChildren[0].Data);
224
225 // Root clone = ((Root)(root.Clone()));
226 // child = clone.Children[0];
227 // Assert.AreEqual<int>(1, child.GrandChildren.Count);
228 // Assert.AreEqual<string>("1", child.GrandChildren[0].Data);
229
230 // Assert.AreEqual<string>("root Deserialized", ((string)(TestResults.GetResult("Deserialized"))));
231 // Assert.AreEqual<string>("GC Deserialized", ((string)(TestResults.GetResult("GCDeserialized"))));
232 //}
233
234 [TestMethod]
235 public void ClearChildList()
236 {
237 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
238
240 Root root = NewRoot();
241 root.Children.Add(childDataPortal, "A");
242 root.Children.Add(childDataPortal, "B");
243 root.Children.Add(childDataPortal, "C");
244 root.Children.Clear();
245 Assert.AreEqual(0, root.Children.Count, "Count should be 0");
246 Assert.AreEqual(3, root.Children.DeletedCount, "Deleted count should be 3");
247 }
248
249 [TestMethod]
251 {
252 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
253
255 Root root = NewRoot();
256 root.BeginEdit();
257 root.Children.Add(childDataPortal, "A");
258 root.BeginEdit();
259 root.Children.Add(childDataPortal, "B");
260 root.BeginEdit();
261 root.Children.Add(childDataPortal, "C");
262 root.ApplyEdit();
263 root.ApplyEdit();
264 root.ApplyEdit();
265 Assert.AreEqual(3, root.Children.Count);
266 }
267
268 [TestMethod]
270 {
271 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
272
274 Root root = NewRoot();
275 root.BeginEdit();
276 root.Children.Add(childDataPortal, "A");
277 root.BeginEdit();
278 root.Children.Add(childDataPortal, "B");
279 root.BeginEdit();
280 root.Children.Add(childDataPortal, "C");
281 Child childC = root.Children[2];
282 Assert.AreEqual(true, root.Children.Contains(childC), "Child should be in collection");
283 root.Children.Remove(root.Children[0]);
284 root.Children.Remove(root.Children[0]);
285 root.Children.Remove(root.Children[0]);
286 Assert.AreEqual(false, root.Children.Contains(childC), "Child should not be in collection");
287 Assert.AreEqual(true, root.Children.ContainsDeleted(childC), "Deleted child should be in deleted collection");
288 root.ApplyEdit();
289 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after first applyedit");
290 root.ApplyEdit();
291 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after ApplyEdit");
292 root.ApplyEdit();
293 Assert.AreEqual(0, root.Children.Count, "No children should remain");
294 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after third applyedit");
295 }
296
297 [TestMethod]
298 public void BasicEquality()
299 {
301 Root r1 = NewRoot();
302 r1.Data = "abc";
303 Assert.AreEqual(true, r1.Equals(r1), "objects should be equal on instance compare");
304 Assert.AreEqual(true, Equals(r1, r1), "objects should be equal on static compare");
305
307 Root r2 = NewRoot();
308 r2.Data = "xyz";
309 Assert.AreEqual(false, r1.Equals(r2), "objects should not be equal");
310 Assert.AreEqual(false, Equals(r1, r2), "objects should not be equal");
311
312 Assert.AreEqual(false, r1.Equals(null), "Objects should not be equal");
313 Assert.AreEqual(false, Equals(r1, null), "Objects should not be equal");
314 Assert.AreEqual(false, Equals(null, r2), "Objects should not be equal");
315 }
316
317 [TestMethod]
318 public void ChildEquality()
319 {
320 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
321
323 Root root = NewRoot();
324 root.Children.Add(childDataPortal, "abc");
325 root.Children.Add(childDataPortal, "xyz");
326 root.Children.Add(childDataPortal, "123");
327 Child c1 = root.Children[0];
328 Child c2 = root.Children[1];
329 Child c3 = root.Children[2];
330 root.Children.Remove(c3);
331
332 Assert.AreEqual(true, c1.Equals(c1), "objects should be equal");
333 Assert.AreEqual(true, Equals(c1, c1), "objects should be equal");
334
335 Assert.AreEqual(false, c1.Equals(c2), "objects should not be equal");
336 Assert.AreEqual(false, Equals(c1, c2), "objects should not be equal");
337
338 Assert.AreEqual(false, c1.Equals(null), "objects should not be equal");
339 Assert.AreEqual(false, Equals(c1, null), "objects should not be equal");
340 Assert.AreEqual(false, Equals(null, c2), "objects should not be equal");
341
342 Assert.AreEqual(true, root.Children.Contains(c1), "Collection should contain c1");
343 Assert.AreEqual(true, root.Children.Contains(c2), "collection should contain c2");
344 Assert.AreEqual(false, root.Children.Contains(c3), "collection should not contain c3");
345 Assert.AreEqual(true, root.Children.ContainsDeleted(c3), "Deleted collection should contain c3");
346 }
347
348 [TestMethod]
349 public void DeletedListTest()
350 {
351 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
352
354 Root root = NewRoot();
355 root.Children.Add(childDataPortal, "1");
356 root.Children.Add(childDataPortal, "2");
357 root.Children.Add(childDataPortal, "3");
358 root.BeginEdit();
359 root.Children.Remove(root.Children[0]);
360 root.Children.Remove(root.Children[0]);
361 root.ApplyEdit();
362
363 Root copy = root.Clone();
364
365 var deleted = (List<Child>)(root.Children.GetType().GetProperty("DeletedList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.IgnoreCase).GetValue(copy.Children, null));
366
367 Assert.AreEqual(2, deleted.Count);
368 Assert.AreEqual("1", deleted[0].Data);
369 Assert.AreEqual("2", deleted[1].Data);
370 Assert.AreEqual(1, root.Children.Count);
371 }
372
373 [TestMethod]
375 {
376 IDataPortal<Child> childDataPortal = _testDIContext.CreateDataPortal<Child>();
377
379 Root root = NewRoot();
380 root.Children.Add(childDataPortal, "1");
381 root.Children.Add(childDataPortal, "2");
382 root.Children.Add(childDataPortal, "3");
383 root.BeginEdit();
384 root.Children.Remove(root.Children[0]);
385 root.Children.Remove(root.Children[0]);
386
387 Root copy = root.Clone();
388
389 List<Child> deleted = (List<Child>)(root.Children.GetType().GetProperty("DeletedList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.IgnoreCase).GetValue(copy.Children, null));
390
391 Assert.AreEqual(2, deleted.Count);
392 Assert.AreEqual("1", deleted[0].Data);
393 Assert.AreEqual("2", deleted[1].Data);
394 Assert.AreEqual(1, root.Children.Count);
395
396 root.CancelEdit();
397
398 deleted = (List<Child>)(root.Children.GetType().GetProperty("DeletedList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.IgnoreCase).GetValue(root.Children, null));
399
400 Assert.AreEqual(0, deleted.Count);
401 Assert.AreEqual(3, root.Children.Count);
402
403 }
404
405 [TestMethod]
407 {
408 bool changed = false;
409 var obj = new RootList();
410 obj.ListChanged += (o, e) =>
411 {
412 changed = true;
413 };
414 var child = new RootListChild(); // object is marked as child
415
416 Assert.IsTrue(obj.RaiseListChangedEvents);
417 using (obj.SuppressListChangedEvents)
418 {
419 Assert.IsFalse(obj.RaiseListChangedEvents);
420
421 obj.Add(child);
422 }
423 Assert.IsFalse(changed, "Should not raise ListChanged event");
424 Assert.IsTrue(obj.RaiseListChangedEvents);
425 Assert.AreEqual(child, obj[0]);
426 }
427
428 [TestCleanup]
430 {
432 }
433
434 private Root NewRoot()
435 {
436 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
437 return dataPortal.Create(new Root.Criteria());
438 }
439
440 private GenRoot NewGenRoot()
441 {
442 IDataPortal<GenRoot> dataPortal = _testDIContext.CreateDataPortal<GenRoot>();
443 return dataPortal.Create(new GenRoot.Criteria());
444 }
445 }
446
447 public class FormSimulator
448 {
449 private Core.BusinessBase _obj;
450
451 public FormSimulator(Core.BusinessBase obj)
452 {
453 this._obj.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(obj_IsDirtyChanged);
454 this._obj = obj;
455 }
456
457 private void obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
458 { }
459 }
460
461 [Serializable()]
462 public class SerializableListener
463 {
464 private Core.BusinessBase _obj;
465
466 public SerializableListener(Core.BusinessBase obj)
467 {
468 this._obj.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(obj_IsDirtyChanged);
469 this._obj = obj;
470 }
471
472 public void obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
473 { }
474 }
475
476}
T Clone()
Creates a clone of the object.
Definition: BusinessBase.cs:79
bool ContainsDeleted(C item)
Returns true if the internal deleted list contains the specified child object.
PropertyChangedEventHandler PropertyChanged
Implements a serialization-safe PropertyChanged event.
Definition: BindableBase.cs:40
BusinessBase()
Creates an instance of the type.
static void ClassInitialize(TestContext context)
void AddRemoveGrandChild()
remarks>"the non-generic method AreEqual cannot be used with type arguments" - though it is used with...
override bool Equals(object obj)
override bool IsDirty
start editing
void obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
ParentEntity()
DO NOT USE in UI - use the factory method instead
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
Definition: TestResults.cs:21
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Definition: TestResults.cs:69
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
Definition: TestResults.cs:49
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
object Execute(object obj)
Called to execute a Command object on the server.
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
@ Serializable
Prevents updating or inserting until the transaction is complete.