PageRenderTime 47ms CodeModel.GetById 20ms 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
  1. using System.Linq;
  2. using System.Reflection;
  3. using StructureMap;
  4. using XAct.Diagnostics;
  5. using XAct.Diagnostics.Implementations;
  6. using XAct.Environment;
  7. using XAct.Services.Tests.Services;
  8. using XAct.Threading;
  9. namespace XAct.Services.IoC.Tests
  10. {
  11. using System;
  12. using NUnit.Framework;
  13. using XAct;
  14. using XAct.Bootstrapper.Tests;
  15. using XAct.Tests;
  16. /// <summary>
  17. /// NUNit Test Fixture.
  18. /// </summary>
  19. [TestFixture]
  20. public class StructureMapBasedDependencyResolverTests
  21. {
  22. /// <summary>
  23. /// Sets up to do before any tests
  24. /// within this test fixture are run.
  25. /// </summary>
  26. [TestFixtureSetUp]
  27. public void TestFixtureSetUp()
  28. {
  29. //run once before any tests in this testfixture have been run...
  30. //...setup vars common to all the upcoming tests...
  31. Singleton<IocContext>.Instance.ResetIoC();
  32. }
  33. /// <summary>
  34. /// Tear down after all tests in this fixture.
  35. /// </summary>
  36. [TestFixtureTearDown]
  37. public void TestFixtureTearDown()
  38. {
  39. }
  40. [TearDown]
  41. public void MyTestTearDown()
  42. {
  43. GC.Collect();
  44. }
  45. [Test]
  46. public void Can_Register_A_Service_Using_Container()
  47. {
  48. //Bypass everything in the lib and do it by hand:
  49. IContainer smContainer = new Container();
  50. smContainer.Configure(x=>x.For<ITraceSwitchService>().Use<TraceSwitchService>());
  51. bool isREgistered = smContainer.Model.HasDefaultImplementationFor(typeof(ITraceSwitchService));
  52. Assert.IsTrue(isREgistered);
  53. }
  54. [Test]
  55. public void Can_Resolve_And_Use_A_Service()
  56. {
  57. StructureMapBootstrapper.Initialize();
  58. DependencyResolver.Current.GetInstance<ITracingService>().QuickTrace("Works...");
  59. DateTime check = DependencyResolver.Current.GetInstance<IDateTimeService>().NowUTC;
  60. Assert.AreNotEqual(DateTime.MinValue, check);
  61. }
  62. [Test]
  63. public void Cannot_Resolve_MyService_As_No_Name_Given()
  64. {
  65. StructureMapBootstrapper.Initialize();
  66. //Service was registered with name:
  67. IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>(false);
  68. //So should be null:
  69. Assert.IsNull(serviceA);
  70. }
  71. [Test]
  72. public void Can_Resolve_MyService_With_Given_Name()
  73. {
  74. StructureMapBootstrapper.Initialize();
  75. IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>("A");
  76. Assert.IsNotNull(serviceA);
  77. Assert.AreEqual(typeof (MyServiceA), serviceA.GetType());
  78. }
  79. [Test]
  80. public void GetNamedServiceB()
  81. {
  82. StructureMapBootstrapper.Initialize();
  83. IMyService serviceB = DependencyResolver.Current.GetInstance<IMyService>("B");
  84. Assert.IsNotNull(serviceB);
  85. Assert.AreEqual(typeof (MyServiceB), serviceB.GetType());
  86. }
  87. [Test]
  88. public void GetNamedServiceAandB()
  89. {
  90. StructureMapBootstrapper.Initialize();
  91. var r = XAct.DependencyResolver.BindingResults.BindingScanResults.BindingDescriptors.Select(
  92. x => x.InterfaceType == typeof (IMyService)).ToArray();
  93. IMyService serviceA = DependencyResolver.Current.GetInstance<IMyService>("A");
  94. IMyService serviceB = DependencyResolver.Current.GetInstance<IMyService>("B");
  95. Assert.IsTrue(r.Length > 1);
  96. Assert.IsNotNull(serviceA);
  97. Assert.IsNotNull(serviceB);
  98. Assert.IsTrue(serviceA is MyServiceA);
  99. Assert.IsTrue(serviceB is MyServiceB);
  100. }
  101. [Test]
  102. public void Resolve_Service_Using_Inheritence_Binding()
  103. {
  104. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceA>();
  105. Assert.IsNotNull(service);
  106. Assert.AreEqual(typeof(TestServiceA),service.GetType());
  107. }
  108. [Test]
  109. public void Resolve_Service_Using_Attribute_Binding()
  110. {
  111. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceB>();
  112. Assert.IsNotNull(service);
  113. Assert.AreEqual(typeof(TestServiceB), service.GetType());
  114. }
  115. [Test]
  116. public void Resolve_Service_Using_Inheritence_Binding_Via_Longest_Interface_Name()
  117. {
  118. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceC>(false);
  119. Assert.IsNull(service,"Cis null if not called from more precise binding");
  120. //But will work if invoked by more precise binding:
  121. service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceC>();
  122. Assert.IsNotNull(service, "C was null");
  123. Assert.AreEqual(typeof(APrefixTestServiceC), service.GetType());
  124. }
  125. [Test]
  126. public void Resolve_Service_Using_Inheritence_Binding_Via_Longest_Interface_Name2()
  127. {
  128. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceD>(false);
  129. Assert.IsNull(service,"D was null");
  130. service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceD>(false);
  131. Assert.IsNull(service, "D was null");
  132. //Can resolve against long name:
  133. service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceD>();
  134. Assert.IsNotNull(service, "D found the most specific");
  135. Assert.AreEqual(typeof(ALongerPrefixTestServiceD), service.GetType());
  136. }
  137. [Test]
  138. public void Resolve_Service_Using_Mix_Of_Inheritence_And_Atribute_Binding()
  139. {
  140. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceE>();
  141. Assert.IsNotNull(service);
  142. service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceE>(false);
  143. Assert.IsNull(service);
  144. service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceE>();
  145. Assert.IsNotNull(service);
  146. Assert.AreEqual(typeof (ALongerPrefixTestServiceE), service.GetType());
  147. }
  148. [Test]
  149. public void Resolve_Service_Using_Mix_Of_Inheritence_And_Atribute_Binding2()
  150. {
  151. //Was matched by Attribute:
  152. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceF>();
  153. Assert.IsNotNull(service);
  154. service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceF>(false);
  155. Assert.IsNull(service);
  156. //By longest name
  157. service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceF>();
  158. Assert.IsNotNull(service);
  159. //Even though there is a prefix, it will match,
  160. //as there is nothing more specific:
  161. Assert.AreEqual(typeof(ALongerPrefixTestServiceF), service.GetType());
  162. }
  163. [Test]
  164. public void Resolve_TestServiceG()
  165. {
  166. var service = XAct.DependencyResolver.Current.GetInstance<IALongerPrefixTestServiceG>();
  167. Assert.IsNotNull(service);
  168. //Even though there is a prefix, it will match,
  169. //as there is nothing more specific:
  170. Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
  171. }
  172. [Test]
  173. public void Resolve_TestServiceG2()
  174. {
  175. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceG>();
  176. Assert.IsNotNull(service);
  177. Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
  178. service = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceG>();
  179. Assert.IsNotNull(service);
  180. Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service.GetType());
  181. //Note that it did not match this as it was neither
  182. //the longest name, nor specifically named in the ApplicationBinding:
  183. var service2 = XAct.DependencyResolver.Current.GetInstance<IAPrefixTestServiceG>(false);
  184. Assert.IsNotNull(service2);
  185. Assert.AreEqual(typeof(ALongerPrefixTestServiceG), service2.GetType());
  186. }
  187. [Test]
  188. public void Resolve_TestService_With_Correct_Priority()
  189. {
  190. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceM>();
  191. Assert.IsNotNull(service,"Was null....");
  192. Assert.AreEqual(typeof(TwoTestServiceM),service.GetType());
  193. }
  194. [Test]
  195. public void Resolve_TestService_With_Correct_Priority2()
  196. {
  197. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceN>();
  198. Assert.IsNotNull(service,"Was null...");
  199. Assert.AreEqual(typeof(OneTestServiceN), service.GetType());
  200. }
  201. [Test]
  202. public void Resolve_Service_With_Generic_Interface_Using_Interfaces()
  203. {
  204. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceO<int>>();
  205. Assert.IsNotNull(service);
  206. Assert.AreEqual(typeof (IntTestServiceO), service.GetType());
  207. }
  208. [Test]
  209. public void Resolve_Service_With_Generic_Interface2_Using_Interfaces()
  210. {
  211. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceO<Guid>>();
  212. Assert.IsNotNull(service);
  213. Assert.AreEqual(typeof (GuidTestServiceO), service.GetType());
  214. }
  215. [Test]
  216. public void Resolve_Service_With_Generic_Interface_Using_Attributes()
  217. {
  218. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceP<int>>();
  219. Assert.IsNotNull(service);
  220. Assert.AreEqual(typeof(TestServiceP1), service.GetType());
  221. }
  222. [Test]
  223. public void Resolve_Service_With_Generic_Interface2_Using_Attributes()
  224. {
  225. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceP<Guid>>();
  226. Assert.IsNotNull(service);
  227. Assert.AreEqual(typeof(TestServiceP2), service.GetType());
  228. }
  229. [Test]
  230. public void Resolve_Service_With_Generic_Interface_Using_Attributes_2()
  231. {
  232. var service = XAct.DependencyResolver.Current.GetInstance<ITestServiceQ<int>>();
  233. Assert.IsNotNull(service);
  234. Assert.AreEqual(typeof(TestServiceQ<int>), service.GetType());
  235. }
  236. }
  237. }