KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > PriorityTest


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz;
17
18 import java.util.Calendar JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import org.quartz.JobDetail;
23 import org.quartz.JobExecutionContext;
24 import org.quartz.JobExecutionException;
25 import org.quartz.Scheduler;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.StatefulJob;
28 import org.quartz.Trigger;
29 import org.quartz.impl.StdSchedulerFactory;
30
31 /**
32  * Test Trigger priority support.
33  */

34 public class PriorityTest extends TestCase {
35     
36     private static StringBuffer JavaDoc result = new StringBuffer JavaDoc();
37     
38     public static class TestJob implements StatefulJob {
39         public void execute(JobExecutionContext context) throws JobExecutionException {
40             result.append(context.getTrigger().getName());
41         }
42     }
43     
44     public void testSameDefaultPriority() throws Exception JavaDoc{
45         result = new StringBuffer JavaDoc();
46         
47         Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
48         
49         Calendar cal = Calendar.getInstance();
50         cal.add(Calendar.SECOND, 1);
51         
52         Trigger trig1 = new SimpleTrigger("T1", null, cal.getTime());
53         Trigger trig2 = new SimpleTrigger("T2", null, cal.getTime());
54         
55         JobDetail jobDetail = new JobDetail("JD", null, TestJob.class);
56         
57         sched.scheduleJob(jobDetail, trig1);
58
59         trig2.setJobName(jobDetail.getName());
60         sched.scheduleJob(trig2);
61         
62         sched.start();
63         
64         Thread.sleep(2000);
65         
66         assertEquals("T1T2", result.toString());
67         
68         sched.shutdown();
69     }
70     
71     public void testDifferentPriority() throws Exception JavaDoc{
72         result = new StringBuffer JavaDoc();
73         
74         Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
75         
76         Calendar cal = Calendar.getInstance();
77         cal.add(Calendar.SECOND, 1);
78         
79         Trigger trig1 = new SimpleTrigger("T1", null, cal.getTime());
80         trig1.setPriority(5);
81         
82         Trigger trig2 = new SimpleTrigger("T2", null, cal.getTime());
83         trig2.setPriority(10);
84         
85         JobDetail jobDetail = new JobDetail("JD", null, TestJob.class);
86         
87         sched.scheduleJob(jobDetail, trig1);
88
89         trig2.setJobName(jobDetail.getName());
90         sched.scheduleJob(trig2);
91         
92         sched.start();
93         
94         Thread.sleep(2000);
95         
96         assertEquals("T2T1", result.toString());
97         
98         sched.shutdown();
99     }
100 }
101
Popular Tags