public void IniConfiguration_Does_Not_Throw_On_Optional_Configuration()
        {
            var configSource = new IniFileConfigurationSource("NotExistingConfig.ini", optional: true);

            configSource.Load();
            Assert.Throws <InvalidOperationException>(() => configSource.Get("key"));
        }
        public void LoadMethodCanHandleEmptyValue()
        {
            var ini          = @"DefaultKey=";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal(string.Empty, iniConfigSrc.Get("DefaultKey"));
        }
        public void DoubleQuoteIsPartOfValueIfNotPaired()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=\"TestConnectionString\n" +
                      "Provider=SqlClient\"";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("\"TestConnectionString", iniConfigSrc.Get("ConnectionString:DefaultConnection"));
            Assert.Equal("SqlClient\"", iniConfigSrc.Get("ConnectionString:Provider"));
        }
        public void ThrowExceptionWhenFoundInvalidLine()
        {
            var ini          = @"
ConnectionString
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            var expectedMsg  = Resources.FormatError_UnrecognizedLineFormat("ConnectionString");

            var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void DoubleQuoteIsPartOfValueIfNotPaired()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=\"TestConnectionString\n" +
                      "Provider=SqlClient\"";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("\"TestConnectionString", iniConfigSrc.Get("ConnectionString:DefaultConnection"));
            Assert.Equal("SqlClient\"", iniConfigSrc.Get("ConnectionString:Provider"));
        }
        public void DoubleQuoteIsPartOfValueIfAppearInTheMiddleOfValue()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=Test\"Connection\"String\n" +
                      "Provider=Sql\"Client";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("Test\"Connection\"String", iniConfigSrc.Get("ConnectionString:DefaultConnection"));
            Assert.Equal("Sql\"Client", iniConfigSrc.Get("ConnectionString:Provider"));
        }
        public void DoubleQuoteIsPartOfValueIfAppearInTheMiddleOfValue()
        {
            var ini = "[ConnectionString]\n" +
                      "DefaultConnection=Test\"Connection\"String\n" +
                      "Provider=Sql\"Client";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("Test\"Connection\"String", iniConfigSrc.Get("ConnectionString:DefaultConnection"));
            Assert.Equal("Sql\"Client", iniConfigSrc.Get("ConnectionString:Provider"));
        }
 public void IniConfiguration_Throws_On_Missing_Configuration_File()
 {
     var configSource = new IniFileConfigurationSource("NotExistingConfig.ini", optional: false);
     Assert.Throws<FileNotFoundException>(() =>
     {
         try
         {
             configSource.Load();
         }
         catch (FileNotFoundException exception)
         {
             Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.ini"), exception.Message);
             throw;
         }
     });
 }
Esempio n. 9
0
        public void Load_DontThrowForDuplicateKeyErrors()
        {
            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Error,
                ParsingErrorBehavior = ParsingResultsErrorBehavior.None,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false,
            };

            var provider = new IniFileConfigurationProvider(src);

            provider.Load();    // should not throw anything
            provider.GetAllKeysAndValuesFromProvider().Should().BeEmpty("When an error occured, parsed details must not be returned.");
        }
        public void ThrowExceptionWhenKeyIsDuplicated()
        {
            var ini          = @"
            [Data:DefaultConnection]
            ConnectionString=TestConnectionString
            Provider=SqlClient
            [Data]
            DefaultConnection:ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            var expectedMsg  = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString");

            var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
Esempio n. 11
0
        public void Load_ThrowOnFirstErrorForDuplicateKeys()
        {
            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Error,
                ParsingErrorBehavior = ParsingResultsErrorBehavior.OnFirstError,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false,
            };

            var provider = new IniFileConfigurationProvider(src);

            provider.Invoking(p => p.Load()).Should().ThrowExactly <ArgumentAlreadyDefinedException>(
                "Redefining a key with provider settings {DuplicateKeyBehavior = KeyNameBehavior.Error, ParsingErrorBehavior = ParsingResultsErrorBehavior.ExceptionOnFirstError} must throw an exception for the first error.");
            provider.GetAllKeysAndValuesFromProvider().Should().BeEmpty("When an error occured, parsed details must not be returned.");
        }
        public void LoadKeyValuePairsFromValidIniFile()
        {
            var ini          = @"[DefaultConnection]
ConnectionString=TestConnectionString
Provider=SqlClient
[Data:Inventory]
ConnectionString=AnotherTestConnectionString
SubHeader:Provider=MySql";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("defaultconnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DEFAULTCONNECTION:PROVIDER"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:CONNECTIONSTRING"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:SubHeader:Provider"));
        }
