9using System.Collections.Generic;
12using System.ComponentModel;
13using System.Diagnostics;
18using System.Threading.Tasks;
19using System.Security.Claims;
25using TestClass = NUnit.Framework.TestFixtureAttribute;
26using TestInitialize = NUnit.Framework.SetUpAttribute;
27using TestCleanup = NUnit.Framework.TearDownAttribute;
28using TestMethod = NUnit.Framework.TestAttribute;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
38 public class SerializationTests :
TestBase
43 private class TestCollection : BusinessBindingListBase<TestCollection, TestItem>
48 private class TestItem : BusinessBase<TestItem>
50 protected override object GetIdValue()
61 private static ClaimsPrincipal GetPrincipal(params
string[] roles)
63 var identity =
new ClaimsIdentity();
64 foreach (var item
in roles)
65 identity.AddClaim(
new Claim(ClaimTypes.Role, item));
66 return new ClaimsPrincipal(identity);
72 _testDIContext = TestDIContextFactory.CreateDefaultContext();
79 var applicationContext = _testDIContext.CreateTestApplicationContext();
80 var cloner =
new Core.ObjectCloner(applicationContext);
82 Assert.IsFalse(ReferenceEquals(obj, obj2));
83 Assert.AreEqual(obj.Message, obj2.Message);
103 root.
Data =
"something";
105 root.
Data =
"something else";
110 root.
Data =
"something new";
134 "Deserialized not called");
149 root.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler
152 root.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler
153 (StaticOnIsDirtyChanged);
155 root.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler
158 root.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler
165 context.
Assert.AreEqual(
"abc", root.
Data,
"Data value not set");
170 "Didn't call local handler");
173 "StaticOnIsDirtyChanged",
175 "Didn't call static handler");
178 "PublicStaticOnIsDirtyChanged",
180 "Didn't call public static handler");
183 "Test.OnIsDirtyChanged",
185 "Didn't call serializable handler");
188 "Test.PrivateOnIsDirtyChanged",
190 "Didn't call serializable private handler");
198 context.
Assert.AreEqual(
"xyz", root.
Data,
"Data value not set");
201 "Called local handler after clone");
204 "Called static handler after clone");
207 "PublicStaticOnIsDirtyChanged",
209 "Didn't call public static handler after clone");
212 "Test.OnIsDirtyChanged",
214 "Didn't call serializable handler after clone");
217 "Called serializable private handler after clone");
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;
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;
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;
270 [TestCategory(
"SkipWhenLiveUnitTesting")]
278 context.Assert.AreEqual(
false, root.IsValid,
"root should not start valid");
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");
285 context.Assert.AreEqual(
true, root.IsValid,
"root should be valid after second clone");
286 context.Assert.Success();
300 var applicationContext = _testDIContext.CreateTestApplicationContext();
305 using (var stream =
new MemoryStream())
307 writer.
Write(stream, serialized);
308 data = stream.ToArray();
312 using (var stream =
new MemoryStream(data))
314 var deserialized = reader.
Read(stream);
318 Assert.AreEqual(test.Count, result.Count);
319 for (
int i = 0; i < test.Count; i++)
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);
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);
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);
362 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
368 using (var stream =
new MemoryStream())
370 writer.
Write(stream, serialized);
371 data = stream.ToArray();
375 using (var stream =
new MemoryStream(data))
377 var deserialized = reader.
Read(stream);
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++)
384 Assert.AreEqual(test.ByteArrayTest[i], result.
ByteArrayTest[i]);
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++)
391 Assert.AreEqual(test.CharArrayTest[i], result.
CharArrayTest[i]);
394 Assert.AreEqual(test.CharTest, result.
CharTest);
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);
428 TestDIContext adminDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
429 IDataPortal<Security.PermissionsRoot> dataPortal = _testDIContext.CreateDataPortal<Security.PermissionsRoot>();
431 Security.PermissionsRoot root = dataPortal.Create();
435 root.FirstName =
"something";
436 Assert.Fail(
"Exception didn't occur");
440 Assert.AreEqual(
"Property set not allowed", ex.Message);
443 dataPortal = adminDIContext.CreateDataPortal<Security.PermissionsRoot>();
444 root = dataPortal.Create();
448 root.FirstName =
"something";
452 Assert.Fail(
"exception occurred");
462 rootClone.FirstName =
"something else";
463 Assert.Fail(
"Exception didn't occur");
467 Assert.AreEqual(
"Property set not allowed", ex.Message);
475 rootClone.FirstName =
"something new";
479 Assert.Fail(
"exception occurred");
484 private void OnIsDirtyChanged(
object sender, PropertyChangedEventArgs e)
489 private static void StaticOnIsDirtyChanged(
object sender, PropertyChangedEventArgs e)
500 [TestCategory(
"SkipWhenLiveUnitTesting")]
505 System.Configuration.ConfigurationManager.AppSettings[
"CslaSerializationFormatter"] =
506 "NetDataContractSerializer";
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");
533 Assert.AreEqual(0, root.Data,
"Data should be 0");
539 Assert.AreEqual(123, root.Data,
"Data should be 123");
545 IDataPortal<Basic.Children> dataPortal = _testDIContext.CreateDataPortal<Basic.Children>();
546 IDataPortal<Basic.Child> childDataPortal = _testDIContext.CreateDataPortal<Basic.Child>();
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);
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");
564 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
566 var cmd = dataPortal.
Create();
567 cmd.Name =
"test data";
570 var buffer =
new MemoryStream();
580 buffer =
new MemoryStream();
582 mf.Serialize(buffer, cmd);
585 Assert.AreEqual(cmd.Name, m.Name,
"after MobileFormatter");
589 [TestCategory(
"SkipWhenLiveUnitTesting")]
592 TestDIContext customDIContext = TestDIContextFactory.CreateDefaultContext();
600 var cmd = dataPortal.
Create();
601 cmd.Name =
"test data";
603 var result = dataPortal.
Execute(cmd);
605 Assert.IsFalse(ReferenceEquals(cmd, result),
"References should not match");
606 Assert.AreEqual(cmd.Name +
" server", result.Name);
611 public void UseCustomSerializationFormatter()
613 TestDIContext customDIContext = TestDIContextFactory.CreateContext(options => options
614 .Serialization(cfg => cfg
616 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
618 var formatter = SerializationFormatterFactory.GetFormatter(applicationContext);
649 get {
return ReadProperty(NameProperty); }
650 set { LoadProperty(NameProperty, value); }
655 private void Create()
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.
This is the base class from which command objects will be derived.
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.
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...
Int16 Int16Test
Gets or sets the Int16Test value.
Csla.SmartDate FilledSmartDateTest
Gets or sets the EmptySmartDateTest value.
bool BoolTest
Gets or sets the BoolTest value.
byte[] ByteArrayTest
Gets or sets the ByteArrayTest value.
UInt64 UInt64Test
Gets or sets the UInt64Test value.
UInt16 UInt16Test
Gets or sets the UInt16Test value.
char CharTest
Gets or sets the CharTest value.
decimal DecimalTest
Gets or sets the DecimalTest value.
UInt32 UInt32Test
Gets or sets the UInt32Test 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.
Int32 Int32Test
Gets or sets the IntTest value.
Guid GuidTest
Gets or sets the GuidTest value.
Csla.SmartDate EmptySmartDateTest
Gets or sets the EmptySmartDateTest value.
byte ByteTest
Gets or sets the ByteTest value.
Single SingleTest
Gets or sets the SingleTest value.
string StringTest
Gets or sets the StringTest value.
DateTimeOffset DateTimeOffsetTest
Gets or sets the DateTimeOffsetTest value.
DateTime DateTimeTest
Gets or sets the DateTimeTest value.
Int64 Int64Test
Gets or sets the Int64Test value.
double DoubleTest
Gets or sets the DoubleTest value.
SByte SByteTest
Gets or sets the SByteTest value.
static BinaryReaderWriterTestClassList NewBinaryReaderWriterTestClassList(IDataPortal< BinaryReaderWriterTestClassList > dataPortal)
static DCRoot NewDCRoot(IDataPortal< DCRoot > dataPortal)
static OverrideSerializationRoot NewOverrideSerializationRoot(IDataPortal< OverrideSerializationRoot > dataPortal)
static SerializationRoot NewSerializationRoot(IDataPortal< SerializationRoot > dataPortal)
void TestSerializableEventsActionFails()
void AsyncLoadManagerSerializationTest()
void TestSerializableEventsActionSucceeds()
void TestSerializationCslaBinaryReaderWriterList()
void CorrectDefaultSerializer()
static void ClassInitialize(TestContext context)
async Task TestValidationRulesAfterSerialization()
void TestAuthorizationRulesAfterSerialization()
void SerializableEvents()
static void PublicStaticOnIsDirtyChanged(object sender, PropertyChangedEventArgs e)
void TestWithoutSerializableHandler()
void SerializeDataPortalException()
void CommandOverDataPortal()
void TestSerializationCslaBinaryReaderWriter()
void DataPortal_Execute()
void Reg(Core.BusinessBase obj)
void Reg(Csla.Core.BusinessBase obj)
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
static void AddOrOverwrite(string key, string value)
Overwrite an item in the test results, to indicate an outcome of a particular operation
Type to carry context information for DI in unit tests
UnitTestContext GetContext()
Interface defining the members of the data portal type.
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.
DateTime? Date
Gets or sets the date value.
bool EmptyIsMin
Gets a value indicating whether an empty date is the min or max possible date value.
string FormatString
Gets or sets the format string used to format a date value when it is returned as text.