001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.service.impl;
017:
018: import java.util.List;
019:
020: import org.kuali.kfs.batch.BatchJobStatus;
021: import org.kuali.kfs.context.KualiTestBase;
022: import org.kuali.kfs.context.SpringContext;
023: import org.kuali.kfs.service.SchedulerService;
024: import org.kuali.test.ConfigureContext;
025: import org.kuali.test.fixtures.UserNameFixture;
026:
027: @ConfigureContext(session=UserNameFixture.KULUSER,initializeBatchSchedule=true)
028: public class SchedulerServiceImplTest extends KualiTestBase {
029:
030: // tests added to make sure that the scheduler was available during the tests
031: public void testGetJobs_unscheduled() {
032: SchedulerService s = SpringContext
033: .getBean(SchedulerService.class);
034: List<BatchJobStatus> jobs = s.getJobs("unscheduled");
035: for (BatchJobStatus job : jobs) {
036: System.out.println(job);
037: }
038: }
039:
040: public void testGetJobs_scheduled() {
041: SchedulerService s = SpringContext
042: .getBean(SchedulerService.class);
043: List<BatchJobStatus> jobs = s.getJobs("scheduled");
044: for (BatchJobStatus job : jobs) {
045: System.out.println(job);
046: }
047: }
048:
049: /*
050: * // this job seems to be missing from the system during the test runs public void testScheduledJobTrigger() throws Exception {
051: * SchedulerService s = SpringContext.getBean(SchedulerService.class); BatchJobStatus job = s.getJob( "scheduled", "scheduleJob" );
052: * assertNotNull( "job must not be null", job ); System.out.println( "scheduleJob Next Run Time: " + job.getNextRunDate() );
053: * Calendar cal = Calendar.getInstance(); cal.setTime( job.getNextRunDate() ); assertEquals( "year must be 2099", 2099, cal.get(
054: * Calendar.YEAR ) ); }
055: */
056:
057: /**
058: * Test the running of a job. There is not much to test on the results, except that this does not cause an error.
059: */
060:
061: public void testRunJob() throws Exception {
062: SchedulerService s = SpringContext
063: .getBean(SchedulerService.class);
064: BatchJobStatus job = s.getJob(
065: SchedulerService.UNSCHEDULED_GROUP, "scrubberJob");
066: assertNotNull("job must not be null", job);
067: System.out.println(job);
068: job.runJob(null);
069: System.out.println(s.getRunningJobs());
070: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
071: "scrubberJob"));
072: Thread.sleep(1000);
073: System.out.println(s.getRunningJobs());
074: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
075: "scrubberJob"));
076: Thread.sleep(1000);
077: System.out.println(s.getRunningJobs());
078: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
079: "scrubberJob"));
080: Thread.sleep(1000);
081: System.out.println(s.getRunningJobs());
082: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
083: "scrubberJob"));
084: Thread.sleep(1000);
085: System.out.println(s.getRunningJobs());
086: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
087: "scrubberJob"));
088: Thread.sleep(1000);
089: System.out.println(s.getRunningJobs());
090: System.out.println(s.getJob(SchedulerService.UNSCHEDULED_GROUP,
091: "scrubberJob"));
092: }
093:
094: /**
095: * Test that the unschedule job function works and removes the job from the standard scheduled group. Assumes: scrubberJob
096: * exists as a job in the scheduled group.
097: */
098:
099: public void testUnscheduleJob() throws Exception {
100: SchedulerService s = SpringContext
101: .getBean(SchedulerService.class);
102: BatchJobStatus job = s.getJob(SchedulerService.SCHEDULED_GROUP,
103: "scrubberJob");
104: assertNotNull("job must not be null", job);
105:
106: assertTrue("must return isScheduled == true", job.isScheduled());
107:
108: s.removeScheduled(job.getName());
109: job = s.getJob(SchedulerService.SCHEDULED_GROUP, "scrubberJob");
110: assertNull("new attempt to retrieve job must be null", job);
111:
112: job = s.getJob(SchedulerService.UNSCHEDULED_GROUP,
113: "scrubberJob");
114: assertNotNull("job must not be null", job);
115: assertFalse("must return isScheduled == false", job
116: .isScheduled());
117: }
118:
119: /**
120: * Test that the schedule job function works and puts the job into the standard scheduled group. Also tests to make sure that
121: * BatchJobStatus detects the scheduled status even if it is in the unscheduled group. Assumes: clearOldOriginEntriesJob exists
122: * as a job in the unscheduled group.
123: */
124:
125: public void testScheduleJob() throws Exception {
126: SchedulerService s = SpringContext
127: .getBean(SchedulerService.class);
128: BatchJobStatus job = s.getJob(
129: SchedulerService.UNSCHEDULED_GROUP,
130: "clearOldOriginEntriesJob");
131: assertNotNull("job must not be null", job);
132:
133: assertFalse("must return isScheduled == false", job
134: .isScheduled());
135:
136: job.schedule();
137:
138: job = s.getJob(SchedulerService.UNSCHEDULED_GROUP,
139: "clearOldOriginEntriesJob");
140: assertNotNull("job (in unsched group) must not be null", job);
141: assertTrue("must return isScheduled == true", job.isScheduled());
142:
143: job = s.getJob(SchedulerService.SCHEDULED_GROUP,
144: "clearOldOriginEntriesJob");
145: assertNotNull("job (in sched group) must not be null", job);
146: assertTrue("must return isScheduled == true", job.isScheduled());
147:
148: }
149:
150: /*
151: * This test has problems with timing. It needs a job that it can rely on running for a specified period of time before being
152: * included in the automated tests.
153: */
154: /*
155: * public void testJobInterrupt() throws Exception{ SchedulerService s = SpringContext.getBean(SchedulerService.class);
156: * BatchJobStatus job = s.getJob(SchedulerService.UNSCHEDULED_GROUP, "scrubberJob" ); job.runJob( null ); // wait for job to
157: * enter running status int waitCount = 0; // provide an "out" in case things fail badly while ( !job.isRunning() && waitCount <
158: * 500 ) { Thread.sleep( 50 ); waitCount++; } // stop the job job.interrupt(); waitCount = 0; while ( job.isRunning() &&
159: * waitCount < 500 ) { Thread.sleep( 50 ); waitCount++; } Thread.sleep( 2000 ); job =
160: * s.getJob(SchedulerService.UNSCHEDULED_GROUP, "scrubberJob" ); List<BatchJobStatus> jobs = s.getJobs(); for ( BatchJobStatus
161: * b : jobs ) { System.out.println( b ); } assertEquals( "job status not correct", SchedulerService.CANCELLED_JOB_STATUS_CODE,
162: * job.getStatus() ); }
163: */
164: }
|