Fix #194 : Split implementation of IConfigurationBuilder and IConfiguration

This commit is contained in:
Kirthi Krishnamraju 2015-05-21 14:19:11 -07:00
Родитель e4b714a8e4
Коммит 046a5860b7
17 изменённых файлов: 276 добавлений и 162 удалений

Просмотреть файл

@ -25,5 +25,7 @@ namespace Microsoft.Framework.Configuration
IEnumerable<KeyValuePair<string, IConfiguration>> GetConfigurationSections(string key);
void Set(string key, string value);
void Reload();
}
}

Просмотреть файл

@ -5,7 +5,7 @@ using System.Collections.Generic;
namespace Microsoft.Framework.Configuration
{
public interface IConfigurationBuilder : IConfiguration
public interface IConfigurationBuilder
{
string BasePath { get; }
@ -13,6 +13,6 @@ namespace Microsoft.Framework.Configuration
IConfigurationBuilder Add(IConfigurationSource configurationSource);
void Reload();
IConfiguration Build();
}
}

Просмотреть файл

@ -0,0 +1,76 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.Framework.Configuration
{
public class ConfigurationBuilder : IConfigurationBuilder
{
private readonly IList<IConfigurationSource> _sources = new List<IConfigurationSource>();
public ConfigurationBuilder(params IConfigurationSource[] sources)
: this(null, sources)
{
}
public ConfigurationBuilder(string basePath, params IConfigurationSource[] sources)
{
if (sources != null)
{
foreach (var singleSource in sources)
{
Add(singleSource);
}
}
BasePath = basePath;
}
public IEnumerable<IConfigurationSource> Sources
{
get
{
return _sources;
}
}
public string BasePath
{
get;
}
/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <returns>The same configuration source.</returns>
public IConfigurationBuilder Add(IConfigurationSource configurationSource)
{
return Add(configurationSource, load: true);
}
/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <param name="load">If true, the configuration source's <see cref="IConfigurationSource.Load"/> method will
/// be called.</param>
/// <returns>The same configuration source.</returns>
/// <remarks>This method is intended only for test scenarios.</remarks>
public IConfigurationBuilder Add(IConfigurationSource configurationSource, bool load)
{
if (load)
{
configurationSource.Load();
}
_sources.Add(configurationSource);
return this;
}
public IConfiguration Build()
{
return new ConfigurationSection(_sources);
}
}
}

Просмотреть файл

@ -9,26 +9,13 @@ using Microsoft.Framework.Internal;
namespace Microsoft.Framework.Configuration
{
public class ConfigurationSection : IConfiguration, IConfigurationBuilder
public class ConfigurationSection : IConfiguration
{
private readonly IList<IConfigurationSource> _sources = new List<IConfigurationSource>();
public ConfigurationSection(params IConfigurationSource[] sources)
: this(null, sources)
public ConfigurationSection(IList<IConfigurationSource> sources)
{
}
public ConfigurationSection(string basePath, params IConfigurationSource[] sources)
{
if (sources != null)
{
foreach (var singleSource in sources)
{
Add(singleSource);
}
}
BasePath = basePath;
_sources = sources;
}
public string this[string key]
@ -51,11 +38,6 @@ namespace Microsoft.Framework.Configuration
}
}
public string BasePath
{
get;
}
public string Get([NotNull] string key)
{
string value;
@ -78,7 +60,6 @@ namespace Microsoft.Framework.Configuration
return false;
}
public void Set([NotNull] string key, [NotNull] string value)
{
if (!_sources.Any())
@ -131,33 +112,5 @@ namespace Microsoft.Framework.Configuration
segment,
new ConfigurationFocus(this, prefix + segment + Constants.KeyDelimiter));
}
/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <returns>The same configuration source.</returns>
public IConfigurationBuilder Add(IConfigurationSource configurationSource)
{
return Add(configurationSource, load: true);
}
/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <param name="load">If true, the configuration source's <see cref="IConfigurationSource.Load"/> method will
/// be called.</param>
/// <returns>The same configuration source.</returns>
/// <remarks>This method is intended only for test scenarios.</remarks>
public IConfigurationBuilder Add(IConfigurationSource configurationSource, bool load)
{
if (load)
{
configurationSource.Load();
}
_sources.Add(configurationSource);
return this;
}
}
}

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace Microsoft.Framework.Configuration.Internal
@ -72,5 +73,10 @@ namespace Microsoft.Framework.Configuration.Internal
{
return _root.GetConfigurationSections(_prefix + key);
}
public void Reload()
{
throw new InvalidOperationException(Resources.Error_InvalidReload);
}
}
}

Просмотреть файл

@ -10,6 +10,22 @@ namespace Microsoft.Framework.Configuration
private static readonly ResourceManager _resourceManager
= new ResourceManager("Microsoft.Framework.Configuration.Resources", typeof(Resources).GetTypeInfo().Assembly);
/// <summary>
/// Reload can only be called on top-level configuration element
/// </summary>
internal static string Error_InvalidReload
{
get { return GetString("Error_InvalidReload"); }
}
/// <summary>
/// Reload can only be called on top-level configuration element
/// </summary>
internal static string FormatError_InvalidReload()
{
return GetString("Error_InvalidReload");
}
/// <summary>
/// Unable to resolve path '{0}'; construct this {1} with a non-null {2}.
/// </summary>

Просмотреть файл

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Error_InvalidReload" xml:space="preserve">
<value>Reload can only be called on top-level configuration element</value>
</data>
<data name="Error_MissingBasePath" xml:space="preserve">
<value>Unable to resolve path '{0}'; construct this {1} with a non-null {2}.</value>
</data>

Просмотреть файл

@ -74,7 +74,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"Boolean", "TRUe"},
{"Nested:Integer", "11"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(dic));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(dic));
var config = builder.Build();
var options = ConfigurationBinder.Bind<ComplexOptions>(config);
Assert.True(options.Boolean);
Assert.Equal(-2, options.Integer);
@ -91,7 +92,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"Nested:Integer", "11"},
{"Virtual","Sup"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(dic));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(dic));
var config = builder.Build();
var options = ConfigurationBinder.Bind<DerivedOptions>(config);
Assert.True(options.Boolean);
Assert.Equal(-2, options.Integer);
@ -106,7 +108,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{
{"StaticProperty", "stuff"},
};
var config = new ConfigurationSection(new MemoryConfigurationSource(dic));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(dic));
var config = builder.Build();
var options = ConfigurationBinder.Bind<ComplexOptions>(config);
Assert.Equal("stuff", ComplexOptions.StaticProperty);
}
@ -122,7 +125,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{
{property, "stuff"},
};
var config = new ConfigurationSection(new MemoryConfigurationSource(dic));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(dic));
var config = builder.Build();
var options = ConfigurationBinder.Bind<ComplexOptions>(config);
Assert.Null(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options));
}
@ -135,7 +139,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ISomeInterfaceProperty:Subkey", "x"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var exception = Assert.Throws<InvalidOperationException>(
() => ConfigurationBinder.Bind<TestOptions>(config));
@ -152,7 +157,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ClassWithoutPublicConstructorProperty:Subkey", "x"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var exception = Assert.Throws<InvalidOperationException>(
() => ConfigurationBinder.Bind<TestOptions>(config));
@ -171,7 +177,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"IntProperty", IncorrectValue}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var exception = Assert.Throws<InvalidOperationException>(
() => ConfigurationBinder.Bind<TestOptions>(config));
@ -189,7 +196,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ThrowsWhenActivatedProperty:subkey", "x"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var exception = Assert.Throws<InvalidOperationException>(
() => ConfigurationBinder.Bind<TestOptions>(config));
@ -207,7 +215,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"NestedOptionsProperty:NestedOptions2Property:ISomeInterfaceProperty:subkey", "x"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var exception = Assert.Throws<InvalidOperationException>(
() => ConfigurationBinder.Bind<TestOptions>(config));

Просмотреть файл

@ -20,7 +20,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"StringList:x", "valx"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -45,7 +46,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"IntList:x", "45"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -70,7 +72,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"AlreadyInitializedList:x", "valx"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -96,7 +99,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"CustomList:x", "valx"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -120,7 +124,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ObjectList:2:Integer", "32"},
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -143,7 +148,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"NestedLists:1:2", "val12"},
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -168,7 +174,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"StringDictionary:ghi", "val_3"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithDictionary>(config);
@ -189,7 +196,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"IntDictionary:ghi", "44"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithDictionary>(config);
@ -210,7 +218,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ObjectDictionary:ghi:Integer", "3"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithDictionary>(config);
@ -233,7 +242,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ListDictionary:def:2", "def_2"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithDictionary>(config);
@ -260,7 +270,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"ObjectList:1:ListInNestedOption:2", "12"},
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithLists>(config);
@ -285,7 +296,8 @@ namespace Microsoft.Framework.Configuration.Binder.Test
{"NonStringKeyDictionary:ghi", "val_3"}
};
var config = new ConfigurationSection(new MemoryConfigurationSource(input));
var builder = new ConfigurationBuilder(new MemoryConfigurationSource(input));
var config = builder.Build();
var options = ConfigurationBinder.Bind<OptionsWithDictionary>(config);

