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.TestCase;
19  
20  
21  /**
22   * DOCUMENT ME!
23   *
24   * @author $author$
25   * @version $Revision: 2025 $
26   */
27  public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessorTests extends TestCase {
28      //~ Instance fields ================================================================================================
29  
30      AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor;
31  
32      //~ Methods ========================================================================================================
33  
34      protected void setUp() throws Exception {
35          super.setUp();
36          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor = new AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor();
37      }
38  
39      public void testEqualsThresold() {
40          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
41          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.setThresold(100);
42  
43          context.setHuman();
44  
45          long now = System.currentTimeMillis();
46  /*
47          while ((System.currentTimeMillis() - now) <= 100) {
48              assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
49                  .isContextValidConcerningHumanity(context));
50          }
51  
52          context.incrementHumanRestrictedRessoucesRequestsCount();
53          assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.isContextValidConcerningHumanity(
54                  context));
55  
56          context.setHuman();
57          context.incrementHumanRestrictedRessoucesRequestsCount();
58          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.isContextValidConcerningHumanity(
59                  context));
60  
61          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.setThresold(0);
62          context.setHuman();
63          context.incrementHumanRestrictedRessoucesRequestsCount();
64          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.isContextValidConcerningHumanity(
65                  context));
66          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.setThresold(0);
67          */
68      }
69  /*
70      public void testIsContextValidConcerningHumanity()
71          throws Exception {
72          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
73          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.setThresold(10);
74          context.setHuman();
75  
76          while ((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()) < (10 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
77              .getThresold())) {
78              assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
79                  .isContextValidConcerningHumanity(context));
80          }
81      }
82  
83      public void testNewContext() {
84          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
85          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.isContextValidConcerningHumanity(
86                  context));
87  
88          context.setHuman();
89          assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor.isContextValidConcerningHumanity(
90                  context));
91      }
92  
93      public void testShouldPassAbove() {
94          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
95  
96          context.setHuman();
97  
98          int i = 0;
99  
100         while ((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()) < (100 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
101             .getThresold())) {
102             System.out.println((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()));
103 
104             context.incrementHumanRestrictedRessoucesRequestsCount();
105             i++;
106 
107             while ((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()) < (alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
108                 .getThresold() * i)) {}
109 
110             System.out.println((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()));
111 
112             assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
113                 .isContextValidConcerningHumanity(context));
114         }
115     }
116     */
117 }