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.TestSuite;
024:
025: import org.apache.commons.configuration.BaseConfiguration;
026: import org.apache.commons.configuration.Configuration;
027:
028: import org.apache.turbine.modules.scheduledjob.SimpleJob;
029: import org.apache.turbine.services.ServiceManager;
030: import org.apache.turbine.services.TurbineServices;
031: import org.apache.turbine.test.BaseTestCase;
032:
033: /**
034: * Unit testing for the non-persistent implementation of the scheduler service.
035: *
036: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
037: * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 534527 2007-05-02 16:10:59Z tv $
038: */
039: public class TurbineNonPersistentSchedulerServiceTest extends
040: BaseTestCase {
041: private static final String PREFIX = "services."
042: + ScheduleService.SERVICE_NAME + '.';
043:
044: public TurbineNonPersistentSchedulerServiceTest(String name)
045: throws Exception {
046: super (name);
047:
048: ServiceManager serviceManager = TurbineServices.getInstance();
049: serviceManager.setApplicationRoot(".");
050:
051: Configuration cfg = new BaseConfiguration();
052: cfg.setProperty(PREFIX + "classname",
053: TurbineNonPersistentSchedulerService.class.getName());
054:
055: cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob");
056: cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1");
057: cfg
058: .setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND",
059: "10");
060: cfg
061: .setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE",
062: "-1");
063: cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1");
064: cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY",
065: "-1");
066: cfg.setProperty(
067: PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1");
068: cfg.setProperty(PREFIX + "enabled", "true");
069:
070: serviceManager.setConfiguration(cfg);
071:
072: try {
073: serviceManager.init();
074: } catch (Exception e) {
075: e.printStackTrace();
076: fail();
077: }
078: }
079:
080: public static Test suite() {
081: return new TestSuite(
082: TurbineNonPersistentSchedulerServiceTest.class);
083: }
084:
085: /**
086: * Tests the ability to enable and disable the service.
087: */
088: public void testEnableDisable() {
089: try {
090: TurbineScheduler.startScheduler();
091: assertEquals(true, TurbineScheduler.isEnabled());
092:
093: TurbineScheduler.stopScheduler();
094: assertEquals(false, TurbineScheduler.isEnabled());
095: } catch (Exception e) {
096: e.printStackTrace();
097: fail();
098: }
099: }
100:
101: /**
102: * Tests the ability to add and remove a job. A list of jobs will be obtained from
103: * the service to determine if the operation were successful.
104: */
105: public void testAddRemoveJob() {
106: try {
107: // get the current job count for later comparison
108: int jobCount = TurbineScheduler.listJobs().size();
109:
110: // Add a new job entry
111: JobEntry je = new JobEntry();
112: je.setJobId(jobCount + 1);
113: je.setSecond(0);
114: je.setMinute(1);
115: je.setHour(-1);
116: je.setDayOfMonth(-1);
117: je.setWeekDay(-1);
118: je.setTask("SimpleJob");
119:
120: TurbineScheduler.addJob(je);
121: assertEquals(jobCount + 1, TurbineScheduler.listJobs()
122: .size());
123:
124: TurbineScheduler.removeJob(je);
125: assertEquals(jobCount, TurbineScheduler.listJobs().size());
126:
127: } catch (Exception e) {
128: e.printStackTrace();
129: fail();
130: }
131: }
132:
133: /**
134: * Tests the ability to retrieve the job added during initialization.
135: */
136: public void testGetJob() {
137: try {
138: JobEntry je = TurbineScheduler.getJob(1);
139: assertEquals(je.getJobId(), 1);
140: assertEquals(je.getSecond(), 10);
141: assertEquals(je.getMinute(), -1);
142: assertEquals(je.getHour(), -1);
143: assertEquals(je.getDayOfMonth(), -1);
144: assertEquals(je.getWeekDay(), -1);
145: assertEquals(je.getTask(), "SimpleJob");
146: } catch (Exception e) {
147: e.printStackTrace();
148: fail();
149: }
150: }
151:
152: /** Test to make sure a job actually runs. Currently not work.
153: * @TODO Must get testRunningJob to work.
154: *
155: */
156: public void OFFtestRunningJob() {
157: try {
158: int beforeCount = SimpleJob.getCounter();
159: Thread.sleep(120000);
160: int afterCount = SimpleJob.getCounter();
161: assertTrue(beforeCount < afterCount);
162:
163: } catch (Exception e) {
164: e.printStackTrace();
165: fail();
166: }
167: }
168:
169: }
|