PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionIdTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 103 lines | 74 code | 14 blank | 15 comment | 1 complexity | e6f7bc7fbac1633e69e2244110de11c1 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Net;
  17. using FluentAssertions;
  18. using MongoDB.Driver.Core.Clusters;
  19. using MongoDB.Driver.Core.Connections;
  20. using MongoDB.Driver.Core.Servers;
  21. using NUnit.Framework;
  22. namespace MongoDB.Driver.Core.Connections
  23. {
  24. [TestFixture]
  25. public class ConnectionIdTests
  26. {
  27. private static readonly ClusterId __clusterId = new ClusterId();
  28. private static readonly ServerId __serverId = new ServerId(__clusterId, new DnsEndPoint("localhost", 27017));
  29. [Test]
  30. public void Constructor_should_throw_an_ArgumentNullException_when_serverId_is_null()
  31. {
  32. Action act = () => new ConnectionId(null);
  33. act.ShouldThrow<ArgumentNullException>();
  34. }
  35. [TestCase(1, 2, 3, 1, 2, 3, true, true)]
  36. [TestCase(1, 2, 3, 4, 2, 3, false, false)]
  37. [TestCase(1, 2, 3, 1, 4, 3, false, false)]
  38. [TestCase(1, 2, 3, 1, 2, 4, true, false)]
  39. public void Equals_should_return_expected_result(
  40. int port1,
  41. int localValue1,
  42. int serverValue1,
  43. int port2,
  44. int localValue2,
  45. int serverValue2,
  46. bool expectedEqualsResult,
  47. bool expectedStructurallyEqualsResult)
  48. {
  49. var clusterId = new ClusterId();
  50. var serverId1 = new ServerId(clusterId, new DnsEndPoint("localhost", port1));
  51. var serverId2 = new ServerId(clusterId, new DnsEndPoint("localhost", port2));
  52. var subject1 = new ConnectionId(serverId1, localValue1).WithServerValue(serverValue1);
  53. var subject2 = new ConnectionId(serverId2, localValue2).WithServerValue(serverValue2);
  54. // note: Equals ignores the server values and StructurallyEquals compares all fields
  55. var equalsResult1 = subject1.Equals(subject2);
  56. var equalsResult2 = subject2.Equals(subject1);
  57. var structurallyEqualsResult1 = subject1.StructurallyEquals(subject2);
  58. var structurallyEqualsResult2 = subject2.StructurallyEquals(subject1);
  59. var hashCode1 = subject1.GetHashCode();
  60. var hashCode2 = subject2.GetHashCode();
  61. equalsResult1.Should().Be(expectedEqualsResult);
  62. equalsResult2.Should().Be(expectedEqualsResult);
  63. structurallyEqualsResult1.Should().Be(expectedStructurallyEqualsResult);
  64. structurallyEqualsResult2.Should().Be(expectedStructurallyEqualsResult);
  65. (hashCode1 == hashCode2).Should().Be(expectedEqualsResult);
  66. }
  67. [Test]
  68. public void LocalValues_of_2_ids_should_not_be_the_same_when_automically_constructed()
  69. {
  70. var subject = new ConnectionId(__serverId);
  71. var subject2 = new ConnectionId(__serverId);
  72. subject.LocalValue.Should().NotBe(subject2.LocalValue);
  73. }
  74. [Test]
  75. public void LocalValue_should_be_what_was_specified_in_the_constructor()
  76. {
  77. var subject = new ConnectionId(__serverId, 10);
  78. subject.LocalValue.Should().Be(10);
  79. }
  80. [Test]
  81. public void WithServerValue_should_set_the_server_value_and_leave_the_LocalValue_alone()
  82. {
  83. var subject = new ConnectionId(__serverId, 10)
  84. .WithServerValue(11);
  85. subject.LocalValue.Should().Be(10);
  86. subject.ServerValue.Should().Be(11);
  87. }
  88. }
  89. }