Skip to content

Commit 2b3e754

Browse files
smolarozza
authored andcommitted
Add equals() and hashCode() to MongoClientURI.
1 parent 22a0a66 commit 2b3e754

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/main/com/mongodb/MongoClientURI.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,4 +653,48 @@ public String toString() {
653653
return uri;
654654
}
655655

656+
@Override
657+
public boolean equals(Object o) {
658+
if (this == o) {
659+
return true;
660+
}
661+
if (o == null || getClass() != o.getClass()) {
662+
return false;
663+
}
664+
665+
MongoClientURI that = (MongoClientURI) o;
666+
667+
if (collection != null ? !collection.equals(that.collection) : that.collection != null) {
668+
return false;
669+
}
670+
if (credentials != null ? !credentials.equals(that.credentials) : that.credentials != null) {
671+
return false;
672+
}
673+
if (database != null ? !database.equals(that.database) : that.database != null) {
674+
return false;
675+
}
676+
if (hosts != null ? !hosts.equals(that.hosts) : that.hosts != null) {
677+
return false;
678+
}
679+
if (options != null ? !options.equals(that.options) : that.options != null) {
680+
return false;
681+
}
682+
if (uri != null ? !uri.equals(that.uri) : that.uri != null) {
683+
return false;
684+
}
685+
686+
return true;
687+
}
688+
689+
@Override
690+
public int hashCode() {
691+
int result = options != null ? options.hashCode() : 0;
692+
result = 31 * result + (credentials != null ? credentials.hashCode() : 0);
693+
result = 31 * result + (hosts != null ? hosts.hashCode() : 0);
694+
result = 31 * result + (database != null ? database.hashCode() : 0);
695+
result = 31 * result + (collection != null ? collection.hashCode() : 0);
696+
result = 31 * result + (uri != null ? uri.hashCode() : 0);
697+
return result;
698+
}
699+
656700
}

src/test/com/mongodb/MongoClientURITest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.Assert.assertArrayEquals;
2626
import static org.junit.Assert.assertEquals;
2727
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertNotEquals;
2829
import static org.junit.Assert.assertNull;
2930
import static org.junit.Assert.assertTrue;
3031
import static org.junit.Assert.fail;
@@ -327,6 +328,31 @@ public void testMultipleIPV6ServersWithPorts() {
327328
assertEquals("[2010:836B:4179::836B:4179]:2000", u.getHosts().get(1));
328329
}
329330

331+
@Test
332+
public void testEqualsAndHashCode() {
333+
MongoClientURI uris[] = new MongoClientURI[] {
334+
new MongoClientURI("mongodb://user:pass@[2010:836B:4179::836B:4179]"),
335+
new MongoClientURI("mongodb://localhost/?readPreference=secondaryPreferred"),
336+
new MongoClientURI("mongodb://[::1]:1000,[2010:836B:4179::836B:4179]:2000"),
337+
new MongoClientURI("mongodb://localhost/?" +
338+
"maxPoolSize=10;waitQueueMultiple=5;waitQueueTimeoutMS=150;" +
339+
"minPoolSize=7;maxIdleTimeMS=1000;maxLifeTimeMS=2000;" +
340+
"replicaSet=test;" +
341+
"connectTimeoutMS=2500;socketTimeoutMS=5500;autoConnectRetry=true;" +
342+
"slaveOk=true;safe=false;w=1;wtimeout=2500;fsync=true")
343+
};
344+
for (MongoClientURI uri : uris) {
345+
assertEquals(uri, uri);
346+
MongoClientURI reinstantiatedUri = new MongoClientURI(uri.getURI());
347+
assertEquals(uri, reinstantiatedUri);
348+
assertEquals(uri.hashCode(), reinstantiatedUri.hashCode());
349+
for (MongoClientURI anotherURI : uris) {
350+
if (uri == anotherURI) continue;
351+
assertNotEquals(uri, anotherURI);
352+
}
353+
}
354+
}
355+
330356
@SuppressWarnings("deprecation")
331357
private void assertOnOptions(MongoClientOptions options) {
332358
assertEquals(10, options.getConnectionsPerHost(), 10);

0 commit comments

Comments
 (0)