    001package org.apache.turbine.services.schedule;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022
    023
    024import static org.junit.Assert.assertEquals;
    025import static org.junit.Assert.assertFalse;
    026import static org.junit.Assert.assertThat;
    027import static org.junit.Assert.assertTrue;
    028import static org.junit.Assert.fail;
    029
    030import org.apache.turbine.modules.scheduledjobs.SimpleJob;
    031import org.apache.turbine.services.TurbineServices;
    032import org.apache.turbine.test.BaseTestCase;
    033import org.apache.turbine.util.TurbineConfig;
    034import org.apache.turbine.util.TurbineException;
    035import org.hamcrest.CoreMatchers;
    036import org.junit.After;
    037import org.junit.Before;
    038import org.junit.Test;
    039import org.quartz.JobKey;
    040
    041/**
    042 * Unit testing for the quartz implementation of the scheduler service.
    043 *
    044 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
    045 */
    046public class QuartzSchedulerServiceTest extends BaseTestCase
    047{
    048    private TurbineConfig tc = null;
    049
    050    private ScheduleService scheduler = null;
    051
    052    @Before
    053    public void setUp() throws Exception
    054    {
    055        tc =
    056            new TurbineConfig(
    057                ".",
    058                "/conf/test/TestFulcrumComponents.properties");
    059        tc.initialize();
    060
    061        scheduler = (ScheduleService)TurbineServices.getInstance().getService(ScheduleService.SERVICE_NAME);
    062    }
    063
    064    @After
    065    public void tearDown() throws Exception
    066    {
    067        if (tc != null)
    068        {
    069            tc.dispose();
    070        }
    071    }
    072
    073    /**
    074     * Tests the ability to enable and disable the service.
    075     */
    076    @Test public void testEnableDisable()
    077    {
    078        try
    079        {
    080            scheduler.startScheduler();
    081            assertTrue(scheduler.isEnabled());
    082
    083            scheduler.stopScheduler();
    084            assertFalse(scheduler.isEnabled());
    085        }
    086        catch (Exception e)
    087        {
    088            e.printStackTrace();
    089            fail();
    090        }
    091    }
    092
    093    /**
    094     * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    095     * the service to determine if the operation were successful.
    096     */
    097    @Test public void testAddRemoveJob()
    098    {
    099        try
    100        {
    101            // get the current job count for later comparison
    102            int jobCount = scheduler.listJobs().size();
    103
    104            // Add a new job entry
    105                        JobEntry je = scheduler.newJob(10, -1, -1, -1, -1, "SimpleJob1");
    106            je.setJobId(jobCount + 1);
    107
    108            scheduler.addJob(je);
    109            assertEquals(jobCount + 1, scheduler.listJobs().size());
    110
    111            scheduler.removeJob(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            JobKey jk = new JobKey("SimpleJob", JobEntryQuartz.DEFAULT_JOB_GROUP_NAME);
    130                        JobEntry je = scheduler.getJob(jk.hashCode());
    131                        assertThat(je, CoreMatchers.instanceOf(JobEntryQuartz.class));
    132                        JobEntryQuartz jeq = (JobEntryQuartz)je;
    133            assertEquals(jeq.getJobTrigger().getJobKey(), jk);
    134            assertEquals(jeq.getTask(), "SimpleJob");
    135        }
    136        catch (TurbineException e)
    137        {
    138            e.printStackTrace();
    139            fail();
    140        }
    141    }
    142
    143    /**
    144     * Test to make sure a job actually runs.
    145     */
    146    @Test public void testRunningJob()
    147    {
    148        try
    149        {
    150           int beforeCount = SimpleJob.getCounter();
    151           Thread.sleep(1200);
    152           int afterCount = SimpleJob.getCounter();
    153           assertTrue(beforeCount < afterCount);
    154
    155        }
    156        catch (Exception e)
    157        {
    158            e.printStackTrace();
    159            fail();
    160        }
    161    }
    162
    163}