1 package org.apache.turbine.services.schedule; 2 3 18 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 22 import org.apache.commons.configuration.BaseConfiguration; 23 import org.apache.commons.configuration.Configuration; 24 25 import org.apache.turbine.modules.scheduledjob.SimpleJob; 26 import org.apache.turbine.services.ServiceManager; 27 import org.apache.turbine.services.TurbineServices; 28 import org.apache.turbine.test.BaseTestCase; 29 30 36 public class TurbineNonPersistentSchedulerServiceTest extends BaseTestCase 37 { 38 private static final String PREFIX = "services." + ScheduleService.SERVICE_NAME + '.'; 39 40 public TurbineNonPersistentSchedulerServiceTest(String name) 41 throws Exception 42 { 43 super(name); 44 45 ServiceManager serviceManager = TurbineServices.getInstance(); 46 serviceManager.setApplicationRoot("."); 47 48 Configuration cfg = new BaseConfiguration(); 49 cfg.setProperty(PREFIX + "classname", TurbineNonPersistentSchedulerService.class.getName()); 50 51 cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob"); 52 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1"); 53 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND", "10"); 54 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE", "-1"); 55 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1"); 56 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY", "-1"); 57 cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1"); 58 cfg.setProperty(PREFIX + "enabled", "true"); 59 60 serviceManager.setConfiguration(cfg); 61 62 try 63 { 64 serviceManager.init(); 65 } 66 catch (Exception e) 67 { 68 e.printStackTrace(); 69 fail(); 70 } 71 } 72 73 public static Test suite() 74 { 75 return new TestSuite(TurbineNonPersistentSchedulerServiceTest.class); 76 } 77 78 81 public void testEnableDisable() 82 { 83 try 84 { 85 TurbineScheduler.startScheduler(); 86 assertEquals(true, TurbineScheduler.isEnabled()); 87 88 TurbineScheduler.stopScheduler(); 89 assertEquals(false, TurbineScheduler.isEnabled()); 90 } 91 catch (Exception e) 92 { 93 e.printStackTrace(); 94 fail(); 95 } 96 } 97 98 102 public void testAddRemoveJob() 103 { 104 try 105 { 106 int jobCount = TurbineScheduler.listJobs().size(); 108 109 JobEntry je = new JobEntry(); 111 je.setJobId(jobCount + 1); 112 je.setSecond(0); 113 je.setMinute(1); 114 je.setHour(-1); 115 je.setDayOfMonth(-1); 116 je.setWeekDay(-1); 117 je.setTask("SimpleJob"); 118 119 TurbineScheduler.addJob(je); 120 assertEquals(jobCount + 1, TurbineScheduler.listJobs().size()); 121 122 TurbineScheduler.removeJob(je); 123 assertEquals(jobCount, TurbineScheduler.listJobs().size()); 124 125 } 126 catch (Exception e) 127 { 128 e.printStackTrace(); 129 fail(); 130 } 131 } 132 133 136 public void testGetJob() 137 { 138 try 139 { 140 JobEntry je = TurbineScheduler.getJob(1); 141 assertEquals(je.getJobId(), 1); 142 assertEquals(je.getSecond(), 10); 143 assertEquals(je.getMinute(), -1); 144 assertEquals(je.getHour(), -1); 145 assertEquals(je.getDayOfMonth(), -1); 146 assertEquals(je.getWeekDay(), -1); 147 assertEquals(je.getTask(), "SimpleJob"); 148 } 149 catch (Exception e) 150 { 151 e.printStackTrace(); 152 fail(); 153 } 154 } 155 156 160 public void OFFtestRunningJob() 161 { 162 try 163 { 164 int beforeCount = SimpleJob.getCounter(); 165 Thread.sleep(120000); 166 int afterCount = SimpleJob.getCounter(); 167 assertTrue(beforeCount < afterCount); 168 169 } 170 catch (Exception e) 171 { 172 e.printStackTrace(); 173 fail(); 174 } 175 } 176 177 } 178 | Popular Tags |