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.AlwaysTestAfterTimeInMillisCaptchaChannelProcessor;
21  
22  
23  /**
24   * WARNING! This test class make some assumptions concerning the compute speed! For example the two following
25   * instructions should be computed in the same millis or the test is not valid.<pre><code>context.setHuman();
26   * assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
27   * </code></pre>This should be the case for most environements unless
28   *  <ul>
29   *      <li>you run it on a good old TRS-80</li>
30   *      <li>you start M$office during this test ;)</li>
31   *  </ul>
32   */
33  public class AlwaysTestAfterTimeInMillisCaptchaChannelProcessorTests extends TestCase {
34      //~ Instance fields ================================================================================================
35  
36      AlwaysTestAfterTimeInMillisCaptchaChannelProcessor alwaysTestAfterTimeInMillisCaptchaChannelProcessor;
37  
38      //~ Methods ========================================================================================================
39  
40      protected void setUp() throws Exception {
41          super.setUp();
42          alwaysTestAfterTimeInMillisCaptchaChannelProcessor = new AlwaysTestAfterTimeInMillisCaptchaChannelProcessor();
43      }
44  
45      public void testEqualsThresold() {
46          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
47          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
48  
49          //the two following instructions should be computed or the test is not valid (never fails). This should be the case
50          // for most environements unless if you run it on a good old TRS-80 (thanks mom).
51          context.setHuman();
52          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
53      }
54  /* Commented out as it makes assumptions about the speed of the build server and fails intermittently on
55     build.springframework.org - L.T.
56     
57      public void testIsContextValidConcerningHumanity()
58          throws Exception {
59          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
60          alwaysTestAfterTimeInMillisCaptchaChannelProcessor.setThresold(100);
61          context.setHuman();
62  
63          while ((System.currentTimeMillis() - context.getLastPassedCaptchaDateInMillis()) < alwaysTestAfterTimeInMillisCaptchaChannelProcessor
64              .getThresold()) {
65              assertTrue(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
66              context.incrementHumanRestrictedRessoucesRequestsCount();
67  
68              long now = System.currentTimeMillis();
69  
70              while ((System.currentTimeMillis() - now) < 1) {}
71  
72              ;
73          }
74  
75          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
76      }
77  */
78      public void testNewContext() {
79          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
80  
81          //alwaysTestAfterTimeInMillisCaptchaChannelProcessor.setThresold(10);
82          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
83      }
84  }