Refactor where/how client settings are built for config server provider extension methods

This commit is contained in:
Dave Tillman 2016-01-08 08:24:47 -08:00
Родитель 50704f7560
Коммит 275a0dc1e4
10 изменённых файлов: 343 добавлений и 265 удалений

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

@ -28,28 +28,42 @@ namespace Spring.Extensions.Configuration.Server
public class ConfigServerClientSettings
{
/// <summary>
/// The prefix (<see cref="IConfigurationSection"/> under which all Spring Cloud Config Server
/// configuration settings are found. (e.g. spring:cloud:config:uri, spring:cloud:config:enabled, etc.)
/// Default Config Server address used by provider
/// </summary>
public const string PREFIX = "spring:cloud:config";
public const string DEFAULT_URI = "http://localhost:8888";
/// <summary>
/// The Config Servers address (defaults: https://localhost:8888)
/// Default enironment used when accessing configuration data
/// </summary>
public string Uri { get; set; } = "http://localhost:8888";
public const string DEFAULT_ENVIRONMENT = "Production";
/// <summary>
/// Enables/Disables the Config Server provider (defaults: true)
/// Default fail fast setting
/// </summary>
public bool Enabled { get; set; } = true;
public const bool DEFAULT_FAILFAST = false;
/// <summary>
/// The environment used when accessing configuration data (defaults: Development)
/// Default Config Server provider enabled setting
/// </summary>
public string Environment { get; set; } = "Development";
public const bool DEFAULT_PROVIDER_ENABLED = true;
/// <summary>
/// The application name used when accessing configuration data (defaults: tbd)
/// The Config Server address (defaults: DEFAULT_URI)
/// </summary>
public string Uri { get; set; } = DEFAULT_URI;
/// <summary>
/// Enables/Disables the Config Server provider (defaults: DEFAULT_PROVIDER_ENABLED)
/// </summary>
public bool Enabled { get; set; } = DEFAULT_PROVIDER_ENABLED;
/// <summary>
/// The environment used when accessing configuration data (defaults: DEFAULT_ENVIRONMENT)
/// </summary>
public string Environment { get; set; } = DEFAULT_ENVIRONMENT;
/// <summary>
/// The application name used when accessing configuration data (defaults: TODO:)
/// </summary>
public string Name { get; set; }
@ -69,77 +83,17 @@ namespace Spring.Extensions.Configuration.Server
public string Password { get; set; }
/// <summary>
/// Enables/Disables failfast behavior (defaults: false)
/// Enables/Disables failfast behavior (defaults: DEFAULT_FAILFAST)
/// </summary>
public bool FailFast { get; set; } = false;
private ILogger _logger;
public bool FailFast { get; set; } = DEFAULT_FAILFAST;
/// <summary>
/// Initialize Config Server client settings with defaults
/// </summary>
public ConfigServerClientSettings(ILoggerFactory logFactory = null)
public ConfigServerClientSettings()
{
_logger = logFactory?.CreateLogger<ConfigServerClientSettings>();
}
internal ILogger Logger
{
get
{
return _logger;
}
}
internal ConfigServerClientSettings(IEnumerable<IConfigurationProvider> providers, ILoggerFactory logFactory = null)
{
_logger = logFactory?.CreateLogger<ConfigServerClientSettings>();
if (providers == null)
return;
Initialize(new List<IConfigurationProvider>(providers));
}
private void Initialize(List<IConfigurationProvider> list)
{
ConfigurationRoot root = new ConfigurationRoot(list);
var section = root.GetSection(PREFIX);
Name = section["name"];
Label = section["label"];
Username = section["username"];
Password = section["password"];
var env = section["environment"];
if (!string.IsNullOrEmpty(env))
{
Environment = env;
}
var uri = section["uri"];
if (!string.IsNullOrEmpty(uri))
{
Uri = uri;
}
var enabled = section["enabled"];
if (!string.IsNullOrEmpty(enabled))
{
bool result;
if (Boolean.TryParse(enabled, out result))
Enabled = result;
}
var failFast = section["failFast"];
if (!string.IsNullOrEmpty(failFast))
{
bool result;
if (Boolean.TryParse(failFast, out result))
FailFast = result;
}
}
}
}

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

