public void GetCurrent_CustomServiceLocatorReloadsSiteOnChange() { //Arrange var f = new BSPConfiguredFarm();; var sharepointLocator = new TestableSharePointServiceLocator(); //Arrange BIServiceLocatorFactory.Reset(); BIServiceLocatorFactory.LocatorTypeToUse = typeof(FakeLocator); var farmTypeMappings = new List <TypeMapping> { TypeMapping.Create <IServiceLocatorFactory, BIServiceLocatorFactory>() }; sharepointLocator.FarmTypeMappings = farmTypeMappings; var expectedSite = new BSPSite(); var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; expectedSite.ID = TestsConstants.TestGuid; var time = new DateTime(2010, 5, 30); sharepointLocator.SiteTypeMappings = typeMappings; MDateTime.NowGet = () => time; var target = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory time = time.AddSeconds(SharePointServiceLocator.SiteCachingTimeoutInSeconds + 1); sharepointLocator.SiteLastUpdatedRetVal = time; //Act var targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>(); //Assert Assert.IsTrue(BIServiceLocatorFactory.LoadCount == 2); }
public void RemoveTypeMappingsGeneric_RemoveAMapping() { //Arrange var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>(), TypeMapping.Create <IConfigManager, ConfigManager>(), TypeMapping.Create <IHierarchicalConfig, HierarchicalConfig>() }; var config = new ServiceLocationConfigData(typeMappings); var bag = new BIPropertyBag(); BSPFarm.SetLocal(); var cfgMgr = new SIConfigManager { ContainsKeyInPropertyBagStringIPropertyBag = (key, propertyBag) => true, CanAccessFarmConfigGet = () => true, GetPropertyBagConfigLevel = (configlevel) => bag, }; cfgMgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, propetyBag) => config); cfgMgr.BehaveAsDefaultValue(); //Act var target = new ServiceLocatorConfig(cfgMgr); target.RemoveTypeMappings <ILogger>(); List <TypeMapping> registeredTypeMappings = target.GetTypeMappings().ToList(); //Assert Assert.AreEqual(2, registeredTypeMappings.Count); Assert.AreSame(typeMappings[1], registeredTypeMappings[0]); Assert.AreSame(typeMappings[2], registeredTypeMappings[1]); }
public void GetCurrent_AddNewFarmMappingAlreadyDefinedInSiteUsesSite() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; BSPFarm.SetLocal(); var expectedSite = new BSPSite { ID = TestsConstants.TestGuid }; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = new List <TypeMapping>(); sharepointLocator.SiteTypeMappings = typeMappings; var target = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory var farmLocator = sharepointLocator.GetCurrent() as ActivatingServiceLocator; //Act farmLocator.RegisterTypeMapping <ILogger, SBaseLogger>(); var targetLogger = target.GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(targetLogger, typeof(SharePointLogger)); }
public void GetCurrent_SiteUsesCachedInstance() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; BSPFarm.SetLocal(); var expectedSite = new BSPSite { ID = TestsConstants.TestGuid }; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = new List <TypeMapping>(); sharepointLocator.SiteTypeMappings = typeMappings; sharepointLocator.SiteLastUpdatedRetVal = DateTime.Now; var target = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory Assert.IsTrue(sharepointLocator.SiteGotTypeMappingsFromConfig); //getting from config and populating cache sharepointLocator.SiteGotTypeMappingsFromConfig = false; // reset getting config... //Act sharepointLocator.GetCurrent(expectedSite); //Assert Assert.IsFalse(sharepointLocator.SiteGotTypeMappingsFromConfig); }
public void GetCurrent_LoadsSiteTypeMappings() { //Arrange var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; var siteTypeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SBaseLogger>() }; BSPFarm.SetLocal(); var expectedSite = new BSPSite(); expectedSite.ID = TestsConstants.TestGuid; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; sharepointLocator.SiteTypeMappings = siteTypeMappings; //Act var target = sharepointLocator.GetCurrent(expectedSite); var targetLogger = target.GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger)); }
public void GetCurrent_SiteTimeoutReloadDoesntDropProgrammaticOverride() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; BSPFarm.SetLocal(); var expectedSite = new BSPSite { ID = TestsConstants.TestGuid }; var time = new DateTime(2010, 5, 30); var sharepointLocator = new TestableSharePointServiceLocator { FarmTypeMappings = new List <TypeMapping>(), SiteTypeMappings = typeMappings }; MDateTime.NowGet = () => time; var target = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory time = time.AddSeconds(SharePointServiceLocator.SiteCachingTimeoutInSeconds + 1); sharepointLocator.SiteLastUpdatedRetVal = time; //Act var initialLogger = target.GetInstance <ILogger>(); ((ActivatingServiceLocator)target).RegisterTypeMapping <ILogger, SBaseLogger>(); var targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(initialLogger, typeof(SharePointLogger)); Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger)); }
public void GetCurrent_WithContext_UsesSiteMappings() { //Arrange BSPFarm.SetLocal(); var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; var siteTypeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SBaseLogger>() }; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; sharepointLocator.SiteTypeMappings = siteTypeMappings; var site = new MSPSite() { IDGet = () => TestsConstants.TestGuid, }; var context = new MSPContext { SiteGet = () => site, }; MSPContext.CurrentGet = () => context; //Act var target = sharepointLocator.GetCurrent().GetInstance <ILogger>(); // Assert Assert.IsInstanceOfType(target, typeof(SBaseLogger)); }
public void CanRegisterSingleton() { ActivatingServiceLocator locator = new ActivatingServiceLocator(); locator.RegisterTypeMapping(TypeMapping.Create <IMyObject, MyObject>(InstantiationType.AsSingleton)); Assert.AreSame(locator.GetInstance <IMyObject>(), locator.GetInstance <IMyObject>()); }
public virtual void RegisterTypeMapping <TFrom, TTo>(string key, InstantiationType instantiationType) where TTo : TFrom { var typeMappings = GetConfigData(); var newTypeMapping = TypeMapping.Create <TFrom, TTo>(key, instantiationType); RemovePreviousMappingsForFromType(typeMappings, newTypeMapping); typeMappings.Add(newTypeMapping); SetTypeMappingsList(typeMappings); }
public void CanRegisterDifferentServiceLocator() { var typeMappings = new List <TypeMapping> { TypeMapping.Create <IServiceLocatorFactory, MockServiceLocatorFactory>() }; SetupConfigToReturnTypeMappings(typeMappings); Assert.IsInstanceOfType(SharePointServiceLocator.Current, typeof(MockServiceLocator)); }
public void CanOverrideDefaultTypemapping() { var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, MockLogger>() }; SetupConfigToReturnTypeMappings(typeMappings); Assert.IsInstanceOfType(SharePointServiceLocator.Current.GetInstance <ILogger>(), typeof(MockLogger)); }
public void Create1_Test() { TypeMapping ExpectedMapping = new TypeMapping(typeof(IInterface1), typeof(FirstClass1), null); TypeMapping ActualMapping = TypeMapping.Create <IInterface1, FirstClass1>(); Assert.AreEqual(ExpectedMapping.FromAssembly, ActualMapping.FromAssembly); Assert.AreEqual(ExpectedMapping.FromType, ActualMapping.FromType); Assert.AreEqual(ExpectedMapping.ToAssembly, ActualMapping.ToAssembly); Assert.AreEqual(ExpectedMapping.ToType, ActualMapping.ToType); Assert.AreEqual(ExpectedMapping.Key, ActualMapping.Key, "Test Case Failed: Key Mismatch. Key should be null"); }
public void Create3_Test() { string strKey = "Key"; TypeMapping ExpectedMapping = new TypeMapping(typeof(IInterface1), typeof(FirstClass1), strKey); TypeMapping ActualMapping = TypeMapping.Create <IInterface1, FirstClass1>(strKey); Assert.AreEqual(ExpectedMapping.FromAssembly, ActualMapping.FromAssembly); Assert.AreEqual(ExpectedMapping.FromType, ActualMapping.FromType); Assert.AreEqual(ExpectedMapping.ToAssembly, ActualMapping.ToAssembly); Assert.AreEqual(ExpectedMapping.ToType, ActualMapping.ToType); Assert.AreEqual(ExpectedMapping.Key, ActualMapping.Key, "Test Case Failed: Key Mismatch."); }
public void CanLoadTypeMappings() { var target = new ActivatingServiceLocatorFactory(); List <TypeMapping> typeMappings = new List <TypeMapping>(); typeMappings.Add(TypeMapping.Create <ISomething, Something>()); var serviceLocator = new ActivatingServiceLocator(); target.LoadTypeMappings(serviceLocator, typeMappings); Assert.IsTrue(serviceLocator.IsTypeRegistered <ISomething>()); }
public void ResolveAllRetrievesSingletons() { ActivatingServiceLocator locator = new ActivatingServiceLocator(); locator.RegisterTypeMapping(TypeMapping.Create <IMyObject, MyObject>("key1", InstantiationType.AsSingleton)); locator.RegisterTypeMapping(TypeMapping.Create <IMyObject, MyObject2>("key2", InstantiationType.AsSingleton)); Assert.AreSame(locator.GetInstance <IMyObject>("key1"), locator.GetInstance <IMyObject>("key1")); var all1 = locator.GetAllInstances <IMyObject>(); var all2 = locator.GetAllInstances <IMyObject>(); Assert.AreEqual(all1.First(), all2.First()); Assert.AreEqual(all1.Skip(1).First(), all2.Skip(1).First()); }
public void ServiceLocatorIsLoadedWithTypes() { var typeMappings = new List <TypeMapping> { TypeMapping.Create <ISomething, Something>("key") }; SetupConfigToReturnTypeMappings(typeMappings); Assert.IsInstanceOfType(SharePointServiceLocator.Current, typeof(ActivatingServiceLocator)); ActivatingServiceLocator target = SharePointServiceLocator.Current as ActivatingServiceLocator; Assert.IsTrue(target.IsTypeRegistered <ISomething>()); }
public void GetCurrent_ServiceLocatorIsLoadedWithTypes() { //Arrange var typeMappings = new List <TypeMapping> { TypeMapping.Create <ISomething, Something>("key") }; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; BSPFarm.SetLocal(); //Act var target = sharepointLocator.GetCurrent() as ActivatingServiceLocator; //Assert Assert.IsInstanceOfType(SharePointServiceLocator.GetCurrent(), typeof(ActivatingServiceLocator)); Assert.IsTrue(target.IsTypeRegistered <ISomething>()); }
public void GetCurrent_CanOverrideDefaultTypemapping() { //Arrange var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SBaseLogger>() }; BSPFarm.SetLocal(); var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; //Act var target = sharepointLocator.GetCurrent().GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(target, typeof(SBaseLogger)); }
public void GetCurrent_CanRegisterDifferentServiceLocator() { //Arrange var typeMappings = new List <TypeMapping> { TypeMapping.Create <IServiceLocatorFactory, BIServiceLocatorFactory>() }; BSPFarm.SetLocal(); var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; //Act IServiceLocator target = sharepointLocator.GetCurrent(); //Assert Assert.IsInstanceOfType(target, typeof(SIServiceLocator)); }
public void GetCurrent_LoadsFarmTypeMappings() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; BSPFarm.SetLocal(); var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; //Act var target = sharepointLocator.GetCurrent(); var targetLogger = target.GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(targetLogger, typeof(SharePointLogger)); }
public void GetCurrent_WithoutContext_UsesFarmMappings() { //Arrange BSPFarm.SetLocal(); var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; var siteTypeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SBaseLogger>() }; var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = typeMappings; sharepointLocator.SiteTypeMappings = siteTypeMappings; //Act var target = sharepointLocator.GetCurrent().GetInstance <ILogger>(); // Assert Assert.IsInstanceOfType(target, typeof(SharePointLogger)); }
public void GetCurrent_SiteTimeoutReloadCachesInstanceAfterReload() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; var f = new BSPConfiguredFarm(); var expectedSite = new BSPSite { ID = TestsConstants.TestGuid }; var time = new DateTime(2010, 5, 30); var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = new List <TypeMapping>(); sharepointLocator.SiteTypeMappings = typeMappings; MDateTime.NowGet = () => time; sharepointLocator.SiteGotTypeMappingsFromConfig = false; sharepointLocator.GetCurrent(expectedSite); Assert.IsTrue(sharepointLocator.SiteGotTypeMappingsFromConfig); sharepointLocator.SiteGotTypeMappingsFromConfig = false; time = time.AddSeconds(SharePointServiceLocator.SiteCachingTimeoutInSeconds + 1); sharepointLocator.SiteLastUpdatedRetVal = time; var targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>(); //Act sharepointLocator.SiteGotTypeMappingsFromConfig = false; targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>(); //Assert Assert.IsFalse(sharepointLocator.SiteGotTypeMappingsFromConfig); }
public void GetCurrent_SiteTimeoutReloadWorks() { //Setup var typeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SharePointLogger>() }; var f = new BSPConfiguredFarm(); var expectedSite = new BSPSite { ID = TestsConstants.TestGuid }; var time = new DateTime(2010, 5, 30); var sharepointLocator = new TestableSharePointServiceLocator(); sharepointLocator.FarmTypeMappings = new List <TypeMapping>(); sharepointLocator.SiteTypeMappings = typeMappings; MDateTime.NowGet = () => time; var target = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory time = time.AddSeconds(SharePointServiceLocator.SiteCachingTimeoutInSeconds + 1); sharepointLocator.SiteLastUpdatedRetVal = time; //Act var initialLogger = target.GetInstance <ILogger>(); sharepointLocator.SiteTypeMappings = new List <TypeMapping> { TypeMapping.Create <ILogger, SBaseLogger>() }; var targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>(); //Assert Assert.IsInstanceOfType(initialLogger, typeof(SharePointLogger)); Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger)); }