Author: olegk Date: Wed Jun 22 12:38:51 2005 New Revision: 192979 URL: http://svn.apache.org/viewcvs?rev=192979&view=rev Log: PR #35365 (No equals operation for Credentials implementations)
Contributed by Eric Johnson <eric at tibco.com> Reviewed by Oleg Kalnichevski & Michael Becke Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/NTCredentials.java jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestCredentials.java Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/NTCredentials.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/NTCredentials.java?rev=192979&r1=192978&r2=192979&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/NTCredentials.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/NTCredentials.java Wed Jun 22 12:38:51 2005 @@ -29,6 +29,8 @@ package org.apache.commons.httpclient; +import org.apache.commons.httpclient.util.LangUtils; + /** [EMAIL PROTECTED] 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 <code>true</code> 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; + } } Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java?rev=192979&r1=192978&r2=192979&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java Wed Jun 22 12:38:51 2005 @@ -29,6 +29,8 @@ package org.apache.commons.httpclient; +import org.apache.commons.httpclient.util.LangUtils; + /** * <p>Username and password [EMAIL PROTECTED] Credentials}.</p> * @@ -169,6 +171,44 @@ result.append(":"); result.append((this.password == null) ? "null" : this.password); 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. + * + * @return <code>true</code> if the object is equivalent. + */ + public boolean equals(Object o) { + if (o == this) { + return true; + } + + // note - to allow for sub-classing, this checks that class is the same + // rather than do "instanceof". + if (o.getClass() == this.getClass() ) { + UsernamePasswordCredentials that = (UsernamePasswordCredentials) o; + + if (LangUtils.equals(this.userName, that.userName) + && LangUtils.equals(this.password, that.password) ) { + return true; + } + } + return false; } } Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestCredentials.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestCredentials.java?rev=192979&r1=192978&r2=192979&view=diff ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestCredentials.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestCredentials.java Wed Jun 22 12:38:51 2005 @@ -86,4 +86,32 @@ 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, creds1); + assertNotSame(creds1, ntCreds1); + assertNotSame(ntCreds1, ntCreds2); + assertNotSame(ntCreds1, ntCreds3); + assertNotSame(ntCreds1, ntCreds4); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]