Browse Source

Added unit tests for and debugged MongoConnectionStringBuilder.

pull/1/head
rstam 15 years ago
parent
commit
b49339e6d5
  1. 25
      MongoDBClient/MongoConnectionStringBuilder.cs
  2. 17
      MongoDBClient/MongoServerAddress.cs
  3. 2
      MongoDBClientTests/.gitignore
  4. 139
      MongoDBClientTests/MongoConnectionStringBuilderTests.cs
  5. 66
      MongoDBClientTests/MongoDBClientTests.csproj
  6. 51
      MongoDBClientTests/Properties/AssemblyInfo.cs
  7. 6
      mongo-csharp-driver.sln

25
MongoDBClient/MongoConnectionStringBuilder.cs

@ -1,4 +1,19 @@
using System;
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -56,9 +71,9 @@ namespace MongoDB.MongoDBClient {
) {
const string pattern =
@"^mongodb://" +
@"((?<username>.+?):(?<password>.+?)@)?" +
@"(?<addresses>.+?(:\d+)?(,.+?(:\d+)?)*)" +
@"(/(?<database>.+))?";
@"((?<username>[^:]+):(?<password>[^@]+)@)?" +
@"(?<addresses>[^:,/]+(:\d+)?(,[^:,/]+(:\d+)?)*)" +
@"(/(?<database>.+))?$";
Match match = Regex.Match(connectionString, pattern);
if (match.Success) {
string username = match.Groups["username"].Value;
@ -67,7 +82,7 @@ namespace MongoDB.MongoDBClient {
string database = match.Groups["database"].Value;
List<MongoServerAddress> servers = new List<MongoServerAddress>();
foreach (string address in addresses.Split(',')) {
match = Regex.Match(address, @"^(?<host>.+?)(:(?<port>\d+))?$");
match = Regex.Match(address, @"^(?<host>[^:]+)(:(?<port>\d+))?$");
if (match.Success) {
string host = match.Groups["host"].Value;
string port = match.Groups["port"].Value;

17
MongoDBClient/MongoServerAddress.cs

@ -1,4 +1,19 @@
using System;
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

2
MongoDBClientTests/.gitignore

@ -0,0 +1,2 @@
*.VisualState.xml
TestResult.xml

139
MongoDBClientTests/MongoConnectionStringBuilderTests.cs

@ -0,0 +1,139 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.MongoDBClient;
namespace MongoDB.MongoDBClient.Tests {
[TestFixture]
public class MongoConnectionStringBuilderTests {
[Test]
public void TestL() {
string connectionString = "mongodb://localhost";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(1, csb.Servers.Count);
Assert.AreEqual("localhost", csb.Servers[0].Host);
Assert.AreEqual(27017, csb.Servers[0].Port);
Assert.IsNull(csb.Database);
Assert.IsNull(csb.Username);
Assert.IsNull(csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestH() {
string connectionString = "mongodb://mongo.xyz.com";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(1, csb.Servers.Count);
Assert.AreEqual("mongo.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(27017, csb.Servers[0].Port);
Assert.IsNull(csb.Database);
Assert.IsNull(csb.Username);
Assert.IsNull(csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestHP() {
string connectionString = "mongodb://mongo.xyz.com:12345";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(1, csb.Servers.Count);
Assert.AreEqual("mongo.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(12345, csb.Servers[0].Port);
Assert.IsNull(csb.Database);
Assert.IsNull(csb.Username);
Assert.IsNull(csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestH1H2() {
string connectionString = "mongodb://mongo1.xyz.com,mongo2.xyz.com";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(2, csb.Servers.Count);
Assert.AreEqual("mongo1.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(27017, csb.Servers[0].Port);
Assert.AreEqual("mongo2.xyz.com", csb.Servers[1].Host);
Assert.AreEqual(27017, csb.Servers[1].Port);
Assert.IsNull(csb.Database);
Assert.IsNull(csb.Username);
Assert.IsNull(csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestH1P1H2P2() {
string connectionString = "mongodb://mongo1.xyz.com:12345,mongo2.xyz.com:23456";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(2, csb.Servers.Count);
Assert.AreEqual("mongo1.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(12345, csb.Servers[0].Port);
Assert.AreEqual("mongo2.xyz.com", csb.Servers[1].Host);
Assert.AreEqual(23456, csb.Servers[1].Port);
Assert.IsNull(csb.Database);
Assert.IsNull(csb.Username);
Assert.IsNull(csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestUPLD() {
string connectionString = "mongodb://userx:pwd@localhost/dbname";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(1, csb.Servers.Count);
Assert.AreEqual("localhost", csb.Servers[0].Host);
Assert.AreEqual(27017, csb.Servers[0].Port);
Assert.AreEqual("dbname", csb.Database);
Assert.AreEqual("userx", csb.Username);
Assert.AreEqual("pwd", csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestUPH1H2D() {
string connectionString = "mongodb://userx:pwd@mongo1.xyz.com,mongo2.xyz.com/dbname";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(2, csb.Servers.Count);
Assert.AreEqual("mongo1.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(27017, csb.Servers[0].Port);
Assert.AreEqual("mongo2.xyz.com", csb.Servers[1].Host);
Assert.AreEqual(27017, csb.Servers[1].Port);
Assert.AreEqual("dbname", csb.Database);
Assert.AreEqual("userx", csb.Username);
Assert.AreEqual("pwd", csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
[Test]
public void TestUPH1P1H2P2D() {
string connectionString = "mongodb://userx:pwd@mongo1.xyz.com:12345,mongo2.xyz.com:23456/dbname";
MongoConnectionStringBuilder csb = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(2, csb.Servers.Count);
Assert.AreEqual("mongo1.xyz.com", csb.Servers[0].Host);
Assert.AreEqual(12345, csb.Servers[0].Port);
Assert.AreEqual("mongo2.xyz.com", csb.Servers[1].Host);
Assert.AreEqual(23456, csb.Servers[1].Port);
Assert.AreEqual("dbname", csb.Database);
Assert.AreEqual("userx", csb.Username);
Assert.AreEqual("pwd", csb.Password);
Assert.AreEqual(connectionString, csb.ToString());
}
}
}

66
MongoDBClientTests/MongoDBClientTests.csproj

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MongoDB.MongoDBClient.Tests</RootNamespace>
<AssemblyName>MongoDBClientTests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.5.5.10112, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MongoConnectionStringBuilderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDBClient\MongoDBClient.csproj">
<Project>{AE5166CD-76B0-4911-BD80-CED9521F37A1}</Project>
<Name>MongoDBClient</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

51
MongoDBClientTests/Properties/AssemblyInfo.cs

@ -0,0 +1,51 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MongoDBClientTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("MongoDBClientTests")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d1d7aede-4d9c-407d-959a-118b166fcf3f")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

6
mongo-csharp-driver.sln

@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDBClient", "MongoDBCli
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDBClientTest", "MongoDBClientTest\MongoDBClientTest.csproj", "{4FC21822-317C-49BF-8B10-150BD9AADFDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDBClientTests", "MongoDBClientTests\MongoDBClientTests.csproj", "{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -19,6 +21,10 @@ Global
{4FC21822-317C-49BF-8B10-150BD9AADFDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FC21822-317C-49BF-8B10-150BD9AADFDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FC21822-317C-49BF-8B10-150BD9AADFDE}.Release|Any CPU.Build.0 = Release|Any CPU
{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save