Esempio n. 13
0
        public void Load_UsingLastAssignmentForDuplicateKeys()
        {
            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Update,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false
            };

            var provider = new IniFileConfigurationProvider(src);

            provider.Load();
            var allKeysAndValues = provider.GetAllKeysAndValuesFromProvider();

            allKeysAndValues.Should().Equal(IniFileOneContents_UsingUpdate, "Keys and values read from ini-file should represent expected values WITH update for duplicate keys.");
        }
        public void LoadKeyValuePairsFromValidIniFileWithQuotedValues()
        {
            var ini = "[DefaultConnection]\n" +
                      "ConnectionString=\"TestConnectionString\"\n" +
                      "Provider=\"SqlClient\"\n" +
                      "[Data:Inventory]\n" +
                      "ConnectionString=\"AnotherTestConnectionString\"\n" +
                      "Provider=\"MySql\"";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }
        public void LoadKeyValuePairsFromValidIniFile()
        {
            var ini = @"[DefaultConnection]
            ConnectionString=TestConnectionString
            Provider=SqlClient
            [Data:Inventory]
            ConnectionString=AnotherTestConnectionString
            SubHeader:Provider=MySql";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("defaultconnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DEFAULTCONNECTION:PROVIDER"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:CONNECTIONSTRING"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:SubHeader:Provider"));
        }
        public void IniConfiguration_Throws_On_Missing_Configuration_File()
        {
            var configSource = new IniFileConfigurationSource("NotExistingConfig.ini", optional: false);

            Assert.Throws <FileNotFoundException>(() =>
            {
                try
                {
                    configSource.Load();
                }
                catch (FileNotFoundException exception)
                {
                    Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.ini"), exception.Message);
                    throw;
                }
            });
        }
        public void LoadKeyValuePairsFromValidIniFileWithoutSectionHeader()
        {
            var ini          = @"
            DefaultConnection:ConnectionString=TestConnectionString
            DefaultConnection:Provider=SqlClient
            Data:Inventory:ConnectionString=AnotherTestConnectionString
            Data:Inventory:Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }
Esempio n. 18
0
        public void Load_ThrowForAllDuplicateKeyErrors()
        {
            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Error,
                ParsingErrorBehavior = ParsingResultsErrorBehavior.Aggregate,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false,
            };

            var provider = new IniFileConfigurationProvider(src);

            provider.Invoking(p => p.Load()).Should().ThrowExactly <AggregateException>(
                "Redefining a key with provider settings {DuplicateKeyBehavior = KeyNameBehavior.Error, ParsingErrorBehavior = ParsingResultsErrorBehavior.ExceptionForAllErrors} must throw an exception for the first error.")
            .And.InnerExceptions.Should().AllBeOfType <ArgumentAlreadyDefinedException>("Only errors for duplicate keys are expected.").And.HaveCount(2, "Exactly two errors for duplicate keys was expected.");
            provider.GetAllKeysAndValuesFromProvider().Should().BeEmpty("When an error occured, parsed details must not be returned.");
        }
 public void IniConfiguration_Throws_On_Missing_Configuration_File()
 {
     var configSource = new IniFileConfigurationSource("NotExistingConfig.ini", optional: false);
     Assert.Throws<FileNotFoundException>(() =>
     {
         try
         {
             configSource.Load();
         }
         catch (FileNotFoundException exception)
         {
             Assert.Equal(
                 string.Format(Resources.Error_FileNotFound,
                 Path.Combine(Directory.GetCurrentDirectory(), "NotExistingConfig.ini")),
                 exception.Message);
             throw;
         }
     });
 }
Esempio n. 20
0
        public void Load_DontManuallyHandleDuplicateKeyErrors()
        {
            IEnumerable <Exception>           errors   = null;
            Action <IEnumerable <Exception> > function = x => errors = x.ToList();

            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Error,
                ParsingErrorBehavior = ParsingResultsErrorBehavior.None,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false,
            }.WithErrorHandlingFunction(function);

            var provider = new IniFileConfigurationProvider(src);

            provider.Load();    // should not throw anything
            provider.GetAllKeysAndValuesFromProvider().Should().BeEmpty("When an error occured, parsed details must not be returned.");
            errors.Should().BeNull("Since error handling was disbled the error variable was expected to be NULL.");
        }
