Skip to content

Commit

Permalink
ShouldThrowDependencyExceptionOnLoadAssemblyIfExternalExceptionOccurs…
Browse files Browse the repository at this point in the history
… -> FAIL
  • Loading branch information
LBoullosa committed May 25, 2024
1 parent 7c5db2d commit ec0d6af
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Reflection;
using FluentAssertions;
using Moq;
using STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.Assemblies
{
public partial class AssemblyServiceTests
{
[Theory]
[MemberData(nameof(AssemblyLoadExceptions))]
public void ShouldThrowDependencyExceptionOnLoadAssemblyIfExternalExceptionOccurs(
Exception externalException)
{
// given
string someAssemblyPath = CreateRandomPathAssembly();

var assemblyLoadException =
new AssemblyLoadException(
message: "Assembly load error occurred, contact support.",
innerException: externalException);

var expectedAssemblyDependencyException =
new AssemblyDependencyException(
message: "Assembly dependency error occurred, contact support.",
innerException: assemblyLoadException);

this.assemblyBroker
.Setup(broker =>
broker.GetAssembly(
It.Is<string>(actualAssemblyPath =>
actualAssemblyPath == someAssemblyPath)))
.Throws(externalException);

// when
Func<Assembly> getAssemblyFunction = () =>
this.assemblyService.GetAssembly(someAssemblyPath);

AssemblyDependencyException actualAssemblyDependencyException =
Assert.Throws<AssemblyDependencyException>(
getAssemblyFunction);

//then
actualAssemblyDependencyException.Should().BeEquivalentTo(
expectedAssemblyDependencyException);

this.assemblyBroker
.Verify(broker =>
broker.GetAssembly(It.IsAny<string>()),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private async Task ShouldGetAssembly()
Assembly randomAssembly = CreateRandomAssembly();
Assembly expectedAssembly = randomAssembly;
Assembly returnedAssembly = randomAssembly;
string randomPathAssembly = GetRandomPathAssembly();
string randomPathAssembly = CreateRandomPathAssembly();
string inputPathAssembly = randomPathAssembly;

this.assemblyBroker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void ShouldThrowValidationExceptionIfInvalidAssemblyPath(
Assembly randomAssembly = CreateRandomAssembly();
Assembly expectedAssembly = randomAssembly;
Assembly returnedAssembly = randomAssembly;
string randomPathAssembly = GetRandomPathAssembly();
string randomPathAssembly = CreateRandomPathAssembly();
string inputAssemblyPath = assemblyPath;

var invalidAssemblyPathException =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using Moq;
using STX.SPAL.Core.Brokers.Assemblies;
using STX.SPAL.Core.Services.Foundations.Assemblies;
Expand All @@ -32,7 +34,7 @@ private static string GetRandomString() =>
private static int GetRandomNumber() =>
new IntRange(min: 2, max: 10).GetValue();

private static string GetRandomPathAssembly()
private static string CreateRandomPathAssembly()
{
string randomPathName = GetRandomString();
string randomFileName = GetRandomString();
Expand All @@ -43,7 +45,7 @@ private static string GetRandomPathAssembly()
private static string[] CreateRandomPathArray()
{
return Enumerable.Range(0, GetRandomNumber())
.Select(i => GetRandomPathAssembly())
.Select(i => CreateRandomPathAssembly())
.ToArray();
}

Expand All @@ -60,5 +62,20 @@ private static Assembly CreateRandomAssembly()

return assemblyBuilder;
}

public static TheoryData AssemblyLoadExceptions()
{
return new TheoryData<Exception>
{
new SecurityException(),
new FileLoadException(),
new FileNotFoundException(),
new BadImageFormatException(),
new InvalidOperationException(),
new NotSupportedException(),
new IOException(),
new UnauthorizedAccessException()
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Xeptions;

namespace STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions
{
internal class AssemblyDependencyException : Xeption
{
public AssemblyDependencyException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Xeptions;

namespace STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions
{
internal class AssemblyLoadException : Xeption
{
public AssemblyLoadException(string message, Exception innerException)
: base(message: message, innerException)
{ }
}
}

0 comments on commit ec0d6af

Please sign in to comment.