001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestMemoryTasks.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.taskmanagers;
009:
010: import com.uwyn.rife.scheduler.Task;
011: import com.uwyn.rife.scheduler.TaskManager;
012: import com.uwyn.rife.scheduler.TestTasktypes;
013: import com.uwyn.rife.scheduler.exceptions.FrequencyException;
014: import com.uwyn.rife.scheduler.exceptions.TaskManagerException;
015: import com.uwyn.rife.scheduler.taskmanagers.MemoryTasks;
016: import com.uwyn.rife.tools.ExceptionUtils;
017: import java.util.Calendar;
018: import java.util.Collection;
019: import junit.framework.TestCase;
020:
021: public class TestMemoryTasks extends TestCase {
022: public TestMemoryTasks(String name) {
023: super (name);
024: }
025:
026: public void testInstantiateTaskManager() {
027: TaskManager manager = null;
028: manager = new MemoryTasks();
029: assertNotNull(manager);
030: }
031:
032: public void testAddTask() {
033: int task_id = -1;
034:
035: String type = TestTasktypes.UPLOAD_GROUPS;
036: Calendar cal = Calendar.getInstance();
037: cal.set(2001, 10, 24, 0, 0, 0);
038: long planned = cal.getTime().getTime();
039: String frequency = "* * * * *";
040: boolean busy = false;
041:
042: Task task = new Task();
043: try {
044: task.setType(type);
045: task.setPlanned(planned);
046: task.setFrequency(frequency);
047: task.setBusy(busy);
048: } catch (FrequencyException e) {
049: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
050: }
051:
052: TaskManager manager = new MemoryTasks();
053: try {
054: task_id = manager.addTask(task);
055: assertTrue(task_id >= 0);
056: } catch (TaskManagerException e) {
057: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
058: }
059: }
060:
061: public void testGetTask() {
062: int task_id = -1;
063: Task task = null;
064: TaskManager manager = new MemoryTasks();
065: try {
066: Calendar cal = Calendar.getInstance();
067: cal.set(2001, 10, 24, 0, 0, 0);
068:
069: task = new Task();
070: task.setType(TestTasktypes.UPLOAD_GROUPS);
071: task.setPlanned(cal.getTime().getTime());
072: task.setFrequency("* * * * *");
073: task.setBusy(false);
074: task_id = manager.addTask(task);
075:
076: task = manager.getTask(task_id);
077: assertNotNull(task);
078:
079: assertEquals(task.getId(), task_id);
080: assertEquals(task.getType(), TestTasktypes.UPLOAD_GROUPS);
081: assertTrue(task.getPlanned() <= cal.getTime().getTime());
082: assertEquals(task.getFrequency(), "* * * * *");
083: assertEquals(task.isBusy(), false);
084: assertSame(task.getTaskManager(), manager);
085: } catch (FrequencyException e) {
086: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
087: } catch (TaskManagerException e) {
088: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
089: }
090: }
091:
092: public void testUpdateTask() {
093: int task_id = -1;
094: Task task = null;
095: TaskManager manager = new MemoryTasks();
096: try {
097: Calendar cal = Calendar.getInstance();
098: cal.set(2001, 10, 24, 0, 0, 0);
099:
100: task = new Task();
101: task.setType(TestTasktypes.UPLOAD_GROUPS);
102: task.setPlanned(cal.getTime().getTime());
103: task.setFrequency("* * * * *");
104: task.setBusy(false);
105: task_id = manager.addTask(task);
106:
107: cal.set(2002, 02, 12, 0, 0, 0);
108: String type = TestTasktypes.SEND_RANKING;
109: long planned = cal.getTime().getTime();
110: String frequency = "20 */3 * * *";
111: boolean busy = true;
112:
113: task = new Task();
114: task.setId(task_id);
115: task.setType(type);
116: task.setPlanned(planned);
117: task.setFrequency(frequency);
118: task.setBusy(busy);
119:
120: assertTrue(true == manager.updateTask(task));
121:
122: task = manager.getTask(task_id);
123: assertNotNull(task);
124:
125: assertEquals(task.getId(), task_id);
126: assertEquals(task.getType(), type);
127: assertTrue(task.getPlanned() <= planned);
128: assertEquals(task.getFrequency(), frequency);
129: assertEquals(task.isBusy(), busy);
130: assertSame(task.getTaskManager(), manager);
131: } catch (FrequencyException e) {
132: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
133: } catch (TaskManagerException e) {
134: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
135: }
136: }
137:
138: public void testRemoveTask() {
139: int task_id = -1;
140: Task task = null;
141: TaskManager manager = new MemoryTasks();
142: try {
143: Calendar cal = Calendar.getInstance();
144: cal.set(2001, 10, 24, 0, 0, 0);
145:
146: task = new Task();
147: task.setType(TestTasktypes.UPLOAD_GROUPS);
148: task.setPlanned(cal.getTime().getTime());
149: task.setFrequency("* * * * *");
150: task.setBusy(false);
151: task_id = manager.addTask(task);
152:
153: assertTrue(manager.removeTask(task_id));
154: task_id = -1;
155: } catch (FrequencyException e) {
156: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
157: } catch (TaskManagerException e) {
158: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
159: }
160: }
161:
162: public void testGetNonExistingTask() {
163: TaskManager manager = new MemoryTasks();
164: int task_nonexisting_id = 0;
165: try {
166: assertNull(manager.getTask(task_nonexisting_id));
167: } catch (TaskManagerException e) {
168: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
169: }
170: }
171:
172: public void testRemoveNonExistingTask() {
173: TaskManager manager = new MemoryTasks();
174: int task_nonexisting_id = 0;
175: try {
176: assertTrue(false == manager.removeTask(task_nonexisting_id));
177: } catch (TaskManagerException e) {
178: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
179: }
180: }
181:
182: public void testGetTasksToProcess() {
183: int one_hour = 1000 * 60 * 60;
184:
185: TaskManager manager = new MemoryTasks();
186: try {
187: Task task1 = new Task();
188: task1.setType(TestTasktypes.UPLOAD_GROUPS);
189: task1.setPlanned(System.currentTimeMillis() - one_hour);
190: task1.setFrequency(null);
191: task1.setBusy(false);
192:
193: Task task2 = new Task();
194: task2.setType(TestTasktypes.UPLOAD_GROUPS);
195: task2.setPlanned(System.currentTimeMillis() - one_hour);
196: task2.setFrequency(null);
197: task2.setBusy(false);
198:
199: Task task3 = new Task();
200: task3.setType(TestTasktypes.UPLOAD_GROUPS);
201: task3.setPlanned(System.currentTimeMillis() - one_hour);
202: task3.setFrequency(null);
203: task3.setBusy(true);
204:
205: Task task4 = new Task();
206: task4.setType(TestTasktypes.UPLOAD_GROUPS);
207: task4.setPlanned(System.currentTimeMillis() + one_hour);
208: task4.setFrequency(null);
209: task4.setBusy(false);
210:
211: task1.setId(manager.addTask(task1));
212: task2.setId(manager.addTask(task2));
213: task3.setId(manager.addTask(task3));
214: task4.setId(manager.addTask(task4));
215:
216: Collection<Task> tasks_to_process = manager
217: .getTasksToProcess();
218:
219: manager.removeTask(task1.getId());
220: manager.removeTask(task2.getId());
221: manager.removeTask(task3.getId());
222: manager.removeTask(task4.getId());
223:
224: assertEquals(2, tasks_to_process.size());
225: } catch (FrequencyException e) {
226: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
227: } catch (TaskManagerException e) {
228: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
229: }
230: }
231:
232: public void testGetScheduledTasks() {
233: int one_hour = 1000 * 60 * 60;
234:
235: TaskManager manager = new MemoryTasks();
236: try {
237: Task task1 = new Task();
238: task1.setType(TestTasktypes.UPLOAD_GROUPS);
239: task1.setPlanned(System.currentTimeMillis() - one_hour);
240: task1.setFrequency(null);
241: task1.setBusy(false);
242:
243: Task task2 = new Task();
244: task2.setType(TestTasktypes.UPLOAD_GROUPS);
245: task2.setPlanned(System.currentTimeMillis() + one_hour);
246: task2.setFrequency(null);
247: task2.setBusy(true);
248:
249: Task task3 = new Task();
250: task3.setType(TestTasktypes.UPLOAD_GROUPS);
251: task3.setPlanned(System.currentTimeMillis() + one_hour);
252: task3.setFrequency(null);
253: task3.setBusy(false);
254:
255: Task task4 = new Task();
256: task4.setType(TestTasktypes.UPLOAD_GROUPS);
257: task4.setPlanned(System.currentTimeMillis() + one_hour);
258: task4.setFrequency(null);
259: task4.setBusy(false);
260:
261: task1.setId(manager.addTask(task1));
262: task2.setId(manager.addTask(task2));
263: task3.setId(manager.addTask(task3));
264: task4.setId(manager.addTask(task4));
265:
266: Collection<Task> scheduled_tasks = manager
267: .getScheduledTasks();
268:
269: manager.removeTask(task1.getId());
270: manager.removeTask(task2.getId());
271: manager.removeTask(task3.getId());
272: manager.removeTask(task4.getId());
273:
274: assertEquals(2, scheduled_tasks.size());
275: } catch (FrequencyException e) {
276: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
277: } catch (TaskManagerException e) {
278: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
279: }
280: }
281:
282: public void testTaskConclusion() {
283: int one_hour = 1000 * 60 * 60;
284:
285: TaskManager manager = new MemoryTasks();
286: try {
287: Task task1 = new Task();
288: task1.setType(TestTasktypes.UPLOAD_GROUPS);
289: task1.setPlanned(System.currentTimeMillis() - one_hour);
290: task1.setFrequency(null);
291: task1.setBusy(false);
292:
293: Task task2 = new Task();
294: task2.setType(TestTasktypes.UPLOAD_GROUPS);
295: task2.setPlanned(System.currentTimeMillis() - one_hour);
296: task2.setFrequency("0 * * * *");
297: task2.setBusy(false);
298:
299: Task task3 = new Task();
300: task3.setType(TestTasktypes.UPLOAD_GROUPS);
301: task3.setPlanned(System.currentTimeMillis() + one_hour);
302: task3.setFrequency(null);
303: task3.setBusy(false);
304:
305: task1.setId(manager.addTask(task1));
306: task2.setId(manager.addTask(task2));
307: task3.setId(manager.addTask(task3));
308: task1 = manager.getTask(task1.getId());
309: task2 = manager.getTask(task2.getId());
310: task3 = manager.getTask(task3.getId());
311:
312: boolean was_task1_concluded = manager.concludeTask(task1);
313: boolean was_task2_concluded = manager.concludeTask(task2);
314: boolean was_task3_concluded = manager.concludeTask(task3);
315:
316: Task task1_new = manager.getTask(task1.getId());
317: Task task2_new = manager.getTask(task2.getId());
318: Task task3_new = manager.getTask(task3.getId());
319:
320: manager.removeTask(task2.getId());
321: manager.removeTask(task3.getId());
322:
323: assertTrue(was_task1_concluded);
324: assertTrue(was_task2_concluded);
325: assertTrue(false == was_task3_concluded);
326: assertNull(task1_new);
327: assertNotNull(task2_new);
328: assertNotNull(task3_new);
329: assertSame(task2_new.getTaskManager(), manager);
330: assertSame(task3_new.getTaskManager(), manager);
331: assertEquals(task2_new.getId(), task2.getId());
332: assertEquals(task2_new.getType(), task2.getType());
333: assertTrue(task2_new.getPlanned() >= task2.getPlanned());
334: assertEquals(task2_new.getFrequency(), task2.getFrequency());
335: assertEquals(task2_new.isBusy(), task2.isBusy());
336: assertEquals(task3_new.getId(), task3.getId());
337: assertEquals(task3_new.getType(), task3.getType());
338: assertTrue(task3_new.getPlanned() <= task3.getPlanned());
339: assertEquals(task3_new.getFrequency(), task3.getFrequency());
340: assertEquals(task3_new.isBusy(), task3_new.isBusy());
341: } catch (FrequencyException e) {
342: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
343: } catch (TaskManagerException e) {
344: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
345: }
346: }
347:
348: public void testTaskActivation() {
349: TaskManager manager = new MemoryTasks();
350: try {
351: Task task = new Task();
352: task.setType(TestTasktypes.UPLOAD_GROUPS);
353: task.setPlanned(System.currentTimeMillis());
354: task.setFrequency(null);
355: task.setBusy(false);
356:
357: int taskid = manager.addTask(task);
358:
359: manager.activateTask(taskid);
360: task = manager.getTask(taskid);
361: assertSame(task.getTaskManager(), manager);
362: assertEquals(true, task.isBusy());
363: manager.deactivateTask(taskid);
364: task = manager.getTask(taskid);
365: assertSame(task.getTaskManager(), manager);
366: assertEquals(false, task.isBusy());
367:
368: manager.removeTask(task.getId());
369: } catch (FrequencyException e) {
370: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
371: } catch (TaskManagerException e) {
372: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
373: }
374: }
375: }
|