KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > TestCmsScheduler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/scheduler/TestCmsScheduler.java,v $
3  * Date : $Date: 2006/03/27 14:53:06 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.scheduler;
33
34 import org.opencms.main.CmsContextInfo;
35 import org.opencms.main.OpenCms;
36 import org.opencms.util.CmsStringUtil;
37 import org.opencms.util.CmsUUID;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Date JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Properties JavaDoc;
43
44 import junit.framework.TestCase;
45
46 import org.quartz.CronTrigger;
47 import org.quartz.JobDataMap;
48 import org.quartz.JobDetail;
49 import org.quartz.Scheduler;
50 import org.quartz.SchedulerFactory;
51 import org.quartz.SimpleTrigger;
52 import org.quartz.impl.StdSchedulerFactory;
53
54 /**
55  * Test cases for the OpenCms scheduler thread pool.<p>
56  *
57  * @author Alexander Kandzior
58  *
59  * @version $Revision: 1.17 $
60  *
61  * @since 6.0.0
62  */

63 public class TestCmsScheduler extends TestCase {
64
65     /** Number of seconds to wait. */
66     public static final int SECONDS_TO_WAIT = 30;
67
68     /** Number of threads to run. */
69     public static final int THREADS_TO_RUN = 20;
70     
71     /**
72      * Default JUnit constructor.<p>
73      *
74      * @param arg0 JUnit parameters
75      */

76     public TestCmsScheduler(String JavaDoc arg0) {
77
78         super(arg0);
79     }
80     
81     /**
82      * Tests activating and deactivating of scheduled jobs.<p>
83      *
84      * @throws Exception if something goes wrong
85      */

86     public void testActivateAndDeactivateJob() throws Exception JavaDoc {
87
88         System.out.println("Trying to activate and deactivate an OpenCms job from the OpenCms scheduler.");
89         TestScheduledJob.m_runCount = 0;
90         // also make sure CmsUUID is initialized
91
CmsUUID.init(CmsUUID.getDummyEthernetAddress());
92         
93         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
94         CmsContextInfo contextInfo = new CmsContextInfo();
95         contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
96         jobInfo.setContextInfo(contextInfo);
97         jobInfo.setClassName(TestScheduledJob.class.getName());
98         jobInfo.setCronExpression("0/2 * * * * ?");
99         
100         // set this job as "not active"
101
jobInfo.setActive(false);
102                 
103         List JavaDoc jobs = new ArrayList JavaDoc();
104         jobs.add(jobInfo);
105         // create the scheduler with the test job
106
CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
107         
108         // initialize the manager, this will start the scheduled jobs
109
scheduler.initialize(null);
110         
111         int seconds = 0;
112         do {
113             try {
114                 Thread.sleep(1000);
115             } catch (InterruptedException JavaDoc e) {
116                 fail("Something caused the waiting test thread to interrupt!");
117             }
118             seconds++;
119         } while (seconds < 5);
120
121         // make sure the job was not run but still exists in the OpenCms scheduler
122
if (TestScheduledJob.m_runCount > 0) {
123             fail("Test job was incorrectly run '" + TestScheduledJob.m_runCount + "' times in OpenCms scheduler.");
124         }
125         assertEquals(1, scheduler.getJobs().size());
126         CmsScheduledJobInfo info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
127         assertEquals(jobInfo.getId(), info.getId());
128         assertEquals(jobInfo.getClassName(), info.getClassName());
129         assertEquals(false, info.isActive());
130         assertNull(info.getExecutionTimeNext());
131         
132         // no set the job active and re-schedule it
133
info = (CmsScheduledJobInfo)info.clone();
134         info.setActive(true);
135         scheduler.scheduleJob(null, info);
136         
137         seconds = 0;
138         do {
139             try {
140                 Thread.sleep(1000);
141             } catch (InterruptedException JavaDoc e) {
142                 fail("Something caused the waiting test thread to interrupt!");
143             }
144             seconds++;
145         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));
146
147         if (TestScheduledJob.m_runCount == 3) {
148             System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
149         } else {
150             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
151         }
152         
153         assertEquals(1, scheduler.getJobs().size());
154         info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
155         assertEquals(jobInfo.getId(), info.getId());
156         assertEquals(jobInfo.getClassName(), info.getClassName());
157         assertEquals(true, info.isActive());
158         assertNotNull(info.getExecutionTimeNext());
159         
160         // reset the count
161
TestScheduledJob.m_runCount = 0;
162
163         // deactivate the job again and re-schedule it
164
info = (CmsScheduledJobInfo)info.clone();
165         info.setActive(false);
166         scheduler.scheduleJob(null, info);
167         
168         seconds = 0;
169         do {
170             try {
171                 Thread.sleep(1000);
172             } catch (InterruptedException JavaDoc e) {
173                 fail("Something caused the waiting test thread to interrupt!");
174             }
175             seconds++;
176         } while (seconds < 5);
177
178         // make sure the job was not run but still exists in the OpenCms scheduler
179
if (TestScheduledJob.m_runCount > 0) {
180             fail("Test job was incorrectly run '" + TestScheduledJob.m_runCount + "' times in OpenCms scheduler.");
181         }
182         assertEquals(1, scheduler.getJobs().size());
183         info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
184         assertEquals(jobInfo.getId(), info.getId());
185         assertEquals(jobInfo.getClassName(), info.getClassName());
186         assertEquals(false, info.isActive());
187         assertNull(info.getExecutionTimeNext());
188         
189         // shutdown the scheduler
190
scheduler.shutDown();
191     }
192
193     /**
194      * Tests adding and removing a job to the OpenCms schedule manager.<p>
195      *
196      * @throws Exception if something goes wrong
197      */

