1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.configuration.BaseConfiguration;
23  import org.apache.commons.configuration.Configuration;
24  
25  import org.apache.turbine.modules.scheduledjob.SimpleJob;
26  import org.apache.turbine.services.ServiceManager;
27  import org.apache.turbine.services.TurbineServices;
28  import org.apache.turbine.test.BaseTestCase;
29  
30  /***
31   * Unit testing for the non-persistent implementation of the scheduler service.
32   *
33   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34   * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 264148 2005-08-29 14:21:04Z henning $
35   */
36  public class TurbineNonPersistentSchedulerServiceTest extends BaseTestCase
37  {
38      private static final String PREFIX = "services." + ScheduleService.SERVICE_NAME + '.';
39  
40      public TurbineNonPersistentSchedulerServiceTest(String name)
41              throws Exception
42      {
43          super(name);
44  
45          ServiceManager serviceManager = TurbineServices.getInstance();
46          serviceManager.setApplicationRoot(".");
47  
48          Configuration cfg = new BaseConfiguration();
49          cfg.setProperty(PREFIX + "classname", TurbineNonPersistentSchedulerService.class.getName());
50  
51          cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob");
52          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1");
53          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND", "10");
54          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE", "-1");
55          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1");
56          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY", "-1");
57          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1");
58          cfg.setProperty(PREFIX + "enabled", "true");
59  
60          serviceManager.setConfiguration(cfg);
61  
62          try
63          {
64              serviceManager.init();
65          }
66          catch (Exception e)
67          {
68              e.printStackTrace();
69              fail();
70          }
71      }
72  
73      public static Test suite()
74      {
75          return new TestSuite(TurbineNonPersistentSchedulerServiceTest.class);
76      }
77  
78      /***
79       * Tests the ability to enable and disable the service.
80       */
81      public void testEnableDisable()
82      {
83          try
84          {
85              TurbineScheduler.startScheduler();
86              assertEquals(true, TurbineScheduler.isEnabled());
87  
88              TurbineScheduler.stopScheduler();
89              assertEquals(false, TurbineScheduler.isEnabled());
90          }
91          catch (Exception e)
92          {
93              e.printStackTrace();
94              fail();
95          }
96      }
97  
98      /***
99       * Tests the ability to add and remove a job.  A list of jobs will be obtained from
100      * the service to determine if the operation were successful.
101      */
102     public void testAddRemoveJob()
103     {
104         try
105         {
106             // get the current job count for later comparison
107             int jobCount = TurbineScheduler.listJobs().size();
108 
109             // Add a new job entry
110             JobEntry je = new JobEntry();
111             je.setJobId(jobCount + 1);
112             je.setSecond(0);
113             je.setMinute(1);
114             je.setHour(-1);
115             je.setDayOfMonth(-1);
116             je.setWeekDay(-1);
117             je.setTask("SimpleJob");
118 
119             TurbineScheduler.addJob(je);
120             assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
121 
122             TurbineScheduler.removeJob(je);
123             assertEquals(jobCount, TurbineScheduler.listJobs().size());
124 
125         }
126         catch (Exception e)
127         {
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     {
138         try
139         {
140             JobEntry je = TurbineScheduler.getJob(1);
141             assertEquals(je.getJobId(), 1);
142             assertEquals(je.getSecond(), 10);
143             assertEquals(je.getMinute(), -1);
144             assertEquals(je.getHour(), -1);
145             assertEquals(je.getDayOfMonth(), -1);
146             assertEquals(je.getWeekDay(), -1);
147             assertEquals(je.getTask(), "SimpleJob");
148         }
149         catch (Exception e)
150         {
151             e.printStackTrace();
152             fail();
153         }
154     }
155 
156     /*** Test to make sure a job actually runs.  Currently not work.
157      * @TODO Must get testRunningJob to work.
158      *
159      */
160     public void OFFtestRunningJob()
161     {
162         try
163         {
164            int beforeCount = SimpleJob.getCounter();
165            Thread.sleep(120000);
166            int afterCount = SimpleJob.getCounter();
167            assertTrue(beforeCount < afterCount);
168 
169         }
170         catch (Exception e)
171         {
172             e.printStackTrace();
173             fail();
174         }
175     }
176 
177 }