View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.service;
8   
9   import java.security.SecureRandom;
10  import java.util.Locale;
11  import java.util.Random;
12  
13  import junit.framework.TestCase;
14  
15  import com.octo.captcha.MockCaptcha;
16  import com.octo.captcha.engine.MockCaptchaEngine;
17  import com.octo.captcha.service.captchastore.MapCaptchaStore;
18  import com.octo.captcha.service.captchastore.MockCaptchaStore;
19  
20  public class RunableAbstractCaptchaServiceTest extends TestCase {
21  
22      protected Random myRandom = new SecureRandom();
23  
24      protected AbstractCaptchaService service = new MockedCaptchaService(new MapCaptchaStore(),
25              new MockCaptchaEngine());
26  
27      public static final int SIZE = 1000;
28  
29      public void testAbstractCaptchaService() throws Exception {
30          try {
31              new MockedCaptchaService(null, new MockCaptchaEngine());
32              fail("should have thrown an exception");
33          } catch (Exception e) {
34              assertTrue("IllegalArgumentException attended", e instanceof IllegalArgumentException);
35          }
36  
37          try {
38              new MockedCaptchaService(new MapCaptchaStore(), null);
39              fail("should have thrown an exception");
40          } catch (Exception e) {
41              assertTrue("IllegalArgumentException attended", e instanceof IllegalArgumentException);
42          }
43      }
44  
45      public void testGetChallengeForID() throws Exception {
46  
47          for (int i = 0; i < SIZE; i++) {
48              String id = String.valueOf(myRandom.nextInt());
49              assertEquals("Should always return a cloned challenge",
50                      MockCaptcha.CHALLENGE + MockedCaptchaService.CLONE_CHALLENGE, service.getChallengeForID(id));
51              assertEquals("Should always return a cloned challenge",
52                      MockCaptcha.CHALLENGE + MockedCaptchaService.CLONE_CHALLENGE, service.getChallengeForID(id));
53          }
54  
55      }
56  
57      public void testGetQuestionForID() throws Exception {
58          for (int i = 0; i < SIZE; i++) {
59              String id = String.valueOf(myRandom.nextInt());
60              assertEquals("Should always return The mock question and default locale",
61                      MockCaptcha.QUESTION + Locale.getDefault(), service.getQuestionForID(id));
62              assertEquals("Should always return The mock question and specified locale",
63                      MockCaptcha.QUESTION + Locale.CHINESE, service.getQuestionForID(id, Locale.CHINESE));
64          }
65  
66      }
67  
68      public void testValidateResponseForID() throws Exception {
69  
70          for (int i = 0; i < SIZE; i++) {
71              String id = String.valueOf(myRandom.nextInt());
72              try {
73                  service.validateResponseForID(id, "true");
74                  fail("The tiket is invalid, should throw an exception");
75              } catch (CaptchaServiceException e) {
76              	assertNotNull(e.getMessage());
77              }
78              //Should be ok after new question
79              service.getQuestionForID(id);
80              assertTrue("Sould be ok", service.validateResponseForID(id, "true").booleanValue());
81  
82              //Should be ok after new challenge
83              service.getChallengeForID(id);
84              assertTrue("Sould be ok", service.validateResponseForID(id, "true").booleanValue());
85  
86              //Should be ok after new challenge and question
87              service.getChallengeForID(id);
88              service.getQuestionForID(id);
89              assertTrue("Sould be ok", service.validateResponseForID(id, "true").booleanValue());
90  
91              //Should be ok after new question and challenge
92              service.getChallengeForID(id);
93              service.getQuestionForID(id);
94              assertTrue("Sould be ok", service.validateResponseForID(id, "true").booleanValue());
95          }
96      }
97  
98      public void testGenerateAndStoreCaptcha() throws Exception {
99          for (int i = 0; i < SIZE; i++) {
100             String id = String.valueOf(myRandom.nextInt());
101             service.generateAndStoreCaptcha(Locale.getDefault(), id);
102             assertTrue("Sould be ok", service.validateResponseForID(id, "true").booleanValue());
103 
104         }
105     }
106 
107 
108     public void testCaptchaRegenerationWhenNewLocaleIsAsked() throws Exception {
109         String french = service.getQuestionForID("1", Locale.FRENCH);
110         String english = service.getQuestionForID("1", Locale.ENGLISH);
111         assertFalse(french.equals(english));
112 
113     }
114     
115     public void testInitStore() {
116     	MockCaptchaStore store = new MockCaptchaStore();
117     	new MockedCaptchaService(store, new MockCaptchaEngine());
118     	assertTrue(store.isInitCalled());
119     }
120 
121 
122 }