    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.captcha;
    17
    18  import org.acegisecurity.context.SecurityContextImplTests;
    19
    20
    21  /***
    22   * Tests {@link CaptchaSecurityContextImpl}.
    23   *
    24   * @author marc antoine Garrigue
    25   * @version $Id: CaptchaSecurityContextImplTests.java,v 1.4 2005/11/17 00:56:08 benalex Exp $
    26   */
    27  public class CaptchaSecurityContextImplTests extends SecurityContextImplTests {
    28      //~ Methods ================================================================
    29
    30      public void testDefaultValues() {
    31          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    32          assertEquals("should not be human", false, context.isHuman());
    33          assertEquals("should be 0", 0,
    34              context.getLastPassedCaptchaDateInMillis());
    35          assertEquals("should be 0", 0,
    36              context.getHumanRestrictedResourcesRequestsCount());
    37      }
    38
    39      public void testIncrementRequests() {
    40          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    41          context.setHuman();
    42          assertEquals("should be human", true, context.isHuman());
    43          assertEquals("should be 0", 0,
    44              context.getHumanRestrictedResourcesRequestsCount());
    45          context.incrementHumanRestrictedRessoucesRequestsCount();
    46          assertEquals("should be 1", 1,
    47              context.getHumanRestrictedResourcesRequestsCount());
    48      }
    49
    50      public void testResetHuman() {
    51          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    52          context.setHuman();
    53          assertEquals("should be human", true, context.isHuman());
    54          assertEquals("should be 0", 0,
    55              context.getHumanRestrictedResourcesRequestsCount());
    56          context.incrementHumanRestrictedRessoucesRequestsCount();
    57          assertEquals("should be 1", 1,
    58              context.getHumanRestrictedResourcesRequestsCount());
    59
    60          long now = System.currentTimeMillis();
    61          context.setHuman();
    62          assertEquals("should be 0", 0,
    63              context.getHumanRestrictedResourcesRequestsCount());
    64          assertTrue("should be more than 0",
    65              (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
    66          assertTrue("should be less than 0,1 seconde",
    67              (context.getLastPassedCaptchaDateInMillis() - now) < 100);
    68      }
    69
    70      public void testSetHuman() {
    71          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    72          long now = System.currentTimeMillis();
    73          context.setHuman();
    74          assertEquals("should be human", true, context.isHuman());
    75          assertTrue("should be more than 0",
    76              (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
    77          assertTrue("should be less than 0,1 seconde",
    78              (context.getLastPassedCaptchaDateInMillis() - now) < 100);
    79          assertEquals("should be 0", 0,
    80              context.getHumanRestrictedResourcesRequestsCount());
    81      }
    82  }