Index: src/test/org/apache/commons/httpclient/TestCredentials.java
===================================================================
--- src/test/org/apache/commons/httpclient/TestCredentials.java (revision 190665)
+++ src/test/org/apache/commons/httpclient/TestCredentials.java (working copy)
@@ -86,4 +86,30 @@
assertNotNull(creds.getHost());
}
+ /**
+ * Verifies that credentials report equal when they should.
+ */
+ public void testCredentialEquals() {
+
+ Credentials creds1 = new UsernamePasswordCredentials("user1", "password1");
+ Credentials creds1Again = new UsernamePasswordCredentials("user1", "password1");
+ Credentials creds2 = new UsernamePasswordCredentials("user2", "password2");
+ Credentials creds3 = new UsernamePasswordCredentials("user3", null);
+ Credentials creds3Again = new UsernamePasswordCredentials("user3", null);
+
+ assertEquals(creds1, creds1Again);
+ assertNotSame(creds1, creds2);
+ assertEquals(creds3, creds3Again);
+
+ Credentials ntCreds1 = new NTCredentials("user1", "password1", "host1", "domain1");
+ Credentials ntCreds1Again = new NTCredentials("user1", "password1", "host1", "domain1");
+ Credentials ntCreds2 = new NTCredentials("user1", "password2", "host1", "domain1");
+ Credentials ntCreds3 = new NTCredentials("user1", "password1", "host2", "domain1");
+ Credentials ntCreds4 = new NTCredentials("user1", "password1", "host1", "domain2");
+
+ assertEquals(ntCreds1, ntCreds1Again);
+ assertNotSame(ntCreds1, ntCreds2);
+ assertNotSame(ntCreds1, ntCreds3);
+ assertNotSame(ntCreds1, ntCreds4);
+ }
}
Index: src/java/org/apache/commons/httpclient/NTCredentials.java
===================================================================
--- src/java/org/apache/commons/httpclient/NTCredentials.java (revision 190665)
+++ src/java/org/apache/commons/httpclient/NTCredentials.java (working copy)
@@ -29,6 +29,8 @@
package org.apache.commons.httpclient;
+import org.apache.commons.httpclient.util.LangUtils;
+
/** {@link Credentials} for use with the NTLM authentication scheme which requires additional
* information.
*
@@ -152,4 +154,39 @@
return sbResult.toString();
}
+ /**
+ * Computes a hash code based on all the case-sensitive parts of the credentials object.
+ *
+ * @return The hash code for the credentials.
+ */
+ public int hashCode() {
+ int hash = super.hashCode();
+ hash = LangUtils.hashCode(hash, this.host);
+ hash = LangUtils.hashCode(hash, this.domain);
+ return hash;
+ }
+
+ /**
+ * Performs a case-sensitive check to see if the components of the credentials
+ * are the same.
+ *
+ * @param o The object to match.
+ *
+ * @return true if all of the credentials match.
+ */
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (super.equals(o) ) {
+ if (o instanceof NTCredentials) {
+ NTCredentials that = (NTCredentials) o;
+
+ return LangUtils.equals(this.domain, that.domain)
+ && LangUtils.equals(this.host, that.host);
+ }
+ }
+
+ return false;
+ }
}
Index: src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java
===================================================================
--- src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java (revision 190665)
+++ src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java (working copy)
@@ -29,6 +29,8 @@
package org.apache.commons.httpclient;
+import org.apache.commons.httpclient.util.LangUtils;
+
/**
*
Username and password {@link Credentials}.
* @@ -171,5 +173,40 @@ return result.toString(); } + /** + * Does a hash of both user name and password. + * + * @return The hash code including user name and password. + */ + public int hashCode() { + int hash = LangUtils.HASH_SEED; + hash = LangUtils.hashCode(hash, this.userName); + hash = LangUtils.hashCode(hash, this.password); + return hash; + } + + /** + * These credentials are assumed equal if the username and password are the + * same. + * + * @param o The other object to compare with. + * + * @returntrue if the object is equivalent.
+ */
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (o instanceof UsernamePasswordCredentials) {
+ UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
+
+ if (LangUtils.equals(this.userName, that.userName)
+ && LangUtils.equals(this.password, that.password) ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
}