1   /* Copyright 2004, 2005, 2006 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 1496 2006-05-23 13:38:33Z benalex $
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, context.getLastPassedCaptchaDateInMillis());
34          assertEquals("should be 0", 0, context.getHumanRestrictedResourcesRequestsCount());
35      }
36  
37      public void testEquals() {
38          CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
39          CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
40  
41          assertEquals(context1, context2);
42  
43          assertFalse(context1.isHuman());
44          context1.setHuman();
45          assertNotSame(context1, context2);
46  
47          // Get fresh copy
48          context1 = new CaptchaSecurityContextImpl();
49          assertEquals(context1, context2);
50  
51          context1.incrementHumanRestrictedRessoucesRequestsCount();
52          assertNotSame(context1, context2);
53      }
54  
55      public void testHashcode() {
56          CaptchaSecurityContext context1 = new CaptchaSecurityContextImpl();
57          CaptchaSecurityContext context2 = new CaptchaSecurityContextImpl();
58  
59          assertEquals(context1.hashCode(), context2.hashCode());
60  
61          assertFalse(context1.isHuman());
62          context1.setHuman();
63          assertTrue(context1.hashCode() != context2.hashCode());
64  
65          // Get fresh copy
66          context1 = new CaptchaSecurityContextImpl();
67          assertEquals(context1.hashCode(), context2.hashCode());
68  
69          context1.incrementHumanRestrictedRessoucesRequestsCount();
70          assertTrue(context1 != context2);
71      }
72  
73      public void testIncrementRequests() {
74          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
75          context.setHuman();
76          assertEquals("should be human", true, context.isHuman());
77          assertEquals("should be 0", 0, context.getHumanRestrictedResourcesRequestsCount());
78          context.incrementHumanRestrictedRessoucesRequestsCount();
79          assertEquals("should be 1", 1, context.getHumanRestrictedResourcesRequestsCount());
80      }
81  
82      public void testResetHuman() {
83          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
84          context.setHuman();
85          assertEquals("should be human", true, context.isHuman());
86          assertEquals("should be 0", 0, context.getHumanRestrictedResourcesRequestsCount());
87          context.incrementHumanRestrictedRessoucesRequestsCount();
88          assertEquals("should be 1", 1, context.getHumanRestrictedResourcesRequestsCount());
89  
90          long now = System.currentTimeMillis();
91          context.setHuman();
92          assertEquals("should be 0", 0, context.getHumanRestrictedResourcesRequestsCount());
93          assertTrue("should be more than 0", (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
94          assertTrue("should be less than 0,1 seconde", (context.getLastPassedCaptchaDateInMillis() - now) < 100);
95      }
96  
97      public void testSetHuman() {
98          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
99          long now = System.currentTimeMillis();
100         context.setHuman();
101         assertEquals("should be human", true, context.isHuman());
102         assertTrue("should be more than 0", (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
103         assertTrue("should be less than 0,1 seconde", (context.getLastPassedCaptchaDateInMillis() - now) < 100);
104         assertEquals("should be 0", 0, context.getHumanRestrictedResourcesRequestsCount());
105     }
106 }