/src/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionIdTests.cs
C# | 103 lines | 74 code | 14 blank | 15 comment | 1 complexity | e6f7bc7fbac1633e69e2244110de11c1 MD5 | raw file
Possible License(s): Apache-2.0
- /* Copyright 2013-2014 MongoDB 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.Net;
- using FluentAssertions;
- using MongoDB.Driver.Core.Clusters;
- using MongoDB.Driver.Core.Connections;
- using MongoDB.Driver.Core.Servers;
- using NUnit.Framework;
- namespace MongoDB.Driver.Core.Connections
- {
- [TestFixture]
- public class ConnectionIdTests
- {
- private static readonly ClusterId __clusterId = new ClusterId();
- private static readonly ServerId __serverId = new ServerId(__clusterId, new DnsEndPoint("localhost", 27017));
- [Test]
- public void Constructor_should_throw_an_ArgumentNullException_when_serverId_is_null()
- {
- Action act = () => new ConnectionId(null);
- act.ShouldThrow<ArgumentNullException>();
- }
- [TestCase(1, 2, 3, 1, 2, 3, true, true)]
- [TestCase(1, 2, 3, 4, 2, 3, false, false)]
- [TestCase(1, 2, 3, 1, 4, 3, false, false)]
- [TestCase(1, 2, 3, 1, 2, 4, true, false)]
- public void Equals_should_return_expected_result(
- int port1,
- int localValue1,
- int serverValue1,
- int port2,
- int localValue2,
- int serverValue2,
- bool expectedEqualsResult,
- bool expectedStructurallyEqualsResult)
- {
- var clusterId = new ClusterId();
- var serverId1 = new ServerId(clusterId, new DnsEndPoint("localhost", port1));
- var serverId2 = new ServerId(clusterId, new DnsEndPoint("localhost", port2));
- var subject1 = new ConnectionId(serverId1, localValue1).WithServerValue(serverValue1);
- var subject2 = new ConnectionId(serverId2, localValue2).WithServerValue(serverValue2);
- // note: Equals ignores the server values and StructurallyEquals compares all fields
- var equalsResult1 = subject1.Equals(subject2);
- var equalsResult2 = subject2.Equals(subject1);
- var structurallyEqualsResult1 = subject1.StructurallyEquals(subject2);
- var structurallyEqualsResult2 = subject2.StructurallyEquals(subject1);
- var hashCode1 = subject1.GetHashCode();
- var hashCode2 = subject2.GetHashCode();
- equalsResult1.Should().Be(expectedEqualsResult);
- equalsResult2.Should().Be(expectedEqualsResult);
- structurallyEqualsResult1.Should().Be(expectedStructurallyEqualsResult);
- structurallyEqualsResult2.Should().Be(expectedStructurallyEqualsResult);
- (hashCode1 == hashCode2).Should().Be(expectedEqualsResult);
- }
- [Test]
- public void LocalValues_of_2_ids_should_not_be_the_same_when_automically_constructed()
- {
- var subject = new ConnectionId(__serverId);
- var subject2 = new ConnectionId(__serverId);
- subject.LocalValue.Should().NotBe(subject2.LocalValue);
- }
- [Test]
- public void LocalValue_should_be_what_was_specified_in_the_constructor()
- {
- var subject = new ConnectionId(__serverId, 10);
- subject.LocalValue.Should().Be(10);
- }
- [Test]
- public void WithServerValue_should_set_the_server_value_and_leave_the_LocalValue_alone()
- {
- var subject = new ConnectionId(__serverId, 10)
- .WithServerValue(11);
- subject.LocalValue.Should().Be(10);
- subject.ServerValue.Should().Be(11);
- }
- }
- }