//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.PlugIns.Tests;
using System.Reflection;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
///
/// Tests for the .
///
[TestFixture]
public class CustomPlugInContainerTest
{
///
/// Tests if creating the plugin container with an interface without a throws an exception.
///
[Test]
public void CreatingContainerWithNonMarkedTypeThrowsException()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
var mock = new Mock>(manager);
var exception = Assert.Throws(() =>
{
_ = mock.Object;
});
Assert.That(exception?.InnerException, Is.InstanceOf());
}
///
/// Tests if a plugin can be retrieved from the custom container, when the plugin has been registered after the container was created.
///
[Test]
public void GetPlugInFromCustomContainerWithRegisteredPlugInAfterRegistration()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
var container = new CustomTestPlugInContainer(manager);
manager.RegisterPlugIn();
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.Not.Null);
}
///
/// Tests if a plugin can be retrieved from the custom container, when the plugin has been registered before the container was created.
///
[Test]
public void GetPlugInFromCustomContainerWithInitiallyRegisteredPlugIn()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
manager.RegisterPlugIn();
var container = new CustomTestPlugInContainer(manager);
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.Not.Null);
}
///
/// Tests if a plugin can't be retrieved from the custom container when the plugin has been deactivated before.
///
[Test]
public void DontGetPlugInFromCustomContainerAfterDeactivation()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
manager.RegisterPlugIn();
var container = new CustomTestPlugInContainer(manager);
manager.DeactivatePlugIn();
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.Null);
}
///
/// Tests if a plugin can't be retrieved from the custom container when the plugin isn't suitable for the container and therefore not effective.
///
[Test]
public void DontGetPlugInFromCustomContainerIfItDoesntSuit()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
var container = new CustomTestPlugInContainer(manager) { CreateNewPlugIns = false };
manager.RegisterPlugIn();
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.Null);
}
///
/// Tests if the custom container replaces the plugin implementation if a new (more suitable) plugin is registered.
///
[Test]
public void ReplacePlugInAtCustomContainer()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
manager.RegisterPlugIn();
var container = new CustomTestPlugInContainer(manager);
manager.RegisterPlugIn();
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.InstanceOf());
}
///
/// Tests if a plugin which got deactivated by another plugin, gets reactivated as soon as the other plugin gets deactivated.
///
[Test]
public void ReactivatePlugInAtCustomContainer()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
manager.RegisterPlugIn();
var container = new CustomTestPlugInContainer(manager);
manager.RegisterPlugIn();
manager.DeactivatePlugIn();
var plugIn = container.GetPlugIn();
Assert.That(plugIn, Is.InstanceOf());
}
///
/// Tests if a plugin can be retrieved with both of its implemented interfaces.
///
[Test]
public void GetPlugInFromCustomContainerWithAllImplementedInterfaces()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), null, null);
var container = new CustomTestPlugInContainer(manager);
container.AddPlugIn(new TestCustomPlugIn2(), true);
Assert.That(container.GetPlugIn(), Is.Not.Null);
Assert.That(container.GetPlugIn(), Is.Not.Null);
}
}