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  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.configuration.BaseConfiguration;
26  import org.apache.commons.configuration.Configuration;
27  
28  import org.apache.turbine.modules.scheduledjob.SimpleJob;
29  import org.apache.turbine.services.ServiceManager;
30  import org.apache.turbine.services.TurbineServices;
31  import org.apache.turbine.test.BaseTestCase;
32  
33  /***
34   * Unit testing for the non-persistent implementation of the scheduler service.
35   *
36   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
37   * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 534527 2007-05-02 16:10:59Z tv $
38   */
39  public class TurbineNonPersistentSchedulerServiceTest extends BaseTestCase
40  {
41      private static final String PREFIX = "services." + ScheduleService.SERVICE_NAME + '.';
42  
43      public TurbineNonPersistentSchedulerServiceTest(String name)
44              throws Exception
45      {
46          super(name);
47  
48          ServiceManager serviceManager = TurbineServices.getInstance();
49          serviceManager.setApplicationRoot(".");
50  
51          Configuration cfg = new BaseConfiguration();
52          cfg.setProperty(PREFIX + "classname", TurbineNonPersistentSchedulerService.class.getName());
53  
54          cfg.setProperty(PREFIX + "scheduler.jobs", "SimpleJob");
55          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.ID", "1");
56          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.SECOND", "10");
57          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.MINUTE", "-1");
58          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.HOUR", "-1");
59          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.WEEK_DAY", "-1");
60          cfg.setProperty(PREFIX + "scheduler.job.SimpleJob.DAY_OF_MONTH", "-1");
61          cfg.setProperty(PREFIX + "enabled", "true");
62  
63          serviceManager.setConfiguration(cfg);
64  
65          try
66          {
67              serviceManager.init();
68          }
69          catch (Exception e)
70          {
71              e.printStackTrace();
72              fail();
73          }
74      }
75  
76      public static Test suite()
77      {
78          return new TestSuite(TurbineNonPersistentSchedulerServiceTest.class);
79      }
80  
81      /***
82       * Tests the ability to enable and disable the service.
83       */
84      public void testEnableDisable()
85      {
86          try
87          {
88              TurbineScheduler.startScheduler();
89              assertEquals(true, TurbineScheduler.isEnabled());
90  
91              TurbineScheduler.stopScheduler();
92              assertEquals(false, TurbineScheduler.isEnabled());
93          }
94          catch (Exception e)
95          {
96              e.printStackTrace();
97              fail();
98          }
99      }
100 
101     /***
102      * Tests the ability to add and remove a job.  A list of jobs will be obtained from
103      * the service to determine if the operation were successful.
104      */
105     public void testAddRemoveJob()
106     {
107         try
108         {
109             // get the current job count for later comparison
110             int jobCount = TurbineScheduler.listJobs().size();
111 
112             // Add a new job entry
113             JobEntry je = new JobEntry();
114             je.setJobId(jobCount + 1);
115             je.setSecond(0);
116             je.setMinute(1);
117             je.setHour(-1);
118             je.setDayOfMonth(-1);
119             je.setWeekDay(-1);
120             je.setTask("SimpleJob");
121 
122             TurbineScheduler.addJob(je);
123             assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
124 
125             TurbineScheduler.removeJob(je);
126             assertEquals(jobCount, TurbineScheduler.listJobs().size());
127 
128         }
129         catch (Exception e)
130         {
131             e.printStackTrace();
132             fail();
133         }
134     }
135 
136     /***
137      * Tests the ability to retrieve the job added during initialization.
138      */
139     public void testGetJob()
140     {
141         try
142         {
143             JobEntry je = TurbineScheduler.getJob(1);
144             assertEquals(je.getJobId(), 1);
145             assertEquals(je.getSecond(), 10);
146             assertEquals(je.getMinute(), -1);
147             assertEquals(je.getHour(), -1);
148             assertEquals(je.getDayOfMonth(), -1);
149             assertEquals(je.getWeekDay(), -1);
150             assertEquals(je.getTask(), "SimpleJob");
151         }
152         catch (Exception e)
153         {
154             e.printStackTrace();
155             fail();
156         }
157     }
158 
159     /*** Test to make sure a job actually runs.  Currently not work.
160      * @TODO Must get testRunningJob to work.
161      *
162      */
163     public void OFFtestRunningJob()
164     {
165         try
166         {
167            int beforeCount = SimpleJob.getCounter();
168            Thread.sleep(120000);
169            int afterCount = SimpleJob.getCounter();
170            assertTrue(beforeCount < afterCount);
171 
172         }
173         catch (Exception e)
174         {
175             e.printStackTrace();
176             fail();
177         }
178     }
179 
180 }