Browse Source

Correct binary serialization for exceptions.

pull/320/head
Sebastian Stehle 8 years ago
parent
commit
b2001e6c7b
  1. 11
      src/Squidex.Infrastructure/DomainObjectException.cs
  2. 11
      src/Squidex.Infrastructure/DomainObjectVersionException.cs
  3. 11
      src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs
  4. 11
      src/Squidex.Infrastructure/States/InconsistentStateException.cs
  5. 2
      src/Squidex.Infrastructure/ValidationError.cs
  6. 11
      src/Squidex.Infrastructure/ValidationException.cs
  7. 57
      tests/Squidex.Infrastructure.Tests/DomainObjectExceptionTests.cs
  8. 27
      tests/Squidex.Infrastructure.Tests/EventSourcing/WrongEventVersionExceptionTests.cs
  9. 31
      tests/Squidex.Infrastructure.Tests/States/InconsistentStateExceptionTests.cs
  10. 28
      tests/Squidex.Infrastructure.Tests/TestHelpers/BinaryFormatterHelper.cs
  11. 14
      tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs

11
src/Squidex.Infrastructure/DomainObjectException.cs

@ -37,6 +37,17 @@ namespace Squidex.Infrastructure
protected DomainObjectException(SerializationInfo info, StreamingContext context) protected DomainObjectException(SerializationInfo info, StreamingContext context)
: base(info, 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);
} }
} }
} }

11
src/Squidex.Infrastructure/DomainObjectVersionException.cs

@ -37,6 +37,17 @@ namespace Squidex.Infrastructure
protected DomainObjectVersionException(SerializationInfo info, StreamingContext context) protected DomainObjectVersionException(SerializationInfo info, StreamingContext context)
: base(info, 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) private static string FormatMessage(string id, Type type, long currentVersion, long expectedVersion)

11
src/Squidex.Infrastructure/EventSourcing/WrongEventVersionException.cs

@ -37,6 +37,17 @@ namespace Squidex.Infrastructure.EventSourcing
protected WrongEventVersionException(SerializationInfo info, StreamingContext context) protected WrongEventVersionException(SerializationInfo info, StreamingContext context)
: base(info, 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) private static string FormatMessage(long currentVersion, long expectedVersion)

11
src/Squidex.Infrastructure/States/InconsistentStateException.cs

@ -37,6 +37,17 @@ namespace Squidex.Infrastructure.States
protected InconsistentStateException(SerializationInfo info, StreamingContext context) protected InconsistentStateException(SerializationInfo info, StreamingContext context)
: base(info, 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) private static string FormatMessage(long currentVersion, long expectedVersion)

2
src/Squidex.Infrastructure/ValidationError.cs

@ -5,11 +5,13 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace Squidex.Infrastructure namespace Squidex.Infrastructure
{ {
[Serializable]
public sealed class ValidationError public sealed class ValidationError
{ {
private static readonly string[] FallbackProperties = new string[0]; private static readonly string[] FallbackProperties = new string[0];

11
src/Squidex.Infrastructure/ValidationException.cs

@ -53,6 +53,17 @@ namespace Squidex.Infrastructure
protected ValidationException(SerializationInfo info, StreamingContext context) protected ValidationException(SerializationInfo info, StreamingContext context)
: base(info, context) : base(info, context)
{ {
Summary = info.GetString(nameof(Summary));
errors = (List<ValidationError>)info.GetValue(nameof(errors), typeof(List<ValidationError>));
}
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<ValidationError> errors) private static string FormatMessage(string summary, IReadOnlyList<ValidationError> errors)

57
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);
}
}
}

27
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);
}
}
}

31
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<InvalidOperationException>(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);
}
}
}

28
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<T>(this T source)
{
var stream = new MemoryStream();
Formatter.Serialize(stream, source);
stream.Position = 0;
return (T)Formatter.Deserialize(stream);
}
}
}

14
tests/Squidex.Infrastructure.Tests/ValidationExceptionTests.cs

@ -5,6 +5,8 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using FluentAssertions;
using Squidex.Infrastructure.TestHelpers;
using Xunit; using Xunit;
namespace Squidex.Infrastructure namespace Squidex.Infrastructure
@ -50,5 +52,17 @@ namespace Squidex.Infrastructure
Assert.Equal("Summary: Error1. Error2.", ex.Message); 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);
}
} }
} }

Loading…
Cancel
Save