Explorar el Código

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

Ben Alex hace 19 años
padre
commit
14683dcbc7

+ 28 - 19
core/src/main/java/org/acegisecurity/captcha/CaptchaSecurityContextImpl.java

@@ -33,9 +33,6 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
 
     //~ Constructors ===========================================================
 
-    /**
-     *
-     */
     public CaptchaSecurityContextImpl() {
         super();
         human = false;
@@ -46,7 +43,7 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
     //~ Methods ================================================================
 
     /**
-     * reset the lastPassedCaptchaDate and count.
+     * Reset the lastPassedCaptchaDate and count.
      */
     public void setHuman() {
         this.human = true;
@@ -54,29 +51,14 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
         this.humanRestrictedResourcesRequestsCount = 0;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see org.acegisecurity.context.CaptchaSecurityContext#isHuman()
-     */
     public boolean isHuman() {
         return human;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see org.acegisecurity.context.CaptchaSecurityContext#getHumanRestrictedResourcesRequestsCount()
-     */
     public int getHumanRestrictedResourcesRequestsCount() {
         return humanRestrictedResourcesRequestsCount;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see org.acegisecurity.context.CaptchaSecurityContext#getLastPassedCaptchaDateInMillis()
-     */
     public long getLastPassedCaptchaDateInMillis() {
         return lastPassedCaptchaDate;
     }
@@ -87,4 +69,31 @@ public class CaptchaSecurityContextImpl extends SecurityContextImpl
     public void incrementHumanRestrictedRessoucesRequestsCount() {
         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;
+	}
 }

+ 37 - 1
core/src/test/java/org/acegisecurity/captcha/CaptchaSecurityContextImplTests.java

@@ -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");
  * you may not use this file except in compliance with the License.
@@ -36,6 +36,42 @@ public class CaptchaSecurityContextImplTests extends SecurityContextImplTests {
             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() {
         CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
         context.setHuman();