Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void Providers_UninitializedInstance_ThrowsException()
        {
            // Arrange
            var providerUnderTest = new CompositeLoggingProvider();

            // Act
            var providers = providerUnderTest.Providers;
        }
Ejemplo n.º 3
0
        public void Initialization_NoProvidersConfigured_ThrowsException()
        {
            // Arrange
            var providerUnderTest    = new CompositeLoggingProvider();
            var invalidConfiguration = new NameValueCollection();

            // Act
            providerUnderTest.Initialize("Valid provider name", invalidConfiguration);
        }
Ejemplo n.º 4
0
        public void Initialize_WithNullConfiguration_ThrowsException()
        {
            // Arrange
            var provider = new CompositeLoggingProvider();
            NameValueCollection invalidConfiguration = null;

            // Act
            provider.Initialize("Valid provider name", invalidConfiguration);
        }
Ejemplo n.º 5
0
        public void Initialize_WithValidConfiguration_Succeeds()
        {
            // Arrange
            var provider           = new CompositeLoggingProvider();
            var validConfiguration = CreateValidConfiguration("OtherProvider");

            // Act
            provider.Initialize("Valid provider name", validConfiguration);
        }
Ejemplo n.º 6
0
        public void Initialization_WithInvalidAttribute_ThrowsException()
        {
            // Arrange
            var providerUnderTest    = new CompositeLoggingProvider();
            var invalidConfiguration = new NameValueCollection();

            invalidConfiguration["_provider1"] = "MemoryProvider";

            // Act
            providerUnderTest.Initialize("Valid provider name", invalidConfiguration);
        }
Ejemplo n.º 7
0
        public void Initialize_ConfigurationWithoutDescription_SetsDefaultDescription()
        {
            // Arrange
            var expectedDescription = "Composite logging provider";
            var provider            = new CompositeLoggingProvider();
            var validConfiguration  = CreateValidConfiguration("Referenced Provider");

            // Act
            provider.Initialize("Valid provider name", validConfiguration);

            // Assert
            Assert.AreEqual(expectedDescription, provider.Description);
        }
Ejemplo n.º 8
0
        public void Log_OnPartiallyInitializedProvider_Fails()
        {
            // Arrange
            var provider           = new CompositeLoggingProvider();
            var validConfiguration = CreateValidConfiguration("Referenced Provider");

            provider.Initialize("Valid provider name", validConfiguration);

            // Act
            // The Composite logging provider is the only provider that can not be initialized by hand by
            // calling the Initialize() method. It needs to be either initialized by configuring it in the
            // application configuration file, or by instantiating it with an overloaded constructor.
            provider.Log("Some message");
        }
Ejemplo n.º 9
0
        public void Initialize_ConfigurationWithCustomDescription_SetsSpecifiedDescription()
        {
            // Arrange
            var expectedDescription = "My forwarder";
            var provider            = new CompositeLoggingProvider();
            var validConfiguration  = CreateValidConfiguration("Referenced Provider");

            validConfiguration["description"] = expectedDescription;

            // Act
            provider.Initialize("Valid provider name", validConfiguration);

            // Assert
            Assert.AreEqual(expectedDescription, provider.Description);
        }
Ejemplo n.º 10
0
        public void Log_WithProviderInitializedWithCustomConstructor_LogsToAllSuppliedProviders()
        {
            // Arrange
            var provider1 = new MemoryLoggingProvider();
            var provider2 = new MemoryLoggingProvider();

            var provider =
                new CompositeLoggingProvider(LoggingEventType.Debug, null, provider1, provider2);

            // Act
            provider.Log("Test");

            // Assert
            Assert.AreEqual(1, provider1.GetLoggedEntries().Count(), "CompositeLoggingProvider did not log.");
            Assert.AreEqual(1, provider2.GetLoggedEntries().Count(), "CompositeLoggingProvider did not log.");
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
        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);
            }
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        public void CompleteInitialization_SameProviderNameSpelledTwice_ThrowsExceptoin()
        {
            // Arrange
            const string ProviderName           = "Valid provider name";
            const string ReferencedProviderName = "MemoryProvider";
            var          defaultProvider        = CreateInitializedMemoryLogger(ReferencedProviderName);
            var          providerUnderTest      = new CompositeLoggingProvider();
            var          validConfiguration     = new NameValueCollection();

            validConfiguration["provider1"] = ReferencedProviderName;
            validConfiguration["provider2"] = ReferencedProviderName;

            try
            {
                // Act
                providerUnderTest.Initialize(ProviderName, validConfiguration);

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ProviderException ex)
            {
                string msg = ex.Message ?? string.Empty;

                Assert.IsTrue(msg.Contains("references provider") && msg.Contains("multiple times"),
                              "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(ReferencedProviderName),
                              "Exception message should describe the name of the referenced provider. Actual: " + msg);

                Assert.IsTrue(msg.Contains("A provider should only be referenced once"),
                              "Exception message should describe how to solve the problem. Actual: " + msg);
            }
        }
Ejemplo n.º 16
0
        public void Log_UnitializedProvider_ShouldFail()
        {
            // Arrange
            var provider = new CompositeLoggingProvider();

            try
            {
                // Act
                provider.Log("Some message");

                // Assert
                Assert.Fail("Exception expected");
            }
            catch (InvalidOperationException ex)
            {
                Assert.IsTrue(ex.Message.Contains("The provider has not been initialized"),
                              "A provider that hasn't been initialized correctly, should throw a descriptive " +
                              "exception. Actual: " + ex.Message + Environment.NewLine + ex.StackTrace);

                Assert.IsTrue(ex.Message.Contains("CompositeLoggingProvider"),
                              "The message should contain the type name of the unitialized provider. Actual: " +
                              ex.Message);
            }
        }