PageRenderTime 104ms CodeModel.GetById 51ms app.highlight 12ms RepoModel.GetById 38ms app.codeStats 0ms

/src/UnitTests/IOC/Configuration/ConfigurationTests.cs

http://github.com/philiplaureano/LinFu
C# | 220 lines | 155 code | 44 blank | 21 comment | 8 complexity | c939909a78d2f47ba967b3ed9b509a88 MD5 | raw file
  1using System;
  2using System.Linq;
  3using LinFu.IoC;
  4using LinFu.IoC.Configuration;
  5using LinFu.IoC.Interfaces;
  6using LinFu.Reflection;
  7using Moq;
  8using Xunit;
  9using SampleLibrary;
 10using SampleLibrary.IOC;
 11
 12namespace LinFu.UnitTests.IOC.Configuration
 13{
 14    public class ConfigurationTests
 15    {
 16        [Fact]
 17        public void AssemblyLoaderMustLoadTargetAssemblyFromDisk()
 18        {
 19            IAssemblyLoader loader = new AssemblyLoader();
 20
 21            // The loader should return a valid assembly
 22            var result = loader.Load(typeof(SampleClass).Assembly.Location);
 23            Assert.NotNull(result);
 24        }
 25
 26        [Fact]
 27        public void ClassMarkedWithPostProcessorAttributeMustBeInjectedIntoContainer()
 28        {
 29            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
 30            var loader = new Loader();
 31            loader.LoadDirectory(baseDirectory, "*.dll");
 32            var container = new ServiceContainer();
 33
 34            loader.LoadInto(container);
 35
 36            var matches = from p in container.PostProcessors
 37                where p != null &&
 38                      p.GetType() == typeof(SamplePostProcessor)
 39                select p;
 40
 41            Assert.True(matches.Count() > 0, "The postprocessor failed to load.");
 42        }
 43
 44        [Fact]
 45        public void ClassMarkedWithPreprocessorAttributeMustBeInjectedIntoContainer()
 46        {
 47            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
 48            var loader = new Loader();
 49            loader.LoadDirectory(baseDirectory, "*.dll");
 50            var container = new ServiceContainer();
 51
 52            loader.LoadInto(container);
 53
 54            var matches = from p in container.PreProcessors
 55                where p != null &&
 56                      p.GetType() == typeof(SamplePreprocessor)
 57                select p;
 58
 59            Assert.True(matches.Count() > 0, "The preprocessor failed to load.");
 60        }
 61
 62        [Fact]
 63        public void CreatedServicesMustBeAbleToInitializeThemselves()
 64        {
 65            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
 66            var loader = new Loader();
 67            loader.LoadDirectory(baseDirectory, "*.dll");
 68
 69            var mockInitialize = new Mock<IInitialize>();
 70            var container = new ServiceContainer();
 71
 72            // The service container should call the Initialize method
 73            mockInitialize.Expect(target => target.Initialize(container));
 74            loader.LoadInto(container);
 75
 76            container.AddService(mockInitialize.Object);
 77
 78            var result = container.GetService<IInitialize>();
 79            Assert.NotNull(result);
 80            Assert.Same(mockInitialize.Object, result);
 81
 82            mockInitialize.VerifyAll();
 83        }
 84
 85        [Fact]
 86        public void LoaderMustCallCustomLoaderActions()
 87        {
 88            var mockContainer = new Mock<IServiceContainer>();
 89            var mockListing = new Mock<IDirectoryListing>();
 90
 91            var loader = new Loader<IServiceContainer>();
 92            loader.DirectoryLister = mockListing.Object;
 93
 94            // Return an empty file listing
 95            mockListing.Expect(listing => listing.GetFiles(It.IsAny<string>(), It.IsAny<string>()))
 96                .Returns(new string[0]);
 97
 98            // Use the initializer mock to verify
 99            // that the custom action was called
100
101            var mockInitializer = new Mock<IInitialize>();
102            mockInitializer.Expect(initializer => initializer.Initialize(mockContainer.Object));
103
104            // HACK: In order to verify the call,
105            // we need to adapt the mock to an 
106            // Action<ILoader<IServiceContainer>> instance
107            Action<ILoader<IServiceContainer>> customAction =
108                targetLoader =>
109                {
110                    var initializer = mockInitializer.Object;
111                    var container = mockContainer.Object;
112
113                    // The test will only succeed if
114                    // the following line of code
115                    // is invoked:
116                    initializer.Initialize(container);
117                };
118
119            loader.CustomLoaderActions.Add(customAction);
120            loader.LoadInto(mockContainer.Object);
121
122            mockInitializer.VerifyAll();
123        }
124
125        [Fact]
126        public void LoaderMustPassFilenameToContainerLoaders()
127        {
128            var mockContainer = new Mock<IServiceContainer>();
129            var mockLoader = new Mock<IContainerLoader>(MockBehavior.Loose);
130            var mockListing = new Mock<IDirectoryListing>();
131
132            var loader = new Loader<IServiceContainer>
133            {
134                DirectoryLister = mockListing.Object
135            };
136
137            var filename = "input.dll";
138            mockListing.Expect(listing => listing.GetFiles(It.IsAny<string>(), filename))
139                .Returns(new[] {filename});
140
141            loader.FileLoaders.Add(mockLoader.Object);
142            // The container should call the load method
143            // with the given filename
144            var path = string.Empty;
145
146            var emptyActions = new Action<IServiceContainer>[] { };
147            mockLoader.Expect(l => l.CanLoad(filename)).Returns(true);
148            mockLoader.Expect(l => l.Load(filename)).Returns(emptyActions);
149
150            loader.LoadDirectory(path, filename);
151            loader.LoadInto(mockContainer.Object);
152
153            mockLoader.VerifyAll();
154            mockListing.VerifyAll();
155        }
156
157        [Fact]
158        public void LoaderMustRevertToDefaultsAfterReset()
159        {
160            var mockFileLoader = new Mock<IActionLoader<IServiceContainer, string>>();
161            var mockPlugin = new Mock<ILoaderPlugin<IServiceContainer>>();
162            var loader = new Loader();
163
164            // Fill the loader with random data
165            loader.FileLoaders.Add(mockFileLoader.Object);
166            loader.Plugins.Add(mockPlugin.Object);
167            loader.QueuedActions.Add(container => Console.WriteLine("Hello, World"));
168            loader.CustomLoaderActions.Add(targetLoader => Console.WriteLine("Hello, World"));
169
170            // Reset the loader and make sure everything was cleared
171            loader.Reset();
172
173            Assert.True(loader.FileLoaders.Count == 0);
174            Assert.True(loader.Plugins.Count == 0);
175            Assert.True(loader.QueuedActions.Count == 0);
176            Assert.True(loader.CustomLoaderActions.Count == 0);
177        }
178
179        [Fact]
180        public void LoaderMustSignalToPluginsWhenTheLoadBeginsAndEnds()
181        {
182            var mockPlugin = new Mock<ILoaderPlugin<IServiceContainer>>();
183            var container = new Mock<IServiceContainer>();
184            var mockListing = new Mock<IDirectoryListing>();
185            var loader = new Loader<IServiceContainer>();
186
187            // Setup the directory listing and            
188            // return an empty listing since
189            // we're only interested in the plugin behavior
190            mockListing.Expect(listing => listing.GetFiles(It.IsAny<string>(), string.Empty))
191                .Returns(new string[0]);
192
193            // Initialize the loader
194            loader.DirectoryLister = mockListing.Object;
195            loader.Plugins.Add(mockPlugin.Object);
196
197            // Both the BeginLoad and EndLoad methods should be called
198            mockPlugin.Expect(p => p.BeginLoad(container.Object));
199            mockPlugin.Expect(p => p.EndLoad(container.Object));
200
201            // Execute the loader
202            loader.LoadDirectory(string.Empty, string.Empty);
203            loader.LoadInto(container.Object);
204
205            mockPlugin.VerifyAll();
206            mockListing.VerifyAll();
207        }
208
209        [Fact]
210        public void TypeExtractorMustListTypesFromGivenAssembly()
211        {
212            var targetAssembly = typeof(SampleClass).Assembly;
213
214            ITypeExtractor extractor = new TypeExtractor();
215            var results = extractor.GetTypes(targetAssembly);
216            Assert.NotNull(results);
217            Assert.True(results.Count() > 0);
218        }
219    }
220}