1
2
3
4
5
6
7
8
9
10
11
12 package com.octo.captcha.engine.bufferedengine;
13
14 import com.octo.captcha.engine.MockCaptchaEngine;
15 import com.octo.captcha.engine.bufferedengine.buffer.MemoryCaptchaBuffer;
16
17 import java.util.HashMap;
18 import java.util.Locale;
19
20 /***
21 * <p><ul><li></li></ul></p>
22 *
23 * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
24 * @version 1.0
25 */
26 public class MockedBufferedEngineContainerTest extends BufferedEngineContainerTestAbstract {
27
28 int swap = 100;
29 int feed = 1000;
30 int maxVolatil = 2 * swap;
31 int maxPersistent = 3 * feed;
32 BufferedEngineContainer engine;
33
34 protected void setUp() throws Exception {
35 super.setUp();
36 HashMap localRatio = new HashMap();
37 localRatio.put(Locale.FRANCE, new Double(0.2));
38 localRatio.put(Locale.US, new Double(0.8));
39 ContainerConfiguration config = new ContainerConfiguration(localRatio, maxVolatil, maxPersistent, swap, feed);
40 this.engine = new MockedBufferedCaptchaEngine(config);
41 }
42
43
44 public void testFeedAndSwap() {
45 assertEquals("buffers should be empty", 0, engine.getVolatileBuffer().size());
46 assertEquals("buffers should be empty", 0, engine.getPersistentBuffer().size());
47 engine.swapCaptchasFromPersistentToVolatileMemory();
48 assertEquals("buffers should be empty", 0, engine.getVolatileBuffer().size());
49 assertEquals("buffers should be empty", 0, engine.getPersistentBuffer().size());
50 engine.feedPersistentBuffer();
51 assertEquals("volatil buffer should be empty", 0, engine.getVolatileBuffer().size());
52 assertEquals("pers buffer should be filled", feed, engine.getPersistentBuffer().size());
53 engine.swapCaptchasFromPersistentToVolatileMemory();
54 assertEquals("volatil buffer should be filled", swap, engine.getVolatileBuffer().size());
55 assertEquals("pers buffer should be updated", feed - swap, engine.getPersistentBuffer().size());
56 engine.swapCaptchasFromPersistentToVolatileMemory();
57 assertEquals("volatil buffer should be filled", 2 * swap, engine.getVolatileBuffer().size());
58 assertEquals("pers buffer should be updated", feed - 2 * swap, engine.getPersistentBuffer().size());
59 engine.swapCaptchasFromPersistentToVolatileMemory();
60 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
61 assertEquals("pers buffer should be updated", feed - 2 * swap, engine.getPersistentBuffer().size());
62 engine.getNextCaptcha(Locale.FRANCE);
63 assertEquals("volatil buffer should be updated", maxVolatil - 1, engine.getVolatileBuffer().size());
64 engine.swapCaptchasFromPersistentToVolatileMemory();
65 assertEquals("volatil buffer should be filled", 2 * swap, engine.getVolatileBuffer().size());
66 assertEquals("pers buffer should be updated", feed - 2 * swap - 1, engine.getPersistentBuffer().size());
67 engine.getNextCaptcha(Locale.US);
68 assertEquals("volatil buffer should be updated", maxVolatil - 1, engine.getVolatileBuffer().size());
69 engine.swapCaptchasFromPersistentToVolatileMemory();
70 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
71 assertEquals("pers buffer should be updated", feed - 2 * swap - 2, engine.getPersistentBuffer().size());
72 engine.getNextCaptcha(Locale.CHINA);
73 assertEquals("volatil buffer should be updated", maxVolatil, engine.getVolatileBuffer().size());
74 engine.swapCaptchasFromPersistentToVolatileMemory();
75 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
76 assertEquals("pers buffer should be updated", feed - 2 * swap - 2, engine.getPersistentBuffer().size());
77 engine.feedPersistentBuffer();
78 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
79 assertEquals("pers buffer should be updated", 2 * feed - 2 * swap - 2, engine.getPersistentBuffer().size());
80
81 engine.feedPersistentBuffer();
82 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
83 assertEquals("pers buffer should be updated", 3 * feed - 2 * swap - 2, engine.getPersistentBuffer().size());
84
85 engine.feedPersistentBuffer();
86 assertEquals("volatil buffer should be filled", maxVolatil, engine.getVolatileBuffer().size());
87 assertEquals("pers buffer should be updated", maxPersistent, engine.getPersistentBuffer().size());
88
89
90 }
91
92
93 public BufferedEngineContainer getEngine() {
94 return this.engine;
95 }
96
97 public void releaseEngine(BufferedEngineContainer engine) {
98 this.engine.getVolatileBuffer().clear();
99 this.engine.getPersistentBuffer().clear();
100
101 }
102
103 private class MockedBufferedCaptchaEngine extends BufferedEngineContainer {
104 /***
105 * Construct an BufferedEngineContainer with and Captcha engine, a memory buffer, a diskBuffer and a
106 * ContainerConfiguration.
107 *
108 * @param containerConfiguration the container configuration
109 */
110 public MockedBufferedCaptchaEngine(ContainerConfiguration containerConfiguration) {
111 super(new MockCaptchaEngine(), new MemoryCaptchaBuffer(), new MemoryCaptchaBuffer(), containerConfiguration);
112 }
113
114
115 }
116
117 }