    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  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25
    26  import org.apache.commons.configuration.BaseConfiguration;
    27  import org.apache.commons.configuration.Configuration;
    28
    29  import org.apache.turbine.modules.scheduledjob.SimpleJob;
    30  import org.apache.turbine.services.ServiceManager;
    31  import org.apache.turbine.services.TurbineServices;
    32  //import org.apache.turbine.test.BaseTestCase;
    33
    34  /**
    35   * Unit testing for the non-persistent implementation of the scheduler service.
    36   *
    37   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    38   * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $
    39   */
    40  public class TurbineNonPersistentSchedulerServiceTest extends TestCase
    41  {
    42      private static final String PREFIX = "services." + ScheduleService.SERVICE_NAME + '.';
    43
    44      public TurbineNonPersistentSchedulerServiceTest(String name)
    45              throws Exception
    46      {
    47          super(name);
    48
    49          ServiceManager serviceManager = TurbineServices.getInstance();
    50          serviceManager.setApplicationRoot(".");
    51
    52          Configuration cfg = new BaseConfiguration();
    53          cfg.setProperty(PREFIX + "classname", TurbineNonPersistentSchedulerService.class.getName());
    54
    55          cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob");
    56          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1");
    57          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND", "10");
    58          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE", "-1");
    59          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1");
    60          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY", "-1");
    61          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1");
    62          cfg.setProperty(PREFIX + "enabled", "true");
    63
    64          serviceManager.setConfiguration(cfg);
    65
    66          try
    67          {
    68              serviceManager.init();
    69          }
    70          catch (Exception e)
    71          {
    72              e.printStackTrace();
    73              fail();
    74          }
    75      }
    76
    77      public static Test suite()
    78      {
    79          return new TestSuite(TurbineNonPersistentSchedulerServiceTest.class);
    80      }
    81
    82      /**
    83       * Tests the ability to enable and disable the service.
    84       */
    85      public void testEnableDisable()
    86      {
    87          try
    88          {
    89              TurbineScheduler.startScheduler();
    90              assertEquals(true, TurbineScheduler.isEnabled());
    91
    92              TurbineScheduler.stopScheduler();
    93              assertEquals(false, TurbineScheduler.isEnabled());
    94          }
    95          catch (Exception e)
    96          {
    97              e.printStackTrace();
    98              fail();
    99          }
    100     }
    101
    102     /**
    103      * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    104      * the service to determine if the operation were successful.
    105      */
    106     public void testAddRemoveJob()
    107     {
    108         try
    109         {
    110             // get the current job count for later comparison
    111             int jobCount = TurbineScheduler.listJobs().size();
    112
    113             // Add a new job entry
    114 			JobEntry je = new JobEntry();
    115             je.setJobId(jobCount + 1);
    116             je.setSecond(0);
    117             je.setMinute(1);
    118             je.setHour(-1);
    119             je.setDayOfMonth(-1);
    120             je.setWeekDay(-1);
    121             je.setTask("SimpleJob");
    122
    123             TurbineScheduler.addJob(je);
    124             assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
    125
    126             TurbineScheduler.removeJob(je);
    127             assertEquals(jobCount, TurbineScheduler.listJobs().size());
    128
    129         }
    130         catch (Exception e)
    131         {
    132             e.printStackTrace();
    133             fail();
    134         }
    135     }
    136
    137     /**
    138      * Tests the ability to retrieve the job added during initialization.
    139      */
    140     public void testGetJob()
    141     {
    142         try
    143         {
    144 			JobEntry je = (JobEntry)TurbineScheduler.getJob(1);
    145             assertEquals(je.getJobId(), 1);
    146             assertEquals(je.getSecond(), 10);
    147             assertEquals(je.getMinute(), -1);
    148             assertEquals(je.getHour(), -1);
    149             assertEquals(je.getDayOfMonth(), -1);
    150             assertEquals(je.getWeekDay(), -1);
    151             assertEquals(je.getTask(), "SimpleJob");
    152         }
    153         catch (Exception e)
    154         {
    155             e.printStackTrace();
    156             fail();
    157         }
    158     }
    159
    160     /** Test to make sure a job actually runs.  Currently not work.
    161      * @TODO Must get testRunningJob to work.
    162      *
    163      */
    164     public void OFFtestRunningJob()
    165     {
    166         try
    167         {
    168            int beforeCount = SimpleJob.getCounter();
    169            Thread.sleep(120000);
    170            int afterCount = SimpleJob.getCounter();
    171            assertTrue(beforeCount < afterCount);
    172
    173         }
    174         catch (Exception e)
    175         {
    176             e.printStackTrace();
    177             fail();
    178         }
    179     }
    180
    181 }