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   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  
13  /*
14   * jcaptcha, the open source java framework for captcha definition and integration
15   * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
16   * See the LICENSE.txt file distributed with this package.
17   */
18  
19  package com.octo.captcha.component.word.wordgenerator;
20  
21  import com.octo.captcha.CaptchaException;
22  import com.octo.captcha.component.word.ArrayDictionary;
23  import junit.framework.TestCase;
24  
25  import java.util.Locale;
26  
27  /***
28   * <p>Description: </p>
29   *
30   * @author <a href="mailto:mga@octo.com">Mathieu Gandin</a>
31   * @version 1.0
32   */
33  public class DictionaryWordGeneratorTest extends TestCase {
34  
35      private DictionaryWordGenerator dictionaryWordGenerator;
36      private static String[] wordlist = {"1", "1234", "123456", "123456789", "123"};
37      private static int[] lengths = {1, 4, 6, 9, 3};
38      private static Integer UNKNOWN_LENGTH = new Integer(100);
39  
40      public void setUp() {
41          this.dictionaryWordGenerator = new DictionaryWordGenerator(new ArrayDictionary(wordlist));
42      }
43  
44      public void testGetWordInteger() {
45          for (int i = 0; i < lengths.length; i++) {
46              Integer length = new Integer(lengths[i]);
47              String test = this.dictionaryWordGenerator.getWord(length);
48              assertNotNull(test);
49              assertTrue(test.length() > 0);
50              assertEquals(length.intValue(), test.length());
51  
52          }
53          try {
54              this.dictionaryWordGenerator.getWord(UNKNOWN_LENGTH);
55              fail("Should throw a CaptchaException");
56          } catch (CaptchaException e) {
57              assertNotNull(e.getMessage());
58          }
59      }
60  
61      public void testGetWordIntegerLocale() {
62          for (int i = 0; i < lengths.length; i++) {
63              Integer length = new Integer(lengths[i]);
64              String test = this.dictionaryWordGenerator.getWord(length, Locale.US);
65              assertNotNull(test);
66              assertTrue(test.length() > 0);
67              assertEquals(length.intValue(), test.length());
68          }
69          try {
70              this.dictionaryWordGenerator.getWord(UNKNOWN_LENGTH);
71              fail("Should throw a CaptchaException");
72          } catch (CaptchaException e) {
73              assertNotNull(e.getMessage());
74          }
75      }
76  
77  
78  }