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/Serialization/SerializationTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="SerializationTests.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 System.Linq;
12using System.ComponentModel;
13using System.Diagnostics;
17using UnitDriven;
18using System.Threading.Tasks;
19using System.Security.Claims;
20using Csla.TestHelpers;
22
23#if NUNIT
24using NUnit.Framework;
25using TestClass = NUnit.Framework.TestFixtureAttribute;
26using TestInitialize = NUnit.Framework.SetUpAttribute;
27using TestCleanup = NUnit.Framework.TearDownAttribute;
28using TestMethod = NUnit.Framework.TestAttribute;
29#else
30using Microsoft.VisualStudio.TestTools.UnitTesting;
32using System.IO;
33#endif
34
36{
37 [TestClass()]
38 public class SerializationTests : TestBase
39 {
40 private static TestDIContext _testDIContext;
41
43 private class TestCollection : BusinessBindingListBase<TestCollection, TestItem>
44 {
45 }
46
48 private class TestItem : BusinessBase<TestItem>
49 {
50 protected override object GetIdValue()
51 {
52 return 0;
53 }
54
55 public TestItem()
56 {
57 MarkAsChild();
58 }
59 }
60
61 private static ClaimsPrincipal GetPrincipal(params string[] roles)
62 {
63 var identity = new ClaimsIdentity();
64 foreach (var item in roles)
65 identity.AddClaim(new Claim(ClaimTypes.Role, item));
66 return new ClaimsPrincipal(identity);
67 }
68
70 public static void ClassInitialize(TestContext context)
71 {
72 _testDIContext = TestDIContextFactory.CreateDefaultContext();
73 }
74
75 [TestMethod]
77 {
78 var obj = new Csla.Server.DataPortalException("test message", new Exception("inner message"), null);
79 var applicationContext = _testDIContext.CreateTestApplicationContext();
80 var cloner = new Core.ObjectCloner(applicationContext);
81 var obj2 = (Csla.Server.DataPortalException)cloner.Clone(obj);
82 Assert.IsFalse(ReferenceEquals(obj, obj2));
83 Assert.AreEqual(obj.Message, obj2.Message);
84 }
85
86 [TestMethod]
88 {
90 Assert.IsTrue(serializer == typeof(MobileFormatter));
91 }
92
93 [TestMethod()]
95 {
96 IDataPortal<SerializationRoot> dataPortal = _testDIContext.CreateDataPortal<SerializationRoot>();
97
99 UnitTestContext context = GetContext();
102 handler.Reg(root);
103 root.Data = "something";
104 context.Assert.AreEqual("1", TestResults.GetResult("PropertyChangedFiredCount"));
105 root.Data = "something else";
106 context.Assert.AreEqual("2", TestResults.GetResult("PropertyChangedFiredCount"));
107
108 //serialize an object with eventhandling objects that are nonserializable
109 root = root.Clone();
110 root.Data = "something new";
111
112 //still at 2 even though we changed the property again
113 //when the clone method performs serialization, the nonserializable
114 //object containing an event handler for the propertyChanged event
115 //is lost
116 context.Assert.AreEqual("2", TestResults.GetResult("PropertyChangedFiredCount"));
117 context.Assert.Success();
118 }
119
120 [TestMethod()]
121 public void Clone()
122 {
123 IDataPortal<SerializationRoot> dataPortal = _testDIContext.CreateDataPortal<SerializationRoot>();
124
126 UnitTestContext context = GetContext();
127 SerializationRoot root = dataPortal.Create();
128
129 root = (SerializationRoot)root.Clone();
130
131 context.Assert.AreEqual(
132 "true",
133 TestResults.GetResult("Deserialized"),
134 "Deserialized not called");
135 context.Assert.Success();
136 }
137
138 [TestMethod()]
139 public void SerializableEvents()
140 {
141 IDataPortal<SerializationRoot> dataPortal = _testDIContext.CreateDataPortal<SerializationRoot>();
142
144 UnitTestContext context = GetContext();
145
147 TestEventSink handler = new TestEventSink();
148
149 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
150 (OnIsDirtyChanged);
151
152 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
153 (StaticOnIsDirtyChanged);
154
155 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
157
158 root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
159 (OnIsDirtyChanged); //will call this method twice since it is assigned twice
160
161 handler.Reg(root);
162
163 root.Data = "abc";
164
165 context.Assert.AreEqual("abc", root.Data, "Data value not set");
166
167 context.Assert.AreEqual(
168 "OnIsDirtyChanged",
169 TestResults.GetResult("OnIsDirtyChanged"),
170 "Didn't call local handler");
171
172 context.Assert.AreEqual(
173 "StaticOnIsDirtyChanged",
174 TestResults.GetResult("StaticOnIsDirtyChanged"),
175 "Didn't call static handler");
176
177 Assert.AreEqual(
178 "PublicStaticOnIsDirtyChanged",
179 TestResults.GetResult("PublicStaticOnIsDirtyChanged"),
180 "Didn't call public static handler");
181
182 Assert.AreEqual(
183 "Test.OnIsDirtyChanged",
184 TestResults.GetResult("Test.OnIsDirtyChanged"),
185 "Didn't call serializable handler");
186
187 Assert.AreEqual(
188 "Test.PrivateOnIsDirtyChanged",
189 TestResults.GetResult("Test.PrivateOnIsDirtyChanged"),
190 "Didn't call serializable private handler");
191
192 root = (SerializationRoot)root.Clone();
193
195
196 root.Data = "xyz";
197
198 context.Assert.AreEqual("xyz", root.Data, "Data value not set");
199
200 context.Assert.AreEqual("", TestResults.GetResult("OnIsDirtyChanged"),
201 "Called local handler after clone");
202
203 context.Assert.AreEqual("", TestResults.GetResult("StaticOnIsDirtyChanged"),
204 "Called static handler after clone");
205
206 context.Assert.AreEqual(
207 "PublicStaticOnIsDirtyChanged",
208 TestResults.GetResult("PublicStaticOnIsDirtyChanged"),
209 "Didn't call public static handler after clone");
210
211 context.Assert.AreEqual(
212 "Test.OnIsDirtyChanged",
213 TestResults.GetResult("Test.OnIsDirtyChanged"),
214 "Didn't call serializable handler after clone");
215
216 context.Assert.AreEqual("", TestResults.GetResult("Test.PrivateOnIsDirtyChanged"),
217 "Called serializable private handler after clone");
218
219 context.Assert.Success();
220 }
221
222 [TestMethod()]
224 {
225 IDataPortal<SerializationRoot> dataPortal = _testDIContext.CreateDataPortal<SerializationRoot>();
226
227 var root = SerializationRoot.NewSerializationRoot(dataPortal);
228 var nonSerClass = new NonSerializedClass();
229 Action<object, PropertyChangedEventArgs> h = (sender, eventArgs) => { nonSerClass.Do(); };
230 var method = typeof(Action<object, PropertyChangedEventArgs>).GetMethod("Invoke");
231 var delgate = (PropertyChangedEventHandler)(object)method.CreateDelegate(typeof(PropertyChangedEventHandler), h);
232 root.PropertyChanged += delgate;
233 // TODO: Should this test target another formatter, or just be deleted?
234 //var b = new BinaryFormatterWrapper();
235 //try
236 //{
237 // b.Serialize(new MemoryStream(), root);
238 // Assert.Fail("Serialization should have thrown an exception");
239 //}
240 //catch (System.Runtime.Serialization.SerializationException)
241 //{
242 // // serialization failed as expected
243 //}
244 }
245
246 [TestMethod()]
248 {
249 IDataPortal<OverrideSerializationRoot> dataPortal = _testDIContext.CreateDataPortal<OverrideSerializationRoot>();
250
252 var nonSerClass = new NonSerializedClass();
253
254 Action<object, PropertyChangedEventArgs> h = (sender, eventArgs) => { nonSerClass.Do(); };
255 var method = typeof(Action<object, PropertyChangedEventArgs>).GetMethod("Invoke");
256 var delgate = (PropertyChangedEventHandler)(object)method.CreateDelegate(typeof(PropertyChangedEventHandler), h);
257 root.PropertyChanged += delgate;
258
259 Action<object, PropertyChangingEventArgs> h1 = (sender, eventArgs) => { nonSerClass.Do(); };
260 var method1 = typeof(Action<object, PropertyChangingEventArgs>).GetMethod("Invoke");
261 var delgate1 = (PropertyChangingEventHandler)(object)method1.CreateDelegate(typeof(PropertyChangingEventHandler), h1);
262 root.PropertyChanging += delgate1;
263
264 // TODO: Would this test make sense if upgraded to MobileFormatter?
265 //var b = new BinaryFormatterWrapper();
266 //b.Serialize(new MemoryStream(), root);
267 }
268
269 [TestMethod()]
270 [TestCategory("SkipWhenLiveUnitTesting")]
272 {
273 IDataPortal<HasRulesManager> dataPortal = _testDIContext.CreateDataPortal<HasRulesManager>();
274
275 UnitTestContext context = GetContext();
276 var root = await dataPortal.CreateAsync(new HasRulesManager.Criteria());
277 root.Name = "";
278 context.Assert.AreEqual(false, root.IsValid, "root should not start valid");
279
280 root = root.Clone();
281 context.Assert.AreEqual(false, root.IsValid, "root should not be valid after clone");
282 root.Name = "something";
283 context.Assert.AreEqual(true, root.IsValid, "root should be valid after property set");
284 root = root.Clone();
285 context.Assert.AreEqual(true, root.IsValid, "root should be valid after second clone");
286 context.Assert.Success();
287
288 context.Complete();
289 }
290
291 [TestMethod()]
293 {
294 IDataPortal<BinaryReaderWriterTestClassList> dataPortal = _testDIContext.CreateDataPortal<BinaryReaderWriterTestClassList>();
295
298 test.Setup();
299
300 var applicationContext = _testDIContext.CreateTestApplicationContext();
301 MobileFormatter formatter = new MobileFormatter(applicationContext);
302 var serialized = formatter.SerializeToDTO(test);
303 CslaBinaryWriter writer = new CslaBinaryWriter(applicationContext);
304 byte[] data;
305 using (var stream = new MemoryStream())
306 {
307 writer.Write(stream, serialized);
308 data = stream.ToArray();
309 }
310
311 CslaBinaryReader reader = new CslaBinaryReader(applicationContext);
312 using (var stream = new MemoryStream(data))
313 {
314 var deserialized = reader.Read(stream);
315 result = (BinaryReaderWriterTestClassList)formatter.DeserializeFromDTO(deserialized);
316 }
317
318 Assert.AreEqual(test.Count, result.Count);
319 for (int i = 0; i < test.Count; i++)
320 {
321 Assert.AreEqual(test[i].CharTest, result[i].CharTest);
322 Assert.AreEqual(test[i].DateTimeOffsetTest, result[i].DateTimeOffsetTest);
323 Assert.AreEqual(test[i].DateTimeTest, result[i].DateTimeTest);
324 Assert.AreEqual(test[i].DecimalTest, result[i].DecimalTest);
325 Assert.AreEqual(test[i].DoubleTest, result[i].DoubleTest);
326 Assert.AreEqual(test[i].EnumTest, result[i].EnumTest);
327 Assert.AreEqual(test[i].GuidTest, result[i].GuidTest);
328 Assert.AreEqual(test[i].Int16Test, result[i].Int16Test);
329 Assert.AreEqual(test[i].Int32Test, result[i].Int32Test);
330 Assert.AreEqual(test[i].Int64Test, result[i].Int64Test);
331 Assert.AreEqual(test[i].SByteTest, result[i].SByteTest);
332 Assert.AreEqual(test[i].SingleTest, result[i].SingleTest);
333 Assert.AreEqual(test[i].StringTest, result[i].StringTest);
334 Assert.AreEqual(test[i].TimeSpanTest, result[i].TimeSpanTest);
335 Assert.AreEqual(test[i].UInt16Test, result[i].UInt16Test);
336 Assert.AreEqual(test[i].UInt32Test, result[i].UInt32Test);
337 Assert.AreEqual(test[i].UInt64Test, result[i].UInt64Test);
338
339 Assert.AreEqual(test[i].EmptySmartDateTest, result[i].EmptySmartDateTest);
340 Assert.AreEqual(test[i].EmptySmartDateTest.FormatString, result[i].EmptySmartDateTest.FormatString);
341 Assert.AreEqual(test[i].EmptySmartDateTest.EmptyIsMin, result[i].EmptySmartDateTest.EmptyIsMin);
342 Assert.AreEqual(test[i].EmptySmartDateTest.IsEmpty, result[i].EmptySmartDateTest.IsEmpty);
343 Assert.AreEqual(test[i].EmptySmartDateTest.Date, result[i].EmptySmartDateTest.Date);
344
345 Assert.AreEqual(test[i].FilledSmartDateTest, result[i].FilledSmartDateTest);
346 Assert.AreEqual(test[i].FilledSmartDateTest.FormatString, result[i].FilledSmartDateTest.FormatString);
347 Assert.AreEqual(test[i].FilledSmartDateTest.EmptyIsMin, result[i].FilledSmartDateTest.EmptyIsMin);
348 Assert.AreEqual(test[i].FilledSmartDateTest.IsEmpty, result[i].FilledSmartDateTest.IsEmpty);
349 Assert.AreEqual(test[i].FilledSmartDateTest.Date, result[i].FilledSmartDateTest.Date);
350 }
351 }
352
353
354 [TestMethod()]
356 {
357 IDataPortal<BinaryReaderWriterTestClass> dataPortal = _testDIContext.CreateDataPortal<BinaryReaderWriterTestClass>();
358
359 var test = BinaryReaderWriterTestClass.NewBinaryReaderWriterTestClass(dataPortal);
361 test.Setup();
362 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
363
364 MobileFormatter formatter = new MobileFormatter(applicationContext);
365 var serialized = formatter.SerializeToDTO(test);
366 CslaBinaryWriter writer = new CslaBinaryWriter(applicationContext);
367 byte[] data;
368 using (var stream = new MemoryStream())
369 {
370 writer.Write(stream, serialized);
371 data = stream.ToArray();
372 }
373
374 CslaBinaryReader reader = new CslaBinaryReader(applicationContext);
375 using (var stream = new MemoryStream(data))
376 {
377 var deserialized = reader.Read(stream);
378 result = (BinaryReaderWriterTestClass)formatter.DeserializeFromDTO(deserialized);
379 }
380 Assert.AreEqual(test.BoolTest, result.BoolTest);
381 Assert.AreEqual(test.ByteArrayTest.Length, result.ByteArrayTest.Length);
382 for (int i = 0; i < test.ByteArrayTest.Length; i++)
383 {
384 Assert.AreEqual(test.ByteArrayTest[i], result.ByteArrayTest[i]);
385 }
386
387 Assert.AreEqual(test.ByteTest, result.ByteTest);
388 Assert.AreEqual(test.CharArrayTest.Length, result.CharArrayTest.Length);
389 for (int i = 0; i < test.CharArrayTest.Length; i++)
390 {
391 Assert.AreEqual(test.CharArrayTest[i], result.CharArrayTest[i]);
392 }
393
394 Assert.AreEqual(test.CharTest, result.CharTest);
395 Assert.AreEqual(test.DateTimeOffsetTest, result.DateTimeOffsetTest);
396 Assert.AreEqual(test.DateTimeTest, result.DateTimeTest);
397 Assert.AreEqual(test.DecimalTest, result.DecimalTest);
398 Assert.AreEqual(test.DoubleTest, result.DoubleTest);
399 Assert.AreEqual(test.EnumTest, result.EnumTest);
400 Assert.AreEqual(test.GuidTest, result.GuidTest);
401 Assert.AreEqual(test.Int16Test, result.Int16Test);
402 Assert.AreEqual(test.Int32Test, result.Int32Test);
403 Assert.AreEqual(test.Int64Test, result.Int64Test);
404 Assert.AreEqual(test.SByteTest, result.SByteTest);
405 Assert.AreEqual(test.SingleTest, result.SingleTest);
406 Assert.AreEqual(test.StringTest, result.StringTest);
407 Assert.AreEqual(test.TimeSpanTest, result.TimeSpanTest);
408 Assert.AreEqual(test.UInt16Test, result.UInt16Test);
409 Assert.AreEqual(test.UInt32Test, result.UInt32Test);
410 Assert.AreEqual(test.UInt64Test, result.UInt64Test);
411
412 Assert.AreEqual(test.EmptySmartDateTest, result.EmptySmartDateTest);
413 Assert.AreEqual(test.EmptySmartDateTest.FormatString, result.EmptySmartDateTest.FormatString);
414 Assert.AreEqual(test.EmptySmartDateTest.EmptyIsMin, result.EmptySmartDateTest.EmptyIsMin);
415 Assert.AreEqual(test.EmptySmartDateTest.IsEmpty, result.EmptySmartDateTest.IsEmpty);
416 Assert.AreEqual(test.EmptySmartDateTest.Date, result.EmptySmartDateTest.Date);
417
418 Assert.AreEqual(test.FilledSmartDateTest, result.FilledSmartDateTest);
419 Assert.AreEqual(test.FilledSmartDateTest.FormatString, result.FilledSmartDateTest.FormatString);
420 Assert.AreEqual(test.FilledSmartDateTest.EmptyIsMin, result.FilledSmartDateTest.EmptyIsMin);
421 Assert.AreEqual(test.FilledSmartDateTest.IsEmpty, result.FilledSmartDateTest.IsEmpty);
422 Assert.AreEqual(test.FilledSmartDateTest.Date, result.FilledSmartDateTest.Date);
423 }
424
425 [TestMethod()]
427 {
428 TestDIContext adminDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
429 IDataPortal<Security.PermissionsRoot> dataPortal = _testDIContext.CreateDataPortal<Security.PermissionsRoot>();
430
431 Security.PermissionsRoot root = dataPortal.Create();
432
433 try
434 {
435 root.FirstName = "something";
436 Assert.Fail("Exception didn't occur");
437 }
439 {
440 Assert.AreEqual("Property set not allowed", ex.Message);
441 }
442
443 dataPortal = adminDIContext.CreateDataPortal<Security.PermissionsRoot>();
444 root = dataPortal.Create();
445
446 try
447 {
448 root.FirstName = "something";
449 }
451 {
452 Assert.Fail("exception occurred");
453 }
454
455 // TODO: Not sure how to recreate this test now; can't change context under the data portal mid flight
456 //Csla.ApplicationContext.User = new ClaimsPrincipal();
457
458 Csla.Test.Security.PermissionsRoot rootClone = root.Clone();
459
460 try
461 {
462 rootClone.FirstName = "something else";
463 Assert.Fail("Exception didn't occur");
464 }
466 {
467 Assert.AreEqual("Property set not allowed", ex.Message);
468 }
469
470 // TODO: Not sure how to recreate this test now; can't change context under the data portal mid flight
471 //Csla.ApplicationContext.User = GetPrincipal("Admin");
472
473 try
474 {
475 rootClone.FirstName = "something new";
476 }
478 {
479 Assert.Fail("exception occurred");
480 }
481
482 }
483
484 private void OnIsDirtyChanged(object sender, PropertyChangedEventArgs e)
485 {
486 TestResults.AddOrOverwrite("OnIsDirtyChanged", "OnIsDirtyChanged");
487 }
488
489 private static void StaticOnIsDirtyChanged(object sender, PropertyChangedEventArgs e)
490 {
491 TestResults.AddOrOverwrite("StaticOnIsDirtyChanged", "StaticOnIsDirtyChanged");
492 }
493
494 public static void PublicStaticOnIsDirtyChanged(object sender, PropertyChangedEventArgs e)
495 {
496 TestResults.AddOrOverwrite("PublicStaticOnIsDirtyChanged", "PublicStaticOnIsDirtyChanged");
497 }
498
499 [TestMethod]
500 [TestCategory("SkipWhenLiveUnitTesting")]
501 public void DCClone()
502 {
503 IDataPortal<DCRoot> dataPortal = _testDIContext.CreateDataPortal<DCRoot>();
504
505 System.Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"] =
506 "NetDataContractSerializer";
507 // TODO: NDCS has been dropped I think; is there a way to replicate this test with another formatter?
508 //Assert.AreEqual(
509 // Csla.ApplicationContext.SerializationFormatters.NetDataContractSerializer,
510 // Csla.ApplicationContext.SerializationFormatter,
511 // "Formatter should be NetDataContractSerializer");
512
513 DCRoot root = DCRoot.NewDCRoot(dataPortal);
514 root.Data = 123;
515 DCRoot clone = root.Clone();
516
517 Assert.IsFalse(ReferenceEquals(root, clone), "Object instance should be different");
518 Assert.AreEqual(root.Data, clone.Data, "Data should match");
519 Assert.IsTrue(root.IsDirty, "Root IsDirty should be true");
520 Assert.IsTrue(clone.IsDirty, "Clone IsDirty should be true");
521 }
522
523 [TestMethod]
524 public void DCEditLevels()
525 {
526 IDataPortal<DCRoot> dataPortal = _testDIContext.CreateDataPortal<DCRoot>();
527
528 DCRoot root = DCRoot.NewDCRoot(dataPortal);
529 root.BeginEdit();
530 root.Data = 123;
531 root.CancelEdit();
532
533 Assert.AreEqual(0, root.Data, "Data should be 0");
534
535 root.BeginEdit();
536 root.Data = 123;
537 root.ApplyEdit();
538
539 Assert.AreEqual(123, root.Data, "Data should be 123");
540 }
541
542 [TestMethod]
544 {
545 IDataPortal<Basic.Children> dataPortal = _testDIContext.CreateDataPortal<Basic.Children>();
546 IDataPortal<Basic.Child> childDataPortal = _testDIContext.CreateDataPortal<Basic.Child>();
547
548 Csla.Test.Basic.Children list = Csla.Test.Basic.Children.NewChildren(dataPortal);
549 list.Add(childDataPortal, "1");
550 list.Add(childDataPortal, "2");
551 IEditableObject item = list[1] as IEditableObject;
552 int editLevel = (int)item.GetType().GetProperty("EditLevel", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(item, null);
553 object manager = item.GetType().GetProperty("LoadManager", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(item, null);
554
555 item.BeginEdit();
556 int newEditLevel = (int)item.GetType().GetProperty("EditLevel", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(item, null);
557 Assert.AreEqual(editLevel + 1, newEditLevel, "Edit level incorrect after begin edit");
558 }
559
560 [TestMethod]
561 public void SerializeCommand()
562 {
563 IDataPortal<TestCommand> dataPortal = _testDIContext.CreateDataPortal<TestCommand>();
564 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
565
566 var cmd = dataPortal.Create();
567 cmd.Name = "test data";
568
569 // TODO: Not sure how to replicate the object cloner in Csla 6
570 var buffer = new MemoryStream();
571 // var bf = (TestCommand)Csla.Core.ObjectCloner.Clone(cmd);
572 // Assert.AreEqual(cmd.Name, bf.Name, "after BinaryFormatter");
573
574 // var ndcs = new System.Runtime.Serialization.NetDataContractSerializer();
575 // ndcs.Serialize(buffer, cmd);
576 // buffer.Position = 0;
577 // var n = (TestCommand)ndcs.Deserialize(buffer);
578 // Assert.AreEqual(cmd.Name, n.Name, "after NDCS");
579
580 buffer = new MemoryStream();
581 var mf = new MobileFormatter(applicationContext);
582 mf.Serialize(buffer, cmd);
583 buffer.Position = 0;
584 var m = (TestCommand)mf.Deserialize(buffer);
585 Assert.AreEqual(cmd.Name, m.Name, "after MobileFormatter");
586 }
587
588 [TestMethod]
589 [TestCategory("SkipWhenLiveUnitTesting")]
591 {
592 TestDIContext customDIContext = TestDIContextFactory.CreateDefaultContext();
593 // TODO: Get this custom proxy code included and working
594 //TestDIContext customDIContext = TestDIContextFactory.CreateContext(
595 //options => options
596 //.Services.AddTransient<DataPortalClient.IDataPortalProxy, Csla.Testing.Business.TestProxies.AppDomainProxy>()
597 //);
598 IDataPortal<TestCommand> dataPortal = customDIContext.CreateDataPortal<TestCommand>();
599
600 var cmd = dataPortal.Create();
601 cmd.Name = "test data";
602
603 var result = dataPortal.Execute(cmd);
604
605 Assert.IsFalse(ReferenceEquals(cmd, result), "References should not match");
606 Assert.AreEqual(cmd.Name + " server", result.Name);
607 }
608
609#if NETFRAMEWORK
610 [TestMethod]
611 public void UseCustomSerializationFormatter()
612 {
613 TestDIContext customDIContext = TestDIContextFactory.CreateContext(options => options
614 .Serialization(cfg => cfg
615 .SerializationFormatter(typeof(NetDataContractSerializerWrapper))));
616 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
617
618 var formatter = SerializationFormatterFactory.GetFormatter(applicationContext);
619
620 Assert.IsInstanceOfType(formatter, typeof(NetDataContractSerializerWrapper));
621 }
622
623 // TODO: I don't think this test is relevant - NDCS has been dropped?
624 //[TestMethod]
625 //public void UseNetDataContractSerializer()
626 //{
627 // System.Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"] = "NetDataContractSerializer";
628 // try
629 // {
630 // var formatter = SerializationFormatterFactory.GetFormatter();
631
632 // Assert.AreEqual(ApplicationContext.SerializationFormatter, ApplicationContext.SerializationFormatters.NetDataContractSerializer);
633 // Assert.IsInstanceOfType(formatter, typeof(NetDataContractSerializerWrapper));
634 // }
635 // finally
636 // {
637 // System.Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"] = null;
638 // }
639 //}
640#endif
641 }
642
644 public class TestCommand : CommandBase<TestCommand>
645 {
646 private static PropertyInfo<string> NameProperty = RegisterProperty<string>("Name");
647 public string Name
648 {
649 get { return ReadProperty(NameProperty); }
650 set { LoadProperty(NameProperty, value); }
651 }
652
653 [RunLocal]
654 [Create]
655 private void Create()
656 { }
657
658 [Execute]
659 protected void DataPortal_Execute()
660 {
661 Name = Name + " server";
662 }
663 }
664}
Provides consistent context information between the client and server DataPortal objects.
static Type SerializationFormatter
Gets the serialization formatter type used by CSLA .NET for all explicit object serialization (such a...
T Clone()
Creates a clone of the object.
Definition: BusinessBase.cs:79
This is the base class from which command objects will be derived.
Definition: CommandBase.cs:51
Maintains metadata about a property.
This is a class that is responsible for deserializing SerializationInfo objects for receiving the dat...
List< SerializationInfo > Read(Stream serializationStream)
Read a data from a stream, typically MemoryStream, and convert it into a list of SerializationInfo ob...
This is a class that is responsible for serializing SerializationInfo objects into a Stream for sendi...
void Write(Stream serializationStream, List< SerializationInfo > objectData)
Write a list of SerializationInfo objects into stream, typically MemoryStream.
Serializes and deserializes objects at the field level.
object DeserializeFromDTO(List< SerializationInfo > serialized)
Serializes an object from a DTO graph
List< SerializationInfo > SerializeToDTO(object obj)
Serializes the object into a DTO.
Wraps the NetDataContractSerializer in the ISerializationFormatter interface so it can be used in a s...
This exception is returned from the server-side DataPortal and contains the exception and context dat...
Csla.SmartDate FilledSmartDateTest
Gets or sets the EmptySmartDateTest value.
byte[] ByteArrayTest
Gets or sets the ByteArrayTest value.
decimal DecimalTest
Gets or sets the DecimalTest value.
RandomEnum EnumTest
Gets or sets the EnumTest value.
TimeSpan TimeSpanTest
Gets or sets the TimeSpanTest value.
char[] CharArrayTest
Gets or sets the CharArrayTest value.
Csla.SmartDate EmptySmartDateTest
Gets or sets the EmptySmartDateTest value.
DateTimeOffset DateTimeOffsetTest
Gets or sets the DateTimeOffsetTest value.
DateTime DateTimeTest
Gets or sets the DateTimeTest value.
static BinaryReaderWriterTestClassList NewBinaryReaderWriterTestClassList(IDataPortal< BinaryReaderWriterTestClassList > dataPortal)
static DCRoot NewDCRoot(IDataPortal< DCRoot > dataPortal)
Definition: DCRoot.cs:34
static OverrideSerializationRoot NewOverrideSerializationRoot(IDataPortal< OverrideSerializationRoot > dataPortal)
static SerializationRoot NewSerializationRoot(IDataPortal< SerializationRoot > dataPortal)
static void PublicStaticOnIsDirtyChanged(object sender, PropertyChangedEventArgs e)
void Reg(Core.BusinessBase obj)
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
static void AddOrOverwrite(string key, string value)
Overwrite an item in the test results, to indicate an outcome of a particular operation
Definition: TestResults.cs:39
Type to carry context information for DI in unit tests
UnitTestContext GetContext()
Definition: TestBase.cs:12
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
Task< object > CreateAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.
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.
@ Execute
Execute operation.
bool IsEmpty
Gets a value indicating whether this object contains an empty date.
Definition: SmartDate.cs:548
DateTime? Date
Gets or sets the date value.
Definition: SmartDate.cs:413
bool EmptyIsMin
Gets a value indicating whether an empty date is the min or max possible date value.
Definition: SmartDate.cs:568
string FormatString
Gets or sets the format string used to format a date value when it is returned as text.
Definition: SmartDate.cs:371