-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from pengweiqhca/master
添加CommonSectionBuilder
- Loading branch information
Showing
21 changed files
with
474 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Com.Ctrip.Framework.Apollo; | ||
using Xunit; | ||
|
||
namespace Apollo.ConfigurationManager.Tests; | ||
|
||
public class ConfigExtensionsTest | ||
{ | ||
[Fact] | ||
public void GetChildren() | ||
{ | ||
var config = new TestConfig(Create("a:B", "1"), Create("A:c", "2"), Create("A:C:d", "4"), Create("b", "3")); | ||
|
||
Assert.Equal(new[] { "a", "b" }, config.GetChildren("").Select(x => x.Name)); | ||
Assert.Equal(new[] { "B", "c" }, config.GetChildren("a").Select(x => x.Name)); | ||
Assert.Equal(new[] { "a:B", "A:c" }, config.GetChildren("a").Select(x => x.FullName)); | ||
} | ||
|
||
[Fact] | ||
public void GetConnectionStrings_ConnectionString_Prior() | ||
{ | ||
var config = new TestConfig(Create("a:b", "123"), Create("a:b:ConnectionString", "abc")); | ||
|
||
var connectionString = config.GetConnectionStrings("a", "sql").Single(); | ||
|
||
Assert.Equal("b", connectionString.Name); | ||
Assert.Equal("abc", connectionString.ConnectionString); | ||
Assert.Equal("sql", connectionString.ProviderName); | ||
} | ||
|
||
[Fact] | ||
public void GetConnectionStrings_ProviderName() | ||
{ | ||
var config = new TestConfig(Create("a:b", "123"), Create("a:b:ProviderName", "abc")); | ||
|
||
var connectionString = config.GetConnectionStrings("a", "sql").Single(); | ||
|
||
Assert.Equal("123", connectionString.ConnectionString); | ||
Assert.Equal("abc", connectionString.ProviderName); | ||
} | ||
|
||
[Fact] | ||
public void GetConnectionStrings_No_Prefix() | ||
{ | ||
var config = new TestConfig(Create("a", "123")); | ||
|
||
var connectionString = config.GetConnectionStrings("", "sql").Single(); | ||
|
||
Assert.Equal("a", connectionString.Name); | ||
Assert.Equal("123", connectionString.ConnectionString); | ||
} | ||
|
||
[Fact] | ||
public void WithPrefix() | ||
{ | ||
IConfig config = new TestConfig(Create("a:B", "1"), Create("A:c", "2"), Create("A:C:d", "4"), Create("b", "3")); | ||
|
||
Assert.Same(config, config.WithPrefix(" ")); | ||
|
||
config = config.WithPrefix("a"); | ||
|
||
Assert.Equal(new[] { "B", "c", "C:d" }, config.GetPropertyNames()); | ||
Assert.Equal(new[] { "1", "2", "4" }, config.GetPropertyNames().Select(p => config.GetProperty(p, ""))); | ||
} | ||
|
||
private static KeyValuePair<string, string> Create(string key, string value) => new(key, value); | ||
|
||
private class TestConfig : IConfig | ||
{ | ||
private readonly IReadOnlyDictionary<string, string> _dict; | ||
|
||
public TestConfig(params KeyValuePair<string, string>[] keyValues) | ||
{ | ||
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
|
||
foreach (var kv in keyValues) dict[kv.Key] = kv.Value; | ||
|
||
_dict = dict; | ||
} | ||
|
||
public bool TryGetProperty(string key, [NotNullWhen(true)] out string? value) => | ||
_dict.TryGetValue(key, out value); | ||
|
||
public IEnumerable<string> GetPropertyNames() => _dict.Keys; | ||
|
||
public event ConfigChangeEvent ConfigChanged = default!; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
Apollo.ConfigurationManager.Tests/TestConfigurationSection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Configuration; | ||
|
||
namespace Apollo.ConfigurationManager.Tests; | ||
|
||
public class TestConfigurationSection : ConfigurationSection | ||
{ | ||
[ConfigurationProperty("timeout")] | ||
public TimeSpan Timeout => (TimeSpan)this["timeout"]; | ||
|
||
[ConfigurationProperty("maxValue")] | ||
public int MaxValue => (int)this["maxValue"]; | ||
|
||
[ConfigurationProperty("defaultValue", DefaultValue = 3L)] | ||
public long DefaultValue => (long)this["defaultValue"]; | ||
|
||
[ConfigurationProperty("element")] | ||
public NameValueConfigurationElement Element => (NameValueConfigurationElement)this["element"]; | ||
|
||
[ConfigurationProperty("map")] | ||
public KeyValueConfigurationCollection Map => (KeyValueConfigurationCollection)this["map"]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.