Esempio n. 21
0
        public void Load_ManuallyHandleAllDuplicateKeyErrors()
        {
            IEnumerable <Exception>           errors   = null;
            Action <IEnumerable <Exception> > function = x => errors = x.ToList();

            var src = new IniFileConfigurationSource(IniFileOneName)
            {
                DuplicateKeyBehavior = KeyNameBehavior.Error,
                ParsingErrorBehavior = ParsingResultsErrorBehavior.Aggregate,
                FileProvider         = new PhysicalFileProvider(AppContext.BaseDirectory),
                Optional             = false,
                ReloadOnChange       = false,
            }.WithErrorHandlingFunction(function);

            var provider = new IniFileConfigurationProvider(src);

            provider.Load();    // should not throw anything
            provider.GetAllKeysAndValuesFromProvider().Should().BeEmpty("When an error occured, parsed details must not be returned.");
            errors.Should()
            .HaveCount(2, "Exactly two errors for duplicate keys were expected.")
            .And.AllBeOfType <ArgumentAlreadyDefinedException>("Only errors for duplicate keys are expected.");
        }
        public void SupportAndIgnoreComments()
        {
            var ini          = @"
            ; Comments
            [DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient
            [Data:Inventory]
            ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }
        public void ThrowExceptionWhenFoundBrokenSectionHeader()
        {
            var ini = @"
            [ConnectionString
            DefaultConnection=TestConnectionString
            ";
            var iniConfigSrc = new IniFileConfigurationSource(ArbitraryFilePath);
            var expectedMsg = Resources.FormatError_UnrecognizedLineFormat("[ConnectionString");

            var exception = Assert.Throws<FormatException>(() => iniConfigSrc.Load(StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void LoadKeyValuePairsFromValidIniFileWithQuotedValues()
        {
            var ini = "[DefaultConnection]\n" +
                      "ConnectionString=\"TestConnectionString\"\n" +
                      "Provider=\"SqlClient\"\n" +
                      "[Data:Inventory]\n" +
                      "ConnectionString=\"AnotherTestConnectionString\"\n" +
                      "Provider=\"MySql\"";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }
        public void LoadMethodCanHandleEmptyValue()
        {
            var ini = @"DefaultKey=";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal(string.Empty, iniConfigSrc.Get("DefaultKey"));
        }
        public void SupportAndIgnoreComments()
        {
            var ini = @"
            ; Comments
            [DefaultConnection]
            # Comments
            ConnectionString=TestConnectionString
            / Comments
            Provider=SqlClient
            [Data:Inventory]
            ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }
        public void ThrowExceptionWhenFoundInvalidLine()
        {
            var ini = @"
            ConnectionString
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            var expectedMsg = Resources.FormatError_UnrecognizedLineFormat("ConnectionString");

            var exception = Assert.Throws<FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void ThrowExceptionWhenKeyIsDuplicated()
        {
            var ini = @"
            [Data:DefaultConnection]
            ConnectionString=TestConnectionString
            Provider=SqlClient
            [Data]
            DefaultConnection:ConnectionString=AnotherTestConnectionString
            Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            var expectedMsg = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString");

            var exception = Assert.Throws<FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)));

            Assert.Equal(expectedMsg, exception.Message);
        }
 public void IniConfiguration_Does_Not_Throw_On_Optional_Configuration()
 {
     var configSource = new IniFileConfigurationSource("NotExistingConfig.ini", optional: true);
     configSource.Load();
     Assert.Throws<InvalidOperationException>(() => configSource.Get("key"));
 }
        public void LoadKeyValuePairsFromValidIniFileWithoutSectionHeader()
        {
            var ini = @"
            DefaultConnection:ConnectionString=TestConnectionString
            DefaultConnection:Provider=SqlClient
            Data:Inventory:ConnectionString=AnotherTestConnectionString
            Data:Inventory:Provider=MySql
            ";
            var iniConfigSrc = new IniFileConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini));

            Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString"));
            Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString"));
            Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider"));
        }