View Javadoc
1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertThat;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import org.apache.turbine.modules.scheduledjobs.SimpleJob;
31  import org.apache.turbine.services.TurbineServices;
32  import org.apache.turbine.test.BaseTestCase;
33  import org.apache.turbine.util.TurbineConfig;
34  import org.apache.turbine.util.TurbineException;
35  import org.hamcrest.CoreMatchers;
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.quartz.JobKey;
40  
41  /**
42   * Unit testing for the quartz implementation of the scheduler service.
43   *
44   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
45   */
46  public class QuartzSchedulerServiceTest extends BaseTestCase
47  {
48      private TurbineConfig tc = null;
49  
50      private ScheduleService scheduler = null;
51  
52      @Before
53      public void setUp() throws Exception
54      {
55          tc =
56              new TurbineConfig(
57                  ".",
58                  "/conf/test/TestFulcrumComponents.properties");
59          tc.initialize();
60  
61          scheduler = (ScheduleService)TurbineServices.getInstance().getService(ScheduleService.SERVICE_NAME);
62      }
63  
64      @After
65      public void tearDown() throws Exception
66      {
67          if (tc != null)
68          {
69              tc.dispose();
70          }
71      }
72  
73      /**
74       * Tests the ability to enable and disable the service.
75       */
76      @Test public void testEnableDisable()
77      {
78          try
79          {
80              scheduler.startScheduler();
81              assertTrue(scheduler.isEnabled());
82  
83              scheduler.stopScheduler();
84              assertFalse(scheduler.isEnabled());
85          }
86          catch (Exception e)
87          {
88              e.printStackTrace();
89              fail();
90          }
91      }
92  
93      /**
94       * Tests the ability to add and remove a job.  A list of jobs will be obtained from
95       * the service to determine if the operation were successful.
96       */
97      @Test public void testAddRemoveJob()
98      {
99          try
100         {
101             // get the current job count for later comparison
102             int jobCount = scheduler.listJobs().size();
103 
104             // Add a new job entry
105 			JobEntry je = scheduler.newJob(10, -1, -1, -1, -1, "SimpleJob1");
106             je.setJobId(jobCount + 1);
107 
108             scheduler.addJob(je);
109             assertEquals(jobCount + 1, scheduler.listJobs().size());
110 
111             scheduler.removeJob(je);
112             assertEquals(jobCount, scheduler.listJobs().size());
113 
114         }
115         catch (Exception e)
116         {
117             e.printStackTrace();
118             fail();
119         }
120     }
121 
122     /**
123      * Tests the ability to retrieve the job added during initialization.
124      */
125     @Test public void testGetJob()
126     {
127         try
128         {
129             JobKey jk = new JobKey("SimpleJob", JobEntryQuartz.DEFAULT_JOB_GROUP_NAME);
130 			JobEntry je = scheduler.getJob(jk.hashCode());
131 			assertThat(je, CoreMatchers.instanceOf(JobEntryQuartz.class));
132 			JobEntryQuartz jeq = (JobEntryQuartz)je;
133             assertEquals(jeq.getJobTrigger().getJobKey(), jk);
134             assertEquals(jeq.getTask(), "SimpleJob");
135         }
136         catch (TurbineException e)
137         {
138             e.printStackTrace();
139             fail();
140         }
141     }
142 
143     /**
144      * Test to make sure a job actually runs.
145      */
146     @Test public void testRunningJob()
147     {
148         try
149         {
150            int beforeCount = SimpleJob.getCounter();
151            Thread.sleep(1200);
152            int afterCount = SimpleJob.getCounter();
153            assertTrue(beforeCount < afterCount);
154 
155         }
156         catch (Exception e)
157         {
158             e.printStackTrace();
159             fail();
160         }
161     }
162 
163 }