Skip to content

Commit aa87ba9

Browse files
committed
CSHARP-2756: Implement CompressorConfiguration Equals method.
1 parent 9a47a7b commit aa87ba9

File tree

2 files changed

+177
-4
lines changed

2 files changed

+177
-4
lines changed

src/MongoDB.Driver.Core/Core/Configuration/CompressorConfiguration.cs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Collections.Generic;
1717
using MongoDB.Driver.Core.Compression;
18+
using MongoDB.Shared;
1819

1920
namespace MongoDB.Driver.Core.Configuration
2021
{
@@ -23,24 +24,90 @@ namespace MongoDB.Driver.Core.Configuration
2324
/// </summary>
2425
public sealed class CompressorConfiguration
2526
{
27+
// private fields
28+
private readonly IDictionary<string, object> _properties;
29+
private readonly CompressorType _type;
30+
31+
// constructors
2632
/// <summary>
2733
/// Initializes an instance of <see cref="CompressorConfiguration"/>.
2834
/// </summary>
2935
/// <param name="type">The compressor type.</param>
3036
public CompressorConfiguration(CompressorType type)
3137
{
32-
Type = type;
33-
Properties = new Dictionary<string, object>();
38+
_type = type;
39+
_properties = new Dictionary<string, object>();
3440
}
3541

42+
// public properties
3643
/// <summary>
3744
/// Gets the compression properties.
3845
/// </summary>
39-
public IDictionary<string, object> Properties { get; }
46+
public IDictionary<string, object> Properties => _properties;
4047

4148
/// <summary>
4249
/// Gets the compressor type.
4350
/// </summary>
44-
public CompressorType Type { get; }
51+
public CompressorType Type => _type;
52+
53+
// public methods
54+
/// <inheritdoc/>
55+
public override bool Equals(object obj)
56+
{
57+
if (object.ReferenceEquals(obj, this))
58+
{
59+
return true;
60+
}
61+
62+
if (object.ReferenceEquals(obj, null) || obj.GetType() != typeof(CompressorConfiguration))
63+
{
64+
return false;
65+
}
66+
67+
var rhs = (CompressorConfiguration)obj;
68+
return
69+
_type == rhs._type &&
70+
IsEquivalentTo(_properties, rhs._properties);
71+
}
72+
73+
/// <inheritdoc/>
74+
public override int GetHashCode()
75+
{
76+
return new Hasher()
77+
.Hash(_type)
78+
.HashElements(_properties.Keys) // keep it cheap
79+
.GetHashCode();
80+
}
81+
82+
// private methods
83+
private bool IsEquivalentTo(IDictionary<string, object> x, IDictionary<string, object> y)
84+
{
85+
if (object.ReferenceEquals(x, y))
86+
{
87+
return true;
88+
}
89+
90+
if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null))
91+
{
92+
return false;
93+
}
94+
95+
if (x.Count != y.Count)
96+
{
97+
return false;
98+
}
99+
100+
foreach (var keyValuePair in x)
101+
{
102+
var key = keyValuePair.Key;
103+
var xValue = keyValuePair.Value;
104+
if (!y.TryGetValue(key, out var yValue) || !object.Equals(xValue, yValue))
105+
{
106+
return false;
107+
}
108+
}
109+
110+
return true;
111+
}
45112
}
46113
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* Copyright 2019-present 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+
16+
using FluentAssertions;
17+
using MongoDB.Driver.Core.Compression;
18+
using MongoDB.Driver.Core.Configuration;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Core.Tests.Core.Configuration
22+
{
23+
public class CompressorConfigurationTests
24+
{
25+
[Theory]
26+
[InlineData(CompressorType.Noop)]
27+
[InlineData(CompressorType.Snappy)]
28+
[InlineData(CompressorType.Zlib)]
29+
public void constructor_should_initialize_instance(CompressorType type)
30+
{
31+
var subject = new CompressorConfiguration(type);
32+
33+
subject.Type.Should().Be(type);
34+
subject.Properties.Should().NotBeNull();
35+
subject.Properties.Count.Should().Be(0);
36+
}
37+
38+
[Theory]
39+
[InlineData("Properties")]
40+
[InlineData("Type")]
41+
public void Equals_should_return_false_if_any_field_is_not_equal(string fieldName)
42+
{
43+
var type = CompressorType.Snappy;
44+
var key = "x";
45+
var subject1 = new CompressorConfiguration(type);
46+
subject1.Properties.Add(key, 1);
47+
switch (fieldName)
48+
{
49+
case "Properties": key = "y"; break;
50+
case "Type": type = CompressorType.Zlib; break;
51+
}
52+
var subject2 = new CompressorConfiguration(type);
53+
subject2.Properties.Add(key, 1);
54+
55+
var result = subject1.Equals(subject2);
56+
var hashCode1 = subject1.GetHashCode();
57+
var hashCode2 = subject2.GetHashCode();
58+
59+
result.Should().BeFalse();
60+
hashCode2.Should().NotBe(hashCode1);
61+
}
62+
63+
[Fact]
64+
public void Equals_should_return_false_if_obj_is_null()
65+
{
66+
var subject = new CompressorConfiguration(CompressorType.Noop);
67+
68+
var result = subject.Equals(null);
69+
70+
result.Should().BeFalse();
71+
}
72+
73+
[Fact]
74+
public void Equals_should_return_false_if_obj_is_wrong_type()
75+
{
76+
var subject = new CompressorConfiguration(CompressorType.Noop);
77+
var obj = new object();
78+
79+
var result = subject.Equals(new object());
80+
var hashCode1 = subject.GetHashCode();
81+
var hashCode2 = obj.GetHashCode();
82+
83+
result.Should().BeFalse();
84+
hashCode2.Should().NotBe(hashCode1);
85+
}
86+
87+
[Theory]
88+
[InlineData(CompressorType.Noop)]
89+
[InlineData(CompressorType.Snappy)]
90+
[InlineData(CompressorType.Zlib)]
91+
public void Equals_should_return_true_if_all_fields_are_equal(CompressorType type)
92+
{
93+
var subject1 = new CompressorConfiguration(type);
94+
var subject2 = new CompressorConfiguration(type);
95+
subject1.Properties.Add("x", 1);
96+
subject2.Properties.Add("x", 1);
97+
98+
var result = subject1.Equals(subject2);
99+
var hashCode1 = subject1.GetHashCode();
100+
var hashCode2 = subject2.GetHashCode();
101+
102+
result.Should().BeTrue();
103+
hashCode2.Should().Be(hashCode1);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)