198     public void testAddAndRemoveJobFromScheduler() throws Exception JavaDoc {
199
200         System.out.println("Trying to add and remove an OpenCms job from the OpenCms scheduler.");
201         TestScheduledJob.m_runCount = 0;
202         // also make sure CmsUUID is initialized
203
CmsUUID.init(CmsUUID.getDummyEthernetAddress());
204         
205         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
206         CmsContextInfo contextInfo = new CmsContextInfo();
207         contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
208         jobInfo.setContextInfo(contextInfo);
209         jobInfo.setClassName(TestScheduledJob.class.getName());
210         
211         jobInfo.setCronExpression("0/2 * * * * ?");
212                 
213         List JavaDoc jobs = new ArrayList JavaDoc();
214         jobs.add(jobInfo);
215         // create the scheduler with the test job
216
CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
217         
218         // initialize the manager, this will start the scheduled jobs
219
scheduler.initialize(null);
220         
221         int seconds = 0;
222         do {
223             try {
224                 Thread.sleep(1000);
225             } catch (InterruptedException JavaDoc e) {
226                 fail("Something caused the waiting test thread to interrupt!");
227             }
228             seconds++;
229         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));
230
231         if (TestScheduledJob.m_runCount == 3) {
232             System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
233         } else {
234             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
235         }
236         
237         CmsScheduledJobInfo result;
238         assertEquals(1, scheduler.getJobs().size());
239         result = scheduler.unscheduleJob(null, jobInfo.getId());
240         assertNotNull(result);
241         assertEquals(0, scheduler.getJobs().size());
242
243         result = scheduler.unscheduleJob(null, "iDontExist");
244         assertNull(result);
245         
246         // shutdown the scheduler
247
scheduler.shutDown();
248     }
249     
250     /**
251      * Tests adding an existing job again to the OpenCms scheduler.<p>
252      *
253      * @throws Exception if something goes wrong
254      */

255     public void testAddExistingJobAgainToScheduler() throws Exception JavaDoc {
256
257         System.out.println("Trying to schedule an existing job again with the OpenCms scheduler.");
258         TestScheduledJob.m_runCount = 0;
259         // also make sure CmsUUID is initialized
260
CmsUUID.init(CmsUUID.getDummyEthernetAddress());
261         
262         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
263         CmsContextInfo contextInfo = new CmsContextInfo();
264         contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
265         jobInfo.setContextInfo(contextInfo);
266         jobInfo.setJobName("My job");
267         jobInfo.setClassName(TestScheduledJob.class.getName());
268         
269         jobInfo.setCronExpression("0/2 * * * * ?");
270                 
271         List JavaDoc jobs = new ArrayList JavaDoc();
272         jobs.add(jobInfo);
273         // create the scheduler with the test job
274
CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
275         
276         // initialize the manager, this will start the scheduled jobs
277
scheduler.initialize(null);
278         
279         int seconds = 0;
280         do {
281             try {
282                 Thread.sleep(1000);
283             } catch (InterruptedException JavaDoc e) {
284                 fail("Something caused the waiting test thread to interrupt!");
285             }
286             seconds++;
287         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));
288
289         if (TestScheduledJob.m_runCount == 3) {
290             System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
291         } else {
292             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
293         }
294         
295         jobInfo = scheduler.getJob(jobInfo.getId());
296         assertEquals("My job", jobInfo.getJobName());
297         
298         CmsScheduledJobInfo newInfo = (CmsScheduledJobInfo)jobInfo.clone();
299         newInfo.setJobName("My CHANGED name");
300         newInfo.setActive(true);
301         assertEquals(1, scheduler.getJobs().size());
302         
303         // re-schedule the job with a different name
304
scheduler.scheduleJob(null, newInfo);
305         // size must still be the same
306
assertEquals(jobInfo.getId(), newInfo.getId());
307         assertEquals(1, scheduler.getJobs().size());
308         jobInfo = scheduler.getJob(newInfo.getId());
309         assertEquals("My CHANGED name", jobInfo.getJobName());
310         
311         // change cron expression to something invalid
312
newInfo = (CmsScheduledJobInfo)jobInfo.clone();
313         newInfo.setActive(true);
314         newInfo.setCronExpression("* * * * * *");
315         assertEquals(1, scheduler.getJobs().size());
316         
317         CmsSchedulerException error = null;
318         try {
319             // re-schedule the job with a different name
320
scheduler.scheduleJob(null, newInfo);
321         } catch (CmsSchedulerException e) {
322             error = e;
323         }
324         assertNotNull(error);
325         // ensure the job is still scheduled
326
assertEquals(1, scheduler.getJobs().size());
327         
328         // shutdown the scheduler
329
scheduler.shutDown();
330     }
331
332     /**
333      * Tests execution of jobs using CmsSchedulerThreadPool.<p>
334      *
335      * @throws Exception if something goes wrong
336      */

