1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class TransientSchedulerTest extends LedgeTestCase
63 {
64 private FileSystem fs = null;
65
66 private TransientScheduler scheduler;
67
68
69
70
71 protected void setUp() throws Exception
72 {
73 super.setUp();
74 fs = FileSystem.getStandardFileSystem("src/test/resources");
75 InputSource source = new InputSource(fs.getInputStream(
76 "config/org.objectledge.logging.LoggingConfigurator.xml"));
77 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
78 Document logConfig = builder.parse(source);
79 LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
80 configurator.doConfigure(logConfig.getDocumentElement(), LogManager.getLoggerRepository());
81 Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(MailSystem.class));
82 Configuration config = getConfig("config/org.objectledge.threads.ThreadPool.xml");
83 Context context = new Context();
84 ThreadPool threadPool = new ThreadPool(null, context,config, logger);
85 config = getConfig("config/org.objectledge.scheduler.TransientScheduler.xml");
86 XMLValidator validator = new XMLValidator(new XMLGrammarCache());
87 I18n i18n = new XMLI18n(config, logger, fs, validator);
88
89 ScheduleFactory[] scheduleFactories = new ScheduleFactory[2];
90 scheduleFactories[0] = new AtScheduleFactory();
91 scheduleFactories[1] = new CronScheduleFactory(i18n);
92 MutablePicoContainer container = new DefaultPicoContainer();
93 scheduler = new TransientScheduler(container, config,
94 logger, threadPool, scheduleFactories);
95 scheduler.start();
96 }
97
98 public void testCreateJobDescriptor()
99 throws Exception
100 {
101 assertNotNull(scheduler);
102 try
103 {
104 scheduler.createJobDescriptor("foo",null,null);
105 fail("should throw the exception");
106 }
107 catch(UnsupportedOperationException e)
108 {
109
110 }
111 }
112
113
114 public void testDeleteJobDescriptor()
115 throws Exception
116 {
117 try
118 {
119 scheduler.deleteJobDescriptor(null);
120 fail("should throw the exception");
121 }
122 catch(UnsupportedOperationException e)
123 {
124
125 }
126 }
127
128 public void testAllowsModifications()
129 {
130 assertEquals(false,scheduler.allowsModifications());
131 }
132
133 public void testLoadJobs()
134 {
135
136 }
137
138
139 public void testEnable()
140 throws Exception
141 {
142 AbstractJobDescriptor job = scheduler.getJobDescriptor("foo");
143 assertNotNull(job);
144 assertEquals(true, job.isEnabled());
145 scheduler.disable(job);
146 assertEquals(false, job.isEnabled());
147 scheduler.enable(job);
148 assertEquals(true, job.isEnabled());
149 }
150
151 public void testGetJobDescriptor()
152 {
153 AbstractJobDescriptor job = scheduler.getJobDescriptor("foo");
154 try
155 {
156 job.setArgument("");
157 fail("should throw the exception");
158 }
159 catch(JobModificationException e)
160 {
161
162 }
163 try
164 {
165 job.setSchedule(null);
166 fail("should throw the exception");
167 }
168 catch(JobModificationException e)
169 {
170
171 }
172 try
173 {
174 job.setAutoClean(true);
175 fail("should throw the exception");
176 }
177 catch(JobModificationException e)
178 {
179
180 }
181 try
182 {
183 job.setJobClassName("");
184 fail("should throw the exception");
185 }
186 catch(JobModificationException e)
187 {
188
189 }
190 try
191 {
192 job.setRunCountLimit(1);
193 fail("should throw the exception");
194 }
195 catch(JobModificationException e)
196 {
197
198 }
199 try
200 {
201 job.setTimeLimit(null,null);
202 fail("should throw the exception");
203 }
204 catch(JobModificationException e)
205 {
206
207 }
208 try
209 {
210 job.setReentrant(false);
211 fail("should throw the exception");
212 }
213 catch(JobModificationException e)
214 {
215
216 }
217
218
219
220 }
221
222
223 public void testGetJobDescriptors()
224 {
225 AbstractJobDescriptor[] jobs = scheduler.getJobDescriptors();
226 assertEquals(2, jobs.length);
227 }
228
229 public void testGetScheduleTypes()
230 {
231 String[] types = scheduler.getScheduleTypes();
232 assertEquals(2, types.length);
233 }
234
235 public void testCreateSchedule()
236 throws Exception
237 {
238 Schedule schedule = scheduler.createSchedule("at","");
239 assertNotNull(schedule);
240 assertEquals("",schedule.getConfig());
241 assertEquals("at",schedule.getType());
242 assertNull(null,schedule.getNextRunTime(new Date(), new Date()));
243 try
244 {
245 scheduler.createSchedule("foo","");
246 fail("should throw the exception");
247 }
248 catch(InvalidScheduleException e)
249 {
250
251 }
252 schedule = scheduler.createSchedule("cron","* * * * *");
253 Calendar calendar = Calendar.getInstance();
254 calendar.set(Calendar.SECOND,15);
255 calendar.set(Calendar.MILLISECOND,0);
256 Date currentTime = calendar.getTime();
257
258 calendar.add(Calendar.MINUTE,-2);
259 Date lastRun = calendar.getTime();
260 calendar.add(Calendar.MINUTE, 3);
261 calendar.set(Calendar.SECOND,0);
262 Date expected = calendar.getTime();
263 assertEquals(expected,schedule.getNextRunTime(currentTime, lastRun));
264 assertEquals("cron",schedule.getType());
265 assertEquals("* * * * *",schedule.getConfig());
266
267
268
269 schedule = scheduler.createSchedule("cron","* * * jan mon");
270 schedule = scheduler.createSchedule("cron","0-15/60 * * jan mon");
271 schedule = scheduler.createSchedule("cron","@yearly");
272 try
273 {
274 schedule = scheduler.createSchedule("cron","foo bar");
275 fail("should throw the exception");
276 }
277 catch(TokenMgrError e)
278 {
279
280 }
281 try
282 {
283 schedule = scheduler.createSchedule("cron","\ufefe");
284 fail("should throw the exception");
285 }
286 catch(TokenMgrError e)
287 {
288
289 }
290
291
292 schedule = scheduler.createSchedule("at","2003-12-01 10:22");
293 assertNotNull(schedule);
294 calendar.set(Calendar.YEAR, 2000);
295 Date oldDate = calendar.getTime();
296 calendar.set(Calendar.YEAR, 2003);
297 calendar.set(Calendar.MINUTE,22);
298 calendar.set(Calendar.HOUR_OF_DAY,10);
299 calendar.set(Calendar.MONTH,11);
300 calendar.set(Calendar.DAY_OF_MONTH,1);
301 calendar.set(Calendar.SECOND,0);
302 calendar.set(Calendar.MILLISECOND,0);
303 assertEquals(calendar.getTime(),schedule.getNextRunTime(oldDate, oldDate));
304 calendar.set(Calendar.YEAR,2005);
305 oldDate = calendar.getTime();
306 assertNull(schedule.getNextRunTime(oldDate, oldDate));
307 try
308 {
309 schedule.setConfig("foo-bar");
310 fail("should throw the exception");
311 }
312 catch(InvalidScheduleException e)
313 {
314
315 }
316 }
317
318 public void testGetDateFormat()
319 {
320 assertEquals(new SimpleDateFormat(AbstractScheduler.DATE_FORMAT_DEFAULT),
321 scheduler.getDateFormat());
322 }
323
324
325
326
327 private Configuration getConfig(String name)
328 throws Exception
329 {
330 return getConfig(fs, name);
331 }
332
333 }