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: TestXml2MemoryScheduler.java 3714 2007-04-08 02:57:38Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.schedulermanagers;
009:
010: import com.uwyn.rife.scheduler.*;
011:
012: import com.uwyn.rife.config.RifeConfig;
013: import com.uwyn.rife.resources.ResourceFinderClasspath;
014: import com.uwyn.rife.scheduler.exceptions.SchedulerManagerException;
015: import com.uwyn.rife.scheduler.exceptions.TaskManagerException;
016: import com.uwyn.rife.scheduler.exceptions.TaskoptionManagerException;
017: import com.uwyn.rife.scheduler.schedulermanagers.exceptions.SchedulerNotFoundException;
018: import com.uwyn.rife.tools.ExceptionUtils;
019: import com.uwyn.rife.tools.Localization;
020: import java.util.Calendar;
021: import java.util.Collection;
022: import junit.framework.TestCase;
023:
024: public class TestXml2MemoryScheduler extends TestCase {
025: public TestXml2MemoryScheduler(String name) {
026: super (name);
027: }
028:
029: public void testInstantiation() {
030: Xml2MemoryScheduler xml2scheduler = new Xml2MemoryScheduler();
031:
032: assertNotNull(xml2scheduler);
033: }
034:
035: public void testParse() {
036: Scheduler scheduler = null;
037:
038: try {
039: scheduler = new MemoryScheduler(
040: "xml/test_xml2scheduler.xml",
041: ResourceFinderClasspath.getInstance())
042: .getScheduler();
043:
044: TaskManager task_manager = scheduler.getTaskManager();
045: TaskoptionManager taskoption_manager = scheduler
046: .getTaskoptionManager();
047: Collection<Task> tasks = task_manager.getTasksToProcess();
048: TestExecutor executor1 = new TestExecutor();
049: TestExecutor2 executor2 = new TestExecutor2();
050:
051: assertEquals(tasks.size(), 4);
052:
053: for (Task task : tasks) {
054: Calendar cal = Calendar
055: .getInstance(RifeConfig.Tools
056: .getDefaultTimeZone(), Localization
057: .getLocale());
058:
059: switch (task.getId()) {
060: case 0:
061: assertNotNull(task);
062: assertEquals(executor1.getHandledTasktype(), task
063: .getType());
064: assertTrue(task.getPlanned() <= Calendar
065: .getInstance().getTimeInMillis());
066: assertNull(task.getFrequency());
067: break;
068: case 1:
069: assertNotNull(task);
070: assertEquals(executor2.getHandledTasktype(), task
071: .getType());
072: assertTrue(task.getPlanned() <= Calendar
073: .getInstance().getTimeInMillis());
074: assertNull(task.getFrequency());
075: break;
076: case 2:
077: assertNotNull(task);
078: assertEquals(executor1.getHandledTasktype(), task
079: .getType());
080: cal.set(2002, Calendar.SEPTEMBER, 10, 17, 10, 0);
081: cal.set(Calendar.MILLISECOND, 0);
082: assertEquals(task.getPlanned(), cal
083: .getTimeInMillis());
084: assertNull(task.getFrequency());
085: break;
086: case 4:
087: assertNotNull(task);
088: assertEquals(executor2.getHandledTasktype(), task
089: .getType());
090: cal.set(2002, Calendar.JULY, 7, 10, 12, 0);
091: cal.set(Calendar.MILLISECOND, 0);
092: assertEquals(task.getPlanned(), cal
093: .getTimeInMillis());
094: assertEquals("*/10 9 15 * *", task.getFrequency());
095: break;
096: default:
097: fail();
098: break;
099: }
100: }
101:
102: tasks = task_manager.getScheduledTasks();
103:
104: assertEquals(tasks.size(), 1);
105:
106: for (Task task : tasks) {
107: switch (task.getId()) {
108: case 3:
109: assertNotNull(task);
110: assertEquals(executor1.getHandledTasktype(), task
111: .getType());
112: assertTrue(task.getPlanned() > Calendar
113: .getInstance().getTimeInMillis());
114: assertEquals("0 9 * * 1", task.getFrequency());
115: break;
116: default:
117: fail();
118: break;
119: }
120: }
121:
122: Collection<Taskoption> taskoptions = taskoption_manager
123: .getTaskoptions(2);
124:
125: assertEquals(taskoptions.size(), 2);
126:
127: for (Taskoption taskoption : taskoptions) {
128: assertNotNull(taskoption);
129:
130: if (taskoption.getName().equals("option1")) {
131: assertEquals("value1", taskoption.getValue());
132: } else if (taskoption.getName().equals("option2")) {
133: assertEquals("value2", taskoption.getValue());
134: } else {
135: fail();
136: }
137: }
138:
139: Collection<Executor> executors = scheduler.getExecutors();
140:
141: assertEquals(executors.size(), 2);
142:
143: for (Executor executor : executors) {
144: assertNotNull(executor);
145:
146: if (executor.getHandledTasktype().equals(
147: "test_executor")) {
148: assertTrue(executor instanceof com.uwyn.rife.scheduler.TestExecutor);
149: } else if (executor.getHandledTasktype().equals(
150: "test_executor2")) {
151: assertTrue(executor instanceof com.uwyn.rife.scheduler.TestExecutor2);
152: } else {
153: fail();
154: }
155: }
156: } catch (TaskManagerException e) {
157: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
158: } catch (TaskoptionManagerException e) {
159: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
160: } catch (SchedulerManagerException e) {
161: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
162: }
163: }
164:
165: public void testSelectedShortClassname() {
166: MemoryScheduler scheduler = null;
167:
168: try {
169: scheduler = new MemoryScheduler("TestSelectorScheduler",
170: ResourceFinderClasspath.getInstance());
171: } catch (SchedulerManagerException e) {
172: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
173: }
174:
175: assertTrue(null != scheduler.getScheduler());
176: }
177:
178: public void testSelectedFullClassname() {
179: MemoryScheduler scheduler = null;
180:
181: try {
182: scheduler = new MemoryScheduler(
183: "com.uwyn.rife.selector.TestSelectorScheduler",
184: ResourceFinderClasspath.getInstance());
185: } catch (SchedulerManagerException e) {
186: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
187: }
188:
189: assertTrue(null != scheduler.getScheduler());
190: }
191:
192: public void testUnavailableXmlFile() {
193: Scheduler scheduler = null;
194:
195: try {
196: scheduler = new MemoryScheduler(
197: "xml/this_file_is_not_there.xml",
198: ResourceFinderClasspath.getInstance())
199: .getScheduler();
200: fail();
201: assertNotNull(scheduler);
202: } catch (SchedulerManagerException e) {
203: assertTrue(e instanceof SchedulerNotFoundException);
204: }
205: }
206: }
|