#region CPL License /* Nuclex Framework Copyright (C) 2002-2010 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 #if UNITTEST using System; using System.IO; using Nuclex.Support.Plugins; using NUnit.Framework; #if ENABLE_SERVICEMANAGER namespace Nuclex.Support.Services { /// Unit Test for the service manager class [TestFixture] public class ServiceManagerTest { #region interface IHelloContract /// A simple contract interface used for testing public interface IHelloContract { } #endregion // interface IHelloContract #region interface IWorldContract /// Another simple contract interface used for testing public interface IWorldContract { } #endregion // interface IWorldContract #region interface IHaveNoImplementation /// A contract interface that is not implementated anywhere public interface IHaveNoImplementation { } #endregion // interface IHaveNoImplementation #region class HelloComponent /// Test component that implements the hello contract public class HelloComponent : IHelloContract { } #endregion // class HelloComponent #region class WorldComponent /// /// Test component that implements the world contract and requires /// an implementation of the hello contract /// public class WorldComponent : IWorldContract, IHelloContract { /// Initializes a new world component /// /// Array of hello contract implementations that will be used /// public WorldComponent(IHelloContract[] helloContracts) { } } #endregion // class WorldComponent #region class IncompleteComponent /// /// Test component that requires an implementation of a contract that has /// no implementation available /// public class IncompleteComponent : IWorldContract { /// Initializes the component /// /// Implementation of the unimplemented interface (:P) to use /// public IncompleteComponent(IHaveNoImplementation noImplementation) { } } #endregion // class IncompleteComponent #region class NeedHello /// Component that needs an implementation of the hello contract public class NeedHello : IWorldContract { /// Initializes the component /// /// Implementation of the hello contract that will be used /// public NeedHello(IHelloContract helloContract) { } } #endregion // class NeedHello #region class NeedWorld /// Component that needs an implementation of the world contract public class NeedWorld : IHelloContract { /// Initializes the component /// /// Implementation of the world contract that will be used /// public NeedWorld(IWorldContract worldContract) { } } #endregion // class NeedWorld /// /// Tests whether the GetComponents() method behaves correctly if it is used /// without any assemblies loaded /// [Test] public void TestGetComponentsWithoutAssembly() { ServiceManager serviceManager = new ServiceManager(new PredefinedTypeLister()); Assert.That(serviceManager.GetComponents(), Is.Empty); } /// /// Tests whether the GetComponents() method can locate a simple component /// [Test] public void TestGetComponents() { RepositoryTypeLister typeLister = new RepositoryTypeLister(); ServiceManager serviceManager = new ServiceManager(typeLister); typeLister.Repository.AddAssembly(typeof(ServiceManagerTest).Assembly); Assert.That( serviceManager.GetComponents(), Has.Member(typeof(HelloComponent)).And.Member(typeof(WorldComponent)) ); } /// /// Tests whether the GetComponents() method correctly determines which /// components can have their dependencies completely provided. /// [Test] public void TestFilteredGetComponents() { RepositoryTypeLister typeLister = new RepositoryTypeLister(); ServiceManager serviceManager = new ServiceManager(typeLister); typeLister.Repository.AddAssembly(typeof(ServiceManagerTest).Assembly); Assert.That( serviceManager.GetComponents(false), Has.Member(typeof(WorldComponent)).And.Member(typeof(IncompleteComponent)) ); Assert.That( serviceManager.GetComponents(true), Has.Member(typeof(WorldComponent)).And.No.Member(typeof(IncompleteComponent)) ); } /// /// Tests whether the GetComponents() method can cope with two components /// that have a circular dependency through their services. /// [Test] public void TestCircularDependency() { } /// /// Verifies that the right exception is thrown if the non-generic GetService() /// is used on a value type /// [Test] public void TestGetComponentOnValueType() { RepositoryTypeLister typeLister = new RepositoryTypeLister(); ServiceManager serviceManager = new ServiceManager(typeLister); typeLister.Repository.AddAssembly(typeof(int).Assembly); Assert.Throws( delegate() { serviceManager.GetService(typeof(int)); } ); } } } // namespace Nuclex.Support.Services #endif // ENABLE_SERVICEMANAGER #endif // UNITTEST