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.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PersonPOCOTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="PersonPOCOTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Tests of serialization behaviour on the AutoSerializable class PersonPOCO</summary>
7//-----------------------------------------------------------------------
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11using System;
16using Csla.TestHelpers;
17
19{
20
24 [TestClass]
25 public class PersonPOCOTests
26 {
27 private static TestDIContext _testDIContext;
28
30 public static void ClassInitialize(TestContext testContext)
31 {
32 _testDIContext = TestDIContextFactory.CreateDefaultContext();
33 }
34
35 #region GetState
36
37 [TestMethod]
39 {
40
41 // Arrange
42 SerializationInfo serializationInfo = new SerializationInfo();
43 int actual;
44 int expected = 5;
45 IMobileObject mobileObject;
46 PersonPOCO person = new PersonPOCO();
47 person.PersonId = 5;
48
49 // Act
50 mobileObject = (IMobileObject)person;
51 mobileObject.GetState(serializationInfo);
52 actual = serializationInfo.GetValue<int>("PersonId");
53
54 // Assert
55 Assert.AreEqual(expected, actual);
56
57 }
58
59 [TestMethod]
61 {
62
63 // Arrange
64 SerializationInfo serializationInfo = new SerializationInfo();
65 string actual;
66 string expected = "Joe";
67 IMobileObject mobileObject;
68 PersonPOCO person = new PersonPOCO();
69 person.FirstName = "Joe";
70
71 // Act
72 mobileObject = (IMobileObject)person;
73 mobileObject.GetState(serializationInfo);
74 actual = serializationInfo.GetValue<string>("FirstName");
75
76 // Assert
77 Assert.AreEqual(expected, actual);
78
79 }
80
81 [TestMethod]
83 {
84
85 // Arrange
86 SerializationInfo serializationInfo = new SerializationInfo();
87 string actual;
88 string expected = "Smith";
89 IMobileObject mobileObject;
90 PersonPOCO person = new PersonPOCO();
91 person.LastName = "Smith";
92
93 // Act
94 mobileObject = (IMobileObject)person;
95 mobileObject.GetState(serializationInfo);
96 actual = serializationInfo.GetValue<string>("LastName");
97
98 // Assert
99 Assert.AreEqual(expected, actual);
100
101 }
102
103 [TestMethod]
105 {
106
107 // Arrange
108 SerializationInfo serializationInfo = new SerializationInfo();
109 bool actual;
110 bool expected = false;
111 IMobileObject mobileObject;
112 PersonPOCO person = new PersonPOCO();
113 person.SetDateOfBirth(new DateTime(2021, 04, 12, 16, 57, 53));
114
115 // Act
116 mobileObject = (IMobileObject)person;
117 mobileObject.GetState(serializationInfo);
118 actual = serializationInfo.Values.ContainsKey("DateOfBirth");
119
120 // Assert
121 Assert.AreEqual(expected, actual);
122
123 }
124
125 [TestMethod]
127 {
128
129 // Arrange
130 SerializationInfo serializationInfo = new SerializationInfo();
131 bool actual;
132 bool expected = false;
133 IMobileObject mobileObject;
134 PersonPOCO person = new PersonPOCO();
135 person.SetMiddleName("Mid");
136
137 // Act
138 mobileObject = (IMobileObject)person;
139 mobileObject.GetState(serializationInfo);
140 actual = serializationInfo.Values.ContainsKey("MiddleName");
141
142 // Assert
143 Assert.AreEqual(expected, actual);
144
145 }
146
147 [TestMethod]
149 {
150
151 // Arrange
152 SerializationInfo serializationInfo = new SerializationInfo();
153 string actual;
154 string expected = "Mid";
155 IMobileObject mobileObject;
156 PersonPOCO person = new PersonPOCO();
157 person.SetMiddleName("Mid");
158
159 // Act
160 mobileObject = (IMobileObject)person;
161 mobileObject.GetState(serializationInfo);
162 actual = serializationInfo.GetValue<string>("_middleName");
163
164 // Assert
165 Assert.AreEqual(expected, actual);
166
167 }
168
169 #endregion
170
171 #region SetState
172
173 [TestMethod]
175 {
176
177 // Arrange
178 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
179 int actual;
180 int expected = 5;
181 PersonPOCO person = new PersonPOCO();
182 IMobileObject mobileObject;
183
184 // Act
185 serializationInfo.Values["PersonId"].Value = 5;
186 mobileObject = (IMobileObject)person;
187 mobileObject.SetState(serializationInfo);
188 actual = person.PersonId;
189
190 // Assert
191 Assert.AreEqual(expected, actual);
192
193 }
194
195 [TestMethod]
197 {
198
199 // Arrange
200 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
201 string actual;
202 string expected = "Joe";
203 PersonPOCO person = new PersonPOCO();
204 IMobileObject mobileObject;
205
206 // Act
207 serializationInfo.Values["FirstName"].Value = "Joe";
208 mobileObject = (IMobileObject)person;
209 mobileObject.SetState(serializationInfo);
210 actual = person.FirstName;
211
212 // Assert
213 Assert.AreEqual(expected, actual);
214
215 }
216
217 [TestMethod]
219 {
220
221 // Arrange
222 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
223 string actual;
224 string expected = "Smith";
225 PersonPOCO person = new PersonPOCO();
226 IMobileObject mobileObject;
227
228 // Act
229 serializationInfo.Values["LastName"].Value = "Smith";
230 mobileObject = (IMobileObject)person;
231 mobileObject.SetState(serializationInfo);
232 actual = person.LastName;
233
234 // Assert
235 Assert.AreEqual(expected, actual);
236
237 }
238
239 [TestMethod]
241 {
242
243 // Arrange
244 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
245 DateTime actual;
246 DateTime expected = DateTime.MinValue;
247 PersonPOCO person = new PersonPOCO();
248 IMobileObject mobileObject;
249
250 // Act
251 serializationInfo.Values["DateOfBirth"].Value = new DateTime(2021, 04, 12, 18, 27, 43);
252 mobileObject = (IMobileObject)person;
253 mobileObject.SetState(serializationInfo);
254 actual = person.GetDateOfBirth();
255
256 // Assert
257 Assert.AreEqual(expected, actual);
258
259 }
260
261 [TestMethod]
263 {
264
265 // Arrange
266 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
267 string actual;
268 string expected = string.Empty;
269 PersonPOCO person = new PersonPOCO();
270 IMobileObject mobileObject;
271
272 // Act
273 serializationInfo.Values["NonSerializedText"].Value = "Fred";
274 mobileObject = (IMobileObject)person;
275 mobileObject.SetState(serializationInfo);
276 actual = person.NonSerializedText;
277
278 // Assert
279 Assert.AreEqual(expected, actual);
280
281 }
282
283 [TestMethod]
285 {
286
287 // Arrange
288 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
289 string actual;
290 string expected = string.Empty;
291 PersonPOCO person = new PersonPOCO();
292 IMobileObject mobileObject;
293
294 // Act
295 serializationInfo.Values["PrivateText"].Value = "Fred";
296 mobileObject = (IMobileObject)person;
297 mobileObject.SetState(serializationInfo);
298 actual = person.GetUnderlyingPrivateText();
299
300 // Assert
301 Assert.AreEqual(expected, actual);
302
303 }
304
305 [TestMethod]
307 {
308
309 // Arrange
310 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
311 string actual;
312 string expected = "Fred";
313 PersonPOCO person = new PersonPOCO();
314 IMobileObject mobileObject;
315
316 // Act
317 serializationInfo.Values["PrivateSerializedText"].Value = "Fred";
318 mobileObject = (IMobileObject)person;
319 mobileObject.SetState(serializationInfo);
320 actual = person.GetPrivateSerializedText();
321
322 // Assert
323 Assert.AreEqual(expected, actual);
324
325 }
326
327 [TestMethod]
329 {
330
331 // Arrange
332 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
333 string actual;
334 string expected = "Mid";
335 PersonPOCO person = new PersonPOCO();
336 IMobileObject mobileObject;
337
338 // Act
339 serializationInfo.Values["_middleName"].Value = "Mid";
340 mobileObject = (IMobileObject)person;
341 mobileObject.SetState(serializationInfo);
342 actual = person.MiddleName;
343
344 // Assert
345 Assert.AreEqual(expected, actual);
346
347 }
348
349 #endregion
350
351 #region GetChildren
352
353 [TestMethod]
355 {
356 var applicationContext = _testDIContext.CreateTestApplicationContext();
357
358 // Arrange
359 SerializationInfo serializationInfo = new SerializationInfo();
360 bool expected = true;
361 bool actual;
362 IMobileObject mobileObject;
363 PersonPOCO person = new PersonPOCO();
364 person.Address = new AddressPOCO() { AddressLine1 = "1 High Street" };
365 MobileFormatter formatter = new MobileFormatter(applicationContext);
366
367 // Act
368 mobileObject = (IMobileObject)person;
369 mobileObject.GetChildren(serializationInfo, formatter);
370 actual = serializationInfo.Children.ContainsKey("Address");
371
372 // Assert
373 Assert.AreEqual(expected, actual);
374
375 }
376
377 [TestMethod]
379 {
380 var applicationContext = _testDIContext.CreateTestApplicationContext();
381
382 // Arrange
383 SerializationInfo serializationInfo = new SerializationInfo();
384 bool expected = true;
385 bool actual;
386 IMobileObject mobileObject;
387 PersonPOCO person = new PersonPOCO();
388 person.EmailAddress = new EmailAddress() { Email = "a@b.com" };
389 MobileFormatter formatter = new MobileFormatter(applicationContext);
390
391 // Act
392 mobileObject = (IMobileObject)person;
393 mobileObject.GetChildren(serializationInfo, formatter);
394 actual = serializationInfo.Children.ContainsKey("EmailAddress");
395
396 // Assert
397 Assert.AreEqual(expected, actual);
398
399 }
400
401 #endregion
402
403 #region SetChildren
404
405 #endregion
406
407 #region Serialize Then Deserialize
408
409 [TestMethod]
411 {
412
413 // Arrange
414 int actual;
415 int expected = 5;
416 PersonPOCO person = new PersonPOCO();
417 person.PersonId = 5;
418 PersonPOCO deserializedPerson;
419
420 // Act
421 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
422 actual = deserializedPerson.PersonId;
423
424 // Assert
425 Assert.AreEqual(expected, actual);
426
427 }
428
429 [TestMethod]
431 {
432
433 // Arrange
434 string actual;
435 string expected = "Joe";
436 PersonPOCO person = new PersonPOCO();
437 person.FirstName = "Joe";
438 PersonPOCO deserializedPerson;
439
440 // Act
441 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
442 actual = deserializedPerson.FirstName;
443
444 // Assert
445 Assert.AreEqual(expected, actual);
446
447 }
448
449 [TestMethod]
451 {
452
453 // Arrange
454 string actual;
455 string expected = "Smith";
456 PersonPOCO person = new PersonPOCO();
457 person.LastName = "Smith";
458 PersonPOCO deserializedPerson;
459
460 // Act
461 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
462 actual = deserializedPerson.LastName;
463
464 // Assert
465 Assert.AreEqual(expected, actual);
466
467 }
468
469 [TestMethod]
471 {
472
473 // Arrange
474 string actual;
475 string expected = "Mid";
476 PersonPOCO person = new PersonPOCO();
477 person.SetMiddleName("Mid");
478 PersonPOCO deserializedPerson;
479
480 // Act
481 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
482 actual = deserializedPerson.MiddleName;
483
484 // Assert
485 Assert.AreEqual(expected, actual);
486
487 }
488
489 [TestMethod]
491 {
492
493 // Arrange
494 string actual;
495 string expected = "";
496 PersonPOCO person = new PersonPOCO();
497 person.NonSerializedText = "Non";
498 PersonPOCO deserializedPerson;
499
500 // Act
501 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
502 actual = deserializedPerson.NonSerializedText;
503
504 // Assert
505 Assert.AreEqual(expected, actual);
506
507 }
508
509 [TestMethod]
511 {
512
513 // Arrange
514 string actual;
515 string expected = "Pri";
516 PersonPOCO person = new PersonPOCO();
517 person.SetPrivateSerializedText("Pri");
518 PersonPOCO deserializedPerson;
519
520 // Act
521 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
522 actual = deserializedPerson.GetPrivateSerializedText();
523
524 // Assert
525 Assert.AreEqual(expected, actual);
526
527 }
528
529 [TestMethod]
531 {
532
533 // Arrange
534 string actual;
535 string expected = "";
536 PersonPOCO person = new PersonPOCO();
537 person.SetUnderlyingPrivateText("Priv");
538 PersonPOCO deserializedPerson;
539
540 // Act
541 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
542 actual = deserializedPerson.GetUnderlyingPrivateText();
543
544 // Assert
545 Assert.AreEqual(expected, actual);
546
547 }
548
549 [TestMethod]
551 {
552
553 // Arrange
554 DateTime actual;
555 DateTime expected = DateTime.MinValue;
556 PersonPOCO person = new PersonPOCO();
557 person.SetDateOfBirth(new DateTime(2021, 04, 12, 16, 57, 53));
558 PersonPOCO deserializedPerson;
559
560 // Act
561 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
562 actual = deserializedPerson.GetDateOfBirth();
563
564 // Assert
565 Assert.AreEqual(expected, actual);
566
567 }
568
569 [TestMethod]
571 {
572
573 // Arrange
574 string actual;
575 string expected = "1 High Street";
576 PersonPOCO person = new PersonPOCO();
577 person.Address = new AddressPOCO() { AddressLine1 = "1 High Street" };
578 PersonPOCO deserializedPerson;
579
580 // Act
581 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
582 actual = deserializedPerson?.Address?.AddressLine1;
583
584 // Assert
585 Assert.AreEqual(expected, actual);
586
587 }
588
589 [TestMethod]
591 {
592
593 // Arrange
594 AddressPOCO actual;
595 PersonPOCO person = new PersonPOCO();
596 PersonPOCO deserializedPerson;
597
598 // Act
599 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
600 actual = deserializedPerson.Address;
601
602 // Assert
603 Assert.IsNull(actual);
604
605 }
606
607 [TestMethod]
609 {
610
611 // Arrange
612 string actual;
613 string expected = "Townsville";
614 PersonPOCO person = new PersonPOCO();
615 person.Address = new AddressPOCO() { Town = "Townsville" };
616 PersonPOCO deserializedPerson;
617
618 // Act
619 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
620 actual = deserializedPerson?.Address?.Town;
621
622 // Assert
623 Assert.AreEqual(expected, actual);
624
625 }
626
627 [TestMethod]
629 {
630
631 // Arrange
632 EmailAddress actual;
633 PersonPOCO person = new PersonPOCO();
634 PersonPOCO deserializedPerson;
635
636 // Act
637 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
638 actual = deserializedPerson.EmailAddress;
639
640 // Assert
641 Assert.IsNull(actual);
642
643 }
644
645 [TestMethod]
647 {
648
649 // Arrange
650 string actual;
651 string expected = "a@b.com";
652 PersonPOCO person = new PersonPOCO();
653 person.EmailAddress = new EmailAddress() { Email = "a@b.com" };
654 PersonPOCO deserializedPerson;
655
656 // Act
657 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
658 actual = deserializedPerson?.EmailAddress?.Email;
659
660 // Assert
661 Assert.AreEqual(expected, actual);
662
663 }
664
665 #endregion
666
667 #region Private Helper Methods
668
675 private PersonPOCO SerializeThenDeserialisePersonPOCO(PersonPOCO valueToSerialize)
676 {
677 var applicationContext = _testDIContext.CreateTestApplicationContext();
678
679 System.IO.MemoryStream serializationStream;
680 PersonPOCO deserializedValue;
681 MobileFormatter formatter = new MobileFormatter(applicationContext);
682
683 // Act
684 using (serializationStream = new System.IO.MemoryStream())
685 {
686 formatter.Serialize(serializationStream, valueToSerialize);
687 serializationStream.Seek(0, System.IO.SeekOrigin.Begin);
688 deserializedValue = formatter.Deserialize(serializationStream) as PersonPOCO;
689 }
690
691 return deserializedValue;
692 }
693
694 #endregion
695
696 }
697}
Object already implementing IMobileObject that can be used for testing serialization behaviour
Definition: EmailAddress.cs:21
A class for which automatic serialization code is to be generated Includes children of various types ...
Definition: PersonPOCO.cs:21
void SetPrivateSerializedText(string newValue)
Definition: PersonPOCO.cs:59
void SetDateOfBirth(DateTime newDateOfBirth)
Definition: PersonPOCO.cs:78
Tests of serialization on the PersonPOCO class and its children
void SerializeThenDeserialize_WithInternalAutoImpPropertyDateOfBirth20210412165753_HasMinDateOfBirth()
void SerializeThenDeserialize_WithIncludedPrivateFieldMiddleNameMid_HasMiddleNameMid()
void SerializeThenDeserialize_WithIMobileObjectEmailAddressAatBdotCom_HasEmailAddressAatBdotCom()
void SerializeThenDeserialize_WithAutoSerializableAddress1HighStreet_HasAddressOf1HighStreet()
static void ClassInitialize(TestContext testContext)
void SerializeThenDeserialize_WithPublicAutoImpPropertyFirstNameJoe_HasFirstNameJoe()
void SerializeThenDeserialize_WithExcludedPublicAutoImpPropertyNonSerializedTextNon_HasEmptyNonSerializedText()
void SerializeThenDeserialize_WithIncludedPrivateAutoImpPropertyPrivateSerializedTextPri_HasPrivateSerializedTextPri()
void SerializeThenDeserialize_WithPrivateAutoImpPropertyPrivateTextPriv_HasEmptyPrivateText()
void SerializeThenDeserialize_WithIMobileObjectEmailAddressNull_HasEmailAddressNull()
void SerializeThenDeserialize_WithAutoSerializableAddressTownsville_HasAddressOfTownsville()
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.
Object containing the serialization data for a specific object.
Dictionary< string, FieldData > Values
Dictionary containg field data.
Dictionary< string, ChildData > Children
Dictionary containing child reference data.
Type to carry context information for DI in unit tests
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.