⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcmsscheduler.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/scheduler/TestCmsScheduler.java,v $
 * Date   : $Date: 2006/03/27 14:53:06 $
 * Version: $Revision: 1.17 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.scheduler;

import org.opencms.main.CmsContextInfo;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;

import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/** 
 * Test cases for the OpenCms scheduler thread pool.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.17 $
 * 
 * @since 6.0.0
 */
public class TestCmsScheduler extends TestCase {

    /** Number of seconds to wait. */
    public static final int SECONDS_TO_WAIT = 30;

    /** Number of threads to run. */
    public static final int THREADS_TO_RUN = 20;
    
    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCmsScheduler(String arg0) {

        super(arg0);
    }
    
    /**
     * Tests activating and deactivating of scheduled jobs.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testActivateAndDeactivateJob() throws Exception {

        System.out.println("Trying to activate and deactivate an OpenCms job from the OpenCms scheduler.");
        TestScheduledJob.m_runCount = 0;
        // also make sure CmsUUID is initialized
        CmsUUID.init(CmsUUID.getDummyEthernetAddress());
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo();
        contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setClassName(TestScheduledJob.class.getName());          
        jobInfo.setCronExpression("0/2 * * * * ?");
        
        // set this job as "not active"
        jobInfo.setActive(false);
                
        List jobs = new ArrayList();
        jobs.add(jobInfo);
        // create the scheduler with the test job
        CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
        
        // initialize the manager, this will start the scheduled jobs
        scheduler.initialize(null);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while (seconds < 5);

        // make sure the job was not run but still exists in the OpenCms scheduler
        if (TestScheduledJob.m_runCount > 0) {
            fail("Test job was incorrectly run '" + TestScheduledJob.m_runCount + "' times in OpenCms scheduler.");
        }
        assertEquals(1, scheduler.getJobs().size());
        CmsScheduledJobInfo info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
        assertEquals(jobInfo.getId(), info.getId());
        assertEquals(jobInfo.getClassName(), info.getClassName());
        assertEquals(false, info.isActive());
        assertNull(info.getExecutionTimeNext());
        
        // no set the job active and re-schedule it
        info = (CmsScheduledJobInfo)info.clone();
        info.setActive(true);
        scheduler.scheduleJob(null, info);
        
        seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));

        if (TestScheduledJob.m_runCount == 3) {
            System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }                        
        
        assertEquals(1, scheduler.getJobs().size());
        info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
        assertEquals(jobInfo.getId(), info.getId());
        assertEquals(jobInfo.getClassName(), info.getClassName());
        assertEquals(true, info.isActive());
        assertNotNull(info.getExecutionTimeNext());
        
        // reset the count
        TestScheduledJob.m_runCount = 0;

        // deactivate the job again and re-schedule it
        info = (CmsScheduledJobInfo)info.clone();
        info.setActive(false);
        scheduler.scheduleJob(null, info);        
        
        seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while (seconds < 5);

        // make sure the job was not run but still exists in the OpenCms scheduler
        if (TestScheduledJob.m_runCount > 0) {
            fail("Test job was incorrectly run '" + TestScheduledJob.m_runCount + "' times in OpenCms scheduler.");
        }
        assertEquals(1, scheduler.getJobs().size());
        info = (CmsScheduledJobInfo)scheduler.getJobs().get(0);
        assertEquals(jobInfo.getId(), info.getId());
        assertEquals(jobInfo.getClassName(), info.getClassName());
        assertEquals(false, info.isActive());
        assertNull(info.getExecutionTimeNext());
        
        // shutdown the scheduler
        scheduler.shutDown();        
    }   

    /**
     * Tests adding and removing a job to the OpenCms schedule manager.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testAddAndRemoveJobFromScheduler() throws Exception {

        System.out.println("Trying to add and remove an OpenCms job from the OpenCms scheduler.");
        TestScheduledJob.m_runCount = 0;
        // also make sure CmsUUID is initialized
        CmsUUID.init(CmsUUID.getDummyEthernetAddress());
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo();
        contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setClassName(TestScheduledJob.class.getName());  
        
        jobInfo.setCronExpression("0/2 * * * * ?");
                
        List jobs = new ArrayList();
        jobs.add(jobInfo);
        // create the scheduler with the test job
        CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
        
        // initialize the manager, this will start the scheduled jobs
        scheduler.initialize(null);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));

        if (TestScheduledJob.m_runCount == 3) {
            System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }
        
        CmsScheduledJobInfo result;
        assertEquals(1, scheduler.getJobs().size());
        result = scheduler.unscheduleJob(null, jobInfo.getId());
        assertNotNull(result);
        assertEquals(0, scheduler.getJobs().size());

        result = scheduler.unscheduleJob(null, "iDontExist");
        assertNull(result);
        
        // shutdown the scheduler
        scheduler.shutDown();        
    }    
    
    /**
     * Tests adding an existing job again to the OpenCms scheduler.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testAddExistingJobAgainToScheduler() throws Exception {

        System.out.println("Trying to schedule an existing job again with the OpenCms scheduler.");
        TestScheduledJob.m_runCount = 0;
        // also make sure CmsUUID is initialized
        CmsUUID.init(CmsUUID.getDummyEthernetAddress());
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo();
        contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setJobName("My job");
        jobInfo.setClassName(TestScheduledJob.class.getName());  
        
        jobInfo.setCronExpression("0/2 * * * * ?");
                
        List jobs = new ArrayList();
        jobs.add(jobInfo);
        // create the scheduler with the test job
        CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
        
        // initialize the manager, this will start the scheduled jobs
        scheduler.initialize(null);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -