1
2
3
4
5
6 package com.octo.captcha.engine.sound;
7
8 import junit.framework.TestCase;
9
10 import com.octo.captcha.engine.CaptchaEngine;
11 import com.octo.captcha.engine.CaptchaEngineException;
12 import com.octo.captcha.engine.MockImageCaptchaFactory;
13 import com.octo.captcha.engine.MockSoundCaptchaFactory;
14 import com.octo.captcha.image.ImageCaptchaFactory;
15 import com.octo.captcha.sound.SoundCaptchaFactory;
16
17 public class DefaultSoundCaptchaEngineTest extends TestCase {
18
19 private static final MockSoundCaptchaFactory MOCK_SOUND_CAPTCHA_FACTORY_1 = new MockSoundCaptchaFactory();
20 private static final MockSoundCaptchaFactory MOCK_SOUND_CAPTCHA_FACTORY_2 = new MockSoundCaptchaFactory();
21
22 DefaultSoundCaptchaEngine defaultSoundCaptchaEngine;
23 private SoundCaptchaFactory[] factories = new SoundCaptchaFactory[]{DefaultSoundCaptchaEngineTest.MOCK_SOUND_CAPTCHA_FACTORY_1};
24 private SoundCaptchaFactory[] otherFactories = new SoundCaptchaFactory[]{DefaultSoundCaptchaEngineTest.MOCK_SOUND_CAPTCHA_FACTORY_2};
25
26 public void testNullOrEmptyFactorySoundCaptchaEngineConstructor() throws Exception {
27
28 try {
29 buildCaptchaEngine(null);
30 fail("Cannot build with null factories");
31 } catch (CaptchaEngineException e) {
32
33 }
34
35 try {
36 buildCaptchaEngine(new SoundCaptchaFactory[]{});
37 fail("Cannot build with null factories");
38 } catch (CaptchaEngineException e) {
39
40 }
41
42 }
43
44
45 public void testNullOrEmptySetFactories() throws Exception {
46 this.defaultSoundCaptchaEngine = (DefaultSoundCaptchaEngine) buildCaptchaEngine(new SoundCaptchaFactory[]{DefaultSoundCaptchaEngineTest.MOCK_SOUND_CAPTCHA_FACTORY_1});
47
48 try {
49 defaultSoundCaptchaEngine.setFactories(null);
50 fail("cannot set null factories");
51 } catch (CaptchaEngineException e) {
52
53 }
54
55 try {
56 defaultSoundCaptchaEngine.setFactories(new SoundCaptchaFactory[]{});
57 fail("cannot set null factories");
58 } catch (CaptchaEngineException e) {
59
60 }
61
62 }
63
64
65 public void testWrongTypeSetFactories() throws Exception {
66 this.defaultSoundCaptchaEngine = (DefaultSoundCaptchaEngine) buildCaptchaEngine(factories);
67
68 try {
69 defaultSoundCaptchaEngine.setFactories(new ImageCaptchaFactory[]{new MockImageCaptchaFactory()});
70 fail("cannot set wrong type factories");
71 } catch (CaptchaEngineException e) {
72
73 }
74
75 }
76
77
78 public void testSetFactories() throws Exception {
79 this.defaultSoundCaptchaEngine = (DefaultSoundCaptchaEngine) buildCaptchaEngine(new MockSoundCaptchaFactory[]{DefaultSoundCaptchaEngineTest.MOCK_SOUND_CAPTCHA_FACTORY_1});
80 assertEquals(factories[0], defaultSoundCaptchaEngine.getFactories()[0]);
81
82
83 defaultSoundCaptchaEngine.setFactories(otherFactories);
84 assertEquals(otherFactories[0], defaultSoundCaptchaEngine.getFactories()[0]);
85
86
87 }
88
89
90 CaptchaEngine buildCaptchaEngine(Object[] parameter) {
91 return new DefaultSoundCaptchaEngineTest.ImplDefaultSoundCaptchaEngine((SoundCaptchaFactory[]) parameter);
92 }
93
94 private class ImplDefaultSoundCaptchaEngine extends DefaultSoundCaptchaEngine {
95 /***
96 * Default constructor : takes an array of SoundCaptchaFactories.
97 */
98 public ImplDefaultSoundCaptchaEngine(final SoundCaptchaFactory[] factories) {
99 super(factories);
100 }
101 }
102
103
104 }