public void Configuration_WithNestedSectionGroup_Succeeds() { // Arrange var config = new TemplatedConfigurationWriter( @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <sectionGroup name=""cutting""> <sectionGroup name=""edge""> <section name=""logging"" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </sectionGroup> </sectionGroup> </configSections> <cutting> <edge> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <add name=""MemoryLoggingProvider"" type=""CuttingEdge.Logging.MemoryLoggingProvider"" /> </providers> </logging> </edge> </cutting> </configuration> "); using (var manager = new UnitTestAppDomainManager(config)) { // Act manager.DomainUnderTest.InitializeLoggingSystem(); } }
public void Configuration_WithNonExistingTypeInProviderType_ThrowsException() { // Arrange string xmlWithMissingProviderType = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <section name=""logging"" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </configSections> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <!-- next line has invalid 'type' attribute --> <add name=""MemoryLoggingProvider"" type=""NonExistingType, CuttingEdge.Logging"" /> </providers> </logging> </configuration>"; var config = new TemplatedConfigurationWriter(xmlWithMissingProviderType); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ConfigurationErrorsException ex) { Assert.IsTrue(ex.Message.Contains("MemoryLoggingProvider"), "The exception message should contain the name of the incorrect provider. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains("NonExistingType, CuttingEdge.Logging"), "The exception message should contain the incorrect type. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains("could not be resolved"), "The exception message should explain the reason for the error. " + "Actual message: " + ex.Message); Assert.IsNotNull(ex.InnerException, "The exception is expected to have an inner exception"); Assert.IsInstanceOfType(ex.InnerException, typeof(TypeLoadException), "The inner exception is expected to be a TypeLoadException."); } } }
public void Configuration_WithNonExistingFallbackProvider_ThrowsException() { string nonExistingProviderName = "NonExistingProviderName"; // Arrange string xmlWithMissingProviderType = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <section name=""logging"" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </configSections> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <!-- next line has invalid 'type' attribute --> <add name=""MemoryLoggingProvider"" type=""CuttingEdge.Logging.MemoryLoggingProvider"" fallbackProvider=""" + nonExistingProviderName + @""" /> </providers> </logging> </configuration>"; var config = new TemplatedConfigurationWriter(xmlWithMissingProviderType); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ProviderException ex) { Assert.IsTrue(ex.Message.Contains("MemoryLoggingProvider"), "The exception message should contain the name of the incorrect provider. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains(nonExistingProviderName), "The exception message should contain the incorrect fallback provider. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains("No provider with that name could be found."), "The exception message should explain the reason for the error. " + "Actual message: " + ex.Message); } } }
public void Configuration_WithProviderTypeThatDoesNotInheritFromLoggingProviderBase_ThrowsException() { Type invalidType = typeof(SqlMembershipProvider); // Arrange string xmlWithMissingProviderType = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <section name=""logging"" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </configSections> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <!-- next line has invalid 'type' attribute --> <add name=""MemoryLoggingProvider"" type=""" + invalidType.AssemblyQualifiedName + @""" /> </providers> </logging> </configuration>"; var config = new TemplatedConfigurationWriter(xmlWithMissingProviderType); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ConfigurationErrorsException ex) { Assert.IsTrue(ex.Message.Contains("MemoryLoggingProvider"), "The exception message should contain the name of the incorrect provider. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains(invalidType.Name), "The exception message should contain the incorrect type. " + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains( "does not inherit from type CuttingEdge.Logging.LoggingProviderBase"), "The exception message should explain the reason for the error. " + "Actual message: " + ex.Message); } } }
public void Configuration_WithInvalidProviderType_ThrowsException() { // Arrange string xmlWithMissingProviderType = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <section name=""logging"" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </configSections> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <!-- next line misses the type --> <add name=""MemoryLoggingProvider"" type="" "" /> </providers> </logging> </configuration>"; var config = new TemplatedConfigurationWriter(xmlWithMissingProviderType); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ConfigurationErrorsException ex) { Assert.IsTrue(ex.Message.Contains("MemoryLoggingProvider"), "The exception message should contain the name of the incorrect provider." + "Actual message: " + ex.Message); Assert.IsTrue(ex.Message.Contains("no type attribute"), "Actual message: " + ex.Message); } } }
public void Configuration_WithAlternativeSectionNameWithoutExpectedName_ThrowsExceptionWithExpectedSectionName() { // Arrange const string AlternativeSectionName = "cuttingEdge_logging"; var config = new TemplatedConfigurationWriter( @"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <configSections> <section name=""" + AlternativeSectionName + @""" type=""CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"" /> </configSections> <logging defaultProvider=""MemoryLoggingProvider""> <providers> <add name=""MemoryLoggingProvider"" type=""CuttingEdge.Logging.MemoryLoggingProvider, CuttingEdge.Logging"" /> </providers> </logging> </configuration> "); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ConfigurationErrorsException ex) { Assert.IsTrue(ex.Message.Contains("<cuttingEdge_logging>"), "Actual message: " + ex.Message); Assert.IsTrue(!ex.Message.Contains("<logging"), "Actual message: " + ex.Message); Assert.IsTrue(!ex.Message.Contains("</logging"), "Actual message: " + ex.Message); } } }
public void Configuration_MissingConfigSectionsElementInConfigFile_ThrowsException() { // Arrange string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?><configuration></configuration>"; var config = new TemplatedConfigurationWriter(xml); using (var manager = new UnitTestAppDomainManager(config)) { try { // Act manager.DomainUnderTest.InitializeLoggingSystem(); // Assert Assert.Fail("Exception expected."); } catch (ProviderException ex) { Assert.AreEqual(MissingConfigSectionsElementExceptionMessage, ex.Message); } } }