Просмотреть файл

@ -50,12 +50,13 @@ i=ini_i.i.i.i
[Fact]
public void DifferentConfigSources_Merged_KeysAreSorted()
{
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
builder.AddJsonFile(_json1ConfigFilePath);
builder.AddIniFile(_iniConfigFilePath);
builder.AddJsonFile(_json2ConfigFilePath);
builder.AddXmlFile(_xmlConfigFilePath);
config.AddJsonFile(_json1ConfigFilePath);
config.AddIniFile(_iniConfigFilePath);
config.AddJsonFile(_json2ConfigFilePath);
config.AddXmlFile(_xmlConfigFilePath);
var config = builder.Build();
var configurationSection = config.GetConfigurationSection("address");
var indexConfigurationSections = configurationSection.GetConfigurationSections().ToArray();
@ -74,12 +75,14 @@ i=ini_i.i.i.i
[Fact]
public void DifferentConfigSources_Merged_WithOverwrites()
{
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
config.AddJsonFile(_json1ConfigFilePath);
config.AddIniFile(_iniConfigFilePath);
config.AddJsonFile(_json2ConfigFilePath);
config.AddXmlFile(_xmlConfigFilePath);
builder.AddJsonFile(_json1ConfigFilePath);
builder.AddIniFile(_iniConfigFilePath);
builder.AddJsonFile(_json2ConfigFilePath);
builder.AddXmlFile(_xmlConfigFilePath);
var config = builder.Build();
Assert.Equal("json_0.0.0.0", config.Get("address:0"));
Assert.Equal("xml_1.1.1.1", config.Get("address:1"));

Просмотреть файл

@ -78,14 +78,16 @@ CommonKey3:CommonKey4=IniValue6";
public void LoadAndCombineKeyValuePairsFromDifferentConfigurationSources()
{
// Arrange
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act
config.AddIniFile(_iniConfigFilePath);
config.AddJsonFile(_jsonConfigFilePath);
config.AddXmlFile(_xmlConfigFilePath);
builder.AddIniFile(_iniConfigFilePath);
builder.AddJsonFile(_jsonConfigFilePath);
builder.AddXmlFile(_xmlConfigFilePath);
var memConfigSrc = new MemoryConfigurationSource(_memConfigContent);
config.Add(memConfigSrc);
builder.Add(memConfigSrc);
var config = builder.Build();
// Assert
Assert.Equal("IniValue1", config.Get("IniKey1"));
@ -119,20 +121,24 @@ CommonKey3:CommonKey4=IniValue6";
public void CanOverrideValuesWithNewConfigurationSource()
{
// Arrange
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act & Assert
config.AddIniFile(_iniConfigFilePath);
builder.AddIniFile(_iniConfigFilePath);
var config = builder.Build();
Assert.Equal("IniValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
config.AddJsonFile(_jsonConfigFilePath);
builder.AddJsonFile(_jsonConfigFilePath);
config = builder.Build();
Assert.Equal("JsonValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
config.AddXmlFile(_xmlConfigFilePath);
builder.AddXmlFile(_xmlConfigFilePath);
config = builder.Build();
Assert.Equal("XmlValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
var memConfigSrc = new MemoryConfigurationSource(_memConfigContent);
config.Add(memConfigSrc);
builder.Add(memConfigSrc);
config = builder.Build();
Assert.Equal("MemValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
}
@ -140,17 +146,19 @@ CommonKey3:CommonKey4=IniValue6";
public void CanSetValuesAndReloadValues()
{
// Arrange
var config = new ConfigurationSection();
config.AddIniFile(_iniConfigFilePath);
config.AddJsonFile(_jsonConfigFilePath);
config.AddXmlFile(_xmlConfigFilePath);
var builder = new ConfigurationBuilder();
builder.AddIniFile(_iniConfigFilePath);
builder.AddJsonFile(_jsonConfigFilePath);
builder.AddXmlFile(_xmlConfigFilePath);
var config = builder.Build();
// Act & Assert
// Set value with Set() method
config.Set("CommonKey1:CommonKey2:CommonKey3:CommonKey4", "NewValue");
// All config sources must be updated
foreach (var src in config.Sources)
foreach (var src in builder.Sources)
{
Assert.Equal("NewValue",
(src as ConfigurationSource).Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
@ -158,13 +166,14 @@ CommonKey3:CommonKey4=IniValue6";
// Recover values by reloading
config.Reload();
Assert.Equal("XmlValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
// Set value with indexer
config["CommonKey1:CommonKey2:CommonKey3:CommonKey4"] = "NewValue";
// All config sources must be updated
foreach (var src in config.Sources)
foreach (var src in builder.Sources)
{
Assert.Equal("NewValue",
(src as ConfigurationSource).Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));

Просмотреть файл

@ -15,11 +15,11 @@ namespace Microsoft.Framework.Configuration.Ini.Test
public void AddIniFile_ThrowsIfFilePathIsNullOrEmpty(string path)
{
// Arrange
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<ArgumentException>(
() => IniConfigurationExtension.AddIniFile(configurationSource, path));
() => IniConfigurationExtension.AddIniFile(builder, path));
Assert.Equal("path", ex.ParamName);
Assert.StartsWith("File path must be a non-empty string.", ex.Message);
}
@ -29,11 +29,11 @@ namespace Microsoft.Framework.Configuration.Ini.Test
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), "file-does-not-exist.ini");
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<FileNotFoundException>(
() => IniConfigurationExtension.AddIniFile(configurationSource, path));
() => IniConfigurationExtension.AddIniFile(builder, path));
Assert.Equal($"The configuration file '{path}' was not found and is not optional.", ex.Message);
}
}

Просмотреть файл

@ -101,9 +101,10 @@ namespace Microsoft.Framework.Configuration.Json.Test
var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));
var config = new ConfigurationSection();
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource1, load: false);
builder.Add(jsonConfigSource2, load: false);
var config = builder.Build();
Assert.Equal(3, config.GetConfigurationSections("ip").Count());
Assert.Equal("15.16.17.18", config.Get("ip:0"));
@ -134,9 +135,10 @@ namespace Microsoft.Framework.Configuration.Json.Test
var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));
var config = new ConfigurationSection();
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource1, load: false);
builder.Add(jsonConfigSource2, load: false);
var config = builder.Build();
Assert.Equal(3, config.GetConfigurationSections("ip").Count());
Assert.Equal("1.2.3.4", config.Get("ip:0"));
@ -167,9 +169,10 @@ namespace Microsoft.Framework.Configuration.Json.Test
var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));
var config = new ConfigurationSection();
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource1, load: false);
builder.Add(jsonConfigSource2, load: false);
var config = builder.Build();
Assert.Equal(4, config.GetConfigurationSections("ip").Count());
Assert.Equal("1.2.3.4", config.Get("ip:0"));
@ -192,8 +195,9 @@ namespace Microsoft.Framework.Configuration.Json.Test
var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));
var config = new ConfigurationSection();
config.Add(jsonConfigSource, load: false);
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource, load: false);
var config = builder.Build();
var configurationSection = config.GetConfigurationSection("setting");
var indexConfigurationSections = configurationSection.GetConfigurationSections().ToArray();
@ -221,8 +225,9 @@ namespace Microsoft.Framework.Configuration.Json.Test
var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));
var config = new ConfigurationSection();
config.Add(jsonConfigSource, load: false);
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource, load: false);
var config = builder.Build();
var configurationSection = config.GetConfigurationSection("setting");
var indexConfigurationSections = configurationSection.GetConfigurationSections().ToArray();