@ -15,6 +15,8 @@
//
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
@ -25,10 +27,20 @@ namespace Spring.Extensions.Configuration.Server
/// </summary>
public static class ConfigServerConfigurationExtensions
{
private const string SPRING_APPLICATION_PREFIX = "spring:application";
/// <summary>
/// Adds the Spring Cloud configuration server provider <see cref="ConfigServerConfigurationProvider"/>
/// The prefix (<see cref="IConfigurationSection"/> under which all Spring Cloud Config Server
/// configuration settings are found. (e.g. spring:cloud:config:uri, spring:cloud:config:enabled, etc.)
/// </summary>
public const string PREFIX = "spring:cloud:config";
/// <summary>
/// Adds the Spring Cloud Config Server provider <see cref="ConfigServerConfigurationProvider"/>
/// to <paramref name="configurationBuilder"/>.
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="environment">The hosting enviroment settings.</param>
/// <param name="logFactory">optional logging factory. Used to enable logging in Config Server client.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
///
/// Default Spring Config Server settings <see cref="ConfigServerClientSettings" /> will used by the provider
@ -42,18 +54,139 @@ namespace Spring.Extensions.Configuration.Server
/// would used default Config Server setting unless overriden by values found in appsettings.json or
/// environment variables.
/// </summary>
public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, ILoggerFactory logFactory = null)
public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, IHostingEnvironment environment, ILoggerFactory logFactory = null)
{
if (configurationBuilder == null)
{
throw new ArgumentNullException(nameof(configurationBuilder));
}
configurationBuilder.Add(new ConfigServerConfigurationProvider(configurationBuilder.Providers, logFactory));
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
ConfigServerClientSettings settings = CreateSettings(environment, configurationBuilder.Providers, logFactory);
configurationBuilder.Add(new ConfigServerConfigurationProvider(settings, logFactory));
return configurationBuilder;
}
private static ConfigServerClientSettings CreateSettings(IHostingEnvironment environment, IEnumerable<IConfigurationProvider> providers, ILoggerFactory logFactory)
{
ConfigServerClientSettings settings = new ConfigServerClientSettings();
if (providers != null)
{
ConfigurationRoot existing = new ConfigurationRoot(new List<IConfigurationProvider>(providers));
Initialize(settings, environment, existing);
}
return settings;
}
private static void Initialize(ConfigServerClientSettings settings, IHostingEnvironment environment, ConfigurationRoot root)
{
var clientConfigsection = root.GetSection(PREFIX);
var appSection = root.GetSection(SPRING_APPLICATION_PREFIX);
settings.Name = GetApplicationName(clientConfigsection, appSection);
settings.Environment = GetEnvironment(clientConfigsection, environment);
settings.Label = GetLabel(clientConfigsection);
settings.Username = GetUsername(clientConfigsection);
settings.Password = GetPassword(clientConfigsection);
settings.Uri = GetUri(clientConfigsection);
settings.Enabled = GetEnabled(clientConfigsection);
settings.FailFast = GetFailFast(clientConfigsection);
}
private static bool GetFailFast(IConfigurationSection section)
{
var failFast = section["failFast"];
if (!string.IsNullOrEmpty(failFast))
{
bool result;
if (Boolean.TryParse(failFast, out result))
return result;
}
return ConfigServerClientSettings.DEFAULT_FAILFAST;
}
private static bool GetEnabled(IConfigurationSection section)
{
var enabled = section["enabled"];
if (!string.IsNullOrEmpty(enabled))
{
bool result;
if (Boolean.TryParse(enabled, out result))
return result;
}
return ConfigServerClientSettings.DEFAULT_PROVIDER_ENABLED; ;
}
private static string GetUri(IConfigurationSection section)
{
var uri = section["uri"];
if (!string.IsNullOrEmpty(uri))
{
return uri;
}
return ConfigServerClientSettings.DEFAULT_URI;
}
private static string GetPassword(IConfigurationSection section)
{
return section["password"];
}
private static string GetUsername(IConfigurationSection section)
{
return section["username"];
}
private static string GetLabel(IConfigurationSection section)
{
// TODO: multi label support
return section["label"];
}
private static string GetApplicationName(IConfigurationSection section, IConfigurationSection appSection)
{
// if spring:cloud:config:name present, use it
var name = section["name"];
if (!string.IsNullOrEmpty(name))
{
return name;
}
// if spring:application:name present, use it
name = appSection["name"];
if (!string.IsNullOrEmpty(name))
{
return name;
}
// TODO: Figure out a sensible "default" app name (e.g apps assembly name?)
return "name";
}
private static string GetEnvironment(IConfigurationSection section, IHostingEnvironment environment)
{
// if spring:cloud:config:environment present, use it
var env = section["environment"];
if (!string.IsNullOrEmpty(env))
{
return env;
}
// Otherwise use frameworks defined value (its default is Production)
return environment.EnvironmentName;
}
}
}

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

