Skip to content

Commit c8d013a

Browse files
committed
Updated equals and hashcode for MongoCredential
userName can now be null so need to null check first. JAVA-2375
1 parent 283ec1d commit c8d013a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

driver-core/src/main/com/mongodb/MongoCredential.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public boolean equals(final Object o) {
408408
if (!source.equals(that.source)) {
409409
return false;
410410
}
411-
if (!userName.equals(that.userName)) {
411+
if (userName != null ? !userName.equals(that.userName) : that.userName != null) {
412412
return false;
413413
}
414414
if (!mechanismProperties.equals(that.mechanismProperties)) {
@@ -421,7 +421,7 @@ public boolean equals(final Object o) {
421421
@Override
422422
public int hashCode() {
423423
int result = mechanism != null ? mechanism.hashCode() : 0;
424-
result = 31 * result + userName.hashCode();
424+
result = 31 * result + (userName != null ? userName.hashCode() : 0);
425425
result = 31 * result + source.hashCode();
426426
result = 31 * result + (password != null ? Arrays.hashCode(password) : 0);
427427
result = 31 * result + mechanismProperties.hashCode();

driver-core/src/test/unit/com/mongodb/MongoCredentialSpecification.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,28 @@ class MongoCredentialSpecification extends Specification {
280280

281281
!credentialOne.toString().contains(password)
282282
}
283+
284+
def 'testEqualsAndHashCode'() {
285+
expect:
286+
credential() == credential()
287+
credential().hashCode() == credential().hashCode()
288+
289+
where:
290+
credential << [
291+
{ MongoCredential.createCredential('user', 'database', 'pwd'.toCharArray()) },
292+
{ MongoCredential.createCredential('user', 'database', 'pwd'.toCharArray()).withMechanismProperty('foo', 'bar') },
293+
{ MongoCredential.createMongoCRCredential('user', 'database', 'pwd'.toCharArray()) },
294+
{ MongoCredential.createMongoCRCredential('user', 'database', 'pwd'.toCharArray()).withMechanismProperty('foo', 'bar') },
295+
{ MongoCredential.createPlainCredential('user', '$external', 'pwd'.toCharArray()) },
296+
{ MongoCredential.createPlainCredential('user', '$external', 'pwd'.toCharArray()).withMechanismProperty('foo', 'bar') },
297+
{ MongoCredential.createScramSha1Credential('user', '$external', 'pwd'.toCharArray()) },
298+
{ MongoCredential.createScramSha1Credential('user', '$external', 'pwd'.toCharArray()).withMechanismProperty('foo', 'bar') },
299+
{ MongoCredential.createGSSAPICredential('user') },
300+
{ MongoCredential.createGSSAPICredential('user').withMechanismProperty('foo', 'bar') },
301+
{ MongoCredential.createMongoX509Credential('user') },
302+
{ MongoCredential.createMongoX509Credential('user').withMechanismProperty('foo', 'bar') },
303+
{ MongoCredential.createMongoX509Credential() },
304+
{ MongoCredential.createMongoX509Credential().withMechanismProperty('foo', 'bar') },
305+
]
306+
}
283307
}

0 commit comments

Comments
 (0)