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: TestMemoryTaskoptions.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.taskoptionmanagers;
009:
010: import com.uwyn.rife.scheduler.*;
011:
012: import com.uwyn.rife.scheduler.exceptions.SchedulerException;
013: import com.uwyn.rife.scheduler.exceptions.TaskoptionManagerException;
014: import com.uwyn.rife.scheduler.taskmanagers.MemoryTasks;
015: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.DuplicateTaskoptionException;
016: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.InexistentTaskIdException;
017: import com.uwyn.rife.tools.ExceptionUtils;
018: import java.util.Calendar;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import junit.framework.TestCase;
022:
023: public class TestMemoryTaskoptions extends TestCase {
024: public TestMemoryTaskoptions(String name) {
025: super (name);
026: }
027:
028: public void testInstantiateTaskoptionManager() {
029: TaskoptionManager manager = new MemoryTaskoptions();
030: assertNotNull(manager);
031: }
032:
033: public void testAddTaskoptionWithInexistentTaskId() {
034: Taskoption taskoption = new Taskoption();
035: taskoption.setTaskId(0);
036: taskoption.setName("name");
037: taskoption.setValue("value");
038:
039: Scheduler scheduler = new Scheduler(new MemoryTasks(),
040: new MemoryTaskoptions());
041: TaskoptionManager manager = scheduler.getTaskoptionManager();
042: try {
043: manager.addTaskoption(taskoption);
044: fail();
045: } catch (InexistentTaskIdException e) {
046: assertTrue(true);
047: } catch (TaskoptionManagerException e) {
048: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
049: }
050: }
051:
052: public void testAddTaskoption() {
053: Scheduler scheduler = new Scheduler(new MemoryTasks(),
054: new MemoryTaskoptions());
055: TaskManager task_manager = scheduler.getTaskManager();
056: TaskoptionManager taskoption_manager = scheduler
057: .getTaskoptionManager();
058: int task_id = 0;
059: Task task = new Task();
060: String taskoption_name = "name";
061: String taskoption_value = "value";
062:
063: try {
064: Calendar cal = Calendar.getInstance();
065: cal.set(2001, 10, 24, 0, 0, 0);
066:
067: task.setType(TestTasktypes.UPLOAD_GROUPS);
068: task.setPlanned(cal.getTime());
069: task.setFrequency("* * * * *");
070:
071: task_id = task_manager.addTask(task);
072:
073: Taskoption taskoption = new Taskoption();
074: taskoption.setTaskId(task_id);
075: taskoption.setName(taskoption_name);
076: taskoption.setValue(taskoption_value);
077:
078: assertTrue(taskoption_manager.addTaskoption(taskoption));
079: } catch (SchedulerException e) {
080: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
081: }
082: }
083:
084: public void testAddDuplicateTaskoption() {
085: Scheduler scheduler = new Scheduler(new MemoryTasks(),
086: new MemoryTaskoptions());
087: TaskManager task_manager = scheduler.getTaskManager();
088: TaskoptionManager taskoption_manager = scheduler
089: .getTaskoptionManager();
090: int task_id = 0;
091: Task task = new Task();
092: String taskoption_name = "name";
093: String taskoption_value = "value";
094:
095: try {
096: Calendar cal = Calendar.getInstance();
097: cal.set(2001, 10, 24, 0, 0, 0);
098:
099: task.setType(TestTasktypes.UPLOAD_GROUPS);
100: task.setPlanned(cal.getTime());
101: task.setFrequency("* * * * *");
102:
103: task_id = task_manager.addTask(task);
104:
105: Taskoption taskoption1 = new Taskoption();
106: taskoption1.setTaskId(task_id);
107: taskoption1.setName(taskoption_name);
108: taskoption1.setValue(taskoption_value);
109:
110: assertTrue(taskoption_manager.addTaskoption(taskoption1));
111: } catch (SchedulerException e) {
112: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
113: }
114:
115: try {
116: Taskoption taskoption2 = new Taskoption();
117: taskoption2.setTaskId(task_id);
118: taskoption2.setName(taskoption_name);
119: taskoption2.setValue(taskoption_value);
120:
121: taskoption_manager.addTaskoption(taskoption2);
122: fail();
123: } catch (DuplicateTaskoptionException e) {
124: assertTrue(true);
125: } catch (TaskoptionManagerException e) {
126: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
127: }
128: }
129:
130: public void testGetTaskoption() {
131: Scheduler scheduler = new Scheduler(new MemoryTasks(),
132: new MemoryTaskoptions());
133: TaskManager task_manager = scheduler.getTaskManager();
134: TaskoptionManager taskoption_manager = scheduler
135: .getTaskoptionManager();
136: int task_id = 0;
137: Task task = new Task();
138: String taskoption_name = "name";
139: String taskoption_value = "value";
140:
141: try {
142: Calendar cal = Calendar.getInstance();
143: cal.set(2001, 10, 24, 0, 0, 0);
144:
145: task.setType(TestTasktypes.UPLOAD_GROUPS);
146: task.setPlanned(cal.getTime());
147: task.setFrequency("* * * * *");
148:
149: task_id = task_manager.addTask(task);
150:
151: Taskoption taskoption1 = new Taskoption();
152: taskoption1.setTaskId(task_id);
153: taskoption1.setName(taskoption_name);
154: taskoption1.setValue(taskoption_value);
155:
156: assertTrue(taskoption_manager.addTaskoption(taskoption1));
157:
158: task = task_manager.getTask(task_id);
159: assertEquals(task.getTaskoptionValue(taskoption_name),
160: taskoption_value);
161:
162: Taskoption taskoption = taskoption_manager.getTaskoption(
163: task_id, taskoption_name);
164: assertNotNull(taskoption);
165:
166: assertEquals(taskoption.getTaskId(), task_id);
167: assertEquals(taskoption.getName(), taskoption_name);
168: assertEquals(taskoption.getValue(), taskoption_value);
169: } catch (SchedulerException e) {
170: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
171: }
172: }
173:
174: public void testUpdateTaskoption() {
175: Scheduler scheduler = new Scheduler(new MemoryTasks(),
176: new MemoryTaskoptions());
177: TaskManager task_manager = scheduler.getTaskManager();
178: TaskoptionManager taskoption_manager = scheduler
179: .getTaskoptionManager();
180: int task_id = 0;
181: Task task = new Task();
182: String taskoption_name = "name";
183: String taskoption_value = "value";
184:
185: try {
186: Calendar cal = Calendar.getInstance();
187: cal.set(2001, 10, 24, 0, 0, 0);
188:
189: task.setType(TestTasktypes.UPLOAD_GROUPS);
190: task.setPlanned(cal.getTime());
191: task.setFrequency("* * * * *");
192:
193: task_id = task_manager.addTask(task);
194:
195: Taskoption taskoption1 = new Taskoption();
196: taskoption1.setTaskId(task_id);
197: taskoption1.setName(taskoption_name);
198: taskoption1.setValue(taskoption_value);
199:
200: assertTrue(taskoption_manager.addTaskoption(taskoption1));
201:
202: taskoption_value = "new_taskoption_value";
203:
204: Taskoption taskoption2 = new Taskoption();
205: taskoption2.setTaskId(task_id);
206: taskoption2.setName(taskoption_name);
207: taskoption2.setValue(taskoption_value);
208:
209: assertTrue(true == taskoption_manager
210: .updateTaskoption(taskoption2));
211:
212: taskoption2 = taskoption_manager.getTaskoption(task_id,
213: taskoption_name);
214: assertNotNull(taskoption2);
215:
216: assertEquals(taskoption2.getTaskId(), task_id);
217: assertEquals(taskoption2.getName(), taskoption_name);
218: assertEquals(taskoption2.getValue(), taskoption_value);
219: } catch (SchedulerException e) {
220: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
221: }
222: }
223:
224: public void testGetTaskoptions() {
225: Scheduler scheduler = new Scheduler(new MemoryTasks(),
226: new MemoryTaskoptions());
227: TaskManager task_manager = scheduler.getTaskManager();
228: TaskoptionManager taskoption_manager = scheduler
229: .getTaskoptionManager();
230: int task_id1 = -1;
231: int task_id2 = -1;
232: Task task = new Task();
233: String taskoption_name = "name";
234: String taskoption_name2 = "name2";
235: String taskoption_value = "value";
236:
237: try {
238: Calendar cal = Calendar.getInstance();
239: cal.set(2001, 10, 24, 0, 0, 0);
240:
241: task.setType(TestTasktypes.UPLOAD_GROUPS);
242: task.setPlanned(cal.getTime());
243: task.setFrequency("* * * * *");
244:
245: task_id1 = task_manager.addTask(task);
246:
247: Taskoption taskoption1 = new Taskoption();
248: taskoption1.setTaskId(task_id1);
249: taskoption1.setName(taskoption_name);
250: taskoption1.setValue(taskoption_value);
251:
252: assertTrue(taskoption_manager.addTaskoption(taskoption1));
253:
254: Taskoption taskoption2 = new Taskoption();
255: taskoption2.setTaskId(task_id1);
256: taskoption2.setName(taskoption_name2);
257: taskoption2.setValue(taskoption_value);
258:
259: assertTrue(taskoption_manager.addTaskoption(taskoption2));
260:
261: task_id2 = task_manager.addTask(task);
262:
263: taskoption1.setTaskId(task_id2);
264:
265: assertTrue(taskoption_manager.addTaskoption(taskoption1));
266:
267: Collection<Taskoption> taskoptions = taskoption_manager
268: .getTaskoptions(task_id1);
269: assertEquals(2, taskoptions.size());
270:
271: Iterator<Taskoption> taskoptions_it = taskoptions
272: .iterator();
273:
274: assertTrue(taskoptions_it.hasNext());
275:
276: Taskoption taskoption = taskoptions_it.next();
277: assertEquals(taskoption.getTaskId(), task_id1);
278: assertEquals(taskoption.getName(), taskoption_name);
279: assertEquals(taskoption.getValue(), "value");
280:
281: assertTrue(taskoptions_it.hasNext());
282:
283: taskoption = taskoptions_it.next();
284: assertEquals(taskoption.getTaskId(), task_id1);
285: assertEquals(taskoption.getName(), taskoption_name2);
286: assertEquals(taskoption.getValue(), "value");
287:
288: assertTrue(!taskoptions_it.hasNext());
289: } catch (SchedulerException e) {
290: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
291: }
292: }
293:
294: public void testRemoveTaskoption() {
295: Scheduler scheduler = new Scheduler(new MemoryTasks(),
296: new MemoryTaskoptions());
297: TaskManager task_manager = scheduler.getTaskManager();
298: TaskoptionManager taskoption_manager = scheduler
299: .getTaskoptionManager();
300: int task_id = 0;
301: Task task = new Task();
302: String taskoption_name = "name";
303: String taskoption_value = "value";
304:
305: try {
306: Calendar cal = Calendar.getInstance();
307: cal.set(2001, 10, 24, 0, 0, 0);
308:
309: task.setType(TestTasktypes.UPLOAD_GROUPS);
310: task.setPlanned(cal.getTime());
311: task.setFrequency("* * * * *");
312:
313: task_id = task_manager.addTask(task);
314:
315: Taskoption taskoption1 = new Taskoption();
316: taskoption1.setTaskId(task_id);
317: taskoption1.setName(taskoption_name);
318: taskoption1.setValue(taskoption_value);
319:
320: assertTrue(taskoption_manager.addTaskoption(taskoption1));
321:
322: assertTrue(taskoption_manager.removeTaskoption(task_id,
323: taskoption_name));
324: } catch (SchedulerException e) {
325: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
326: }
327: }
328:
329: public void testGetNonExistingTaskoption() {
330: Scheduler scheduler = new Scheduler(new MemoryTasks(),
331: new MemoryTaskoptions());
332: TaskManager task_manager = scheduler.getTaskManager();
333: TaskoptionManager taskoption_manager = scheduler
334: .getTaskoptionManager();
335: int task_id = 0;
336: Task task = new Task();
337:
338: try {
339: Calendar cal = Calendar.getInstance();
340: cal.set(2001, 10, 24, 0, 0, 0);
341:
342: task.setType(TestTasktypes.UPLOAD_GROUPS);
343: task.setPlanned(cal.getTime());
344: task.setFrequency("* * * * *");
345:
346: task_id = task_manager.addTask(task);
347:
348: assertNull(taskoption_manager
349: .getTaskoption(task_id, "name"));
350: assertNull(taskoption_manager.getTaskoption(task_id + 1,
351: "name"));
352: } catch (SchedulerException e) {
353: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
354: }
355: }
356:
357: public void testRemoveNonExistingTaskoption() {
358: Scheduler scheduler = new Scheduler(new MemoryTasks(),
359: new MemoryTaskoptions());
360: TaskManager task_manager = scheduler.getTaskManager();
361: TaskoptionManager taskoption_manager = scheduler
362: .getTaskoptionManager();
363: int task_id = 0;
364: Task task = new Task();
365:
366: try {
367: Calendar cal = Calendar.getInstance();
368: cal.set(2001, 10, 24, 0, 0, 0);
369:
370: task.setType(TestTasktypes.UPLOAD_GROUPS);
371: task.setPlanned(cal.getTime());
372: task.setFrequency("* * * * *");
373:
374: task_id = task_manager.addTask(task);
375:
376: assertTrue(false == taskoption_manager.removeTaskoption(
377: task_id, "name"));
378: assertTrue(false == taskoption_manager.removeTaskoption(
379: task_id + 1, "name"));
380: } catch (SchedulerException e) {
381: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
382: }
383: }
384: }
|