    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.util.TurbineConfig;
    032import org.apache.turbine.util.TurbineException;
    033import org.hamcrest.CoreMatchers;
    034import org.junit.After;
    035import org.junit.Before;
    036import org.junit.Test;
    037
    038/**
    039 * Unit testing for the non-persistent implementation of the scheduler service.
    040 *
    041 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    042 * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $
    043 */
    044public class TurbineNonPersistentSchedulerServiceTest
    045{
    046    private TurbineConfig tc = null;
    047
    048    @Before
    049    public void setUp() throws Exception
    050    {
    051        tc =
    052            new TurbineConfig(
    053                ".",
    054                "/conf/test/TurbineNonPersistentSchedulerServiceTest.properties");
    055        tc.initialize();
    056    }
    057
    058    @After
    059    public void tearDown() throws Exception
    060    {
    061        if (tc != null)
    062        {
    063            tc.dispose();
    064        }
    065    }
    066
    067    /**
    068     * Tests the ability to enable and disable the service.
    069     */
    070    @Test public void testEnableDisable()
    071    {
    072        try
    073        {
    074            TurbineScheduler.startScheduler();
    075            assertTrue(TurbineScheduler.isEnabled());
    076
    077            TurbineScheduler.stopScheduler();
    078            assertFalse(TurbineScheduler.isEnabled());
    079        }
    080        catch (Exception e)
    081        {
    082            e.printStackTrace();
    083            fail();
    084        }
    085    }
    086
    087    /**
    088     * Tests the ability to add and remove a job.  A list of jobs will be obtained from
    089     * the service to determine if the operation were successful.
    090     */
    091    @Test public void testAddRemoveJob()
    092    {
    093        try
    094        {
    095            // get the current job count for later comparison
    096            int jobCount = TurbineScheduler.listJobs().size();
    097
    098            // Add a new job entry
    099            JobEntry je = TurbineScheduler.newJob(0, 1, -1, -1, -1, "SimpleJob");
    100
    101            TurbineScheduler.addJob(je);
    102            assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
    103
    104            assertTrue(TurbineScheduler.listJobs().contains( je ));
    105            TurbineScheduler.removeJob(je);
    106            assertTrue(!TurbineScheduler.listJobs().contains( 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                        JobEntry je = TurbineScheduler.getJob(1);
    125                        assertThat(je, CoreMatchers.instanceOf(JobEntryNonPersistent.class));
    126                        JobEntryNonPersistent jenp = (JobEntryNonPersistent)je;
    127            assertEquals(1, jenp.getJobId());
    128            assertEquals(1, jenp.getSecond());
    129            assertEquals(-1, jenp.getMinute());
    130            assertEquals(-1, jenp.getHour());
    131            assertEquals(-1, jenp.getDayOfMonth());
    132            assertEquals(-1, jenp.getWeekDay());
    133            assertEquals("SimpleJob", jenp.getTask());
    134        }
    135        catch (TurbineException e)
    136        {
    137            e.printStackTrace();
    138            fail();
    139        }
    140    }
    141
    142    /**
    143     * Test to make sure a job actually runs.
    144     */
    145    @Test public void testRunningJob()
    146    {
    147        try
    148        {
    149           int beforeCount = SimpleJob.getCounter();
    150           Thread.sleep(1200);
    151           int afterCount = SimpleJob.getCounter();
    152           assertTrue(beforeCount < afterCount);
    153
    154        }
    155        catch (Exception e)
    156        {
    157            e.printStackTrace();
    158            fail();
    159        }
    160    }
    161
    162}