PageRenderTime 44ms CodeModel.GetById 35ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CommonServiceLocator/CommonServiceLocator.LinFuAdapter.Tests/ServiceLocatorTestCase.cs

http://github.com/philiplaureano/LinFu
C# | 118 lines | 102 code | 16 blank | 0 comment | 0 complexity | 4b606c45c374672a53bf7e1586a79014 MD5 | raw file
  1using System.Collections;
  2using System.Collections.Generic;
  3using CommonServiceLocator.LinFuAdapter.Components;
  4using Microsoft.Practices.ServiceLocation;
  5using NUnit.Framework;
  6
  7namespace CommonServiceLocator.LinFuAdapter.Tests
  8{
  9    public abstract class ServiceLocatorTestCase
 10    {
 11        IServiceLocator _locator;
 12
 13        [SetUp]
 14        public void SetUp()
 15        {
 16            _locator = CreateServiceLocator();
 17        }
 18
 19        protected abstract IServiceLocator CreateServiceLocator();
 20
 21        [Test]
 22        public void GetInstance()
 23        {
 24            ILogger instance = _locator.GetInstance<ILogger>();
 25            Assert.IsNotNull(instance, "instance should not be null");
 26        }
 27
 28        [Test]
 29        [ExpectedException(typeof (ActivationException))]
 30        public void AskingForInvalidComponentShouldRaiseActivationException()
 31        {
 32            _locator.GetInstance<IDictionary>();
 33        }
 34
 35        [Test]
 36        public void GetNamedInstance()
 37        {
 38            ILogger instance = _locator.GetInstance<ILogger>(typeof (AdvancedLogger).FullName);
 39            Assert.IsInstanceOfType(typeof (AdvancedLogger), instance, "Should be an advanced logger");
 40        }
 41
 42        [Test]
 43        public void GetNamedInstance2()
 44        {
 45            ILogger instance = _locator.GetInstance<ILogger>(typeof (SimpleLogger).FullName);
 46            Assert.IsInstanceOfType(typeof (SimpleLogger), instance, "Should be a simple logger");
 47        }
 48
 49        [Test]
 50        [ExpectedException(typeof (ActivationException))]
 51        public void GetNamedInstance_WithZeroLenName()
 52        {
 53            _locator.GetInstance<ILogger>("");
 54        }
 55
 56        [Test]
 57        [ExpectedException(typeof (ActivationException))]
 58        public void GetUnknownInstance2()
 59        {
 60            _locator.GetInstance<ILogger>("test");
 61        }
 62
 63        [Test]
 64        public void GetAllInstances()
 65        {
 66            IEnumerable<ILogger> instances = _locator.GetAllInstances<ILogger>();
 67            IList<ILogger> list = new List<ILogger>(instances);
 68            Assert.AreEqual(2, list.Count);
 69        }
 70
 71        [Test]
 72        public void GetlAllInstance_ForUnknownType_ReturnEmptyEnumerable()
 73        {
 74            IEnumerable<IDictionary> instances = _locator.GetAllInstances<IDictionary>();
 75            IList<IDictionary> list = new List<IDictionary>(instances);
 76            Assert.AreEqual(0, list.Count);
 77        }
 78
 79        [Test]
 80        public void GenericOverload_GetInstance()
 81        {
 82            Assert.AreEqual(
 83                _locator.GetInstance<ILogger>().GetType(),
 84                _locator.GetInstance(typeof (ILogger), null).GetType(),
 85                "should get the same type"
 86                );
 87        }
 88
 89        [Test]
 90        public void GenericOverload_GetInstance_WithName()
 91        {
 92            Assert.AreEqual(
 93                _locator.GetInstance<ILogger>(typeof (AdvancedLogger).FullName).GetType(),
 94                _locator.GetInstance(typeof (ILogger), typeof (AdvancedLogger).FullName).GetType(),
 95                "should get the same type"
 96                );
 97        }
 98
 99        [Test]
100        public void Overload_GetInstance_NoName_And_NullName()
101        {
102            Assert.AreEqual(
103                _locator.GetInstance<ILogger>().GetType(),
104                _locator.GetInstance<ILogger>(null).GetType(),
105                "should get the same type"
106                );
107        }
108
109        [Test]
110        public void GenericOverload_GetAllInstances()
111        {
112            List<ILogger> genericLoggers = new List<ILogger>(_locator.GetAllInstances<ILogger>());
113            List<object> plainLoggers = new List<object>(_locator.GetAllInstances(typeof (ILogger)));
114
115            CollectionAssert.AreEqual(genericLoggers, plainLoggers, "instance collections should be equal");
116        }
117    }
118}