Skip to content

Commit

Permalink
Change default configuration section name to mongo instead of Mongo w…
Browse files Browse the repository at this point in the history
…hich match the nameingconventions in app.config files.
  • Loading branch information
lanwin committed May 25, 2010
1 parent b2c7bfb commit 7e1aeeb
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 29 deletions.
8 changes: 4 additions & 4 deletions source/MongoDB.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Mongo" type="MongoDB.Configuration.MongoConfigurationSection, MongoDB" />
<section name="mongo" type="MongoDB.Configuration.MongoConfigurationSection, MongoDB" />
</configSections>
<Mongo>
<mongo>
<connections>
<add key="local21018" connectionString="Server=localhost:27018" />
<add key="auth" connectionString="Server=localhost:27019" />
<add key="defaults" />
<add key="default" />
<add key="tests" connectionString="Server=localhost:27017" />
</connections>
</Mongo>
</mongo>
<appSettings>
<add key="tests" value="Server=localhost:27017" />
<add key="auth" value="Server=localhost:27018" />
Expand Down
5 changes: 4 additions & 1 deletion source/MongoDB.Tests/MongoDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<Compile Include="UnitTests\TestBinary.cs" />
<Compile Include="UnitTests\TestOp.cs" />
<Compile Include="UnitTests\Util\TestJsonUtils.cs" />
<Compile Include="UnitTests\Configuration\TestConfigurationSection.cs" />
<Compile Include="UnitTests\Configuration\ConfigurationSectionTests.cs" />
<Compile Include="UnitTests\TestMongoSymbol.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -212,6 +212,9 @@
<DeployService-RelativeDeployPath>MongoDB.Driver.Tests.dll.config</DeployService-RelativeDeployPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Test-Sections\Test1.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup />
</Project>
31 changes: 31 additions & 0 deletions source/MongoDB.Tests/Test-Sections/Test1.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Mongo" type="MongoDB.Configuration.MongoConfigurationSection, MongoDB" />
</configSections>
<Mongo>
<connections>
<add key="local21018" connectionString="Server=localhost:27018" />
<add key="auth" connectionString="Server=localhost:27019" />
<add key="default" />
<add key="tests" connectionString="Server=localhost:27017" />
</connections>
</Mongo>
<appSettings>
<add key="tests" value="Server=localhost:27017" />
<add key="auth" value="Server=localhost:27018" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Configuration;
using System.IO;
using MongoDB.Configuration;
using NUnit.Framework;

namespace MongoDB.UnitTests.Configuration
{
[TestFixture]
public class ConfigurationSectionTests
{
private static MongoConfigurationSection ReadFromFile(string name)
{
return ReadFromFile(name, MongoConfigurationSection.DefaultSectionName);
}

private static MongoConfigurationSection ReadFromFile(string name, string section)
{
var map = new ExeConfigurationFileMap {ExeConfigFilename = Path.Combine("Test-Sections", name)};
var exeConfiguration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
return exeConfiguration.GetSection(section) as MongoConfigurationSection;
}

[Test]
public void CanReadDefaultFromTestsConfig()
{
var config = MongoConfigurationSection.GetSection();
Assert.IsNotNull(config);
Assert.AreEqual("Server=localhost:27017", config.Connections["default"].ConnectionString);
}

[Test]
public void CanReadlocal21018FromTestsConfig()
{
var config = MongoConfigurationSection.GetSection();
Assert.IsNotNull(config);
Assert.AreEqual("Server=localhost:27018", config.Connections["local21018"].ConnectionString);
}
}
}

This file was deleted.

39 changes: 38 additions & 1 deletion source/MongoDB/Configuration/MongoConfigurationSection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Configuration;
using System.Linq;

namespace MongoDB.Configuration
{
Expand All @@ -7,6 +9,11 @@ namespace MongoDB.Configuration
/// </summary>
public class MongoConfigurationSection : ConfigurationSection
{
/// <summary>
///
/// </summary>
public const string DefaultSectionName = "mongo";

/// <summary>
/// Gets the connections.
/// </summary>
Expand All @@ -26,7 +33,7 @@ public ConnectionCollection Connections{
/// <returns></returns>
public static MongoConfigurationSection GetSection()
{
return GetSection("Mongo");
return GetSection(DefaultSectionName);
}

/// <summary>
Expand All @@ -38,5 +45,35 @@ public static MongoConfigurationSection GetSection(string name)
{
return ConfigurationManager.GetSection(name) as MongoConfigurationSection;
}

/// <summary>
/// Creates the configuration.
/// </summary>
/// <returns></returns>
public MongoConfiguration CreateConfiguration()
{
var configuration = new MongoConfiguration();

UpdateConfiguration(configuration);

return configuration;
}

/// <summary>
/// Updates the configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
public void UpdateConfiguration(MongoConfiguration configuration)
{
if(configuration == null)
throw new ArgumentNullException("configuration");

if(Connections!=null)
{
var connection = Connections.Cast<ConnectionElement>().FirstOrDefault(c=>c.IsDefault);
if(connection != null)
connection.ConnectionString = connection.ConnectionString;
}
}
}
}

0 comments on commit 7e1aeeb

Please sign in to comment.