Просмотреть файл

@ -15,10 +15,10 @@ namespace Microsoft.Framework.Configuration.Json
public void AddJsonFile_ThrowsIfFilePathIsNullOrEmpty(string path)
{
// Arrange
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<ArgumentException>(() => JsonConfigurationExtension.AddJsonFile(configurationSource, path));
var ex = Assert.Throws<ArgumentException>(() => JsonConfigurationExtension.AddJsonFile(builder, path));
Assert.Equal("path", ex.ParamName);
Assert.StartsWith("File path must be a non-empty string.", ex.Message);
}
@ -28,10 +28,10 @@ namespace Microsoft.Framework.Configuration.Json
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), "file-does-not-exist.json");
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<FileNotFoundException>(() => JsonConfigurationExtension.AddJsonFile(configurationSource, path));
var ex = Assert.Throws<FileNotFoundException>(() => JsonConfigurationExtension.AddJsonFile(builder, path));
Assert.Equal($"The configuration file '{path}' was not found and is not optional.", ex.Message);
}
}

Просмотреть файл

@ -114,6 +114,11 @@ namespace Microsoft.Framework.Configuration.Test
{
throw new NotImplementedException();
}
public IConfiguration Build()
{
throw new NotImplementedException();
}
}
}
}

Просмотреть файл

