gt:         if (TestScheduledJob.m_runCount == 3) {
context: /*
   * This library is part of OpenCms -
   * the Open Source Content Management System
   *
   * Copyright (c) Alkacon Software GmbH & Co. KG (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 & Co. KG, 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.CmsIllegalArgumentException;
  import org.opencms.main.OpenCms;
  import org.opencms.test.OpenCmsTestCase;
  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 org.quartz.CronScheduleBuilder;
  import org.quartz.JobBuilder;
  import org.quartz.JobDataMap;
  import org.quartz.JobDetail;
  import org.quartz.Scheduler;
  import org.quartz.SchedulerFactory;
  import org.quartz.SimpleScheduleBuilder;
  import org.quartz.SimpleTrigger;
  import org.quartz.impl.JobDetailImpl;
  import org.quartz.impl.StdSchedulerFactory;
  import org.quartz.impl.triggers.CronTriggerImpl;
  import org.quartz.impl.triggers.SimpleTriggerImpl;
  
  /**
   * Test cases for the OpenCms scheduler thread pool.<p>
   *
   * @since 6.0.0
   */
  public class TestCmsScheduler extends OpenCmsTestCase {
  
      /** 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;
  
      /**
       * 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<CmsScheduledJobInfo> jobs = new ArrayList<CmsScheduledJobInfo>();
          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 = 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 = 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 = 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<CmsScheduledJobInfo> jobs = new ArrayList<CmsScheduledJobInfo>();
          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<CmsScheduledJobInfo> jobs = new ArrayList<CmsScheduledJobInfo>();
          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));
  
  