public void CanRegisterAndResolveTypeMapping()
        {
            var target = new ActivatingServiceLocator();

            target.RegisterTypeMapping <IMyObject, MyObject>();

            var result = target.GetInstance <IMyObject>();

            Assert.IsInstanceOfType(result, typeof(MyObject));
        }
        public void GetWithoutKeyThrows()
        {
            var target = new ActivatingServiceLocator();

            target.RegisterTypeMapping <IMyObject, MyObject>("key1");

            var result = target.GetInstance <IMyObject>();

            Assert.IsInstanceOfType(result, typeof(MyObject));
        }
        public void CanRegisterWithValidTypes()
        {
            ActivatingServiceLocator locator = new ActivatingServiceLocator();

            locator.RegisterTypeMapping(typeof(IMyObject2), typeof(MyObject3));

            var instance = locator.GetInstance <IMyObject2>();

            Assert.IsInstanceOfType(instance, typeof(MyObject3));
        }
Ejemplo n.º 4
0
        public void IsTypeRegistered_Test1()
        {
            TypeMapping TMapping = new TypeMapping(typeof(IInterface1), typeof(FirstClass1), "Key1");

            ActivatingServiceLocator ASLocator = new ActivatingServiceLocator();

            ASLocator.RegisterTypeMapping(TMapping);

            Assert.IsTrue(ASLocator.IsTypeRegistered <IInterface1>(), "IsTypeRegistered returned false for a valid TypeMapping Registration. Should be verified");
        }
        public void CanRegisterMultipleTypes()
        {
            var target = new ActivatingServiceLocator();

            target.RegisterTypeMapping <IMyObject, MyObject>("key1");
            target.RegisterTypeMapping <IMyObject, MyObject2>("key2");

            var result = target.GetInstance <IMyObject>("key1");

            Assert.IsInstanceOfType(result, typeof(MyObject));
        }
Ejemplo n.º 6
0
        public void CanRegisterSingleton()
        {
            //Arrange
            var locator = new ActivatingServiceLocator();

            //Act
            locator.RegisterTypeMapping(TypeMapping.Create <IMyObject, MyObject>(InstantiationType.AsSingleton));

            //Assert
            Assert.AreSame(locator.GetInstance <IMyObject>(), locator.GetInstance <IMyObject>());
        }
Ejemplo n.º 7
0
        public void RegisterWithTwoInterfacesThrows()
        {
            //Arrange
            var locator = new ActivatingServiceLocator();

            //Act
            locator.RegisterTypeMapping(typeof(IMyObject), typeof(IMyObject2));


            // Assert (capture by ExpectedException)
        }
Ejemplo n.º 8
0
        public void RegisterTypeMapping_Test1()
        {
            ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
            ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(typeof(IInterface1), typeof(FirstClass1));

            Assert.IsNotNull(ASLocator2, "RegisterTypeMapping method returned null");
            Assert.AreEqual(ASLocator2.GetType().ToString(), ASLocator.GetType().ToString(), "Return type does not match ActivatingServiceLocator");
            Assert.IsTrue(ASLocator2.IsTypeRegistered <IInterface1>(), "Failed to register the type");
            object FirstClass1Object = ASLocator.GetInstance(typeof(IInterface1));

            Assert.AreEqual(FirstClass1Object.GetType().Name, "FirstClass1");
        }
        public void CanResolveAll()
        {
            var target = new ActivatingServiceLocator();

            target.RegisterTypeMapping <IMyObject, MyObject>("key1");
            target.RegisterTypeMapping <IMyObject, MyObject2>("key2");

            var result = target.GetAllInstances <IMyObject>();

            Assert.IsInstanceOfType(result.First(), typeof(MyObject));
            Assert.IsInstanceOfType(result.Skip(1).First(), typeof(MyObject2));
        }
Ejemplo n.º 10
0
        public void LastRegistrationWins()
        {
            //Arrange
            var target = new ActivatingServiceLocator();

            //Act
            target.RegisterTypeMapping <IMyObject, MyObject>();
            target.RegisterTypeMapping <IMyObject, MyObject2>();

            //Assert
            Assert.IsInstanceOfType(target.GetInstance <IMyObject>(), typeof(MyObject2));
        }
