From b2001e6c7bb25242913b82b29dc6bada319a300e Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Fri, 21 Sep 2018 17:25:44 +0200 Subject: [PATCH] Correct binary serialization for exceptions. --- .../DomainObjectException.cs | 11 ++++ .../DomainObjectVersionException.cs | 11 ++++ .../WrongEventVersionException.cs | 11 ++++ .../States/InconsistentStateException.cs | 11 ++++ src/Squidex.Infrastructure/ValidationError.cs | 2 + .../ValidationException.cs | 11 ++++ .../DomainObjectExceptionTests.cs | 57 +++++++++++++++++++ .../WrongEventVersionExceptionTests.cs | 27 +++++++++ .../States/InconsistentStateExceptionTests.cs | 31 ++++++++++ .../TestHelpers/BinaryFormatterHelper.cs | 28 +++++++++ .../ValidationExceptionTests.cs | 14 +++++ 11 files changed, 214 insertions(+) create mode 100644 tests/Squidex.Infrastructure.Tests/DomainObjectExceptionTests.cs create mode 100644 tests/Squidex.Infrastructure.Tests/EventSourcing/WrongEventVersionExceptionTests.cs create mode 100644 tests/Squidex.Infrastructure.Tests/States/InconsistentStateExceptionTests.cs create mode 100644 tests/Squidex.Infrastructure.Tests/TestHelpers/BinaryFormatterHelper.cs diff --git a/src/Squidex.Infrastructure/DomainObjectException.cs b/src/Squidex.Infrastructure/DomainObjectException.cs index 1fb8fb7cd..24d311688 100644 --- a/src/Squidex.Infrastructure/DomainObjectException.cs +++ b/src/Squidex.Infrastructure/DomainObjectException.cs @@ -37,6 +37,17 @@ namespace Squidex.Infrastructure protected DomainObjectException(SerializationInfo info, StreamingContext context) : base(info, context) { + id = info.GetString(nameof(id)); + + typeName = info.GetString(nameof(typeName)); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(id), id); + info.AddValue(nameof(typeName), typeName); + + base.GetObjectData(info, context); } } } diff --git a/src/Squidex.Infrastructure/DomainObjectVersionException.cs b/src/Squidex.Infrastructure/DomainObjectVersionException.cs index 225ffd9f6..22192c423 100644 --- a/src/Squidex.Infrastructure/DomainObjectVersionException.cs +++ b/src/Squidex.Infrastructure/DomainObjectVersionException.cs @@ -37,6 +37,17 @@ namespace Squidex.Infrastructure protected DomainObjectVersionException(SerializationInfo info, StreamingContext context) : base(info, context) { + currentVersion = info.GetInt64(nameof(currentVersion)); + + expectedVersion = info.GetInt64(nameof(expectedVersion)); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(currentVersion), currentVersion); + info.AddValue(nameof(expectedVersion), expectedVersion); + + base.GetObjectData(info, context); } private static string FormatMessage(string id, Type type, long currentVersion, long expectedVersion) diff --git a/src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs b/src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs index ae88e64d5..0651467b8 100644 --- a/src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs +++ b/src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs @@ -37,6 +37,17 @@ namespace Squidex.Infrastructure.EventSourcing protected WrongEventVersionException(SerializationInfo info, StreamingContext context) : base(info, context) { + currentVersion = info.GetInt64(nameof(currentVersion)); + + expectedVersion = info.GetInt64(nameof(expectedVersion)); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(currentVersion), currentVersion); + info.AddValue(nameof(expectedVersion), expectedVersion); + + base.GetObjectData(info, context); } private static string FormatMessage(long currentVersion, long expectedVersion) diff --git a/src/Squidex.Infrastructure/States/InconsistentStateException.cs b/src/Squidex.Infrastructure/States/InconsistentStateException.cs index 843c65583..d9091b9dd 100644 --- a/src/Squidex.Infrastructure/States/InconsistentStateException.cs +++ b/src/Squidex.Infrastructure/States/InconsistentStateException.cs @@ -37,6 +37,17 @@ namespace Squidex.Infrastructure.States protected InconsistentStateException(SerializationInfo info, StreamingContext context) : base(info, context) { + currentVersion = info.GetInt64(nameof(currentVersion)); + + expectedVersion = info.GetInt64(nameof(expectedVersion)); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(currentVersion), currentVersion); + info.AddValue(nameof(expectedVersion), expectedVersion); + + base.GetObjectData(info, context); } private static string FormatMessage(long currentVersion, long expectedVersion) diff --git a/src/Squidex.Infrastructure/ValidationError.cs b/src/Squidex.Infrastructure/ValidationError.cs index 0324f151a..06e294aaf 100644 --- a/src/Squidex.Infrastructure/ValidationError.cs +++ b/src/Squidex.Infrastructure/ValidationError.cs @@ -5,11 +5,13 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Collections.Generic; using System.Linq; namespace Squidex.Infrastructure { + [Serializable] public sealed class ValidationError { private static readonly string[] FallbackProperties = new string[0]; diff --git a/src/Squidex.Infrastructure/ValidationException.cs b/src/Squidex.Infrastructure/ValidationException.cs index 3d5809a46..16f3d836c 100644 --- a/src/Squidex.Infrastructure/ValidationException.cs +++ b/src/Squidex.Infrastructure/ValidationException.cs @@ -53,6 +53,17 @@ namespace Squidex.Infrastructure protected ValidationException(SerializationInfo info, StreamingContext context) : base(info, context) { + Summary = info.GetString(nameof(Summary)); + + errors = (List)info.GetValue(nameof(errors), typeof(List)); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(Summary), Summary); + info.AddValue(nameof(errors), errors.ToList()); + + base.GetObjectData(info, context); } private static string FormatMessage(string summary, IReadOnlyList errors) diff --git a/tests/Squidex.Infrastructure.Tests/DomainObjectExceptionTests.cs b/tests/Squidex.Infrastructure.Tests/DomainObjectExceptionTests.cs new file mode 100644 index 000000000..5678215e3 --- /dev/null +++ b/tests/Squidex.Infrastructure.Tests/DomainObjectExceptionTests.cs @@ -0,0 +1,57 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Squidex.Infrastructure.TestHelpers; +using Xunit; + +namespace Squidex.Infrastructure +{ + public class DomainObjectExceptionTests + { + private sealed class MyObject + { + } + + [Fact] + public void Should_serialize_and_deserialize_DomainObjectDeletedException() + { + var source = new DomainObjectDeletedException("123", typeof(MyObject)); + var result = source.SerializeAndDeserializeBinary(); + + Assert.Equal(result.Id, source.Id); + Assert.Equal(result.TypeName, source.TypeName); + + Assert.Equal(result.Message, source.Message); + } + + [Fact] + public void Should_serialize_and_deserialize_DomainObjectNotFoundException() + { + var source = new DomainObjectNotFoundException("123", typeof(MyObject)); + var result = source.SerializeAndDeserializeBinary(); + + Assert.Equal(result.Id, source.Id); + Assert.Equal(result.TypeName, source.TypeName); + + Assert.Equal(result.Message, source.Message); + } + + [Fact] + public void Should_serialize_and_deserialize_DomainObjectVersionExceptionn() + { + var source = new DomainObjectVersionException("123", typeof(MyObject), 100, 200); + var result = source.SerializeAndDeserializeBinary(); + + Assert.Equal(result.Id, source.Id); + Assert.Equal(result.TypeName, source.TypeName); + Assert.Equal(result.ExpectedVersion, source.ExpectedVersion); + Assert.Equal(result.CurrentVersion, source.CurrentVersion); + + Assert.Equal(result.Message, source.Message); + } + } +} diff --git a/tests/Squidex.Infrastructure.Tests/EventSourcing/WrongEventVersionExceptionTests.cs b/tests/Squidex.Infrastructure.Tests/EventSourcing/WrongEventVersionExceptionTests.cs new file mode 100644 index 000000000..a8f01091c --- /dev/null +++ b/tests/Squidex.Infrastructure.Tests/EventSourcing/WrongEventVersionExceptionTests.cs @@ -0,0 +1,27 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Squidex.Infrastructure.TestHelpers; +using Xunit; + +namespace Squidex.Infrastructure.EventSourcing +{ + public class WrongEventVersionExceptionTests + { + [Fact] + public void Should_serialize_and_deserialize() + { + var source = new WrongEventVersionException(100, 200); + var result = source.SerializeAndDeserializeBinary(); + + Assert.Equal(result.ExpectedVersion, source.ExpectedVersion); + Assert.Equal(result.CurrentVersion, source.CurrentVersion); + + Assert.Equal(result.Message, source.Message); + } + } +} diff --git a/tests/Squidex.Infrastructure.Tests/States/InconsistentStateExceptionTests.cs b/tests/Squidex.Infrastructure.Tests/States/InconsistentStateExceptionTests.cs new file mode 100644 index 000000000..e36d802dc --- /dev/null +++ b/tests/Squidex.Infrastructure.Tests/States/InconsistentStateExceptionTests.cs @@ -0,0 +1,31 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; +using Squidex.Infrastructure.TestHelpers; +using Xunit; + +namespace Squidex.Infrastructure.States +{ + public class InconsistentStateExceptionTests + { + [Fact] + public void Should_serialize_and_deserialize() + { + var source = new InconsistentStateException(100, 200, new InvalidOperationException("Inner")); + var result = source.SerializeAndDeserializeBinary(); + + Assert.IsType(result.InnerException); + Assert.Equal("Inner", result.InnerException.Message); + + Assert.Equal(result.ExpectedVersion, source.ExpectedVersion); + Assert.Equal(result.CurrentVersion, source.CurrentVersion); + + Assert.Equal(result.Message, source.Message); + } + } +} diff --git a/tests/Squidex.Infrastructure.Tests/TestHelpers/BinaryFormatterHelper.cs b/tests/Squidex.Infrastructure.Tests/TestHelpers/BinaryFormatterHelper.cs new file mode 100644 index 000000000..c67e30e00 --- /dev/null +++ b/tests/Squidex.Infrastructure.Tests/TestHelpers/BinaryFormatterHelper.cs @@ -0,0 +1,28 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Squidex.Infrastructure.TestHelpers +{ + public static class BinaryFormatterHelper + { + private static readonly BinaryFormatter Formatter = new BinaryFormatter(); + + public static T SerializeAndDeserializeBinary(this T source) + { + var stream = new MemoryStream(); + + Formatter.Serialize(stream, source); + + stream.Position = 0; + + return (T)Formatter.Deserialize(stream); + } + } +} diff --git a/tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs b/tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs index 10e19bcee..9acb88b7a 100644 --- a/tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs +++ b/tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs @@ -5,6 +5,8 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using FluentAssertions; +using Squidex.Infrastructure.TestHelpers; using Xunit; namespace Squidex.Infrastructure @@ -50,5 +52,17 @@ namespace Squidex.Infrastructure Assert.Equal("Summary: Error1. Error2.", ex.Message); } + + [Fact] + public void Should_serialize_and_deserialize() + { + var source = new ValidationException("Summary", new ValidationError("Error1"), new ValidationError("Error2")); + var result = source.SerializeAndDeserializeBinary(); + + result.Errors.Should().BeEquivalentTo(source.Errors); + + Assert.Equal(source.Message, result.Message); + Assert.Equal(source.Summary, result.Summary); + } } }