Skip to content

Commit 566c6b5

Browse files
author
rstam
committed
CSHARP-610: merged pull request 134 and added more unit tests.
1 parent a059c7c commit 566c6b5

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

Driver/Core/MongoServerAddress.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public static MongoServerAddress Parse(string value)
7171
}
7272
else
7373
{
74-
throw new FormatException(String.Format("'{0}' is not a valid server address.", value));
74+
var message = string.Format("'{0}' is not a valid server address.", value);
75+
throw new FormatException(message);
7576
}
7677
}
7778

@@ -86,7 +87,7 @@ public static bool TryParse(string value, out MongoServerAddress address)
8687
// don't throw ArgumentNullException if value is null
8788
if (value != null)
8889
{
89-
Match match = Regex.Match(value, @"^(?<host>(\[[^]]+?\]|[^:]+))(:(?<port>\d+))?$");
90+
Match match = Regex.Match(value, @"^(?<host>(\[[^]]+\]|[^:\[\]]+))(:(?<port>\d+))?$");
9091
if (match.Success)
9192
{
9293
string host = match.Groups["host"].Value;

DriverUnitTests/Core/MongoServerAddressTests.cs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,28 @@ namespace MongoDB.DriverUnitTests
2929
public class MongoServerAddressTests
3030
{
3131
[Test]
32-
public void TestCreateWithHost()
32+
[TestCase("host")]
33+
[TestCase("192.168.0.1")]
34+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]")]
35+
public void TestConstructor(string host)
3336
{
34-
var credentials = new MongoServerAddress("host");
35-
Assert.AreEqual("host", credentials.Host);
36-
Assert.AreEqual(27017, credentials.Port);
37+
var address = new MongoServerAddress(host);
38+
Assert.AreEqual(host, address.Host);
39+
Assert.AreEqual(27017, address.Port);
3740
}
3841

3942
[Test]
40-
public void TestCreateWithHostAndPort()
43+
[TestCase("host", 27017)]
44+
[TestCase("host", 27018)]
45+
[TestCase("192.168.0.1", 27017)]
46+
[TestCase("192.168.0.1", 27018)]
47+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]", 27017)]
48+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]", 27018)]
49+
public void TestConstructor(string host, int port)
4150
{
42-
var credentials = new MongoServerAddress("host", 123);
43-
Assert.AreEqual("host", credentials.Host);
44-
Assert.AreEqual(123, credentials.Port);
51+
var address = new MongoServerAddress(host, port);
52+
Assert.AreEqual(host, address.Host);
53+
Assert.AreEqual(port, address.Port);
4554
}
4655

4756
[Test]
@@ -73,35 +82,35 @@ public void TestEquals()
7382
}
7483

7584
[Test]
76-
public void TestParseWithHost()
85+
[TestCase("host", 27017, "host")]
86+
[TestCase("host", 27017, "host:27017")]
87+
[TestCase("host", 27018, "host:27018")]
88+
[TestCase("192.168.0.1", 27017, "192.168.0.1")]
89+
[TestCase("192.168.0.1", 27017, "192.168.0.1:27017")]
90+
[TestCase("192.168.0.1", 27018, "192.168.0.1:27018")]
91+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]", 27017, "[2001:0db8:85a3:0042:0000:8a2e:0370:7334]")]
92+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]", 27017, "[2001:0db8:85a3:0042:0000:8a2e:0370:7334]:27017")]
93+
[TestCase("[2001:0db8:85a3:0042:0000:8a2e:0370:7334]", 27018, "[2001:0db8:85a3:0042:0000:8a2e:0370:7334]:27018")]
94+
public void TestParse(string host, int port, string value)
7795
{
78-
var credentials = MongoServerAddress.Parse("host");
79-
Assert.AreEqual("host", credentials.Host);
80-
Assert.AreEqual(27017, credentials.Port);
96+
var address = MongoServerAddress.Parse(value);
97+
Assert.AreEqual(host, address.Host);
98+
Assert.AreEqual(port, address.Port);
8199
}
82100

83101
[Test]
84-
public void TestParseWithHostAndPort()
102+
[TestCase(null)]
103+
[TestCase("")]
104+
[TestCase("abc:def")]
105+
[TestCase("abc:123:456")]
106+
[TestCase("[]")]
107+
[TestCase("a[]")]
108+
[TestCase("[]b")]
109+
[TestCase("a[]b")]
110+
public void TestParse_InvalidValue(string value)
85111
{
86-
var credentials = MongoServerAddress.Parse("host:123");
87-
Assert.AreEqual("host", credentials.Host);
88-
Assert.AreEqual(123, credentials.Port);
89-
}
90-
91-
[Test]
92-
[ExpectedException(ExpectedException = typeof(FormatException),
93-
ExpectedMessage = "'' is not a valid server address.")]
94-
public void TestParseNullParam()
95-
{
96-
MongoServerAddress.Parse(null);
97-
}
98-
99-
[Test]
100-
[ExpectedException(ExpectedException = typeof(FormatException),
101-
ExpectedMessage = "'' is not a valid server address.")]
102-
public void TestParseEmptyParam()
103-
{
104-
MongoServerAddress.Parse(String.Empty);
112+
var message = string.Format("'{0}' is not a valid server address.", value);
113+
Assert.Throws<FormatException>(() => { var address = MongoServerAddress.Parse(value); }, message);
105114
}
106115
}
107116
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ http://jira.mongodb.org/browse/CSHARP
2727
* Teun Duynstee [email protected]
2828
* Ken Egozi [email protected]
2929
* Simon Green [email protected]
30+
* Nik Kolev [email protected]
3031
* Oleg Kosmakov [email protected]
3132
* Brian Knight [email protected]
3233
* Richard Kreuter [email protected]

0 commit comments

Comments
 (0)