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.
PropertyGetSetTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="PropertyGetSetTests.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.ComponentModel;
10using UnitDriven;
12using Csla.Core;
14using System.IO;
15using Csla.TestHelpers;
16
17#if NUNIT
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23#elif MSTEST
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25#endif
26
28{
29#if TESTING
30 //[System.Diagnostics.DebuggerNonUserCode]
31#endif
32 [TestClass]
34 {
36 private static TestDIContext _testDIContext;
37
39 public static void ClassInitialize(TestContext context)
40 {
41 _testDIContext = TestDIContextFactory.CreateDefaultContext();
42 }
43
44 [TestInitialize]
45 public void Initialize()
46 {
49 _changedName = "";
50 _changedName = "";
51 }
52
53 [TestCleanup]
54 public void Cleanup()
55 {
57 }
58
59 [TestMethod]
60 public void ForceStaticInit()
61 {
62 IDataPortal<EditableGetSetNFI> dataPortal = _testDIContext.CreateDataPortal<EditableGetSetNFI>();
63
65 root.Data = "a";
66 root.Base = "b";
67 root.TopBase = "c";
68 Assert.AreEqual("a", root.Data);
69 Assert.AreEqual("b", root.Base);
70 Assert.AreEqual("c", root.TopBase);
71 }
72
73 [TestMethod]
74 public void NullString()
75 {
76 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
77
78 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
79 root.FieldBackedString = null;
80 Assert.AreEqual(string.Empty, root.FieldBackedString, "FieldBackedString should be empty");
81 root.F06 = null;
82 Assert.AreEqual(string.Empty, root.F06, "F06 should be empty");
83 root.ManagedStringField = null;
84 Assert.AreEqual(string.Empty, root.ManagedStringField, "ManagedStringField should be empty");
85 root.M07 = null;
86 Assert.AreEqual(string.Empty, root.M07, "M07 should be empty");
87 }
88
89 [TestMethod]
91 {
92 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
93
94 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
95 root.LoadM02(123);
96 Assert.AreEqual(123, root.M02);
97
98 root.LoadInternalAndPrivate("Test");
99 Assert.AreEqual("Test", root.M08);
100
101 IDataPortal<Command> commandDataPortal = _testDIContext.CreateDataPortal<Command>();
102 var cmd = commandDataPortal.Create();
103 cmd.Load("abc");
104 Assert.AreEqual("abc", cmd.Name);
105
106 IDataPortal<ReadOnly> roDataPortal = _testDIContext.CreateDataPortal<ReadOnly>();
107 var ro = roDataPortal.Fetch();
108 ro.Load("abc");
109 Assert.AreEqual("abc", ro.Name);
110 }
111
112 [TestMethod]
114 {
115 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
116
117 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
118 root.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
119 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
120 Assert.AreEqual("n/a", root.F03, "Default value should have been set");
121 Assert.AreEqual("", root.FieldBackedString, "String should default to string.Empty");
122 Assert.AreEqual(0, root.F02, "Numeric should default to 0");
123 Assert.AreEqual(false, root.F05, "bool should default to false");
124
125 root.FieldBackedString = "hi there";
126 Assert.AreEqual("hi there", root.FieldBackedString, "String should have been set");
127
128 Assert.AreEqual("FieldBackedString", _changingName, "FieldBackedString should have been changing");
129
130 Assert.AreEqual("FieldBackedString", _changedName, "FieldBackedString should have changed");
131
132 root.F02 = 123;
133 Assert.AreEqual(123, root.F02, "Numeric should have been set");
134
135 root.F05 = true;
136 Assert.AreEqual(true, root.F05, "bool should have been set");
137
138 Assert.IsTrue(root.IsDirty, "Root should be dirty");
139
140 root.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
141 root.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
142 }
143
144 [TestMethod]
146 {
147 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
148
149 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
150 root = root.Clone();
151 Assert.AreEqual("n/a", root.F03, "Default value should have been set");
152 Assert.AreEqual("", root.FieldBackedString, "String should default to string.Empty");
153 Assert.AreEqual(0, root.F02, "Numeric should default to 0");
154
155 root.FieldBackedString = "hi there";
156 root = root.Clone();
157 Assert.AreEqual("hi there", root.FieldBackedString, "String should have been set");
158
159 root.F02 = 123;
160 Assert.AreEqual(123, root.F02, "Numeric should have been set");
161
162 Assert.IsTrue(root.IsDirty, "Root should be dirty");
163 }
164
165 [TestMethod]
167 {
168 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
169
170 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
171 root.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
172 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
173 Assert.AreEqual("n/a", root.M03, "Default value should have been set");
174 Assert.AreEqual("", root.ManagedStringField, "String should default to string.Empty");
175 Assert.AreEqual(0, root.M02, "Numeric should default to 0");
176 Assert.AreEqual(false, root.M05, "bool should default to false");
177
178 root.ManagedStringField = "hi there";
179 Assert.AreEqual("hi there", root.ManagedStringField, "String should have been set");
180 Assert.AreEqual("ManagedStringField", _changingName, "ManagedStringField should have been changing");
181 Assert.AreEqual("ManagedStringField", _changedName, "ManagedStringField should have changed");
182
183 _changedName = string.Empty;
184 _changingName = string.Empty;
185
186 root.ManagedStringField = "hi there";
187 Assert.AreEqual("hi there", root.ManagedStringField, "String should be the same");
188 Assert.AreEqual(string.Empty, _changingName, "ManagedStringField should not have been changing");
189
190 Assert.AreEqual(string.Empty, _changedName, "ManagedStringField should not have changed");
191
192 root.M02 = 123;
193 Assert.AreEqual(123, root.M02, "Numeric should have been set");
194
195 Assert.IsTrue(root.IsDirty, "Root should be dirty");
196
197 root.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
198
199 root.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
200 }
201
202 [TestMethod]
204 {
205 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
206
207 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
208
209 root.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
210
211 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
212 Assert.AreEqual("", root.Base, "String should default to string.Empty");
213 Assert.AreEqual("", root.TopBase, "TopBase should default to string.Empty");
214
215 root.Base = "hi there";
216 Assert.AreEqual("hi there", root.Base, "String should have been set");
217
218 Assert.AreEqual("Base", _changingName, "Base should have been changing");
219
220 Assert.AreEqual("Base", _changedName, "Base should have changed");
221
222 root.TopBase = "hi there";
223 Assert.AreEqual("hi there", root.TopBase, "TopBase should have been set");
224
225 Assert.AreEqual("TopBase", _changingName, "TopBase should have been changing");
226
227 Assert.AreEqual("TopBase", _changedName, "TopBase should have changed");
228
229 root.M05 = true;
230 Assert.AreEqual(true, root.M05, "bool should have been set");
231
232
233 root.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
234
235 root.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
236 }
237
238 [TestMethod]
240 {
241 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
242
243 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
244 root = root.Clone();
245 Assert.AreEqual("n/a", root.M03, "Default value should have been set");
246 Assert.AreEqual("", root.ManagedStringField, "String should default to string.Empty");
247 Assert.AreEqual(0, root.M02, "Numeric should default to 0");
248
249 root.ManagedStringField = "hi there";
250 root = root.Clone();
251 Assert.AreEqual("hi there", root.ManagedStringField, "String should have been set");
252
253 root.M02 = 123;
254 root = root.Clone();
255 Assert.AreEqual(123, root.M02, "Numeric should have been set");
256
257 Assert.IsTrue(root.IsDirty, "Root should be dirty");
258 }
259
260 [TestMethod]
261 public void MarkClean()
262 {
263 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
264
265 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
266
267 root.ManagedStringField = "hi there";
268 root.FieldBackedString = "hi there";
269 Assert.IsTrue(root.IsDirty, "Root should be dirty");
270
271 root.MarkClean();
272
273 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
274 }
275
276 [TestMethod]
278 {
279 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
280
281 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
282 Assert.AreEqual("", root.F04, "Field should default to string.Empty");
283 Assert.AreEqual("", root.M04, "Should default to string.Empty");
284
285 root.F04 = new DateTime(1998, 12, 21).ToShortDateString();
286 Assert.AreEqual(new DateTime(1998, 12, 21).ToShortDateString(), root.F04, "Field SmartDate should have been set");
287
288 root.M04 = new DateTime(1998,12,21).ToShortDateString();
289 Assert.AreEqual(new DateTime(1998, 12, 21).ToShortDateString(), root.M04, "SmartDate should have been set");
290
291 Assert.IsTrue(root.IsDirty, "Root should be dirty");
292 }
293
294 [TestMethod]
296 {
297 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
298
299 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
300
301 root.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
302
303 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
304
305 EditableGetSet child = root.ManagedChild;
306 Assert.IsNotNull(child, "Child should not be null");
307
308 Assert.AreEqual("ManagedChild", _changingName, "ManagedChild should have been changing");
309
310 Assert.AreEqual("ManagedChild", _changedName, "ManagedChild should have changed");
311
312 _changingName = "";
313
314 _changedName = "";
315
316 Assert.IsTrue(root.IsDirty, "Root should be dirty");
317 Assert.IsTrue(child.IsDirty, "Child should be dirty");
318
319
320 child.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
321
322 child.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
323 child.FieldBackedString = "hi there";
324
325 Assert.AreEqual("FieldBackedString", _changingName, "ManagedChild should NOT have been changing");
326 Assert.AreEqual(false, ("ManagedChild" == _changedName), "ManagedChild should have changed");
327
328
329 root.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
330 child.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
331
332 root.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
333 child.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
334 }
335
336 [TestMethod]
338 {
339 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
340
341 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
342 EditableGetSet child = root.ManagedChild;
343 child.FieldBackedString = "hi there";
344
345 root = root.Clone();
346
347 root.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
348
349 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
350
351 child = root.ManagedChild;
352 Assert.IsNotNull(child, "Child should not be null");
353 Assert.AreEqual("hi there", child.FieldBackedString, "Child value should be intact");
354
355 Assert.IsTrue(root.IsDirty, "Root should be dirty");
356 Assert.IsTrue(child.IsDirty, "Child should be dirty");
357
358
359 _changingName = "";
360 child.PropertyChanging += new PropertyChangingEventHandler(root_PropertyChanging);
361
362 _changedName = "";
363 child.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
364 child.FieldBackedString = "I've been cloned!";
365
366 Assert.AreEqual("FieldBackedString", _changingName, "ManagedChild should NOT have been changing");
367 Assert.AreEqual(false, ("ManagedChild" == _changedName), "ManagedChild should have changed");
368
369
370
371 child.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
372 root.PropertyChanging -= new PropertyChangingEventHandler(root_PropertyChanging);
373
374 child.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
375 root.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(root_PropertyChanged);
376 }
377
378 [TestMethod]
379 public void RootUndoCancel()
380 {
381 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
382
383 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
384
385 Assert.IsFalse(root.IsDirty, "Root should not start dirty");
386
387 Assert.AreEqual("", root.FieldBackedString, "Explicit String should default to string.Empty");
388 Assert.AreEqual("", root.ManagedStringField, "Managed String should default to string.Empty");
389
390 Assert.IsFalse(root.IsDirty, "Root should not be dirty after defaults load");
391
392 root.BeginEdit();
393 root.FieldBackedString = "fieldBackedString";
394 root.ManagedStringField = "ManagedStringField";
395 Assert.AreEqual("fieldBackedString", root.FieldBackedString, "String should be fieldBackedString");
396 Assert.AreEqual("ManagedStringField", root.ManagedStringField, "String should be ManagedStringField");
397
398 Assert.IsTrue(root.IsDirty, "Root should be dirty");
399
400 root.CancelEdit();
401 Assert.AreEqual("", root.FieldBackedString, "Explicit String should revert to string.Empty");
402 Assert.AreEqual("", root.ManagedStringField, "Managed String should revert to string.Empty");
403
404 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
405 }
406
407 [TestMethod]
408 [TestCategory("SkipWhenLiveUnitTesting")]
409 public void RootUndoApply()
410 {
411 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
412
413 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
414
415 Assert.IsFalse(root.IsDirty, "Root should not start dirty");
416
417 Assert.AreEqual("", root.FieldBackedString, "Explicit String should default to string.Empty");
418 Assert.AreEqual("", root.ManagedStringField, "Managed String should default to string.Empty");
419
420 Assert.IsFalse(root.IsDirty, "Root should not be dirty after defaults load");
421
422 root.BeginEdit();
423 root.FieldBackedString = "fieldBackedString";
424 root.ManagedStringField = "ManagedStringField";
425 Assert.AreEqual("fieldBackedString", root.FieldBackedString, "String should be fieldBackedString");
426 Assert.AreEqual("ManagedStringField", root.ManagedStringField, "String should be ManagedStringField");
427
428 Assert.IsTrue(root.IsDirty, "Root should be dirty");
429
430 root.ApplyEdit();
431 Assert.AreEqual("fieldBackedString", root.FieldBackedString, "String should be fieldBackedString after apply");
432 Assert.AreEqual("ManagedStringField", root.ManagedStringField, "String should be ManagedStringField after apply");
433
434 Assert.IsTrue(root.IsDirty, "Root should be dirty after ApplyEdit");
435 Assert.IsTrue(root.IsValid, "Root should be valid (no validation rules exist)");
436
437 root = root.Save();
438
439 Assert.IsFalse(root.IsDirty, "Root should not be dirty after Save");
440 }
441
442 [TestMethod]
444 {
445 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
446
447 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
448 Assert.AreEqual(0, root.EditLevel, "Root edit level before BeginEdit");
449 root.BeginEdit();
450 Assert.AreEqual(1, root.EditLevel, "Root edit level after BeginEdit");
451
452 EditableGetSet initialChild = root.ManagedChild;
453 Assert.AreEqual(1, initialChild.EditLevel, "Child edit level after being created");
454
455 Assert.IsTrue(root.IsDirty, "Root should be dirty");
456
457 root.CancelEdit();
458 Assert.AreEqual(0, root.EditLevel, "Root edit level after CancelEdit");
459 EditableGetSet secondChild = root.ManagedChild;
460 Assert.AreEqual(0, secondChild.EditLevel, "Second child edit level after being created");
461 Assert.IsFalse(ReferenceEquals(initialChild, secondChild), "Child objects should be different");
462
463 Assert.IsTrue(root.IsDirty, "Root should be dirty after second child created");
464 }
465
466 [TestMethod]
468 {
469 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
470 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
471
472 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
473 Assert.AreEqual(0, root.EditLevel, "Root edit level before BeginEdit");
474 root.BeginEdit();
475 Assert.AreEqual(1, root.EditLevel, "Root edit level after BeginEdit");
476 Assert.AreEqual(1, root.ManagedChild.EditLevel, "Child edit level after BeginEdit");
477 Assert.AreEqual(1, root.ManagedChildList.EditLevel, "List edit level after BeginEdit");
478 root.ManagedChildList.Add(EditableGetSet.NewChildObject(childDataPortal));
479 Assert.AreEqual(1, root.ManagedChildList[0].EditLevel, "List child edit level after BeginEdit");
480
481 root = root.Clone();
482 Assert.AreEqual(1, root.EditLevel, "Root edit level after Clone");
483 Assert.AreEqual(1, root.ManagedChild.EditLevel, "Child edit level after Clone");
484 Assert.AreEqual(1, root.ManagedChildList.EditLevel, "List edit level after Clone");
485 Assert.AreEqual(1, root.ManagedChildList[0].EditLevel, "List child edit level after Clone");
486
487 Assert.IsTrue(root.IsDirty, "Root should be dirty");
488 }
489
490 [TestMethod]
492 {
493 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
494
495 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
496 root.BeginEdit();
497
498 EditableGetSet initialChild = root.ManagedChild;
499
500 Assert.IsTrue(root.IsDirty, "Root should be dirty");
501 Assert.IsTrue(initialChild.IsDirty, "Child should be dirty");
502
503 root.CancelEdit();
504
505 // root.ManagedChild should be reset to null thus IsDirty should be false again.
506 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
507
508 Assert.IsTrue(root.ManagedChild.IsDirty, "Child should be dirty after lazy loading");
509 Assert.IsTrue(root.IsDirty, "Root should now be dirty since it lazy loaded ManagedChild");
510 }
511
512 [TestMethod]
513 public void RootChildUndoApply()
514 {
515 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
516
517 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
518 Assert.AreEqual(0, root.EditLevel, "Root edit level before BeginEdit");
519 root.BeginEdit();
520 Assert.AreEqual(1, root.EditLevel, "Root edit level after BeginEdit");
521
522 EditableGetSet initialChild = root.ManagedChild;
523 Assert.AreEqual(1, initialChild.EditLevel, "Child edit level after being created");
524
525 Assert.IsTrue(root.IsDirty, "Root should be dirty");
526
527 root.ApplyEdit();
528 Assert.AreEqual(0, root.EditLevel, "Root edit level after ApplyEdit");
529 EditableGetSet secondChild = root.ManagedChild;
530 Assert.AreEqual(0, secondChild.EditLevel, "Second child edit level after ApplyEdit");
531 Assert.IsTrue(ReferenceEquals(initialChild, secondChild), "Child objects should be the same");
532
533 Assert.IsTrue(root.IsDirty, "Root should be dirty after ApplyEdit");
534
535 root = root.Save();
536
537 Assert.IsFalse(root.IsDirty, "Root should not be dirty after Save");
538 }
539
540 [TestMethod]
542 {
543 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
544 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
545
546 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
547 Assert.AreEqual(0, root.EditLevel, "Root edit level before BeginEdit");
548 root.BeginEdit();
549 Assert.AreEqual(1, root.EditLevel, "Root edit level after BeginEdit");
550
551 ChildList list = root.ManagedChildList;
552 Assert.AreEqual(1, list.EditLevel, "List edit level after being created");
553
554 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
555 Assert.IsFalse(list.IsDirty, "List should not be dirty");
556
557 list.Add(EditableGetSet.NewChildObject(childDataPortal));
558 Assert.AreEqual(1, list.Count, "List count should be 1");
559
560 root.CancelEdit();
561 Assert.AreEqual(0, root.EditLevel, "Root edit level after CancelEdit");
562 ChildList secondList = root.ManagedChildList;
563 Assert.AreEqual(0, secondList.EditLevel, "Second list edit level after CancelEdit");
564 Assert.IsFalse(ReferenceEquals(list, secondList), "List objects should not be the same");
565
566 Assert.IsFalse(root.IsDirty, "Root should not be dirty after CancelEdit");
567 Assert.IsFalse(secondList.IsDirty, "Second list should not be dirty");
568 }
569
570 [TestMethod]
571 [TestCategory("SkipWhenLiveUnitTesting")]
573 {
574 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
575 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
576
577 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
578 Assert.AreEqual(0, root.EditLevel, "Root edit level before BeginEdit");
579 root.BeginEdit();
580 Assert.AreEqual(1, root.EditLevel, "Root edit level after BeginEdit");
581
582 var childList = root.ManagedChildList;
583 Assert.AreEqual(1, childList.EditLevel, "List edit level after being created");
584
585 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
586 Assert.IsFalse(childList.IsDirty, "List should not be dirty");
587
588 childList.Add(EditableGetSet.NewChildObject(childDataPortal));
589 Assert.AreEqual(1, childList.Count, "List count should be 1");
590
591 root.ApplyEdit();
592 Assert.AreEqual(0, root.EditLevel, "Root edit level after ApplyEdit");
593 var secondChildList = root.ManagedChildList;
594 Assert.AreEqual(0, secondChildList.EditLevel, "Second list edit level after ApplyEdit");
595 Assert.IsTrue(ReferenceEquals(childList, secondChildList), "List objects should be the same");
596
597 Assert.IsTrue(root.IsDirty, "Root should be dirty after ApplyEdit");
598 Assert.IsTrue(secondChildList.IsDirty, "Second list should be dirty");
599
600 root = root.Save();
601
602 Assert.IsFalse(root.IsDirty, "Root should not be dirty after Save");
603 Assert.IsFalse(root.ManagedChildList.IsDirty, "List should not be dirty after Save");
604 }
605
606 [TestMethod]
607 public void LoadNullProperty()
608 {
609 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
610
611 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
612 Assert.AreEqual(Guid.Empty, root.M06, "Guid should be null");
613 }
614
615#if !WINDOWS_PHONE
616// BUG: This method throws an exception during Type Initialization which causes Visual Studio
617// to crash if the debugger is attached to the emulator at the time this is run.
618// https://connect.microsoft.com/VisualStudio/feedback/details/606930/consistent-visual-studio-crash-on-typeinitializationexception-in-wp7-emulator
619 [TestMethod]
620 [ExpectedException(typeof(InvalidOperationException))]
622 {
623 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
624 IDataPortal<BadGetSet> badDataPortal = _testDIContext.CreateDataPortal<BadGetSet>();
625
626 var first = EditableGetSet.GetObject(dataPortal);
627 try
628 {
629 var root = BadGetSet.GetObject(badDataPortal);
630 var tmp = root.Id;
631 }
632 catch (TypeInitializationException ex)
633 {
634 throw ex.InnerException;
635 }
636 }
637
638 [TestMethod]
639 [ExpectedException(typeof(InvalidOperationException))]
641 {
642 IDataPortal<BadGetSetTwo> dataPortal = _testDIContext.CreateDataPortal<BadGetSetTwo>();
643
644 try
645 {
646 var root = BadGetSetTwo.GetObject(dataPortal);
647 var tmp = root.Id;
648 }
649 catch (TypeInitializationException ex)
650 {
651 if (ex.InnerException != null)
652 throw ex.InnerException;
653 else
654 throw;
655 }
656 }
657#endif
658
659 #region Event Bubbling
660
661 private string _changingName;
662 void root_PropertyChanging(object sender, PropertyChangingEventArgs e)
663 {
664 _changingName = e.PropertyName;
665 }
666
667 private string _changedName;
668 void root_PropertyChanged(object sender, PropertyChangedEventArgs e)
669 {
670 _changedName = e.PropertyName;
671 }
672
673 [TestMethod]
674 public void FieldDirty()
675 {
676 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
677
678 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
679 Assert.IsFalse(root.ManagedStringFieldDirty, "ManagedStringField should not be dirty");
680 root.ManagedStringField = "hi there";
681 Assert.IsTrue(root.ManagedStringFieldDirty, "ManagedStringField should be dirty");
682 }
683
684 [TestMethod]
686 {
687 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
688
689 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
690 root.ChildChanged += (o, e) => { throw new InvalidOperationException(); };
691 root.ManagedStringField = "test";
692 }
693
694 [TestMethod]
696 {
697 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
698 int changed = 0;
699
700 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
701 root.ChildChanged += (o, e) => { changed++; };
702 root.ManagedChild.ChildChanged += (o, e) => { throw new InvalidOperationException();};
703 root.ManagedChild.FieldBackedString = "changed";
704
705 Assert.AreEqual(1, changed);
706 }
707
708 [TestMethod]
710 {
711 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
712 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
713
714 int rootChanged = 0;
715 int listChanged = 0;
716
717 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
718 root.ChildChanged += (o, e) => { rootChanged++; };
719
720 var list = root.ManagedChildList;
721 list.ChildChanged += (o, e) => { listChanged++; };
722
723 list.Add(EditableGetSet.NewChildObject(childDataPortal));
724 list[0].FieldBackedString = "child change";
725
726 Assert.AreEqual(4, rootChanged);//this event fires 4 times: lazy load of the child list, Item[], Count and property change on item in the list
727 Assert.AreEqual(1, listChanged);
728 }
729
730 [TestMethod]
732 {
733 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
734 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
735
736 int rootChanged = 0;
737 int childListChanged = 0;
738 int grandChildListChanged = 0;
739 int childChanged = 0;
740 int grandChildPropertyChanged = 0;
741
742 EditableGetSet root = EditableGetSet.GetObject(dataPortal);
743 root.PropertyChanged += (o, e) => { throw new InvalidOperationException(); };
744 root.ChildChanged += (o, e) => { rootChanged++; };
745 root.ManagedChildList.ChildChanged += (o, e) => { childListChanged++; };
746
747 var child = EditableGetSet.NewChildObject(childDataPortal);
748 child.PropertyChanged += (o, e) => { throw new InvalidOperationException(); };
749 child.ChildChanged += (o, e) => { childChanged++; };
750 child.ManagedChildList.ChildChanged += (o, e) => { grandChildListChanged++; };
751
752 var grandChild = EditableGetSet.NewChildObject(childDataPortal);
753 grandChild.ChildChanged += (o, e) => { throw new InvalidOperationException(); }; // ChildChange only fires when child of self changes
754 grandChild.PropertyChanged += (o, e) => { grandChildPropertyChanged++; };
755
756 root.ManagedChildList.Add(child);
757 child.ManagedChildList.Add(grandChild);
758 root.ManagedChildList[0].ManagedChildList[0].FieldBackedString = "child change"; // or c2.FieldBackedString = "child change";
759
760 Assert.AreEqual(7, rootChanged); //Child, and GrandChild lists lazy loaded + Property changed on GrandChildList Item
761 Assert.AreEqual(4, childChanged); //GrandChild lists lazy loaded + Property changed on GrandChildList Item
762 Assert.AreEqual(4, childListChanged); //GrandChild lists lazy loaded + Property changed on GrandChildList Item
763 Assert.AreEqual(1, grandChildListChanged); //Property changed on GrandChildList Item
764 Assert.AreEqual(1, grandChildPropertyChanged);//Property changed on GrandChildList Item
765 }
766
767 [TestMethod]
769 {
770 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
771 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
772
773 var root = EditableGetSet.GetObject(dataPortal);
774 var child = EditableGetSet.NewChildObject(childDataPortal);
775 var grandChild = EditableGetSet.NewChildObject(childDataPortal);
776 root.ManagedChildList.Add(child);
777 child.ManagedChildList.Add(grandChild);
778
779 root.BeginEdit();
780 root.CancelEdit();
781
782 int changed = 0;
783 root.ChildChanged += (o, e) => { changed++;};
784 child.FieldBackedString = "changed";
785
786 Assert.AreEqual(1, changed);
787 }
788
789 [TestMethod]
791 {
792 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
793 IChildDataPortal<EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal<EditableGetSet>();
794
795 var root = EditableGetSet.GetObject(dataPortal);
796 var child = EditableGetSet.NewChildObject(childDataPortal);
797 var grandChild = EditableGetSet.NewChildObject(childDataPortal);
798 root.ManagedChildList.Add(child);
799 child.ManagedChildList.Add(grandChild);
800
801 var applicationContext = _testDIContext.CreateTestApplicationContext();
802 MemoryStream stream = new MemoryStream();
803 MobileFormatter formatter = new MobileFormatter(applicationContext);
804 formatter.Serialize(stream, root);
805 stream.Seek(0, SeekOrigin.Begin);
806 root = (EditableGetSet)formatter.Deserialize(stream);
807
808 int changed = 0;
809 root.ChildChanged += (o, e) => { changed++; };
810 root.ManagedChildList[0].ManagedChildList[0].FieldBackedString = "changed";
811 Assert.AreEqual(1, changed, "after MobileFormatter");
812
813 changed = 0;
814 root = root.Clone();
815 root.ChildChanged += (o, e) => { changed++; };
816 root.ManagedChildList[0].ManagedChildList[0].FieldBackedString = "changed again";
817 Assert.AreEqual(1, changed, "after clone");
818 }
819
820 [TestMethod]
821 [ExpectedException(typeof(InvalidOperationException))]
823 {
824 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
825
826 var root = EditableGetSet.GetObject(dataPortal);
827 var child = root.LazyChild;
828 }
829
830 [TestMethod]
832 {
833 IDataPortal<EditableGetSet> dataPortal = _testDIContext.CreateDataPortal<EditableGetSet>();
834 IDataPortal<ChildList> childDataPortal = _testDIContext.CreateDataPortal<ChildList>();
835
836 var root = EditableGetSet.GetObject(dataPortal);
837 root.LazyChild = ChildList.NewObject(childDataPortal);
838 var child = root.LazyChild;
839 Assert.IsNotNull(child);
840 }
841
842 #endregion
843 }
844
846 public class Command : CommandBase<Command>
847 {
848 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
849 public string Name
850 {
851 get { return ReadProperty(NameProperty); }
852 }
853
854 public void Load(string name)
855 {
856 LoadProperty((Csla.Core.IPropertyInfo)NameProperty, name);
857 }
858
859 [RunLocal]
860 [Create]
861 private void Create()
862 { }
863
864 }
865
867 public class ReadOnly : ReadOnlyBase<ReadOnly>
868 {
869 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
870 public string Name
871 {
872 get { return ReadProperty(NameProperty); }
873 }
874
875 public static readonly PropertyInfo<string> _originalNameProperty = RegisterProperty<string>(c => c.OriginalName);
876 internal string OriginalName
877 {
878 get { return ReadProperty(_originalNameProperty); }
879 }
880
881 public static readonly PropertyInfo<string> _originalNamePrivateProperty = RegisterProperty<string>(c => c.OriginalNamePrivate);
882 private string OriginalNamePrivate
883 {
885 }
886
887 public void Load(string name)
888 {
892 }
893
894 [Fetch]
895 private void Fetch()
896 { }
897 }
898}
Provides consistent context information between the client and server DataPortal objects.
static PropertyChangedModes PropertyChangedMode
Gets or sets a value specifying how CSLA .NET should raise PropertyChanged events.
PropertyChangedModes
Enum representing the way in which CSLA .NET should raise PropertyChanged events.
bool IsDirty
Gets a value indicating whether this object's data has been changed.
void ApplyEdit()
Commits the current edit process.
void CancelEdit()
Cancels the current edit process, restoring the object's state to its previous values.
void BeginEdit()
Starts a nested edit on the object.
T Clone()
Creates a clone of the object.
This is the base class from which command objects will be derived.
Definition: CommandBase.cs:51
Maintains metadata about a property.
This is a base class from which readonly business classes can be derived.
Definition: ReadOnlyBase.cs:55
virtual void LoadProperty(IPropertyInfo propertyInfo, object newValue)
Loads a property's managed field with the supplied value calling PropertyHasChanged if the value does...
virtual object ReadProperty(IPropertyInfo propertyInfo)
Gets a property's value as a specified type.
Serializes and deserializes objects at the field level.
object Deserialize(Stream serializationStream)
Deserialize an object from XML.
void Serialize(Stream serializationStream, object graph)
Serialize an object graph into XML.
static BadGetSet GetObject(IDataPortal< BadGetSet > dataPortal)
Definition: BadGetSet.cs:31
static BadGetSetTwo GetObject(IDataPortal< BadGetSetTwo > dataPortal)
Definition: BadGetSet.cs:63
static ChildList NewObject(IDataPortal< ChildList > dataPortal)
static readonly PropertyInfo< string > NameProperty
static EditableGetSet GetObject(IDataPortal< EditableGetSet > dataPortal)
static EditableGetSet NewChildObject(IChildDataPortal< EditableGetSet > childDataPortal)
static EditableGetSetNFI GetObject(IDataPortal< EditableGetSetNFI > dataPortal)
void If_FieldBackedString_Is_Changed_On_GrandChild_List_Item_After_Root_Is_Deserialized_Then_Root_ChildChanged_Event_Fires()
void If_FieldBackedString_Property_Is_Changed_On_Child_After_CancelEdit_Then_ChildChanged_Fires_On_Root()
void If_FieldBackedString_Property_Changes_On_Item_In_ManagedChildList_Then_ChildChanged_Fires_On_Root_And_On_ManagedChildList()
void If_FieldBackedString_Property_Changes_On_ManagedChild_Then_ChildChanged_Should_Fire_On_Root_ButNot_On_ManagedChild()
void If_FieldBackedString_Changes_On_GrandChild_Then_ChildChanged_Fires_On_GrandChild_Child_and_Root()
static void ClassInitialize(TestContext context)
static readonly PropertyInfo< string > NameProperty
static readonly PropertyInfo< string > _originalNameProperty
static readonly PropertyInfo< string > _originalNamePrivateProperty
Type to carry context information for DI in unit tests
Maintains metadata about a property.
Interface defining the members of the child data portal type.
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 Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
@ Guid
Globally unique identifier / Guid
@ Serializable
Prevents updating or inserting until the transaction is complete.