001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.captcha;
017:
018: import org.acegisecurity.context.SecurityContextImplTests;
019:
020: /**
021: * Tests {@link CaptchaSecurityContextImpl}.
022: *
023: * @author marc antoine Garrigue
024: * @version $Id: CaptchaSecurityContextImplTests.java 1496 2006-05-23 13:38:33Z benalex $
025: */
026: public class CaptchaSecurityContextImplTests extends
027: SecurityContextImplTests {
028: //~ Methods ========================================================================================================
029:
030: public void testDefaultValues() {
031: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
032: assertEquals("should not be human", false, context.isHuman());
033: assertEquals("should be 0", 0, context
034: .getLastPassedCaptchaDateInMillis());
035: assertEquals("should be 0", 0, context
036: .getHumanRestrictedResourcesRequestsCount());
037: }
038:
039: public void testEquals() {
040: CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
041: CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
042:
043: assertEquals(context1, context2);
044:
045: assertFalse(context1.isHuman());
046: context1.setHuman();
047: assertNotSame(context1, context2);
048:
049: // Get fresh copy
050: context1 = new CaptchaSecurityContextImpl();
051: assertEquals(context1, context2);
052:
053: context1.incrementHumanRestrictedRessoucesRequestsCount();
054: assertNotSame(context1, context2);
055: }
056:
057: public void testHashcode() {
058: CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
059: CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
060:
061: assertEquals(context1.hashCode(), context2.hashCode());
062:
063: assertFalse(context1.isHuman());
064: context1.setHuman();
065: assertTrue(context1.hashCode() != context2.hashCode());
066:
067: // Get fresh copy
068: context1 = new CaptchaSecurityContextImpl();
069: assertEquals(context1.hashCode(), context2.hashCode());
070:
071: context1.incrementHumanRestrictedRessoucesRequestsCount();
072: assertTrue(context1 != context2);
073: }
074:
075: public void testIncrementRequests() {
076: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
077: context.setHuman();
078: assertEquals("should be human", true, context.isHuman());
079: assertEquals("should be 0", 0, context
080: .getHumanRestrictedResourcesRequestsCount());
081: context.incrementHumanRestrictedRessoucesRequestsCount();
082: assertEquals("should be 1", 1, context
083: .getHumanRestrictedResourcesRequestsCount());
084: }
085:
086: public void testResetHuman() {
087: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
088: context.setHuman();
089: assertEquals("should be human", true, context.isHuman());
090: assertEquals("should be 0", 0, context
091: .getHumanRestrictedResourcesRequestsCount());
092: context.incrementHumanRestrictedRessoucesRequestsCount();
093: assertEquals("should be 1", 1, context
094: .getHumanRestrictedResourcesRequestsCount());
095:
096: long now = System.currentTimeMillis();
097: context.setHuman();
098: assertEquals("should be 0", 0, context
099: .getHumanRestrictedResourcesRequestsCount());
100: assertTrue("should be more than 0", (context
101: .getLastPassedCaptchaDateInMillis() - now) >= 0);
102: assertTrue("should be less than 0,1 seconde", (context
103: .getLastPassedCaptchaDateInMillis() - now) < 100);
104: }
105:
106: public void testSetHuman() {
107: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
108: long now = System.currentTimeMillis();
109: context.setHuman();
110: assertEquals("should be human", true, context.isHuman());
111: assertTrue("should be more than 0", (context
112: .getLastPassedCaptchaDateInMillis() - now) >= 0);
113: assertTrue("should be less than 0,1 seconde", (context
114: .getLastPassedCaptchaDateInMillis() - now) < 100);
115: assertEquals("should be 0", 0, context
116: .getHumanRestrictedResourcesRequestsCount());
117: }
118: }
|