#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
using System.Reflection;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// Unit Test for the plugin host class
[TestFixture, NoPlugin] // NoPlugin is used in one of the unit tests
internal class PluginHostTest {
#region class FailingEmployer
/// Employer that unexpectedly fails to employ a given type
private class FailingEmployer : Employer {
/// Employs the specified plugin type
/// Type to be employed
public override void Employ(Type type) {
if(type.Equals(typeof(PluginRepository))) {
throw new InvalidOperationException();
}
}
}
#endregion // class FailingEmployer
/// Tests whether the simple constructor is working
[Test]
public void TestSimpleConstructor() {
new PluginHost(new FactoryEmployer());
}
/// Tests whether the full constructor is working
[Test]
public void TestFullConstructor() {
new PluginHost(new FactoryEmployer(), new PluginRepository());
}
///
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
///
[Test]
public void TestFullConstructorWithPreloadedAssembly() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer testEmployer = new FactoryEmployer();
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testEmployer, testHost.Employer);
Assert.AreEqual(1, testEmployer.Factories.Count);
}
///
/// Verifies that the plugin host correctly stores the provided repository
///
[Test]
public void TestRepositoryStorage() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer testEmployer = new FactoryEmployer();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testRepository, testHost.Repository);
}
///
/// Verifies that the plugin host correctly stores the provided employer
///
[Test]
public void TestEmployerStorage() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer testEmployer = new FactoryEmployer();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testEmployer, testHost.Employer);
}
///
/// Tests whether the plugin host noticed when new assemblies are loaded into
/// the repository
///
[Test]
public void TestAssemblyLoading() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer testEmployer = new FactoryEmployer();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testEmployer, testHost.Employer);
Assert.AreEqual(1, testEmployer.Factories.Count);
}
///
/// Tests whether the plugin host isolates the caller from an exception when the
/// employer fails to employ a type in the assembly
///
[Test]
public void TestAssemblyLoadingWithEmployFailure() {
PluginRepository testRepository = new PluginRepository();
PluginHost testHost = new PluginHost(new FailingEmployer(), testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testRepository, testHost.Repository);
}
///
/// Verifies that the plugin host ignores types which have the NoPluginAttribute
/// assigned to them
///
[Test]
public void TestAssemblyLoadingWithNoPluginAttribute() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer testEmployer = new FactoryEmployer();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testRepository, testHost.Repository);
Assert.AreEqual(0, testEmployer.Factories.Count);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST