From b49339e6d5e2ed7b57c5e92d38458f2c42a6d518 Mon Sep 17 00:00:00 2001 From: rstam Date: Thu, 19 Aug 2010 09:49:16 -0400 Subject: [PATCH] Added unit tests for and debugged MongoConnectionStringBuilder. --- MongoDBClient/MongoConnectionStringBuilder.cs | 25 +++- MongoDBClient/MongoServerAddress.cs | 17 ++- MongoDBClientTests/.gitignore | 2 + .../MongoConnectionStringBuilderTests.cs | 139 ++++++++++++++++++ MongoDBClientTests/MongoDBClientTests.csproj | 66 +++++++++ MongoDBClientTests/Properties/AssemblyInfo.cs | 51 +++++++ mongo-csharp-driver.sln | 6 + 7 files changed, 300 insertions(+), 6 deletions(-) create mode 100644 MongoDBClientTests/.gitignore create mode 100644 MongoDBClientTests/MongoConnectionStringBuilderTests.cs create mode 100644 MongoDBClientTests/MongoDBClientTests.csproj create mode 100644 MongoDBClientTests/Properties/AssemblyInfo.cs diff --git a/MongoDBClient/MongoConnectionStringBuilder.cs b/MongoDBClient/MongoConnectionStringBuilder.cs index 9191763e43..565d7c62a7 100644 --- a/MongoDBClient/MongoConnectionStringBuilder.cs +++ b/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://" + - @"((?.+?):(?.+?)@)?" + - @"(?.+?(:\d+)?(,.+?(:\d+)?)*)" + - @"(/(?.+))?"; + @"((?[^:]+):(?[^@]+)@)?" + + @"(?[^:,/]+(:\d+)?(,[^:,/]+(:\d+)?)*)" + + @"(/(?.+))?$"; 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 servers = new List(); foreach (string address in addresses.Split(',')) { - match = Regex.Match(address, @"^(?.+?)(:(?\d+))?$"); + match = Regex.Match(address, @"^(?[^:]+)(:(?\d+))?$"); if (match.Success) { string host = match.Groups["host"].Value; string port = match.Groups["port"].Value; diff --git a/MongoDBClient/MongoServerAddress.cs b/MongoDBClient/MongoServerAddress.cs index e29a094ba4..3c0e8c7470 100644 --- a/MongoDBClient/MongoServerAddress.cs +++ b/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; diff --git a/MongoDBClientTests/.gitignore b/MongoDBClientTests/.gitignore new file mode 100644 index 0000000000..3e35dcb503 --- /dev/null +++ b/MongoDBClientTests/.gitignore @@ -0,0 +1,2 @@ +*.VisualState.xml +TestResult.xml diff --git a/MongoDBClientTests/MongoConnectionStringBuilderTests.cs b/MongoDBClientTests/MongoConnectionStringBuilderTests.cs new file mode 100644 index 0000000000..d9fdc6f7d3 --- /dev/null +++ b/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()); + } + } +} diff --git a/MongoDBClientTests/MongoDBClientTests.csproj b/MongoDBClientTests/MongoDBClientTests.csproj new file mode 100644 index 0000000000..d32ae8591e --- /dev/null +++ b/MongoDBClientTests/MongoDBClientTests.csproj @@ -0,0 +1,66 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {DBF3A41B-D0EE-4CA5-ADA6-EB1A4DE903E1} + Library + Properties + MongoDB.MongoDBClient.Tests + MongoDBClientTests + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {AE5166CD-76B0-4911-BD80-CED9521F37A1} + MongoDBClient + + + + + \ No newline at end of file diff --git a/MongoDBClientTests/Properties/AssemblyInfo.cs b/MongoDBClientTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..dbfdc96d1e --- /dev/null +++ b/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")] diff --git a/mongo-csharp-driver.sln b/mongo-csharp-driver.sln index ab1c38e9d1..83c3c4092c 100644 --- a/mongo-csharp-driver.sln +++ b/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