001 package 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 import junit.framework.Test; 023 import junit.framework.TestCase; 024 import junit.framework.TestSuite; 025 026 import org.apache.commons.configuration.BaseConfiguration; 027 import org.apache.commons.configuration.Configuration; 028 029 import org.apache.turbine.modules.scheduledjob.SimpleJob; 030 import org.apache.turbine.services.ServiceManager; 031 import org.apache.turbine.services.TurbineServices; 032 //import org.apache.turbine.test.BaseTestCase; 033 034 /** 035 * Unit testing for the non-persistent implementation of the scheduler service. 036 * 037 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 038 * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $ 039 */ 040 public class TurbineNonPersistentSchedulerServiceTest extends TestCase 041 { 042 private static final String PREFIX = "services." + ScheduleService.SERVICE_NAME + '.'; 043 044 public TurbineNonPersistentSchedulerServiceTest(String name) 045 throws Exception 046 { 047 super(name); 048 049 ServiceManager serviceManager = TurbineServices.getInstance(); 050 serviceManager.setApplicationRoot("."); 051 052 Configuration cfg = new BaseConfiguration(); 053 cfg.setProperty(PREFIX + "classname", TurbineNonPersistentSchedulerService.class.getName()); 054 055 cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob"); 056 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1"); 057 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND", "10"); 058 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE", "-1"); 059 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1"); 060 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY", "-1"); 061 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1"); 062 cfg.setProperty(PREFIX + "enabled", "true"); 063 064 serviceManager.setConfiguration(cfg); 065 066 try 067 { 068 serviceManager.init(); 069 } 070 catch (Exception e) 071 { 072 e.printStackTrace(); 073 fail(); 074 } 075 } 076 077 public static Test suite() 078 { 079 return new TestSuite(TurbineNonPersistentSchedulerServiceTest.class); 080 } 081 082 /** 083 * Tests the ability to enable and disable the service. 084 */ 085 public void testEnableDisable() 086 { 087 try 088 { 089 TurbineScheduler.startScheduler(); 090 assertEquals(true, TurbineScheduler.isEnabled()); 091 092 TurbineScheduler.stopScheduler(); 093 assertEquals(false, TurbineScheduler.isEnabled()); 094 } 095 catch (Exception e) 096 { 097 e.printStackTrace(); 098 fail(); 099 } 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 }