Ejemplo n.º 11
0
        public void IsTypeRegistered_WithoutRegisteredService_returnsFalse()
        {
            //Arrange
            ActivatingServiceLocator locator = new ActivatingServiceLocator();

            //Act
            locator.RegisterTypeMapping(typeof(IMyObject2), typeof(MyObject3));
            bool target = locator.IsTypeRegistered(typeof(IMyObject));

            //Assert
            Assert.IsFalse(target);
        }
Ejemplo n.º 12
0
        public void IsTypeRegistered_WithKeyAndRegisteredService_returnsTrue()
        {
            //Arrange
            ActivatingServiceLocator locator = new ActivatingServiceLocator();
            string key = "foobar";

            //Act
            locator.RegisterTypeMapping(typeof(IMyObject2), typeof(MyObject3), key, InstantiationType.NewInstanceForEachRequest);
            bool target = locator.IsTypeRegistered(typeof(IMyObject2), key);

            //Assert
            Assert.IsTrue(target);
        }
Ejemplo n.º 13
0
        public void CanResolveByKey()
        {
            //Arrange
            var target = new ActivatingServiceLocator();

            //Act
            target.RegisterTypeMapping <IMyObject, MyObject>("key1");
            target.RegisterTypeMapping <IMyObject, MyObject2>("key2");

            //Assert
            var result = target.GetInstance <IMyObject>("key2");

            Assert.IsInstanceOfType(result, typeof(MyObject2));
        }
Ejemplo n.º 14
0
        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 TraceWithInvalidCategoryThrowsLoggingException()
        {
            //Arrange
            var          replaceLocator  = new ActivatingServiceLocator();
            const string invalidCategory = TestsConstants.TestGuidName;     // should be of form area/category

            SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);
            replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton);
            replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton);
            var target = new DiagnosticsService();

            //Act
            target.LogTrace("Message", 99, TraceSeverity.Medium, invalidCategory);

            //Assert
        }
Ejemplo n.º 18
0
        public void RegisterTypeMapping_From_To_ITypeTestKey1()
        {
            ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
            ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping <IInterface1, FirstClass1>("Key1", InstantiationType.NewInstanceForEachRequest);

            Assert.IsNotNull(ASLocator2, "RegisterTypeMapping method returned null");
            Assert.AreEqual(ASLocator2.GetType().ToString(), ASLocator.GetType().ToString(), "Return type does not match ActivatingServiceLocator");
            Assert.IsTrue(ASLocator2.IsTypeRegistered <IInterface1>(), "Failed to register the type");
            object FirstClass1Object = ASLocator.GetInstance(typeof(IInterface1), "Key1");

            Assert.AreEqual(FirstClass1Object.GetType().Name, "FirstClass1");

            //Code to check for new instance. Objects should be different instances of same class, not a singleton.
            object FirstClass1Object2 = ASLocator.GetInstance(typeof(IInterface1), "Key1");

            Assert.AreNotEqual(FirstClass1Object, FirstClass1Object2);
        }
        public void LoadTypeMappings2_Test()
        {
            ActivatingServiceLocatorFactory factory = new ActivatingServiceLocatorFactory();
            IServiceLocator servicelocator          = factory.Create();

            List <TypeMapping> typeMappings = new List <TypeMapping>();

            typeMappings.Add(new TypeMapping(typeof(IInterface1), typeof(FirstClass1), null));

            factory.LoadTypeMappings(servicelocator, typeMappings);
            ActivatingServiceLocator AserviceLoc = servicelocator as ActivatingServiceLocator;

            Assert.IsTrue(AserviceLoc.IsTypeRegistered <IInterface1>(), "Failed to register the type");
            object FirstClass1Object = AserviceLoc.GetInstance(typeof(IInterface1));

            Assert.AreEqual(FirstClass1Object.GetType().Name, "FirstClass1");
        }
        public void TraceWithMissingCategoryThrowsLoggingException()
        {
            //Arrange
            var          replaceLocator  = new ActivatingServiceLocator();
            const string testArea        = "{649D6544-ED41-46D1-ABC6-72B9D94C3D4C}";
            string       missingCategory = string.Format("{0}/{1}", testArea, TestsConstants.TestGuidName); // should be of form area/category

            SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);
            replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton);
            replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton);
            var target = new DiagnosticsService();

            //Act
            target.LogTrace("Message", 99, TraceSeverity.Medium, missingCategory);

            //Assert
        }
        public void GetDefaultCategorySucceeds()
        {
            //Arrange
            var target = new DiagnosticsService();

            MSPFarm.LocalGet = () => new MSPFarm();
            var replaceLocator = new ActivatingServiceLocator();

            SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);
            replaceLocator.RegisterTypeMapping <IConfigManager, MockLoggingConfigMgr>(InstantiationType.AsSingleton);

            //Act
            SPDiagnosticsCategory defaultCat = target.DefaultCategory;

            //Assert
            Assert.IsTrue(defaultCat.Area.Name == Constants.DefaultAreaName);
            Assert.IsTrue(defaultCat.Name == Constants.DefaultCategoryName);
        }
        public void FindCategoryWithMissingCategoryReturnsNull()
        {
            //Arrange
            string testAreaGuid   = "{848D873D-EA3C-445A-874A-E80310EC50D4}";
            var    replaceLocator = new ActivatingServiceLocator();

            SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);
            replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton);
            replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton);
            var    target         = new DiagnosticsService();
            string category11Name = string.Format("{0}/{1}", testAreaGuid, TestsConstants.TestGuidName);

            //Act
            SPDiagnosticsCategory category = target.FindCategory(category11Name);

            // Assert
            Assert.IsNull(category);
        }
