|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace OptionStudy.Next
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 5.2 配置模型
|
|
|
/// IConfigurationProvider 测试
|
|
|
/// IConfigrutionProvider 被设计为 IConfigurationSource 构建,并保持在 IConfigurationBuilder 的属性中
|
|
|
/// 所以一般不单独实例化出来使用,而是配合 IConfigurationSource 和 IConfigurationBuilder 使用
|
|
|
/// </summary>
|
|
|
public class IConfigurationProviderTest : IDisposable
|
|
|
{
|
|
|
private readonly ITestOutputHelper testOutput;
|
|
|
|
|
|
private IDictionary<string, string?> memoryData = new Dictionary<string, string?>()
|
|
|
{
|
|
|
["AppName"] = "MemoryAppName",
|
|
|
["AppVersion"] = "0.0.0.1",
|
|
|
["EMail:ReceiveAddress"] = "memory@163.com",
|
|
|
["EMail:Recipient"] = "memory",
|
|
|
};
|
|
|
|
|
|
public IConfigurationProviderTest(ITestOutputHelper testOutputHelper)
|
|
|
{
|
|
|
testOutput = testOutputHelper;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 实例化配置提供者
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void IConfigrutionProvider_New_Test()
|
|
|
{
|
|
|
//不配合 IConfigrutionSource 直接实例化,将会异常
|
|
|
Action createAction = () =>
|
|
|
{
|
|
|
var provider = new MemoryConfigurationProvider(null);
|
|
|
};
|
|
|
|
|
|
Assert.ThrowsAny<ArgumentNullException>(createAction);
|
|
|
|
|
|
//关联 ConfigurationSource
|
|
|
var source = new MemoryConfigurationSource()
|
|
|
{
|
|
|
InitialData = new Dictionary<string, string?>() { ["TestName"] = "MyTest" }
|
|
|
};
|
|
|
var provider = new MemoryConfigurationProvider(source);
|
|
|
Assert.NotNull(provider);
|
|
|
|
|
|
//可以进行各种操作
|
|
|
Action run = () =>
|
|
|
{
|
|
|
provider.Set("TestName", "UpdateName");
|
|
|
if (!provider.TryGet("Demo", out string _v))
|
|
|
{
|
|
|
provider.Add("Demo", "demo");
|
|
|
}
|
|
|
|
|
|
provider.Load();
|
|
|
};
|
|
|
|
|
|
//断言不发生异常
|
|
|
//方法一:使用 FluentAssertions 库
|
|
|
run.Should().NotThrow();
|
|
|
|
|
|
//方法二:xUnit 变通方法
|
|
|
var exception = Record.Exception(run);
|
|
|
Assert.Null(exception);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// IConfigurationProvider 由 Source 构建
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void IConfigrutionProvider_BuildBySource_Test()
|
|
|
{
|
|
|
//内存配置源
|
|
|
MemoryConfigurationSource source = new MemoryConfigurationSource()
|
|
|
{
|
|
|
InitialData = memoryData,
|
|
|
};
|
|
|
|
|
|
|
|
|
var provider = source.Build(null);
|
|
|
var hasName = provider.TryGet("AppName", out _);
|
|
|
|
|
|
Assert.NotNull(provider);
|
|
|
Assert.True(hasName);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// IConfigurationProvider 由 IConfigruationBuilder 构建
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void IConfigrutionProvider_IConfigruationBuilder_Test()
|
|
|
{
|
|
|
var builder = new ConfigurationBuilder().AddInMemoryCollection(memoryData).Build();
|
|
|
var provider = builder.Providers.First();
|
|
|
|
|
|
var hasName = provider.TryGet("AppName", out _);
|
|
|
|
|
|
Assert.NotNull(provider);
|
|
|
Assert.True(hasName);
|
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|