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  package com.octo.captcha.engine.bufferedengine.manager;
13  
14  import java.util.Locale;
15  import java.util.Map;
16  
17  import junit.framework.TestCase;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.beans.factory.config.ConfigurableBeanFactory;
22  import org.springframework.beans.factory.xml.XmlBeanFactory;
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.core.io.Resource;
25  
26  import com.octo.captcha.CaptchaException;
27  import com.octo.captcha.engine.bufferedengine.BufferedEngineContainer;
28  
29  /***
30   * Unit test the QuartzBufferedEngineManager
31   *
32   * @author Benoit Doumas
33   */
34  public class QuartzBufferedEngineManagerTest extends TestCase {
35      private static final Log log = LogFactory.getLog(QuartzBufferedEngineManagerTest.class
36              .getName());
37  
38      // loader init by default
39      //protected Class loader = DefaultEngineLoadTestHelper.class;
40  
41      BufferedEngineContainer container = null;
42  
43      QuartzBufferedEngineManager manager;
44  
45      Object scheduler = null;
46  
47      /*
48       * @see TestCase#setUp()
49       */
50      protected void setUp() throws Exception {
51          super.setUp();
52          Resource ressource = new ClassPathResource("testQuartzBufferedEngine.xml");
53          ConfigurableBeanFactory bf = new XmlBeanFactory(ressource);
54          container = (BufferedEngineContainer) bf.getBean("container");
55          scheduler = bf.getBean("quartz");
56  
57          manager = (QuartzBufferedEngineManager) bf.getBean("manager");
58      }
59  
60      public void testStartStopToFeedPersistentBuffer() {
61          String cronFeed = "0/3 * * * * ?";
62  
63          //there should be swap in order to have feeds (so persistent buffer is not full)
64          String cronSwap = "0/2 * * * * ?";
65  
66          manager.setFeedSize(10);
67          manager.setFeedCronExpr(cronFeed);
68  
69          manager.setSwapSize(10);
70          manager.setSwapCronExpr(cronSwap);
71  
72          manager.stopToFeedPersistentBuffer();
73  
74          //get some captcha
75          for (int i = 0; i < 10; i++)
76              container.getNextCaptcha();
77  
78          int size = manager.getPersistentBufferSize();
79  
80          //wait to see if there is some feed
81          try {
82              Thread.sleep(3500);
83          }
84          catch (InterruptedException e) {
85              throw new CaptchaException(e);
86          }
87  
88          //is ti still the same?
89          assertEquals(size, manager.getPersistentBufferSize());
90  
91          //take the time to have some swap
92          try {
93              Thread.sleep(3500);
94          }
95          catch (InterruptedException e) {
96              throw new CaptchaException(e);
97          }
98  
99          //stop to swap
100         manager.stopToSwapFromPersistentToVolatileMemory();
101 
102         //now start
103         manager.startToFeedPersistantBuffer();
104 
105         //wait, now shoult be some action
106         try {
107             Thread.sleep(3500);
108         }
109         catch (InterruptedException e) {
110             throw new CaptchaException(e);
111         }
112 
113         //the size shoult have increase
114         assertTrue(size < manager.getPersistentBufferSize());
115 
116         manager.startToSwapFromPersistentToVolatileMemory();
117     }
118 
119 
120     public void testStartStopToSwapFromPersistentToVolatileMemory() {
121         //there should be feeds in order to have swap (so persistent buffer contains some stuff)
122         String cronFeed = "0/2 * * * * ?";
123 
124 
125         String cronSwap = "0/3 * * * * ?";
126 
127         manager.setFeedSize(10);
128         manager.setFeedCronExpr(cronFeed);
129 
130         manager.setSwapSize(10);
131         manager.setSwapCronExpr(cronSwap);
132 
133         manager.stopToSwapFromPersistentToVolatileMemory();
134 
135         //get some captcha
136         for (int i = 0; i < 10; i++)
137             container.getNextCaptcha();
138 
139         int size = manager.getVolatileBufferSize();
140 
141         //wait to see if there is some swaps
142         try {
143             Thread.sleep(4000);
144         }
145         catch (InterruptedException e) {
146             throw new CaptchaException(e);
147         }
148 
149         //is ti still the same?
150         assertEquals(size, manager.getVolatileBufferSize());
151 
152         //now start
153         manager.startToSwapFromPersistentToVolatileMemory();
154 
155         //wait, now shoult be some action
156         try {
157             Thread.sleep(4000);
158         }
159         catch (InterruptedException e) {
160             throw new CaptchaException(e);
161         }
162 
163         //the size shoult have increase
164         assertTrue(size < manager.getVolatileBufferSize());
165     }
166 
167     public void testSetFeedCronExpr() {
168         String cron = "0/23 * * * * ?";
169 
170         manager.setFeedCronExpr(cron);
171 
172         assertEquals(cron, manager.getFeedCronExpr());
173     }
174 
175     public void testSetSwapCronExpr() {
176         String cron = "0/23 * * * * ?";
177 
178         manager.setSwapCronExpr(cron);
179 
180         assertEquals(cron, manager.getSwapCronExpr());
181     }
182 
183     public void testPauseResume() {
184         manager.pause();
185         manager.resume();
186     }
187 
188 
189     public void testSetFeedSize() {
190         int size = 10;
191         manager.setFeedSize(size);
192 
193         assertEquals(size, manager.getFeedSize());
194     }
195 
196     public void testSetLocaleRatio() {
197         manager.setLocaleRatio(Locale.GERMANY.toString(), 0.2);
198 
199         Map map = manager.getLocaleRatio();
200 
201         assertEquals(new Double(0.2), (Double) map.get(Locale.GERMANY));
202     }
203 
204     public void testRemoveLocaleRatio() {
205         manager.setLocaleRatio(Locale.GERMANY.getDisplayName(), 0.2);
206 
207         manager.removeLocaleRatio(Locale.GERMANY.getDisplayName());
208 
209         assertEquals(null, manager.getLocaleRatio().get(Locale.GERMANY.getDisplayName()));
210     }
211 
212     public void testSetMaxPersistentMemorySize() {
213         int size = 100;
214         manager.setMaxPersistentMemorySize(size);
215 
216         assertEquals(size, manager.getMaxPersistentMemorySize());
217     }
218 
219     public void testSetMaxVolatileMemorySize() {
220         int size = 100;
221         manager.setMaxVolatileMemorySize(size);
222 
223         assertEquals(size, manager.getMaxVolatileMemorySize());
224     }
225 
226     public void testSetSwapSize() {
227         int size = 10;
228         manager.setSwapSize(size);
229 
230         assertEquals(size, manager.getSwapSize());
231     }
232 
233     public void testClearVolatileBuffer() {
234         manager.pause();
235 
236         manager.clearVolatileBuffer();
237 
238         assertEquals(0, manager.getVolatileBufferSize());
239 
240         manager.resume();
241     }
242 
243     public void testClearPersistentBuffer() {
244         manager.pause();
245 
246         manager.clearPersistentBuffer();
247 
248         assertEquals(0, manager.getPersistentBufferSize());
249 
250         manager.resume();
251     }
252 
253 }