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.util.TurbineConfig;
32  import org.apache.turbine.util.TurbineException;
33  import org.hamcrest.CoreMatchers;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  /**
39   * Unit testing for the non-persistent implementation of the scheduler service.
40   *
41   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
42   * @version $Id: TurbineNonPersistentSchedulerServiceTest.java 615328 2008-01-25 20:25:05Z tv $
43   */
44  public class TurbineNonPersistentSchedulerServiceTest
45  {
46      private TurbineConfig tc = null;
47  
48      @Before
49      public void setUp() throws Exception
50      {
51          tc =
52              new TurbineConfig(
53                  ".",
54                  "/conf/test/TurbineNonPersistentSchedulerServiceTest.properties");
55          tc.initialize();
56      }
57  
58      @After
59      public void tearDown() throws Exception
60      {
61          if (tc != null)
62          {
63              tc.dispose();
64          }
65      }
66  
67      /**
68       * Tests the ability to enable and disable the service.
69       */
70      @Test public void testEnableDisable()
71      {
72          try
73          {
74              TurbineScheduler.startScheduler();
75              assertTrue(TurbineScheduler.isEnabled());
76  
77              TurbineScheduler.stopScheduler();
78              assertFalse(TurbineScheduler.isEnabled());
79          }
80          catch (Exception e)
81          {
82              e.printStackTrace();
83              fail();
84          }
85      }
86  
87      /**
88       * Tests the ability to add and remove a job.  A list of jobs will be obtained from
89       * the service to determine if the operation were successful.
90       */
91      @Test public void testAddRemoveJob()
92      {
93          try
94          {
95              // get the current job count for later comparison
96              int jobCount = TurbineScheduler.listJobs().size();
97  
98              // Add a new job entry
99              JobEntry je = TurbineScheduler.newJob(0, 1, -1, -1, -1, "SimpleJob");
100 
101             TurbineScheduler.addJob(je);
102             assertEquals(jobCount + 1, TurbineScheduler.listJobs().size());
103             
104             assertTrue(TurbineScheduler.listJobs().contains( je ));
105             TurbineScheduler.removeJob(je);
106             assertTrue(!TurbineScheduler.listJobs().contains( je ));
107             assertEquals(jobCount, TurbineScheduler.listJobs().size());
108 
109         }
110         catch (Exception e)
111         {
112             e.printStackTrace();
113             fail();
114         }
115     }
116 
117     /**
118      * Tests the ability to retrieve the job added during initialization.
119      */
120     @Test public void testGetJob()
121     {
122         try
123         {
124 			JobEntry je = TurbineScheduler.getJob(1);
125 			assertThat(je, CoreMatchers.instanceOf(JobEntryNonPersistent.class));
126 			JobEntryNonPersistent jenp = (JobEntryNonPersistent)je;
127             assertEquals(1, jenp.getJobId());
128             assertEquals(1, jenp.getSecond());
129             assertEquals(-1, jenp.getMinute());
130             assertEquals(-1, jenp.getHour());
131             assertEquals(-1, jenp.getDayOfMonth());
132             assertEquals(-1, jenp.getWeekDay());
133             assertEquals("SimpleJob", jenp.getTask());
134         }
135         catch (TurbineException e)
136         {
137             e.printStackTrace();
138             fail();
139         }
140     }
141 
142     /**
143      * Test to make sure a job actually runs.
144      */
145     @Test public void testRunningJob()
146     {
147         try
148         {
149            int beforeCount = SimpleJob.getCounter();
150            Thread.sleep(1200);
151            int afterCount = SimpleJob.getCounter();
152            assertTrue(beforeCount < afterCount);
153 
154         }
155         catch (Exception e)
156         {
157             e.printStackTrace();
158             fail();
159         }
160     }
161 
162 }