337     public void testBasicJobExecution() throws Exception JavaDoc {
338
339         System.out.println("Testing the OpenCms tread pool.");
340         Scheduler scheduler = initOpenCmsScheduler();
341         
342         JobDetail[] jobDetail = new JobDetail[THREADS_TO_RUN];
343         SimpleTrigger[] trigger = new SimpleTrigger[THREADS_TO_RUN];
344
345         for (int i = 0; i < jobDetail.length; i++) {
346             jobDetail[i] = new JobDetail(
347                 "myJob" + i,
348                 Scheduler.DEFAULT_GROUP,
349                 TestCmsJob.class);
350
351             trigger[i] = new SimpleTrigger(
352                 "myTrigger" + i,
353                 Scheduler.DEFAULT_GROUP,
354                 new Date JavaDoc(),
355                 null,
356                 0,
357                 0L);
358         }
359
360         for (int i = 0; i < THREADS_TO_RUN; i++) {
361             scheduler.scheduleJob(jobDetail[i], trigger[i]);
362         }
363
364         int seconds = 0;
365         do {
366             try {
367                 Thread.sleep(1000);
368             } catch (InterruptedException JavaDoc e) {
369                 fail("Something caused the waiting test thread to interrupt!");
370             }
371             seconds++;
372         } while ((seconds < SECONDS_TO_WAIT) && (TestCmsJob.m_running > 0));
373
374
375         if (TestCmsJob.m_running <= 0) {
376             System.out.println("Success: All threads are finished.");
377         } else {
378             fail("Some threads in the pool are still running after " + SECONDS_TO_WAIT + " seconds.");
379         }
380         
381         scheduler.shutdown();
382     }
383     
384     /**
385      * Tests launching of an OpenCms job.<p>
386      *
387      * @throws Exception if something goes wrong
388      */

389     public void testCmsJobLaunch() throws Exception JavaDoc {
390
391         System.out.println("Trying to run an OpenCms job 5x.");
392         TestScheduledJob.m_runCount = 0;
393         
394         Scheduler scheduler = initOpenCmsScheduler();
395         
396         JobDetail jobDetail = new JobDetail(
397             "cmsLaunch",
398             Scheduler.DEFAULT_GROUP,
399             CmsScheduleManager.class);
400         
401         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
402         CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
403         jobInfo.setContextInfo(contextInfo);
404         jobInfo.setClassName(TestScheduledJob.class.getName());
405         
406         JobDataMap jobData = new JobDataMap();
407         jobData.put(CmsScheduleManager.SCHEDULER_JOB_INFO, jobInfo);
408         
409         jobDetail.setJobDataMap(jobData);
410         
411         CronTrigger trigger = new CronTrigger("cmsLaunchTrigger", Scheduler.DEFAULT_GROUP);
412         
413         trigger.setCronExpression("0/2 * * * * ?");
414         
415         scheduler.scheduleJob(jobDetail, trigger);
416         
417         int seconds = 0;
418         do {
419             try {
420                 Thread.sleep(1000);
421             } catch (InterruptedException JavaDoc e) {
422                 fail("Something caused the waiting test thread to interrupt!");
423             }
424             seconds++;
425         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));
426
427
428         if (TestScheduledJob.m_runCount == 5) {
429             System.out.println("Success: Test job was run 5 times.");
430         } else {
431             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
432         }
433        
434         scheduler.shutdown();
435     }
436     
437     /**
438      * Tests launching of an OpenCms job with the OpenCms schedule manager.<p>
439      *
440      * @throws Exception if something goes wrong
441      */