@ -23,6 +23,7 @@ using System.IO;
using System.Net;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Hosting;
namespace Spring.Extensions.Configuration.Server
{
@ -44,7 +45,7 @@ namespace Spring.Extensions.Configuration.Server
/// <param name="logFactory">optional logging factory</param>
/// </summary>
public ConfigServerConfigurationProvider(ILoggerFactory logFactory = null) :
this(new ConfigServerClientSettings(logFactory), logFactory)
this(new ConfigServerClientSettings(), logFactory)
{
}
@ -82,7 +83,6 @@ namespace Spring.Extensions.Configuration.Server
throw new ArgumentNullException(nameof(httpClient));
}
_logger = logFactory?.CreateLogger<ConfigServerConfigurationProvider>();
_settings = settings;
_client = httpClient;
@ -124,11 +124,6 @@ namespace Spring.Extensions.Configuration.Server
}
}
internal ConfigServerConfigurationProvider(IEnumerable<IConfigurationProvider> providers, ILoggerFactory logFactory = null) :
this(new ConfigServerClientSettings(providers, logFactory), logFactory)
{
}
internal IDictionary<string, string> Properties
{
get

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

@ -16,6 +16,7 @@
}
},
"dependencies": {
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",

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

@ -60,10 +60,11 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest
var path = ConfigServerTestHelpers.CreateTempFile(appsettings);
var configurationBuilder = new ConfigurationBuilder();
var hostingEnv = new HostingEnvironment();
configurationBuilder.AddJsonFile(path);
// Act and Assert (expects Spring Cloud Config server to be running)
configurationBuilder.AddConfigServer();
configurationBuilder.AddConfigServer(hostingEnv);
IConfigurationRoot root = configurationBuilder.Build();
Assert.Equal("spam", root["bar"]);

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

@ -32,7 +32,7 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest
public IConfiguration Configuration { get; set; }
public TestServerStartup()
public TestServerStartup(IHostingEnvironment environment)
{
// These settings match the default java config server
var appsettings = @"
@ -50,7 +50,7 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest
var path = ConfigServerTestHelpers.CreateTempFile(appsettings);
var builder = new ConfigurationBuilder()
.AddJsonFile(path)
.AddConfigServer();
.AddConfigServer(environment);
Configuration = builder.Build();
}

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

@ -38,153 +38,6 @@ namespace Spring.Extensions.Configuration.Server.Test
}
[Fact]
public void ProvidersConstructor_InitializedWithDefaultsWhenNull()
{
// Arrange
ConfigServerClientSettings settings = new ConfigServerClientSettings(null);
// Act and Assert
ConfigServerTestHelpers.VerifyDefaults(settings);
}
[Fact]
public void ProvidersConstructor_InitializedWithJsonProvider()
{
// Arrange
var json = @"
{
'spring': {
'cloud': {
'config': {
'uri': 'http://foo.com:9999',
'enabled': false,
'failFast': true,
'label': 'myLabel',
'name': 'myName',
'username': 'myUsername',
'password': 'myPassword'
}
}
}
}";
var path = ConfigServerTestHelpers.CreateTempFile(json);
var provider = new JsonConfigurationProvider(path);
provider.Load();
List<IConfigurationProvider> providers = new List<IConfigurationProvider>() { provider };
ConfigServerClientSettings settings = new ConfigServerClientSettings(providers.AsEnumerable<IConfigurationProvider>());
// Act and Assert
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal(settings.Uri, "http://foo.com:9999");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(settings.Name,"myName");
Assert.Equal(settings.Label, "myLabel");
Assert.Equal(settings.Username, "myUsername");
Assert.Equal(settings.Password, "myPassword");
}
[Fact]
public void ProvidersConstructor_InitializedWithXmlProvider()
{
// Arrange
var xml = @"
<settings>
<spring>
<cloud>
<config>
<uri>http://foo.com:9999</uri>
<enabled>false</enabled>
<failFast>true</failFast>
<label>myLabel</label>
<name>myName</name>
<username>myUsername</username>
<password>myPassword</password>
</config>
</cloud>
</spring>
</settings>";
var path = ConfigServerTestHelpers.CreateTempFile(xml);
var provider = new XmlConfigurationProvider(path);
provider.Load();
List<IConfigurationProvider> providers = new List<IConfigurationProvider>() { provider };
ConfigServerClientSettings settings = new ConfigServerClientSettings(providers.AsEnumerable<IConfigurationProvider>());
// Act and Assert
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal(settings.Uri, "http://foo.com:9999");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(settings.Name, "myName");
Assert.Equal(settings.Label, "myLabel");
Assert.Equal(settings.Username, "myUsername");
Assert.Equal(settings.Password, "myPassword");
}
[Fact]
public void ProvidersConstructor_InitializedWithIniProvider()
{
// Arrange
var ini = @"
[spring:cloud:config]
uri=http://foo.com:9999
enabled=false
failFast=true
label=myLabel
name=myName
username=myUsername
password=myPassword
";
var path = ConfigServerTestHelpers.CreateTempFile(ini);
var provider = new IniConfigurationProvider(path);
provider.Load();
List<IConfigurationProvider> providers = new List<IConfigurationProvider>() { provider };
ConfigServerClientSettings settings = new ConfigServerClientSettings(providers.AsEnumerable<IConfigurationProvider>());
// Act and Assert
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal(settings.Uri, "http://foo.com:9999");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(settings.Name, "myName");
Assert.Equal(settings.Label, "myLabel");
Assert.Equal(settings.Username, "myUsername");
Assert.Equal(settings.Password, "myPassword");
}
[Fact]
public void ProvidersConstructor_InitializedWithCommandLineProvider()
{
// Arrange
var args = new string[]
{
"spring:cloud:config:enabled=false",
"--spring:cloud:config:failFast=true",
"/spring:cloud:config:uri=http://foo.com:9999",
"--spring:cloud:config:name", "myName",
"/spring:cloud:config:label", "myLabel",
"--spring:cloud:config:username", "myUsername",
"--spring:cloud:config:password", "myPassword"
};
var provider = new CommandLineConfigurationProvider(args);
provider.Load();
List<IConfigurationProvider> providers = new List<IConfigurationProvider>() { provider };
ConfigServerClientSettings settings = new ConfigServerClientSettings(providers.AsEnumerable<IConfigurationProvider>());
// Act and Assert
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal(settings.Uri, "http://foo.com:9999");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(settings.Name, "myName");
Assert.Equal(settings.Label, "myLabel");
Assert.Equal(settings.Username, "myUsername");
Assert.Equal(settings.Password, "myPassword");
}
}

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

