public void CompleteInitialization_WithValidConfiguration_Succeeds() { // Arrange var expectedReferencedProvider = CreateInitializedMemoryLogger("Other Provider"); var defaultProvider = CreateInitializedMemoryLogger("Default Provider"); var providerUnderTest = new CompositeLoggingProvider(); var validConfiguration = CreateValidConfiguration(expectedReferencedProvider.Name); providerUnderTest.Initialize("Valid provider name", validConfiguration); var configuredProviders = new LoggingProviderCollection() { providerUnderTest, expectedReferencedProvider, defaultProvider }; // Act providerUnderTest.CompleteInitialization(configuredProviders, defaultProvider); var actualReferencedProvider = providerUnderTest.Providers[0]; // Assert Assert.AreEqual(1, providerUnderTest.Providers.Count, "The provider is expected to reference a single provider."); Assert.IsNotNull(actualReferencedProvider, "The referenced provider should not be a null reference."); Assert.AreEqual(expectedReferencedProvider, actualReferencedProvider, "The referenced provider is not the expected provider. Actual referenced provider: " + actualReferencedProvider.Name); }
private static CompositeLoggingProvider CreateInitializedCompositeLoggingProvider( LoggingProviderCollection providers) { var provider = new CompositeLoggingProvider(); var configuration = new NameValueCollection(); foreach (var p in providers.Select((p, i) => new { Provider = p, Index = i })) { configuration["provider" + p.Index] = p.Provider.Name; } provider.Initialize("Valid provider name", configuration); provider.CompleteInitialization(providers, provider); return(provider); }
public void CompleteInitialization_WithArbitraryNumberedProviders_Succeeds() { // Arrange // List of referenced providers which names would sort them in opposite order. var firstExpectedReferencedProvider = CreateInitializedMemoryLogger("First provider"); var secondExpectedReferencedProvider = CreateInitializedMemoryLogger("Second provider"); var thirdExpectedReferencedProvider = CreateInitializedMemoryLogger("Third provider"); var defaultProvider = CreateInitializedMemoryLogger("Default Provider"); var providerUnderTest = new CompositeLoggingProvider(); var validConfiguration = new NameValueCollection(); // Configuration with provider attributes other than 1, 2 and 3. validConfiguration["provider3"] = firstExpectedReferencedProvider.Name; validConfiguration["provider143"] = thirdExpectedReferencedProvider.Name; validConfiguration["provider66"] = secondExpectedReferencedProvider.Name; providerUnderTest.Initialize("Valid provider name", validConfiguration); // List of configured providers in order var configuredProviders = new LoggingProviderCollection() { thirdExpectedReferencedProvider, defaultProvider, firstExpectedReferencedProvider, secondExpectedReferencedProvider, providerUnderTest }; // Act providerUnderTest.CompleteInitialization(configuredProviders, defaultProvider); var actualFirstReferencedProvider = providerUnderTest.Providers[0]; var actualSecondReferencedProvider = providerUnderTest.Providers[1]; var actualThirdReferencedFirstProvider = providerUnderTest.Providers[2]; // Assert Assert.IsTrue(firstExpectedReferencedProvider == actualFirstReferencedProvider, "The first provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); Assert.AreEqual(secondExpectedReferencedProvider, actualSecondReferencedProvider, "The second provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); Assert.AreEqual(thirdExpectedReferencedProvider, actualThirdReferencedFirstProvider, "The third provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); }
public void CompleteInitialization_NonExistingProviderName_ThrowsException() { // Arrange string providerName = "Valid provider name"; string nonExistingProviderName = "Non existing provider name"; var providerUnderTest = new CompositeLoggingProvider(); var validConfiguration = new NameValueCollection(); validConfiguration["provider1"] = nonExistingProviderName; providerUnderTest.Initialize(providerName, validConfiguration); // List of configured providers in order var configuredProviders = new LoggingProviderCollection() { providerUnderTest }; try { // Act providerUnderTest.CompleteInitialization(configuredProviders, providerUnderTest); // Assert Assert.Fail("Exception expected."); } catch (ProviderException ex) { string msg = ex.Message ?? string.Empty; Assert.IsTrue(msg.Contains("references a provider") && msg.Contains("that does not exist"), "Exception message should describe the problem. Actual: " + msg); Assert.IsTrue(msg.Contains("CompositeLoggingProvider"), "Exception message should describe the provider type. Actual: " + msg); Assert.IsTrue(msg.Contains(providerName), "Exception message should describe the provider name. Actual: " + msg); Assert.IsTrue(msg.Contains(nonExistingProviderName), "Exception message should describe the name of the referenced provider. Actual: " + msg); Assert.IsTrue(msg.Contains("Make sure the name is spelled correctly."), "Exception message should describe how to solve the problem. Actual: " + msg); } }
public void CompleteInitialization_WithMultipleProviders_Succeeds() { // Arrange // List of referenced providers which names would sort them in opposite order. var firstExpectedReferencedProvider = CreateInitializedMemoryLogger("Z first provider"); var secondExpectedReferencedProvider = CreateInitializedMemoryLogger("Y second provider"); var thirdExpectedReferencedProvider = CreateInitializedMemoryLogger("X third provider"); var defaultProvider = CreateInitializedMemoryLogger("Default Provider"); var providerUnderTest = new CompositeLoggingProvider(); var validConfiguration = CreateValidConfiguration( firstExpectedReferencedProvider.Name, secondExpectedReferencedProvider.Name, thirdExpectedReferencedProvider.Name); providerUnderTest.Initialize("Valid provider name", validConfiguration); // List of configured providers in order var configuredProviders = new LoggingProviderCollection() { thirdExpectedReferencedProvider, defaultProvider, firstExpectedReferencedProvider, secondExpectedReferencedProvider, providerUnderTest }; // Act providerUnderTest.CompleteInitialization(configuredProviders, defaultProvider); var actualFirstReferencedProvider = providerUnderTest.Providers[0]; var actualSecondReferencedProvider = providerUnderTest.Providers[1]; var actualThirdReferencedFirstProvider = providerUnderTest.Providers[2]; // Assert Assert.IsTrue(firstExpectedReferencedProvider == actualFirstReferencedProvider, "The first provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); Assert.AreEqual(secondExpectedReferencedProvider, actualSecondReferencedProvider, "The second provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); Assert.AreEqual(thirdExpectedReferencedProvider, actualThirdReferencedFirstProvider, "The third provider in the list is not the expected provider. Expected: {0}, Actual: {1}", firstExpectedReferencedProvider.Name, actualFirstReferencedProvider.Name); }