SEC-190: Add hashCode() and equals() methods.

This commit is contained in:
Ben Alex 2006-04-26 01:41:10 +00:00
parent 36c096858d
commit 14683dcbc7
2 changed files with 65 additions and 20 deletions

View File

@ -33,9 +33,6 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
//~ Constructors =========================================================== //~ Constructors ===========================================================
/**
*
*/
public CaptchaSecurityContextImpl() { public CaptchaSecurityContextImpl() {
super(); super();
human = false; human = false;
@ -46,7 +43,7 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
//~ Methods ================================================================ //~ Methods ================================================================
/** /**
* reset the lastPassedCaptchaDate and count. * Reset the lastPassedCaptchaDate and count.
*/ */
public void setHuman() { public void setHuman() {
this.human = true; this.human = true;
@ -54,29 +51,14 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
this.humanRestrictedResourcesRequestsCount = 0; this.humanRestrictedResourcesRequestsCount = 0;
} }
/*
* (non-Javadoc)
*
* @see org.acegisecurity.context.CaptchaSecurityContext#isHuman()
*/
public boolean isHuman() { public boolean isHuman() {
return human; return human;
} }
/*
* (non-Javadoc)
*
* @see org.acegisecurity.context.CaptchaSecurityContext#getHumanRestrictedResourcesRequestsCount()
*/
public int getHumanRestrictedResourcesRequestsCount() { public int getHumanRestrictedResourcesRequestsCount() {
return humanRestrictedResourcesRequestsCount; return humanRestrictedResourcesRequestsCount;
} }
/*
* (non-Javadoc)
*
* @see org.acegisecurity.context.CaptchaSecurityContext#getLastPassedCaptchaDateInMillis()
*/
public long getLastPassedCaptchaDateInMillis() { public long getLastPassedCaptchaDateInMillis() {
return lastPassedCaptchaDate; return lastPassedCaptchaDate;
} }
@ -87,4 +69,31 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
public void incrementHumanRestrictedRessoucesRequestsCount() { public void incrementHumanRestrictedRessoucesRequestsCount() {
humanRestrictedResourcesRequestsCount++; humanRestrictedResourcesRequestsCount++;
} }
public boolean equals(Object obj) {
if (obj instanceof CaptchaSecurityContextImpl) {
CaptchaSecurityContextImpl rhs = (CaptchaSecurityContextImpl) obj;
if (this.isHuman() != rhs.isHuman()) {
return false;
}
if (this.getHumanRestrictedResourcesRequestsCount() != rhs.getHumanRestrictedResourcesRequestsCount()) {
return false;
}
if (this.getLastPassedCaptchaDateInMillis() != rhs.getLastPassedCaptchaDateInMillis()) {
return false;
}
return super.equals(obj);
}
return false;
}
public int hashCode() {
int code = super.hashCode();
code ^= this.humanRestrictedResourcesRequestsCount;
code ^= this.lastPassedCaptchaDate;
if (this.isHuman()) {
code ^= -37;
}
return code;
}
} }

View File

@ -1,4 +1,4 @@
/* Copyright 2004, 2005 Acegi Technology Pty Limited /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,42 @@ public class CaptchaSecurityContextImplTests extends SecurityContextImplTests {
context.getHumanRestrictedResourcesRequestsCount()); context.getHumanRestrictedResourcesRequestsCount());
} }
public void testEquals() {
CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
assertEquals(context1, context2);
assertFalse(context1.isHuman());
context1.setHuman();
assertNotSame(context1, context2);
// Get fresh copy
context1 = new CaptchaSecurityContextImpl();
assertEquals(context1, context2);
context1.incrementHumanRestrictedRessoucesRequestsCount();
assertNotSame(context1, context2);
}
public void testHashcode() {
CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
assertEquals(context1.hashCode(), context2.hashCode());
assertFalse(context1.isHuman());
context1.setHuman();
assertTrue(context1.hashCode() != context2.hashCode());
// Get fresh copy
context1 = new CaptchaSecurityContextImpl();
assertEquals(context1.hashCode(), context2.hashCode());
context1.incrementHumanRestrictedRessoucesRequestsCount();
assertTrue(context1 != context2);
}
public void testIncrementRequests() { public void testIncrementRequests() {
CaptchaSecurityContext context = new CaptchaSecurityContextImpl(); CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
context.setHuman(); context.setHuman();