KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > crontab > tests > TestCrontabEntry


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.crontab.tests;
66
67 import com.jcorporate.expresso.services.crontab.CronException;
68 import com.jcorporate.expresso.services.crontab.CrontabEntry;
69 import com.jcorporate.expresso.services.crontab.CrontabListenerI;
70 import junit.framework.TestCase;
71 import org.apache.log4j.Logger;
72
73 import java.util.Calendar JavaDoc;
74 import java.util.Date JavaDoc;
75
76
77 /**
78  * Test case to help decipher and bugfix crontab entry. Unfortunately, part of
79  * this test case needs to be individually verifiable. ie, you must include the
80  *
81  * @author Michael Rimov
82  * @version $Revision: 1.2 $ on $Date: 2004/11/17 20:48:24 $
83  */

84 public class TestCrontabEntry extends TestCase {
85
86     private static final Logger log = Logger.getLogger(TestCrontabEntry.class);
87
88     CrontabListenerI listener;
89
90     public TestCrontabEntry(String JavaDoc _name) {
91         super(_name);
92     }
93
94     public void setUp() {
95         listener = new CrontabListenerI() {
96             public void handleCrontabEntry(CrontabEntry entry) {
97                 log.info("Starting Cron process");
98             }
99         };
100     }
101
102     /**
103      * Tests the &quot;execute at such and such date&quot; crontab
104      */

105     public void testConstructor1() {
106         try {
107             //Create a crontab to execute at this time tomorrow
108
Calendar JavaDoc cal = Calendar.getInstance();
109             cal.add(Calendar.DATE, 1);
110             Date JavaDoc dt = cal.getTime();
111             CrontabEntry cronID = new CrontabEntry(dt, listener);
112             if (log.isDebugEnabled()) {
113                 log.debug("Constructor 1: Created cron: " + cronID.toString() + "now is: " + dt.toString());
114             }
115             assertTrue("Cron date should be the exact time we originally set",
116                     dt.equals(new Date JavaDoc(cronID.getAlarmTime())));
117         } catch (CronException ex) {
118             ex.printStackTrace();
119             fail("crontab entry threw an exception");
120         }
121
122     }
123
124     /**
125      * Tests the &quot;Execute in X-many minutes&quot; constructor
126      */

127     public void testConstructor2() {
128
129         Calendar JavaDoc cal = Calendar.getInstance();
130         cal.add(Calendar.MINUTE, 20);
131         Date JavaDoc testDate = cal.getTime();
132         //
133
//Create a crontab to execute in 20 minutes
134
//
135
CrontabEntry cronID = new CrontabEntry(20, false, listener);
136
137         //Execution time MIGHT not be exactly the same time because it takes
138
//a few milliseconds to create the crontab. What we DO want is to verify
139
//the the difference is less than 1 minute
140
assertTrue(Math.abs(testDate.getTime() - cronID.getAlarmTime()) < (1000 * 60));
141
142         if (log.isDebugEnabled()) {
143             log.debug("Constructor 2: Created cron: " + cronID.toString());
144         }
145         assertTrue("Cron should not be repetitive", cronID.isIsRepetitive() == false);
146         assertTrue("Cron should be relative", cronID.isIsRelative() == true);
147
148         //
149
//Create a crontab to execute in 90 minutes.
150
//
151
cal = Calendar.getInstance();
152         cal.add(Calendar.MINUTE, 90);
153         testDate = cal.getTime();
154         cronID = new CrontabEntry(90, true, listener);
155         if (log.isDebugEnabled()) {
156             log.debug("Constructor 2: Created cron: " + cronID.toString());
157         }
158         assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true);
159         assertTrue("Cron should be relative", cronID.isIsRelative() == true);
160         assertTrue(Math.abs(testDate.getTime() - cronID.getAlarmTime()) < (1000 * 60));
161
162
163     }
164
165     /**
166      * Tests the &quot;Execute at this particular given cron-expression time&quot;
167      */