@ -18,6 +18,8 @@ using Microsoft.Extensions.Configuration;
using Xunit;
using System;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration.Xml;
namespace Spring.Extensions.Configuration.Server.Test
{
@ -28,21 +30,35 @@ namespace Spring.Extensions.Configuration.Server.Test
{
// Arrange
IConfigurationBuilder configurationBuilder = null;
var environment = new HostingEnvironment();
// Act and Assert
var ex = Assert.Throws<ArgumentNullException>(() => ConfigServerConfigurationExtensions.AddConfigServer(configurationBuilder));
var ex = Assert.Throws<ArgumentNullException>(() => ConfigServerConfigurationExtensions.AddConfigServer(configurationBuilder, environment));
Assert.Contains(nameof(configurationBuilder), ex.Message);
}
[Fact]
public void AddConfigService_ThrowsIfHostingEnvironmentNull()
{
// Arrange
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
IHostingEnvironment environment = null;
// Act and Assert
var ex = Assert.Throws<ArgumentNullException>(() => ConfigServerConfigurationExtensions.AddConfigServer(configurationBuilder, environment));
Assert.Contains(nameof(environment), ex.Message);
}
[Fact]
public void AddConfigService_AddsConfigServerProviderToProvidersList()
{
// Arrange
var configurationBuilder = new ConfigurationBuilder();
var environment = new HostingEnvironment();
// Act and Assert
configurationBuilder.AddConfigServer();
configurationBuilder.AddConfigServer(environment);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
@ -61,9 +77,10 @@ namespace Spring.Extensions.Configuration.Server.Test
// Arrange
var configurationBuilder = new ConfigurationBuilder();
var loggerFactory = new LoggerFactory();
var environment = new HostingEnvironment();
// Act and Assert
configurationBuilder.AddConfigServer(loggerFactory);
configurationBuilder.AddConfigServer(environment,loggerFactory);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
@ -75,7 +92,6 @@ namespace Spring.Extensions.Configuration.Server.Test
Assert.NotNull(configServerProvider);
Assert.NotNull(configServerProvider.Logger);
Assert.NotNull(configServerProvider.Settings.Logger);
}
@ -86,13 +102,15 @@ namespace Spring.Extensions.Configuration.Server.Test
var appsettings = @"
{
'spring': {
'application': {
'name': 'myName'
},
'cloud': {
'config': {
'uri': 'http://foo.com:9999',
'enabled': false,
'failFast': true,
'label': 'myLabel',
'name': 'myName',
'username': 'myUsername',
'password': 'myPassword'
}
@ -102,10 +120,11 @@ namespace Spring.Extensions.Configuration.Server.Test
var path = ConfigServerTestHelpers.CreateTempFile(appsettings);
var configurationBuilder = new ConfigurationBuilder();
var environment = new HostingEnvironment();
configurationBuilder.AddJsonFile(path);
// Act and Assert
configurationBuilder.AddConfigServer();
configurationBuilder.AddConfigServer(environment);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
@ -119,12 +138,148 @@ namespace Spring.Extensions.Configuration.Server.Test
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal(settings.Uri, "http://foo.com:9999");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(settings.Name, "myName");
Assert.Equal(settings.Label, "myLabel");
Assert.Equal(settings.Username, "myUsername");
Assert.Equal(settings.Password, "myPassword");
Assert.Equal("http://foo.com:9999", settings.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
Assert.Equal("myName", settings.Name);
Assert.Equal("myLabel", settings.Label);
Assert.Equal("myUsername", settings.Username);
Assert.Equal("myPassword", settings.Password);
}
[Fact]
public void AddConfigService_XmlAppSettingsConfiguresClient()
{
// Arrange
var appsettings = @"
<settings>
<spring>
<cloud>
<config>
<uri>http://foo.com:9999</uri>
<enabled>false</enabled>
<failFast>true</failFast>
<label>myLabel</label>
<name>myName</name>
<username>myUsername</username>
<password>myPassword</password>
</config>
</cloud>
</spring>
</settings>";
var path = ConfigServerTestHelpers.CreateTempFile(appsettings);
var configurationBuilder = new ConfigurationBuilder();
var environment = new HostingEnvironment();
configurationBuilder.AddXmlFile(path);
// Act and Assert
configurationBuilder.AddConfigServer(environment);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
{
configServerProvider = provider as ConfigServerConfigurationProvider;
if (configServerProvider != null)
break;
}
Assert.NotNull(configServerProvider);
ConfigServerClientSettings settings = configServerProvider.Settings;
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal("http://foo.com:9999", settings.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
Assert.Equal("myName", settings.Name);
Assert.Equal("myLabel", settings.Label);
Assert.Equal("myUsername", settings.Username);
Assert.Equal("myPassword", settings.Password);
}
[Fact]
public void AddConfigService_IniAppSettingsConfiguresClient()
{
// Arrange
var appsettings = @"
[spring:cloud:config]
uri=http://foo.com:9999
enabled=false
failFast=true
label=myLabel
name=myName
username=myUsername
password=myPassword
";
var path = ConfigServerTestHelpers.CreateTempFile(appsettings);
var configurationBuilder = new ConfigurationBuilder();
var environment = new HostingEnvironment();
configurationBuilder.AddIniFile(path);
// Act and Assert
configurationBuilder.AddConfigServer(environment);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
{
configServerProvider = provider as ConfigServerConfigurationProvider;
if (configServerProvider != null)
break;
}
Assert.NotNull(configServerProvider);
ConfigServerClientSettings settings = configServerProvider.Settings;
// Act and Assert
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal("http://foo.com:9999", settings.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
Assert.Equal("myName", settings.Name);
Assert.Equal("myLabel", settings.Label);
Assert.Equal("myUsername", settings.Username);
Assert.Equal("myPassword", settings.Password);
}
[Fact]
public void AddConfigService_CommandLineAppSettingsConfiguresClient()
{
// Arrange
var appsettings = new string[]
{
"spring:cloud:config:enabled=false",
"--spring:cloud:config:failFast=true",
"/spring:cloud:config:uri=http://foo.com:9999",
"--spring:cloud:config:name", "myName",
"/spring:cloud:config:label", "myLabel",
"--spring:cloud:config:username", "myUsername",
"--spring:cloud:config:password", "myPassword"
};
var configurationBuilder = new ConfigurationBuilder();
var environment = new HostingEnvironment();
configurationBuilder.AddCommandLine(appsettings);
// Act and Assert
configurationBuilder.AddConfigServer(environment);
ConfigServerConfigurationProvider configServerProvider = null;
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
{
configServerProvider = provider as ConfigServerConfigurationProvider;
if (configServerProvider != null)
break;
}
Assert.NotNull(configServerProvider);
ConfigServerClientSettings settings = configServerProvider.Settings;
Assert.False(settings.Enabled);
Assert.True(settings.FailFast);
Assert.Equal("http://foo.com:9999", settings.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
Assert.Equal("myName", settings.Name );
Assert.Equal("myLabel", settings.Label );
Assert.Equal("myUsername", settings.Username);
Assert.Equal("myPassword", settings.Password );
}
}

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

@ -56,29 +56,15 @@ namespace Spring.Extensions.Configuration.Server.Test
{
// Arrange
LoggerFactory logFactory = new LoggerFactory();
ConfigServerClientSettings settings = new ConfigServerClientSettings(logFactory);
ConfigServerClientSettings settings = new ConfigServerClientSettings();
// Act and Assert
var provider = new ConfigServerConfigurationProvider(settings, logFactory);
Assert.NotNull(provider.Logger);
Assert.NotNull(settings.Logger);
}
[Fact]
public void ProvidersConstructor_InitializedWithDefaultsWhenNull()
{
// Arrange
IEnumerable<IConfigurationProvider> providers = null;
ConfigServerConfigurationProvider provider = new ConfigServerConfigurationProvider(providers);
// Act and Assert
ConfigServerTestHelpers.VerifyDefaults(provider.Settings);
}
[Fact]
public void DefaultConstructor_InitializedWithDefaults()
public void DefaultConstructor_InitializedWithDefaultSettings()
{
// Arrange
ConfigServerConfigurationProvider provider = new ConfigServerConfigurationProvider();

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

@ -53,10 +53,10 @@ namespace Spring.Extensions.Configuration.Server.Test
public static void VerifyDefaults(ConfigServerClientSettings settings)
{
Assert.True(settings.Enabled);
Assert.False(settings.FailFast);
Assert.Equal(settings.Uri, "http://localhost:8888");
Assert.Equal(settings.Environment, "Development");
Assert.Equal(ConfigServerClientSettings.DEFAULT_PROVIDER_ENABLED, settings.Enabled);
Assert.Equal(ConfigServerClientSettings.DEFAULT_FAILFAST, settings.FailFast);
Assert.Equal(ConfigServerClientSettings.DEFAULT_URI, settings.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
Assert.Null(settings.Name);
Assert.Null(settings.Label);
Assert.Null(settings.Username);