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 junit.framework.*;
19  
20  import org.acegisecurity.captcha.AlwaysTestAfterMaxRequestsCaptchaChannelProcessor;
21  
22  
23  /**
24   * DOCUMENT ME!
25   *
26   * @author $author$
27   * @version $Revision: 1496 $
28   */
29  public class AlwaysTestAfterMaxRequestsCaptchaChannelProcessorTests extends TestCase {
30      //~ Instance fields ================================================================================================
31  
32      AlwaysTestAfterMaxRequestsCaptchaChannelProcessor alwaysTestAfterMaxRequestsCaptchaChannelProcessor;
33  
34      //~ Methods ========================================================================================================
35  
36      protected void setUp() throws Exception {
37          super.setUp();
38          alwaysTestAfterMaxRequestsCaptchaChannelProcessor = new AlwaysTestAfterMaxRequestsCaptchaChannelProcessor();
39      }
40  
41      public void testIsContextValidConcerningHumanity()
42          throws Exception {
43          alwaysTestAfterMaxRequestsCaptchaChannelProcessor.setThresold(1);
44  
45          CaptchaSecurityContextImpl context = new CaptchaSecurityContextImpl();
46          assertTrue(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
47  
48          context.incrementHumanRestrictedRessoucesRequestsCount();
49  
50          alwaysTestAfterMaxRequestsCaptchaChannelProcessor.setThresold(-1);
51          assertFalse(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
52  
53          alwaysTestAfterMaxRequestsCaptchaChannelProcessor.setThresold(3);
54          assertTrue(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
55          context.incrementHumanRestrictedRessoucesRequestsCount();
56          assertTrue(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
57          context.incrementHumanRestrictedRessoucesRequestsCount();
58          assertFalse(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
59      }
60  
61      public void testNewContext() {
62          CaptchaSecurityContextImpl context = new CaptchaSecurityContextImpl();
63  
64          assertFalse(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
65          alwaysTestAfterMaxRequestsCaptchaChannelProcessor.setThresold(1);
66          assertTrue(alwaysTestAfterMaxRequestsCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
67      }
68  }