    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.test.BaseTestCase;
    032import org.apache.turbine.util.TurbineConfig;
    033import org.apache.turbine.util.TurbineException;
    034import org.hamcrest.CoreMatchers;
    035import org.junit.After;
    036import org.junit.Before;
    037import org.junit.Test;
    038import org.quartz.JobKey;
    039
    040/**
    041 * Unit testing for the quartz implementation of the scheduler service.
    042 *
    043 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
    044 */
    045public class QuartzSchedulerServiceTest extends BaseTestCase
    046{
    047    private TurbineConfig tc = null;
    048
    049    @Before
    050    public void setUp() throws Exception
    051    {
    052        tc =
    053            new TurbineConfig(
    054                ".",
    055                "/conf/test/TestFulcrumComponents.properties");
    056        tc.initialize();
    057    }
    058
    059    @After
    060    public void tearDown() throws Exception
    061    {
    062        if (tc != null)
    063        {
    064            tc.dispose();
    065        }
    066    }
    067
    068    /**
    069     * Tests the ability to enable and disable the service.
    070     */
    071    @Test public void testEnableDisable()
    072    {
    073        try
    074        {
    075            TurbineScheduler.startScheduler();
    076            assertTrue(TurbineScheduler.isEnabled());
    077
    078            TurbineScheduler.stopScheduler();
    079            assertFalse(TurbineScheduler.isEnabled());
    080        }
    081        catch (Exception e)
    082        {
    083            e.printStackTrace();
    084            fail();
    085        }
    086    }
    087
    088    /**
    089     * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    090     * the service to determine if the operation were successful.
    091     */
    092    @Test public void testAddRemoveJob()
    093    {
    094        try
    095        {
    096            // get the current job count for later comparison
    097            int jobCount = TurbineScheduler.listJobs().size();
    098
    099            // 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}