1
2
3
4
5
6
7
8
9
10
11
12 package com.octo.captcha.engine.bufferedengine.buffer;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Locale;
17
18 import junit.framework.TestCase;
19
20 import com.octo.captcha.Captcha;
21 import com.octo.captcha.engine.CaptchaEngine;
22
23 import com.octo.captcha.engine.image.gimpy.DefaultGimpyEngine;
24
25 /***
26 * Abstract class to test Buffers
27 *
28 * @author Benoit Doumas
29 */
30 public abstract class CaptchaBufferTestAbstract extends TestCase {
31
32 protected CaptchaBuffer buffer = null;
33
34
35 public static final int SIZE = 50;
36
37 public CaptchaEngine engine = null;
38
39
40
41
42 protected void setUp() throws Exception {
43 super.setUp();
44
45 engine = new DefaultGimpyEngine();
46 }
47
48
49
50
51 protected void tearDown() throws Exception {
52 super.tearDown();
53 }
54
55
56
57
58 public void testRemoveCaptcha() {
59 buffer = getBuffer();
60 ArrayList listToStore = new ArrayList(SIZE);
61
62 for (int i = 0; i < SIZE; ++i) {
63 listToStore.add(engine.getNextCaptcha());
64 }
65 buffer.putAllCaptcha(listToStore);
66
67 Captcha captcha = buffer.removeCaptcha();
68
69 assertNotNull(captcha);
70 }
71
72
73
74
75 public void testRemoveCaptchaint() {
76 buffer = getBuffer();
77 ArrayList listToStore = new ArrayList(SIZE);
78
79 for (int i = 0; i < SIZE; ++i) {
80 listToStore.add(engine.getNextCaptcha());
81 }
82 buffer.putAllCaptcha(listToStore);
83
84 Collection captchas = buffer.removeCaptcha(SIZE);
85
86 assertEquals(SIZE, captchas.size());
87 }
88
89
90
91
92 public void testPutCaptchaCaptcha() {
93 buffer = getBuffer();
94 buffer.putCaptcha(engine.getNextCaptcha());
95
96 assertEquals(1, buffer.size());
97 }
98
99
100
101
102 public void testPutAllCaptchaCollection() {
103 buffer = getBuffer();
104 ArrayList listToStore = new ArrayList(SIZE);
105
106 for (int i = 0; i < SIZE; ++i) {
107 listToStore.add(engine.getNextCaptcha());
108 }
109 buffer.putAllCaptcha(listToStore);
110
111 assertEquals(SIZE, buffer.size());
112 }
113
114
115
116
117 public void testRemoveCaptchaLocale() {
118 buffer = getBuffer();
119
120 buffer.putCaptcha(engine.getNextCaptcha(), Locale.GERMAN);
121
122
123 assertNotNull(buffer.removeCaptcha(Locale.GERMAN));
124 }
125
126
127
128
129 public void testRemoveCaptchaintLocale() {
130 buffer = getBuffer();
131 ArrayList listToStore = new ArrayList(SIZE);
132
133 for (int i = 0; i < SIZE; ++i) {
134 listToStore.add(engine.getNextCaptcha());
135 }
136 buffer.putAllCaptcha(listToStore, Locale.GERMAN);
137
138 Collection captchas = buffer.removeCaptcha(SIZE, Locale.GERMAN);
139
140 assertEquals(SIZE, captchas.size());
141 }
142
143
144
145
146 public void testPutCaptchaCaptchaLocale() {
147 buffer = getBuffer();
148
149 buffer.putCaptcha(engine.getNextCaptcha(), Locale.GERMAN);
150
151 assertEquals(1, buffer.size(Locale.GERMAN));
152 }
153
154
155
156
157 public void testPutAllCaptchaCollectionLocale() {
158 buffer = getBuffer();
159 ArrayList listToStore = new ArrayList(SIZE);
160
161 for (int i = 0; i < SIZE; ++i) {
162 listToStore.add(engine.getNextCaptcha());
163 }
164 buffer.putAllCaptcha(listToStore, Locale.GERMAN);
165
166 assertEquals(SIZE, buffer.size(Locale.GERMAN));
167 }
168
169 public void testClear() {
170 buffer = getBuffer();
171 ArrayList listToStore = new ArrayList(SIZE);
172
173 for (int i = 0; i < SIZE; ++i) {
174 listToStore.add(engine.getNextCaptcha());
175 }
176 buffer.putAllCaptcha(listToStore);
177
178 buffer.clear();
179
180 assertEquals(0, buffer.size());
181 }
182
183 public void testGetLocales() {
184 buffer = getBuffer();
185 ArrayList listToStore = new ArrayList(100);
186
187 for (int i = 0; i < SIZE; ++i) {
188 listToStore.add(engine.getNextCaptcha());
189 }
190 buffer.putAllCaptcha(listToStore, Locale.GERMAN);
191 buffer.putAllCaptcha(listToStore, Locale.US);
192 buffer.putAllCaptcha(listToStore, Locale.FRANCE);
193
194 assertEquals(3, buffer.getLocales().size());
195 }
196
197 public void testIntegrity() {
198 int numToRemove = 10;
199 buffer = getBuffer();
200 ArrayList listToStore = new ArrayList(100);
201
202 for (int i = 0; i < SIZE; ++i) {
203 listToStore.add(engine.getNextCaptcha());
204 }
205 buffer.putAllCaptcha(listToStore);
206
207 buffer.removeCaptcha(numToRemove);
208
209 assertEquals(SIZE - numToRemove, buffer.size());
210 }
211
212 public void testBufferGoesEmpty() {
213 int numToRemove = 10;
214 buffer = getBuffer();
215 ArrayList listToStore = new ArrayList(100);
216
217 for (int i = 0; i < 1; ++i) {
218 listToStore.add(engine.getNextCaptcha());
219 }
220 buffer.putAllCaptcha(listToStore);
221
222 assertEquals(1, buffer.removeCaptcha(numToRemove).size());
223 }
224
225 public abstract CaptchaBuffer getBuffer();
226
227 }