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: TestTask.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler;
009:
010: import com.uwyn.rife.scheduler.Task;
011: import com.uwyn.rife.scheduler.exceptions.FrequencyException;
012: import com.uwyn.rife.site.ValidationError;
013: import com.uwyn.rife.tools.ExceptionUtils;
014: import java.util.Calendar;
015: import java.util.Iterator;
016: import java.util.Set;
017: import junit.framework.TestCase;
018:
019: public class TestTask extends TestCase {
020: public TestTask(String name) {
021: super (name);
022: }
023:
024: public void testInstantiateTask() {
025: Task task = null;
026:
027: assertNull(task);
028: task = new Task();
029: assertNotNull(task);
030: }
031:
032: public void testPopulateTask() {
033: int id = 1;
034: String type = TestTasktypes.UPLOAD_GROUPS;
035: long planned = System.currentTimeMillis();
036: String frequency = "* * * * *";
037: boolean busy = true;
038:
039: try {
040: Task task = new Task();
041: task.setId(id);
042: task.setType(type);
043: task.setPlanned(planned);
044: task.setFrequency(frequency);
045: task.setBusy(busy);
046:
047: assertEquals(id, task.getId());
048: assertEquals(type, task.getType());
049: Calendar calendar = Calendar.getInstance();
050: calendar.setTimeInMillis(planned);
051: calendar.set(Calendar.SECOND, 0);
052: calendar.set(Calendar.MILLISECOND, 0);
053: assertEquals(calendar.getTimeInMillis(), task.getPlanned());
054: assertEquals(frequency, task.getFrequency());
055: assertEquals(busy, task.isBusy());
056: } catch (FrequencyException e) {
057: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
058: }
059: }
060:
061: public void testCloneTask() {
062: int id = 1;
063: String type = TestTasktypes.UPLOAD_GROUPS;
064: long planned = System.currentTimeMillis();
065: String frequency = "* * * * *";
066: boolean busy = true;
067:
068: try {
069: Task task = new Task();
070: task.setId(id);
071: task.setType(type);
072: task.setPlanned(planned);
073: task.setFrequency(frequency);
074: task.setBusy(busy);
075:
076: Task task_clone = task.clone();
077: assertTrue(task != task_clone);
078: assertNotNull(task_clone);
079: assertTrue(task.equals(task_clone));
080: } catch (FrequencyException e) {
081: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
082: } catch (CloneNotSupportedException e) {
083: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
084: }
085: }
086:
087: public void testTaskValidation() {
088: try {
089: Task task = new Task();
090: assertTrue(false == task.validate());
091: assertTrue(2 == task.countValidationErrors());
092: ValidationError error = null;
093: Iterator<ValidationError> it = task.getValidationErrors()
094: .iterator();
095: error = it.next();
096: assertEquals(error.getIdentifier(), "MANDATORY");
097: assertEquals(error.getSubject(), "type");
098: error = it.next();
099: assertEquals(error.getIdentifier(), "MANDATORY");
100: assertEquals(error.getSubject(), "planned");
101:
102: task.setType(TestTasktypes.UPLOAD_GROUPS);
103: task.setPlanned(System.currentTimeMillis() + 2000);
104: task.setFrequency("* * * * *");
105: task.setBusy(false);
106: task.resetValidation();
107: assertTrue(true == task.validate());
108:
109: Calendar cal = Calendar.getInstance();
110: cal.set(1970, 10, 25);
111: task.setPlanned(cal.getTime());
112: task.resetValidation();
113: assertTrue(false == task.validate());
114: assertTrue(1 == task.countValidationErrors());
115: Set<ValidationError> set = task.getValidationErrors();
116: error = set.iterator().next();
117: assertEquals(error.getIdentifier(), "INVALID");
118: assertEquals(error.getSubject(), "planned");
119: task.setPlanned(System.currentTimeMillis() + 2000);
120:
121: task.setFrequency(null);
122: task.resetValidation();
123: assertTrue(true == task.validate());
124: task.setFrequency("* * * * *");
125: } catch (FrequencyException e) {
126: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
127: }
128: }
129: }
|