168     public void testConstructor3() {
169
170         try {
171             //Every hour cron test Runs at 4 minutes after the hour
172
CrontabEntry cronID = new CrontabEntry(4, -1,
173                     -1, -1,
174                     -1, -1,
175                     listener);
176             {
177                 Calendar JavaDoc now = Calendar.getInstance();
178                 if (now.get(Calendar.MINUTE) > 4) {
179                     now.add(Calendar.HOUR, 1);
180                 }
181                 Calendar JavaDoc cronDate = Calendar.getInstance();
182                 cronDate.setTime(new Date JavaDoc(cronID.getAlarmTime()));
183                 if (log.isInfoEnabled()) {
184                     System.out.println("Constructor 3: Created cron: " + cronID.toString());
185                 }
186                 assertTrue("Cron should execute at minute 4. Got: " +
187                         cronDate.get(Calendar.MINUTE) + " instead", cronDate.get(Calendar.MINUTE) == 4);
188                 assertTrue("Cron should execute either this hour or next hour",
189                         cronDate.get(Calendar.HOUR) == now.get(Calendar.HOUR));
190
191                 assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true);
192                 assertTrue("Cron should not be relative time", cronID.isIsRelative() == false);
193             }
194
195             //Every day at midnight cron test
196
Calendar JavaDoc curDate = Calendar.getInstance();
197             curDate.add(Calendar.DATE, 1);
198             cronID = new CrontabEntry(0, 0,
199                     -1, -1,
200                     -1, -1,
201                     listener);
202
203             assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true);
204             assertTrue("Cron should not be relative time", cronID.isIsRelative() == false);
205             Calendar JavaDoc cal = Calendar.getInstance();
206             cal.setTime(new Date JavaDoc(cronID.getAlarmTime()));
207             //
208
assertTrue("Cron job should execute at midnight", cal.get(Calendar.HOUR) == 0
209                     && cal.get(Calendar.MINUTE) == 0);
210
211
212             //Should execute 'tomorrow'. Unless we execute this test EXACTLY at
213
//midnight, then the job will execute the next calendar day. If by
214
//some freaky reason this executes at midnight, this test will fail.
215
assertTrue("Cron job should execute tomorrow",
216                     cal.get(Calendar.DATE) == curDate.get(Calendar.DATE));
217             if (log.isInfoEnabled()) {
218                 log.info("Constructor 3: Created cron: " + cronID.toString());
219             }
220
221             // every sunday at 20 minutes of every hour cron test
222
cronID = new CrontabEntry(20, -1,
223                     -1, -1,
224                     1, -1,
225                     listener);
226
227             if (log.isInfoEnabled()) {
228                 log.info("Constructor 3: Created cron: " + cronID.toString());
229             }
230
231             curDate = Calendar.getInstance();
232             assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true);
233             assertTrue("Cron should not be relative time", cronID.isIsRelative() == false);
234             cal = Calendar.getInstance();
235             cal.setTime(new Date JavaDoc(cronID.getAlarmTime()));
236             //
237
assertTrue("Cron job should execute every sunday at 20 minutes of every hour", cal.get(
238                     Calendar.DAY_OF_WEEK) == 1
239                     && cal.get(Calendar.MINUTE) == 20);
240             assertTrue("Cron job should execute at 0'th hour of next sunday ",
241                     curDate.get(Calendar.DAY_OF_WEEK) == 1 ? true : cal.get(Calendar.HOUR_OF_DAY) == 0);
242
243
244             // Jan 2, 2004 at midnight cron test
245
cronID = new CrontabEntry(0, 0,
246                     1, 0,
247                     -1, 2024,
248                     listener);
249
250             assertTrue("Cron should not be repetitive", cronID.isIsRepetitive() == false);
251             assertTrue("Cron should not be relative time", cronID.isIsRelative() == false);
252
253             cal = Calendar.getInstance();
254             cal.setTime(new Date JavaDoc(cronID.getAlarmTime()));
255             assertTrue("Cron should execute year 20204", cal.get(Calendar.YEAR) == 2024);
256             assertTrue("Cron should execute in January", cal.get(Calendar.MONTH) == Calendar.JANUARY);
257             assertTrue("Cron should execute the first of the month", cal.get(Calendar.DAY_OF_MONTH) == 1);
258             assertTrue("Cron should execute at midnight",
259                     cal.get(Calendar.HOUR) == 0 && cal.get(Calendar.MINUTE) == 0);
260             if (log.isInfoEnabled()) {
261                 log.info("Constructor 3: Created cron: " + cronID.toString());
262             }
263         } catch (CronException ex) {
264             ex.printStackTrace();
265             fail("crontab entry threw an exception");
266         }
267
268     }
269
270     /**
271      * Same as testConstructor3, but also checks that the job label is getting
272      * set correctly
273      */

274     public void testConstructor4() {
275         final String JavaDoc label = "Test Cron";
276         try {
277             //Every hour cron test
278
CrontabEntry cronID = new CrontabEntry(4, -1,
279                     -1, -1,
280                     -1, -1, label,
281                     listener);
282
283             //Check the label
284
assertTrue(label.equals(cronID.getLabel()));
285             if (log.isInfoEnabled()) {
286                 log.info("Constructor 4: Created cron: " + cronID.toString());
287             }
288         } catch (CronException ex) {
289             ex.printStackTrace();
290             fail("crontab entry threw an exception");
291         }
292
293     }
294
295
296     /**
297      * Executes the test case
298      */

299     public static void main(String JavaDoc[] argv) {
300         String JavaDoc[] testCaseList = {TestCrontabEntry.class.getName()};
301         junit.textui.TestRunner.main(testCaseList);
302     }
303 }
304
Popular Tags