001: /*
002: * Copyright 2004-2006 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: */
016: package org.quartz;
017:
018: import java.util.Calendar;
019:
020: import junit.framework.TestCase;
021:
022: import org.quartz.JobDetail;
023: import org.quartz.JobExecutionContext;
024: import org.quartz.JobExecutionException;
025: import org.quartz.Scheduler;
026: import org.quartz.SimpleTrigger;
027: import org.quartz.StatefulJob;
028: import org.quartz.Trigger;
029: import org.quartz.impl.StdSchedulerFactory;
030:
031: /**
032: * Test Trigger priority support.
033: */
034: public class PriorityTest extends TestCase {
035:
036: private static StringBuffer result = new StringBuffer();
037:
038: public static class TestJob implements StatefulJob {
039: public void execute(JobExecutionContext context)
040: throws JobExecutionException {
041: result.append(context.getTrigger().getName());
042: }
043: }
044:
045: public void testSameDefaultPriority() throws Exception {
046: result = new StringBuffer();
047:
048: Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
049:
050: Calendar cal = Calendar.getInstance();
051: cal.add(Calendar.SECOND, 1);
052:
053: Trigger trig1 = new SimpleTrigger("T1", null, cal.getTime());
054: Trigger trig2 = new SimpleTrigger("T2", null, cal.getTime());
055:
056: JobDetail jobDetail = new JobDetail("JD", null, TestJob.class);
057:
058: sched.scheduleJob(jobDetail, trig1);
059:
060: trig2.setJobName(jobDetail.getName());
061: sched.scheduleJob(trig2);
062:
063: sched.start();
064:
065: Thread.sleep(2000);
066:
067: assertEquals("T1T2", result.toString());
068:
069: sched.shutdown();
070: }
071:
072: public void testDifferentPriority() throws Exception {
073: result = new StringBuffer();
074:
075: Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
076:
077: Calendar cal = Calendar.getInstance();
078: cal.add(Calendar.SECOND, 1);
079:
080: Trigger trig1 = new SimpleTrigger("T1", null, cal.getTime());
081: trig1.setPriority(5);
082:
083: Trigger trig2 = new SimpleTrigger("T2", null, cal.getTime());
084: trig2.setPriority(10);
085:
086: JobDetail jobDetail = new JobDetail("JD", null, TestJob.class);
087:
088: sched.scheduleJob(jobDetail, trig1);
089:
090: trig2.setJobName(jobDetail.getName());
091: sched.scheduleJob(trig2);
092:
093: sched.start();
094:
095: Thread.sleep(2000);
096:
097: assertEquals("T2T1", result.toString());
098:
099: sched.shutdown();
100: }
101: }
|