442     public void testJobInOpenCmsScheduler() throws Exception JavaDoc {
443
444         System.out.println("Trying to run an OpenCms job 5x with the OpenCms scheduler.");
445         TestScheduledJob.m_runCount = 0;
446         
447         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
448         CmsContextInfo contextInfo = new CmsContextInfo();
449         contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
450         jobInfo.setContextInfo(contextInfo);
451         jobInfo.setClassName(TestScheduledJob.class.getName());
452         
453         jobInfo.setCronExpression("0/2 * * * * ?");
454                 
455         List JavaDoc jobs = new ArrayList JavaDoc();
456         jobs.add(jobInfo);
457         // create the scheduler with the test job
458
CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
459         
460         // initialize the manager, this will start the scheduled jobs
461
scheduler.initialize(null);
462         
463         int seconds = 0;
464         do {
465             try {
466                 Thread.sleep(1000);
467             } catch (InterruptedException JavaDoc e) {
468                 fail("Something caused the waiting test thread to interrupt!");
469             }
470             seconds++;
471         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));
472
473         if (TestScheduledJob.m_runCount == 5) {
474             System.out.println("Test job was correctly run 5 times in OpenCms scheduler.");
475         } else {
476             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
477         }
478         
479         if (TestScheduledJob.m_instanceCountCopy == 1) {
480             System.out.println("Instance counter has correct value of 1.");
481         } else {
482             fail("Instance counter value of " + TestScheduledJob.m_instanceCountCopy + " invalid!");
483         }
484        
485         // shutdown the scheduler
486
scheduler.shutDown();
487     }
488     
489     /**
490      * Tests launching of a persistent OpenCms job with the OpenCms schedule manager.<p>
491      *
492      * @throws Exception if something goes wrong
493      */

494     public void testPersitentJobInOpenCmsScheduler() throws Exception JavaDoc {
495
496         System.out.println("Trying to run a persistent OpenCms job 5x with the OpenCms scheduler.");
497         TestScheduledJob.m_runCount = 0;
498         
499         CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
500         CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
501         jobInfo.setContextInfo(contextInfo);
502         jobInfo.setClassName(TestScheduledJob.class.getName());
503         jobInfo.setReuseInstance(true);
504         jobInfo.setCronExpression("0/2 * * * * ?");
505         
506         List JavaDoc jobs = new ArrayList JavaDoc();
507         jobs.add(jobInfo);
508         // create the scheduler with the test job
509
CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
510         
511         // initialize the manager, this will start the scheduled jobs
512
scheduler.initialize(null);
513         
514         int seconds = 0;
515         do {
516             try {
517                 Thread.sleep(1000);
518             } catch (InterruptedException JavaDoc e) {
519                 fail("Something caused the waiting test thread to interrupt!");
520             }
521             seconds++;
522         } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));
523
524
525         if (TestScheduledJob.m_runCount == 5) {
526             System.out.println("Test job was correctly run 5 times in OpenCms scheduler.");
527         } else {
528             fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
529         }
530         
531         if (TestScheduledJob.m_instanceCountCopy == 5) {
532             System.out.println("Instance counter was correctly incremented 5 times.");
533         } else {
534             fail("Instance counter was not incremented!");
535         }
536        
537         // shutdown the scheduler
538
scheduler.shutDown();
539     }
540     
541     /**
542      * Initializes a Quartz scheduler.<p>
543      *
544      * @return the initialized scheduler
545      * @throws Exception in case something goes wrong
546      */

547     private Scheduler initOpenCmsScheduler() throws Exception JavaDoc {
548         
549         Properties JavaDoc properties = new Properties JavaDoc();
550         properties.put("org.quartz.scheduler.instanceName", "OpenCmsScheduler");
551         properties.put("org.quartz.scheduler.threadName", "OpenCms: Scheduler");
552         properties.put("org.quartz.scheduler.rmi.export", CmsStringUtil.FALSE);
553         properties.put("org.quartz.scheduler.rmi.proxy", CmsStringUtil.FALSE);
554         properties.put("org.quartz.scheduler.xaTransacted", CmsStringUtil.FALSE);
555         properties.put("org.quartz.threadPool.class", "org.opencms.scheduler.CmsSchedulerThreadPool");
556         properties.put("org.quartz.jobStore.misfireThreshold", "60000");
557         properties.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");
558
559         SchedulerFactory schedulerFactory = new StdSchedulerFactory(properties);
560         Scheduler scheduler = schedulerFactory.getScheduler();
561
562         scheduler.getMetaData();
563         scheduler.start();
564         
565         // also make sure CmsUUID is initialized
566
CmsUUID.init(CmsUUID.getDummyEthernetAddress());
567         
568         return scheduler;
569     }
570 }
Popular Tags