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.util.TurbineConfig; 033import org.apache.turbine.util.TurbineException; 034import org.hamcrest.CoreMatchers; 035import org.junit.After; 036import org.junit.Before; 037import org.junit.Test; 038 039/** 040 * Unit testing for the non-persistent implementation of the scheduler service. 041 * 042 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 043 * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $ 044 */ 045public class TurbineNonPersistentSchedulerServiceTest 046{ 047 private TurbineConfig tc = null; 048 049 private ScheduleService scheduler = null; 050 051 @Before 052 public void setUp() throws Exception 053 { 054 tc = 055 new TurbineConfig( 056 ".", 057 "/conf/test/TurbineNonPersistentSchedulerServiceTest.properties"); 058 tc.initialize(); 059 060 scheduler = (ScheduleService)TurbineServices.getInstance().getService(ScheduleService.SERVICE_NAME); 061 } 062 063 @After 064 public void tearDown() throws Exception 065 { 066 if (tc != null) 067 { 068 tc.dispose(); 069 } 070 } 071 072 /** 073 * Tests the ability to enable and disable the service. 074 */ 075 @Test public void testEnableDisable() 076 { 077 try 078 { 079 scheduler.startScheduler(); 080 assertTrue(scheduler.isEnabled()); 081 082 scheduler.stopScheduler(); 083 assertFalse(scheduler.isEnabled()); 084 } 085 catch (Exception e) 086 { 087 e.printStackTrace(); 088 fail(); 089 } 090 } 091 092 /** 093 * Tests the ability to add and remove a job. A list of jobs will be obtained from 094 * the service to determine if the operation were successful. 095 */ 096 @Test public void testAddRemoveJob() 097 { 098 try 099 { 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}