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 junit.framework.*;
19  
20  import org.acegisecurity.captcha.TestOnceAfterMaxRequestsCaptchaChannelProcessor;
21  
22  
23  /***
24   * DOCUMENT ME!
25   *
26   * @author $author$
27   * @version $Revision: 1.2 $
28   */
29  public class TestOnceAfterMaxRequestsCaptchaChannelProcessorTests
30      extends TestCase {
31      //~ Instance fields ========================================================
32  
33      TestOnceAfterMaxRequestsCaptchaChannelProcessor testOnceAfterMaxRequestsCaptchaChannelProcessor;
34  
35      //~ Methods ================================================================
36  
37      public void testIsContextValidConcerningHumanity()
38          throws Exception {
39          testOnceAfterMaxRequestsCaptchaChannelProcessor.setThresold(1);
40  
41          CaptchaSecurityContextImpl context = new CaptchaSecurityContextImpl();
42          assertTrue(testOnceAfterMaxRequestsCaptchaChannelProcessor
43              .isContextValidConcerningHumanity(context));
44  
45          context.incrementHumanRestrictedRessoucesRequestsCount();
46  
47          testOnceAfterMaxRequestsCaptchaChannelProcessor.setThresold(-1);
48          assertFalse(testOnceAfterMaxRequestsCaptchaChannelProcessor
49              .isContextValidConcerningHumanity(context));
50  
51          testOnceAfterMaxRequestsCaptchaChannelProcessor.setThresold(3);
52          assertTrue(testOnceAfterMaxRequestsCaptchaChannelProcessor
53              .isContextValidConcerningHumanity(context));
54          context.incrementHumanRestrictedRessoucesRequestsCount();
55          assertTrue(testOnceAfterMaxRequestsCaptchaChannelProcessor
56              .isContextValidConcerningHumanity(context));
57          context.incrementHumanRestrictedRessoucesRequestsCount();
58          assertFalse(testOnceAfterMaxRequestsCaptchaChannelProcessor
59              .isContextValidConcerningHumanity(context));
60          context.setHuman();
61  
62          for (int i = 0;
63              i < (2 * testOnceAfterMaxRequestsCaptchaChannelProcessor
64              .getThresold()); i++) {
65              assertTrue(testOnceAfterMaxRequestsCaptchaChannelProcessor
66                  .isContextValidConcerningHumanity(context));
67          }
68      }
69  
70      public void testNewContext() {
71          CaptchaSecurityContextImpl context = new CaptchaSecurityContextImpl();
72  
73          assertFalse(testOnceAfterMaxRequestsCaptchaChannelProcessor
74              .isContextValidConcerningHumanity(context));
75          testOnceAfterMaxRequestsCaptchaChannelProcessor.setThresold(1);
76          assertTrue(testOnceAfterMaxRequestsCaptchaChannelProcessor
77              .isContextValidConcerningHumanity(context));
78      }
79  
80      protected void setUp() throws Exception {
81          super.setUp();
82          testOnceAfterMaxRequestsCaptchaChannelProcessor = new TestOnceAfterMaxRequestsCaptchaChannelProcessor();
83      }
84  }