public void LogEventWithEmptyStringDefaultCategorySucceeds() { //Arrange var replaceLocator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator); replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton); replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton); var service = new DiagnosticsService(); SPDiagnosticsCategory target = null; bool eventLogWritten = false; MSPDiagnosticsServiceBase.AllInstances.WriteEventUInt16SPDiagnosticsCategoryEventSeverityStringObjectArray = (diagnosticsServiceBase, ID, category, severity, str1, obj) => { eventLogWritten = true; target = category; }; //Act service.LogEvent("Message", 99, EventSeverity.Information, string.Empty); //Assert Assert.IsTrue(eventLogWritten); Assert.AreEqual <SPDiagnosticsCategory>(service.DefaultCategory, target); }
public void TraceWithNullDefaultCategorySucceeds() { //Arrange var replaceLocator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator); replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton); replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton); var service = new DiagnosticsService(); bool traceLogWritten = false; SPDiagnosticsCategory target = null; MSPDiagnosticsServiceBase.AllInstances.WriteTraceUInt32SPDiagnosticsCategoryTraceSeverityStringObjectArray = (diagnosticsServiceBase, ID, category, severity, str1, obj) => { traceLogWritten = true; target = category; }; //Act service.LogTrace("Message", 99, TraceSeverity.Verbose, null); //Assert Assert.IsTrue(traceLogWritten); Assert.AreEqual <SPDiagnosticsCategory>(service.DefaultCategory, target); }
public void FindCategoryWithValidCategorySucceeds() { //Arrange var replaceLocator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator); replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton); replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton); var target = new DiagnosticsService(); var config = SharePointServiceLocator.GetCurrent().GetInstance <IConfigManager>() as MockConfigManager; string category11Name = string.Format("{0}/{1}", MockConfigManager.Area1Name, MockConfigManager.Area1Category1Name); string category21Name = string.Format("{0}/{1}", MockConfigManager.Area2Name, MockConfigManager.Area2Category1Name); string category12Name = string.Format("{0}/{1}", MockConfigManager.Area1Name, MockConfigManager.Area1Category2Name); string category22Name = string.Format("{0}/{1}", MockConfigManager.Area2Name, MockConfigManager.Area2Category2Name); //Act SPDiagnosticsCategory category11 = target.FindCategory(category11Name); SPDiagnosticsCategory category12 = target.FindCategory(category12Name); SPDiagnosticsCategory category21 = target.FindCategory(category21Name); SPDiagnosticsCategory category22 = target.FindCategory(category22Name); // Assert Assert.AreEqual(MockConfigManager.Area1Category1Name, category11.Name); Assert.AreEqual(MockConfigManager.Area1Category2Name, category12.Name); Assert.AreEqual(MockConfigManager.Area2Category1Name, category21.Name); Assert.AreEqual(MockConfigManager.Area2Category2Name, category22.Name); Assert.AreEqual(MockConfigManager.Area1Name, category11.Area.Name); Assert.AreEqual(MockConfigManager.Area1Name, category12.Area.Name); Assert.AreEqual(MockConfigManager.Area2Name, category21.Area.Name); Assert.AreEqual(MockConfigManager.Area2Name, category22.Area.Name); Assert.IsTrue(config.LoadedCount == 1); }
public void ProvideAreasSucceeds() { //Arrange var target = new TestableProvideAreas(); var replaceLocator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator); replaceLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(InstantiationType.AsSingleton); replaceLocator.RegisterTypeMapping <IHierarchicalConfig, MockHierarchicalConfig>(InstantiationType.AsSingleton); //Act IEnumerable <SPDiagnosticsArea> result = target.TestProvideAreas(); // Assert List <SPDiagnosticsArea> list = new List <SPDiagnosticsArea>(result); Assert.IsTrue(list.Count == 3); Assert.AreEqual(MockConfigManager.Area1Name, list[0].Name); Assert.AreEqual(MockConfigManager.Area2Name, list[1].Name); Assert.AreEqual(target.DefaultCategory.Area.Name, list[2].Name); Assert.IsTrue(list[0].Categories[MockConfigManager.Area1Category1Name] != null); Assert.IsTrue(list[0].Categories[MockConfigManager.Area1Category2Name] != null); Assert.IsTrue(list[1].Categories[MockConfigManager.Area2Category1Name] != null); Assert.IsTrue(list[1].Categories[MockConfigManager.Area2Category2Name] != null); Assert.IsTrue(list[2].Categories[target.DefaultCategory.Name] != null); }
public void RegisterTypeMapping_K_IType_Test4() { ActivatingServiceLocator ASLocator = new ActivatingServiceLocator(); ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(typeof(IInterface1), typeof(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(typeof(IInterface1), typeof(FirstClass1), "Key1", InstantiationType.AsSingleton); object FirstClass1Object3 = ASLocator.GetInstance(typeof(IInterface1), "Key1"); Assert.AreEqual(FirstClass1Object, FirstClass1Object3); }
public void LastRegistrationWins() { var target = new ActivatingServiceLocator(); target.RegisterTypeMapping <IMyObject, MyObject>(); target.RegisterTypeMapping <IMyObject, MyObject2>(); Assert.IsInstanceOfType(target.GetInstance <IMyObject>(), typeof(MyObject2)); }
public void CanResolveByKey() { var target = new ActivatingServiceLocator(); target.RegisterTypeMapping <IMyObject, MyObject>("key1"); target.RegisterTypeMapping <IMyObject, MyObject2>("key2"); var result = target.GetInstance <IMyObject>("key2"); Assert.IsInstanceOfType(result, typeof(MyObject2)); }
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)); }
public void CanRegisterMultipleTypes() { //Arrange var target = new ActivatingServiceLocator(); //Act target.RegisterTypeMapping <IMyObject, MyObject>("key1"); target.RegisterTypeMapping <IMyObject, MyObject2>("key2"); var result = target.GetInstance <IMyObject>("key1"); //Assert Assert.IsInstanceOfType(result, typeof(MyObject)); }
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 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 }
public void RegisterTypeMapping3() { TypeMapping TMapping = new TypeMapping(typeof(IInterface1), typeof(SecondClass1), "Key1"); ActivatingServiceLocator ASLocator = new ActivatingServiceLocator(); ActivatingServiceLocator ASLocator2 = ASLocator.RegisterTypeMapping(TMapping); }
public void EnsureConfiguredAreasRegistered_RegistersConfiguredAreas() { //Arrange var areas = new DiagnosticsAreaCollection(); var newLocator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(newLocator); newLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>(); var sourceNames = new List <string>(); var logNames = new List <string>(); DiagnosticsArea area = new DiagnosticsArea("test"); areas.Add(area); MEventLog.SourceExistsString = (s) => false; MEventLog.CreateEventSourceStringString = (s, l) => { sourceNames.Add(s); logNames.Add(l); }; // Act DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered(); // Assert Assert.AreEqual(3, sourceNames.Count); Assert.AreEqual(MockConfigManager.Area1Name, sourceNames[0]); Assert.AreEqual(MockConfigManager.Area2Name, sourceNames[1]); Assert.AreEqual <string>(Constants.EventLogName, logNames[0]); Assert.AreEqual <string>(Constants.EventLogName, logNames[1]); Assert.AreEqual(DiagnosticsArea.DefaultSPDiagnosticsArea.Name, sourceNames[2]); Assert.AreEqual <string>(Constants.EventLogName, logNames[2]); }
public void TestInitialize() { ActivatingServiceLocator locator = new ActivatingServiceLocator(); locator.RegisterTypeMapping <ILogger, MockLogger>(InstantiationType.AsSingleton); SharePointServiceLocator.ReplaceCurrentServiceLocator(locator); }
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 CanRegisterSingleton() { ActivatingServiceLocator locator = new ActivatingServiceLocator(); locator.RegisterTypeMapping(TypeMapping.Create <IMyObject, MyObject>(InstantiationType.AsSingleton)); Assert.AreSame(locator.GetInstance <IMyObject>(), locator.GetInstance <IMyObject>()); }
public void Initialize() { var logger = new MockLogger(); var locator = new ActivatingServiceLocator(); SharePointServiceLocator.ReplaceCurrentServiceLocator(locator); locator.RegisterTypeMapping <ILogger, MockLogger>(); }
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); }
public void RegisterWithNullImplThrows() { //Arrange ActivatingServiceLocator locator = new ActivatingServiceLocator(); //Act locator.RegisterTypeMapping(null); // Assert (capture by ExpectedException) }
public void IsTypeRegistered_Test2() { TypeMapping TMapping = new TypeMapping(typeof(IInterface1), typeof(SecondClass1), "Key1"); ActivatingServiceLocator ASLocator = new ActivatingServiceLocator(); ASLocator.RegisterTypeMapping(TMapping); Assert.IsFalse(ASLocator.IsTypeRegistered <IInterface1>(), "IsTypeRegistered returned true for an invalid TypeMapping Registration input. Should be verified"); }
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 CanRegisterWithValidTypes() { ActivatingServiceLocator locator = new ActivatingServiceLocator(); locator.RegisterTypeMapping(typeof(IMyObject2), typeof(MyObject3)); var instance = locator.GetInstance <IMyObject2>(); Assert.IsInstanceOfType(instance, typeof(MyObject3)); }
public void GetWithoutKeyThrows() { var target = new ActivatingServiceLocator(); target.RegisterTypeMapping <IMyObject, MyObject>("key1"); var result = target.GetInstance <IMyObject>(); Assert.IsInstanceOfType(result, typeof(MyObject)); }
public void RegisterWithTwoInterfacesThrows() { //Arrange var locator = new ActivatingServiceLocator(); //Act locator.RegisterTypeMapping(typeof(IMyObject), typeof(IMyObject2)); // Assert (capture by ExpectedException) }
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 CanRegisterAndResolveTypeMapping() { //Arrange var target = new ActivatingServiceLocator(); //Act target.RegisterTypeMapping <IMyObject, MyObject>(); var result = target.GetInstance <IMyObject>(); //Assert Assert.IsInstanceOfType(result, typeof(MyObject)); }
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); }
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); }
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 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); }