Skip to content

Commit 862da58

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 c2f679a commit 862da58

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/com/mongodb/ClusterSettings.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import org.bson.util.annotations.Immutable;
2020

21+
import java.net.UnknownHostException;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.LinkedHashSet;
2425
import java.util.List;
26+
import java.util.Set;
2527

2628
import static org.bson.util.Assertions.isTrueArgument;
2729
import static org.bson.util.Assertions.notNull;
@@ -65,7 +67,15 @@ public Builder hosts(final List<ServerAddress> hosts) {
6567
if (hosts.isEmpty()) {
6668
throw new IllegalArgumentException("hosts list may not be empty");
6769
}
68-
this.hosts = Collections.unmodifiableList(new ArrayList<ServerAddress>(new LinkedHashSet<ServerAddress>(hosts)));
70+
Set<ServerAddress> hostsSet = new LinkedHashSet<ServerAddress>(hosts.size());
71+
for (ServerAddress host : hosts) {
72+
try {
73+
hostsSet.add(new ServerAddress(host.getHost(), host.getPort()));
74+
} catch (UnknownHostException e) {
75+
throw new MongoInternalException("This can't happen", e);
76+
}
77+
}
78+
this.hosts = Collections.unmodifiableList(new ArrayList<ServerAddress>(hostsSet));
6979
return this;
7080
}
7181

@@ -231,4 +241,4 @@ else if (builder.requiredClusterType != ClusterType.ReplicaSet) {
231241
requiredReplicaSetName = builder.requiredReplicaSetName;
232242
requiredClusterType = builder.requiredClusterType;
233243
}
234-
}
244+
}

src/test/com/mongodb/ClusterSettingsSpecification.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,13 @@ class ClusterSettingsSpecification extends Specification {
110110
then:
111111
settings.getHosts() == [new ServerAddress('server1'), new ServerAddress('server2')]
112112
}
113+
114+
def 'should replace ServerAddress subclass instances with ServerAddress'() {
115+
when:
116+
def settings = ClusterSettings.builder().hosts([new DBAddress('server1/mydb'),
117+
new DBAddress('server2/mydb')]).build();
118+
119+
then:
120+
settings.getHosts() == [new ServerAddress('server1'), new ServerAddress('server2')]
121+
}
113122
}

0 commit comments

Comments
 (0)