Skip to content

Commit 41c37c3

Browse files
committed
In order to avoid equality testing issues when passed ServerAddress subclass instances, construct new ServerAddress instances in ClusterSettings.Builder.hosts method.
JAVA-1814
1 parent eba932a commit 41c37c3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

driver-core/src/main/com/mongodb/connection/ClusterSettings.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
28+
import java.util.Set;
2829
import java.util.concurrent.TimeUnit;
2930

3031
import static com.mongodb.assertions.Assertions.isTrueArgument;
@@ -94,7 +95,11 @@ public Builder hosts(final List<ServerAddress> hosts) {
9495
if (hosts.isEmpty()) {
9596
throw new IllegalArgumentException("hosts list may not be empty");
9697
}
97-
this.hosts = Collections.unmodifiableList(new ArrayList<ServerAddress>(new LinkedHashSet<ServerAddress>(hosts)));
98+
Set<ServerAddress> hostsSet = new LinkedHashSet<ServerAddress>(hosts.size());
99+
for (ServerAddress host : hosts) {
100+
hostsSet.add(new ServerAddress(host.getHost(), host.getPort()));
101+
}
102+
this.hosts = Collections.unmodifiableList(new ArrayList<ServerAddress>(hostsSet));
98103
return this;
99104
}
100105

driver-core/src/test/unit/com/mongodb/connection/ClusterSettingsSpecification.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,19 @@ class ClusterSettingsSpecification extends Specification {
278278
.maxWaitQueueSize(100)
279279
.build().hashCode() != ClusterSettings.builder().hosts(hosts).build().hashCode()
280280
}
281+
282+
def 'should replace ServerAddress subclass instances with ServerAddress'() {
283+
when:
284+
def settings = ClusterSettings.builder().hosts([new ServerAddressSubclass('server1'),
285+
new ServerAddressSubclass('server2')]).build();
286+
287+
then:
288+
settings.getHosts() == [new ServerAddress('server1'), new ServerAddress('server2')]
289+
}
290+
291+
static class ServerAddressSubclass extends ServerAddress {
292+
ServerAddressSubclass(final String host) {
293+
super(host)
294+
}
295+
}
281296
}

0 commit comments

Comments
 (0)