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.services.TurbineServices;
    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
    39  /**
    40   * Unit testing for the non-persistent implementation of the scheduler service.
    41   *
    42   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    43   * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $
    44   */
    45  public class TurbineNonPersistentSchedulerServiceTest
    46  {
    47      private TurbineConfig tc = null;
    48
    49      private ScheduleService scheduler = null;
    50
    51      @Before
    52      public void setUp() throws Exception
    53      {
    54          tc =
    55              new TurbineConfig(
    56                  ".",
    57                  "/conf/test/TurbineNonPersistentSchedulerServiceTest.properties");
    58          tc.initialize();
    59
    60          scheduler = (ScheduleService)TurbineServices.getInstance().getService(ScheduleService.SERVICE_NAME);
    61      }
    62
    63      @After
    64      public void tearDown() throws Exception
    65      {
    66          if (tc != null)
    67          {
    68              tc.dispose();
    69          }
    70      }
    71
    72      /**
    73       * Tests the ability to enable and disable the service.
    74       */
    75      @Test public void testEnableDisable()
    76      {
    77          try
    78          {
    79              scheduler.startScheduler();
    80              assertTrue(scheduler.isEnabled());
    81
    82              scheduler.stopScheduler();
    83              assertFalse(scheduler.isEnabled());
    84          }
    85          catch (Exception e)
    86          {
    87              e.printStackTrace();
    88              fail();
    89          }
    90      }
    91
    92      /**
    93       * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    94       * the service to determine if the operation were successful.
    95       */
    96      @Test public void testAddRemoveJob()
    97      {
    98          try
    99          {
    100             // get the current job count for later comparison
    101             int jobCount = scheduler.listJobs().size();
    102
    103             // Add a new job entry
    104             JobEntry je = scheduler.newJob(0, 1, -1, -1, -1, "SimpleJob");
    105
    106             scheduler.addJob(je);
    107             assertEquals(jobCount + 1, scheduler.listJobs().size());
    108
    109             assertTrue(scheduler.listJobs().contains( je ));
    110             scheduler.removeJob(je);
    111             assertTrue(!scheduler.listJobs().contains( je ));
    112             assertEquals(jobCount, scheduler.listJobs().size());
    113
    114         }
    115         catch (Exception e)
    116         {
    117             e.printStackTrace();
    118             fail();
    119         }
    120     }
    121
    122     /**
    123      * Tests the ability to retrieve the job added during initialization.
    124      */
    125     @Test public void testGetJob()
    126     {
    127         try
    128         {
    129 			JobEntry je = scheduler.getJob(1);
    130 			assertThat(je, CoreMatchers.instanceOf(JobEntryNonPersistent.class));
    131 			JobEntryNonPersistent jenp = (JobEntryNonPersistent)je;
    132             assertEquals(1, jenp.getJobId());
    133             assertEquals(1, jenp.getSecond());
    134             assertEquals(-1, jenp.getMinute());
    135             assertEquals(-1, jenp.getHour());
    136             assertEquals(-1, jenp.getDayOfMonth());
    137             assertEquals(-1, jenp.getWeekDay());
    138             assertEquals("SimpleJob", jenp.getTask());
    139         }
    140         catch (TurbineException e)
    141         {
    142             e.printStackTrace();
    143             fail();
    144         }
    145     }
    146
    147     /**
    148      * Test to make sure a job actually runs.
    149      */
    150     @Test public void testRunningJob()
    151     {
    152         try
    153         {
    154            int beforeCount = SimpleJob.getCounter();
    155            Thread.sleep(1200);
    156            int afterCount = SimpleJob.getCounter();
    157            assertTrue(beforeCount < afterCount);
    158
    159         }
    160         catch (Exception e)
    161         {
    162             e.printStackTrace();
    163             fail();
    164         }
    165     }
    166
    167 }