PageRenderTime 54ms CodeModel.GetById 37ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 0ms

/XAct.Services.IoC/XAct.Services.IoC.StructureMap.Tests/StructureMapBasedDependencyResolverTests.cs

https://bitbucket.org/xact/cs.ff.xact.lib
C# | 361 lines | 212 code | 124 blank | 25 comment | 1 complexity | fb2423de6b9dfe207d97eb1e0502ec6a MD5 | raw file
  1using System.Linq;
  2using System.Reflection;
  3using StructureMap;
  4using XAct.Diagnostics;
  5using XAct.Diagnostics.Implementations;
  6using XAct.Environment;
  7using XAct.Services.Tests.Services;
  8using XAct.Threading;
  9
 10namespace XAct.Services.IoC.Tests
 11{
 12    using System;
 13    using NUnit.Framework;
 14    using XAct;
 15    using XAct.Bootstrapper.Tests;
 16    using XAct.Tests;
 17
 18/// <summary>
 19    ///   NUNit Test Fixture.
 20    /// </summary>
 21    [TestFixture]
 22    public class StructureMapBasedDependencyResolverTests
 23    {
 24        /// <summary>
 25        ///   Sets up to do before any tests 
 26        ///   within this test fixture are run.
 27        /// </summary>
 28        [TestFixtureSetUp]
 29        public void TestFixtureSetUp()
 30        {
 31            //run once before any tests in this testfixture have been run...
 32            //...setup vars common to all the upcoming tests...
 33
 34            Singleton<IocContext>.Instance.ResetIoC();
 35
 36        }
 37
 38        /// <summary>
 39        ///   Tear down after all tests in this fixture.
 40        /// </summary>
 41        [TestFixtureTearDown]
 42        public void TestFixtureTearDown()
 43        {
 44
 45        }
 46        [TearDown]
 47        public void MyTestTearDown()
 48        {
 49            GC.Collect();
 50        }
 51
 52        [Test]
 53        public void Can_Register_A_Service_Using_Container()
 54        {
 55
 56            //Bypass everything in the lib and do it by hand:
 57            IContainer smContainer = new Container();
 58
 59            
 60            smContainer.Configure(x=>x.For<ITraceSwitchService>().Use<TraceSwitchService>());
 61
 62            bool isREgistered = smContainer.Model.HasDefaultImplementationFor(typeof(ITraceSwitchService));
 63
 64
 65            Assert.IsTrue(isREgistered);
 66        }
 67
 68        
 69         
 70
 71        [Test]
 72        public void Can_Resolve_And_Use_A_Service()
 73        {
 74            StructureMapBootstrapper.Initialize();
 75
 76            DependencyResolver.Current.GetInstance<ITracingService>().QuickTrace("Works...");
 77
 78            DateTime check = DependencyResolver.Current.GetInstance<IDateTimeService>().NowUTC;
 79
 80            Assert.AreNotEqual(DateTime.MinValue, check);
 81        }
 82
 83
 84
 85        [Test]
 86        public void Cannot_Resolve_MyService_As_No_Name_Given()
 87        {
 88            StructureMapBootstrapper.Initialize();
 89
 90            //Service was registered with name:
 91            IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>(false);
 92
 93            //So should be null:
 94            Assert.IsNull(serviceA);
 95
 96        }
 97
 98        [Test]
 99        public void Can_Resolve_MyService_With_Given_Name()
100        {
101            StructureMapBootstrapper.Initialize();
102
103            IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>("A");
104
105
106            Assert.IsNotNull(serviceA);
107
108            Assert.AreEqual(typeof (MyServiceA), serviceA.GetType());
109
110        }
111
112
113        [Test]
114        public void GetNamedServiceB()
115        {
116            StructureMapBootstrapper.Initialize();
117
118            IMyService serviceB = DependencyResolver.Current.GetInstance<IMyService>("B");
119
120
121            Assert.IsNotNull(serviceB);
122
123            Assert.AreEqual(typeof (MyServiceB), serviceB.GetType());
124
125        }
126
127        [Test]
128        public void GetNamedServiceAandB()
129        {
130            StructureMapBootstrapper.Initialize();
131
132
133            var r = XAct.DependencyResolver.BindingResults.BindingScanResults.BindingDescriptors.Select(
134                x => x.InterfaceType == typeof (IMyService)).ToArray();
135
136            IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>("A");
137
138            IMyService serviceB = DependencyResolver.Current.GetInstance<IMyService>("B");
139
140
141            Assert.IsTrue(r.Length > 1);
142            Assert.IsNotNull(serviceA);
143            Assert.IsNotNull(serviceB);
144
145            Assert.IsTrue(serviceA is MyServiceA);
146            Assert.IsTrue(serviceB is MyServiceB);
147
148        }
149
150
151
152
153        [Test]
154        public void Resolve_Service_Using_Inheritence_Binding()
155        {
156            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceA>();
157            Assert.IsNotNull(service);
158            Assert.AreEqual(typeof(TestServiceA),service.GetType());
159        }
160        [Test]
161        public void Resolve_Service_Using_Attribute_Binding()
162        {
163            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceB>();
164            Assert.IsNotNull(service);
165            Assert.AreEqual(typeof(TestServiceB), service.GetType());
166        }
167
168
169        [Test]
170        public void Resolve_Service_Using_Inheritence_Binding_Via_Longest_Interface_Name()
171        {
172            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceC>(false);
173            Assert.IsNull(service,"Cis null if not called from more precise binding");
174
175            //But will work if invoked by more precise binding:
176            service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceC>();
177            Assert.IsNotNull(service, "C was null");
178            Assert.AreEqual(typeof(APrefixTestServiceC), service.GetType());
179        }
180
181
182        [Test]
183        public void Resolve_Service_Using_Inheritence_Binding_Via_Longest_Interface_Name2()
184        {
185            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceD>(false);
186            Assert.IsNull(service,"D was null");
187            
188            service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceD>(false);
189            Assert.IsNull(service, "D was null");
190
191            //Can resolve against long name:
192            service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceD>();
193            Assert.IsNotNull(service, "D found the most specific");
194            Assert.AreEqual(typeof(ALongerPrefixTestServiceD), service.GetType());
195        }
196
197
198
199
200
201        [Test]
202        public void Resolve_Service_Using_Mix_Of_Inheritence_And_Atribute_Binding()
203        {
204
205            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceE>();
206            Assert.IsNotNull(service);
207
208            service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceE>(false);
209            Assert.IsNull(service);
210
211
212            service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceE>();
213            Assert.IsNotNull(service);
214            Assert.AreEqual(typeof (ALongerPrefixTestServiceE), service.GetType());
215            
216
217            
218
219        }
220
221
222
223
224
225
226        [Test]
227        public void Resolve_Service_Using_Mix_Of_Inheritence_And_Atribute_Binding2()
228        {
229
230        //Was matched by Attribute:
231            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceF>();
232            Assert.IsNotNull(service);
233
234
235            service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceF>(false);
236            Assert.IsNull(service);
237
238            //By longest name
239            service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceF>();
240            Assert.IsNotNull(service);
241           
242        
243            
244            //Even though there is a prefix, it will match,
245            //as there is nothing more specific:
246            Assert.AreEqual(typeof(ALongerPrefixTestServiceF), service.GetType());
247        }
248
249
250
251
252
253        [Test]
254        public void Resolve_TestServiceG()
255        {
256            var service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceG>();
257            Assert.IsNotNull(service);
258            //Even though there is a prefix, it will match,
259            //as there is nothing more specific:
260            Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
261        }
262
263
264        [Test]
265        public void Resolve_TestServiceG2()
266        {
267            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceG>();
268            Assert.IsNotNull(service);
269
270            Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
271
272
273            service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceG>();
274            Assert.IsNotNull(service);
275
276            Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
277
278            //Note that it did not match this as it was neither
279            //the longest name, nor specifically named in the ApplicationBinding:
280            var service2 = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceG>(false);
281            Assert.IsNotNull(service2);
282            Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service2.GetType());
283
284        }
285
286
287        [Test]
288        public void Resolve_TestService_With_Correct_Priority()
289        {
290            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceM>();
291
292            Assert.IsNotNull(service,"Was null....");
293            Assert.AreEqual(typeof(TwoTestServiceM),service.GetType());
294        }
295        [Test]
296        public void Resolve_TestService_With_Correct_Priority2()
297        {
298            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceN>();
299
300            Assert.IsNotNull(service,"Was null...");
301            Assert.AreEqual(typeof(OneTestServiceN), service.GetType());
302        }
303
304
305        [Test]
306        public void Resolve_Service_With_Generic_Interface_Using_Interfaces()
307        {
308            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceO<int>>();
309
310            Assert.IsNotNull(service);
311            Assert.AreEqual(typeof (IntTestServiceO), service.GetType());
312        }
313
314        [Test]
315        public void Resolve_Service_With_Generic_Interface2_Using_Interfaces()
316        {
317            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceO<Guid>>();
318
319            Assert.IsNotNull(service);
320            Assert.AreEqual(typeof (GuidTestServiceO), service.GetType());
321        }
322
323
324
325
326
327
328        [Test]
329        public void Resolve_Service_With_Generic_Interface_Using_Attributes()
330        {
331            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceP<int>>();
332
333            Assert.IsNotNull(service);
334            Assert.AreEqual(typeof(TestServiceP1), service.GetType());
335        }
336
337        [Test]
338        public void Resolve_Service_With_Generic_Interface2_Using_Attributes()
339        {
340            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceP<Guid>>();
341
342            Assert.IsNotNull(service);
343            Assert.AreEqual(typeof(TestServiceP2), service.GetType());
344        }
345
346        [Test]
347        public void Resolve_Service_With_Generic_Interface_Using_Attributes_2()
348        {
349            var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceQ<int>>();
350
351            Assert.IsNotNull(service);
352
353            Assert.AreEqual(typeof(TestServiceQ<int>), service.GetType());
354        }
355
356    
357    }
358
359}
360
361