@ -15,17 +15,17 @@ namespace Microsoft.Framework.Configuration.Test
public void SetBasePathThroughConstructor()
{
var expectedBasePath = @"C:\ExamplePath";
var config = new ConfigurationSection(basePath: expectedBasePath);
var builder = new ConfigurationBuilder(basePath: expectedBasePath);
Assert.Equal(expectedBasePath, config.BasePath);
Assert.Equal(expectedBasePath, builder.BasePath);
}
[Fact]
public void DefaultBasePathIsNull()
{
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
Assert.Null(config.BasePath);
Assert.Null(builder.BasePath);
}
[Fact]
@ -48,24 +48,26 @@ namespace Microsoft.Framework.Configuration.Test
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
string memVal1, memVal2, memVal3;
bool memRet1, memRet2, memRet3;
// Act
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
memRet1 = config.TryGet("mem1:keyinmem1", out memVal1);
memRet2 = config.TryGet("Mem2:KeyInMem2", out memVal2);
memRet3 = config.TryGet("MEM3:KEYINMEM3", out memVal3);
// Assert
Assert.Contains(memConfigSrc1, config.Sources);
Assert.Contains(memConfigSrc2, config.Sources);
Assert.Contains(memConfigSrc3, config.Sources);
Assert.Contains(memConfigSrc1, builder.Sources);
Assert.Contains(memConfigSrc2, builder.Sources);
Assert.Contains(memConfigSrc3, builder.Sources);
Assert.True(memRet1);
Assert.True(memRet2);
@ -101,11 +103,13 @@ namespace Microsoft.Framework.Configuration.Test
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
var config = builder.Build();
// Assert
Assert.Equal("ValueInMem2", config.Get("Key1:Key2"));
@ -124,10 +128,13 @@ namespace Microsoft.Framework.Configuration.Test
var memConfigSrc2 = new MemoryConfigurationSource(dict);
var memConfigSrc3 = new MemoryConfigurationSource(dict);
var config = new ConfigurationSection();
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Act
config.Set("Key1", "NewValue1");
@ -165,10 +172,12 @@ namespace Microsoft.Framework.Configuration.Test
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var config = new ConfigurationSection();
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
string memVal1, memVal2, memVal3, memVal4, memVal5;
bool memRet1, memRet2, memRet3, memRet4, memRet5;
@ -227,10 +236,12 @@ namespace Microsoft.Framework.Configuration.Test
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var config = new ConfigurationSection();
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Act
var configFocusList = config.GetConfigurationSections("Data");
@ -264,15 +275,17 @@ namespace Microsoft.Framework.Configuration.Test
memConfigSrc3
};
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Assert
var enumerator = config.Sources.GetEnumerator();
var enumerator = builder.Sources.GetEnumerator();
int srcCount = 0;
while (enumerator.MoveNext())
{
@ -302,17 +315,17 @@ namespace Microsoft.Framework.Configuration.Test
memConfigSrc3
};
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var enumerable = config as IEnumerable;
var enumerable = builder as IEnumerable;
// Assert
var enumerator = config.Sources.GetEnumerator();
var enumerator = builder.Sources.GetEnumerator();
int srcCount = 0;
while (enumerator.MoveNext())
{
@ -327,7 +340,9 @@ namespace Microsoft.Framework.Configuration.Test
public void SetValueThrowsExceptionNoSourceRegistered()
{
// Arrange
var config = new ConfigurationSection();
var builder = new ConfigurationBuilder();
var config = builder.Build();
var expectedMsg = Resources.Error_NoSources;
// Act

Просмотреть файл

@ -15,10 +15,10 @@ namespace Microsoft.Framework.Configuration.Xml.Test
public void AddXmlFile_ThrowsIfFilePathIsNullOrEmpty(string path)
{
// Arrange
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<ArgumentException>(() => XmlConfigurationExtension.AddXmlFile(configurationSource, path));
var ex = Assert.Throws<ArgumentException>(() => XmlConfigurationExtension.AddXmlFile(builder, path));
Assert.Equal("path", ex.ParamName);
Assert.StartsWith("File path must be a non-empty string.", ex.Message);
}
@ -28,10 +28,10 @@ namespace Microsoft.Framework.Configuration.Xml.Test
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), "file-does-not-exist.xml");
var configurationSource = new ConfigurationSection();
var builder = new ConfigurationBuilder();
// Act and Assert
var ex = Assert.Throws<FileNotFoundException>(() => XmlConfigurationExtension.AddXmlFile(configurationSource, path));
var ex = Assert.Throws<FileNotFoundException>(() => XmlConfigurationExtension.AddXmlFile(builder, path));
Assert.Equal($"The configuration file '{path}' was not found and is not optional.", ex.Message);
}
}