Ejemplo n.º 23
0
        public void RegisterTypeMapping_From_To_ITypeTestKey2()
        {
            ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
            ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping <IInterface1, FirstClass1>("Key1", InstantiationType.AsSingleton);

            Assert.IsNotNull(ASLocator2, "RegisterTypeMapping method returned null");
            Assert.AreEqual(ASLocator2.GetType().ToString(), ASLocator.GetType().ToString(), "Return type does not match ActivatingServiceLocator");
            Assert.IsTrue(ASLocator2.IsTypeRegistered <IInterface1>(), "Failed to register the type");
            object FirstClass1Object = ASLocator.GetInstance(typeof(IInterface1), "Key1");

            Assert.AreEqual(FirstClass1Object.GetType().Name, "FirstClass1");

            //Singleton instance check.
            object FirstClass1Object2 = ASLocator.GetInstance(typeof(IInterface1), "Key1");

            Assert.AreEqual(FirstClass1Object, FirstClass1Object2);

            //reregistration checking for Singleton.
            ASLocator.RegisterTypeMapping <IInterface1, FirstClass1>("Key1", InstantiationType.AsSingleton);
            object FirstClass1Object3 = ASLocator.GetInstance(typeof(IInterface1), "Key1");

            Assert.AreEqual(FirstClass1Object, FirstClass1Object3);
        }
Ejemplo n.º 24
0
        public void CreateInstanceFromTypeMappingBlankMapping_Test()
        {
            TypeMapping TMapping = new TypeMapping();

            ActivatingServiceLocator.CreateInstanceFromTypeMapping(TMapping);
        }
        public void RegisterWithNonMatchingTypeThrows()
        {
            ActivatingServiceLocator locator = new ActivatingServiceLocator();

            locator.RegisterTypeMapping(typeof(IMyObject), typeof(MyObject3));
        }
        public void RegisterWithTwoInterfacesThrows()
        {
            ActivatingServiceLocator locator = new ActivatingServiceLocator();

            locator.RegisterTypeMapping(typeof(IMyObject), typeof(IMyObject2));
        }
Ejemplo n.º 27
0
 public void CreateInstanceFromTypeMappingBlankMappingNull_Test()
 {
     ActivatingServiceLocator.CreateInstanceFromTypeMapping(null);
 }
Ejemplo n.º 28
0
 public void RegisterTypeMapping_Test3()
 {
     ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
     ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(typeof(IInterface1), typeof(SecondClass1));
 }
Ejemplo n.º 29
0
 public void RegisterTypeMapping_K_IType_Test3()
 {
     ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
     ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(typeof(IInterface1), typeof(SecondClass1)
                                                                         , "Key1", InstantiationType.NewInstanceForEachRequest);
 }
Ejemplo n.º 30
0
 public void RegisterTypeMapping4()
 {
     ActivatingServiceLocator ASLocator  = new ActivatingServiceLocator();
     ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(null);
 }