View Javadoc

    1   package org.apache.turbine.services.schedule;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22
    23
    24  import static org.junit.Assert.assertEquals;
    25  import static org.junit.Assert.assertFalse;
    26  import static org.junit.Assert.assertThat;
    27  import static org.junit.Assert.assertTrue;
    28  import static org.junit.Assert.fail;
    29
    30  import org.apache.turbine.modules.scheduledjobs.SimpleJob;
    31  import org.apache.turbine.test.BaseTestCase;
    32  import org.apache.turbine.util.TurbineConfig;
    33  import org.apache.turbine.util.TurbineException;
    34  import org.hamcrest.CoreMatchers;
    35  import org.junit.After;
    36  import org.junit.Before;
    37  import org.junit.Test;
    38  import org.quartz.JobKey;
    39
    40  /**
    41   * Unit testing for the quartz implementation of the scheduler service.
    42   *
    43   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
    44   */
    45  public class QuartzSchedulerServiceTest extends BaseTestCase
    46  {
    47      private TurbineConfig tc = null;
    48
    49      @Before
    50      public void setUp() throws Exception
    51      {
    52          tc =
    53              new TurbineConfig(
    54                  ".",
    55                  "/conf/test/TestFulcrumComponents.properties");
    56          tc.initialize();
    57      }
    58
    59      @After
    60      public void tearDown() throws Exception
    61      {
    62          if (tc != null)
    63          {
    64              tc.dispose();
    65          }
    66      }
    67
    68      /**
    69       * Tests the ability to enable and disable the service.
    70       */
    71      @Test public void testEnableDisable()
    72      {
    73          try
    74          {
    75              TurbineScheduler.startScheduler();
    76              assertTrue(TurbineScheduler.isEnabled());
    77
    78              TurbineScheduler.stopScheduler();
    79              assertFalse(TurbineScheduler.isEnabled());
    80          }
    81          catch (Exception e)
    82          {
    83              e.printStackTrace();
    84              fail();
    85          }
    86      }
    87
    88      /**
    89       * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    90       * the service to determine if the operation were successful.
    91       */
    92      @Test public void testAddRemoveJob()
    93      {
    94          try
    95          {
    96              // get the current job count for later comparison
    97              int jobCount = TurbineScheduler.listJobs().size();
    98
    99              // Add a new job entry
    100 			JobEntry je = TurbineScheduler.newJob(10, -1, -1, -1, -1, "SimpleJob1");
    101             je.setJobId(jobCount + 1);
    102
    103             TurbineScheduler.addJob(je);
    104             assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
    105
    106             TurbineScheduler.removeJob(je);
    107             assertEquals(jobCount, TurbineScheduler.listJobs().size());
    108
    109         }
    110         catch (Exception e)
    111         {
    112             e.printStackTrace();
    113             fail();
    114         }
    115     }
    116
    117     /**
    118      * Tests the ability to retrieve the job added during initialization.
    119      */
    120     @Test public void testGetJob()
    121     {
    122         try
    123         {
    124             JobKey jk = new JobKey("SimpleJob", JobEntryQuartz.DEFAULT_JOB_GROUP_NAME);
    125 			JobEntry je = TurbineScheduler.getJob(jk.hashCode());
    126 			assertThat(je, CoreMatchers.instanceOf(JobEntryQuartz.class));
    127 			JobEntryQuartz jeq = (JobEntryQuartz)je;
    128             assertEquals(jeq.getJobTrigger().getJobKey(), jk);
    129             assertEquals(jeq.getTask(), "SimpleJob");
    130         }
    131         catch (TurbineException e)
    132         {
    133             e.printStackTrace();
    134             fail();
    135         }
    136     }
    137
    138     /**
    139      * Test to make sure a job actually runs.
    140      */
    141     @Test public void testRunningJob()
    142     {
    143         try
    144         {
    145            int beforeCount = SimpleJob.getCounter();
    146            Thread.sleep(1200);
    147            int afterCount = SimpleJob.getCounter();
    148            assertTrue(beforeCount < afterCount);
    149
    150         }
    151         catch (Exception e)
    152         {
    153             e.printStackTrace();
    154             fail();
    155         }
    156     }
    157
    158 }