PageRenderTime 223ms CodeModel.GetById 100ms app.highlight 7ms RepoModel.GetById 114ms app.codeStats 0ms

/src/UnitTests/IOC/Configuration/AssemblyContainerLoaderTests.cs

http://github.com/philiplaureano/LinFu
C# | 125 lines | 85 code | 28 blank | 12 comment | 0 complexity | 9312addc70cd3d3e7a9739c4584efaed MD5 | raw file
  1using System;
  2using LinFu.IoC.Configuration;
  3using LinFu.IoC.Interfaces;
  4using LinFu.Reflection;
  5using Moq;
  6using Xunit;
  7using SampleLibrary;
  8
  9namespace LinFu.UnitTests.IOC.Configuration
 10{
 11    public class AssemblyContainerLoaderTests : BaseTestFixture
 12    {
 13        protected override void Init()
 14        {
 15            _mockAssemblyLoader = new Mock<IAssemblyLoader>();
 16            _mockTypeExtractor = new Mock<ITypeExtractor>();
 17            _mockTypeLoader = new Mock<ITypeLoader>();
 18        }
 19
 20        protected override void Term()
 21        {
 22            _mockAssemblyLoader.VerifyAll();
 23            _mockTypeExtractor.VerifyAll();
 24            _mockTypeLoader.VerifyAll();
 25
 26            _mockAssemblyLoader = null;
 27            _mockTypeExtractor = null;
 28            _mockTypeLoader = null;
 29        }
 30
 31
 32        private Mock<IAssemblyLoader> _mockAssemblyLoader;
 33        private Mock<ITypeExtractor> _mockTypeExtractor;
 34        private Mock<ITypeLoader> _mockTypeLoader;
 35
 36        [Fact]
 37        public void AssemblyContainerLoaderShouldCallAssemblyLoader()
 38        {
 39            var containerLoader = new AssemblyContainerLoader();
 40
 41            // The container loader should use the assembly loader
 42            // to load the assembly
 43            var filename = "input.dll";
 44
 45            _mockAssemblyLoader.Expect(loader => loader.Load(filename)).Returns(typeof(SampleClass).Assembly);
 46
 47            containerLoader.AssemblyLoader = _mockAssemblyLoader.Object;
 48            containerLoader.Load(filename);
 49        }
 50
 51        [Fact]
 52        public void AssemblyContainerLoaderShouldCallTypeExtractor()
 53        {
 54            var containerLoader = new AssemblyContainerLoader();
 55            var filename = "input.dll";
 56
 57            var targetAssembly = typeof(SampleClass).Assembly;
 58
 59            // Make sure that it calls the assembly loader
 60            _mockAssemblyLoader.Expect(loader => loader.Load(filename)).Returns(targetAssembly);
 61
 62            // It must call the Type Extractor
 63            _mockTypeExtractor.Expect(extractor => extractor.GetTypes(targetAssembly))
 64                .Returns(new[] {typeof(SampleClass)});
 65
 66
 67            var assemblyActionLoader = new AssemblyActionLoader<IServiceContainer>(() => containerLoader.TypeLoaders);
 68            assemblyActionLoader.TypeExtractor = _mockTypeExtractor.Object;
 69
 70            containerLoader.AssemblyLoader = _mockAssemblyLoader.Object;
 71            containerLoader.AssemblyActionLoader = assemblyActionLoader;
 72            containerLoader.Load(filename);
 73        }
 74
 75        [Fact]
 76        public void AssemblyContainerLoaderShouldCallTypeLoader()
 77        {
 78            // HACK: The Cut&Paste is ugly, but it works
 79            var containerLoader = new AssemblyContainerLoader();
 80            var filename = "input.dll";
 81
 82            var targetAssembly = typeof(SampleClass).Assembly;
 83
 84            // Make sure that it calls the assembly loader
 85            _mockAssemblyLoader.Expect(loader => loader.Load(filename)).Returns(targetAssembly);
 86
 87            // It must call the Type Extractor
 88            _mockTypeExtractor.Expect(extractor => extractor.GetTypes(targetAssembly))
 89                .Returns(new[] {typeof(SampleClass)});
 90
 91            // Make sure that it calls the type loaders
 92            _mockTypeLoader.Expect(loader => loader.CanLoad(typeof(SampleClass))).Returns(true);
 93            _mockTypeLoader.Expect(loader => loader.Load(typeof(SampleClass)))
 94                .Returns(new Action<IServiceContainer>[0]);
 95
 96            var assemblyActionLoader = new AssemblyActionLoader<IServiceContainer>(() => containerLoader.TypeLoaders);
 97            assemblyActionLoader.TypeExtractor = _mockTypeExtractor.Object;
 98
 99            containerLoader.AssemblyLoader = _mockAssemblyLoader.Object;
100            containerLoader.AssemblyActionLoader = assemblyActionLoader;
101
102            // The container loader should call the type loader
103            // once the load method is called
104            containerLoader.TypeLoaders.Add(_mockTypeLoader.Object);
105
106            containerLoader.Load(filename);
107        }
108
109        [Fact]
110        public void AssemblyContainerLoaderShouldOnlyLoadDllFiles()
111        {
112            var mockTypeLoader = new Mock<ITypeLoader>();
113            var containerLoader = new AssemblyContainerLoader();
114            containerLoader.TypeLoaders.Add(mockTypeLoader.Object);
115
116            // This should return true
117            var validFile = typeof(AssemblyContainerLoaderTests).Assembly.Location;
118            Assert.True(containerLoader.CanLoad(validFile));
119
120            // This should return false;
121            var invalidFile = "input.txt";
122            Assert.False(containerLoader.CanLoad(invalidFile));
123        }
124    }
125}