1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2003 Digital Clash LLC. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * ChronicJ team (http://www.chronicj.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or
27 * promote products derived from this software without prior written
28 * permission. For written permission, please contact
29 * info@digitalclash.com.
30 *
31 * 5. Products derived from this software may not be called "ChronicJ",
32 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in
33 * their name, without prior written permission of Digital Clash LLC.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 * This product includes software developed by the by the
49 * Apache Software Foundation (http://www.apache.org/).
50 * ====================================================================
51 */
52 package org.chronicj.impl;
53
54 import org.chronicj.BaseTestCase;
55 import org.chronicj.DateRange;
56 import org.chronicj.Event;
57 import org.chronicj.MockEvent;
58 import org.chronicj.ScheduleElement;
59 import org.chronicj.TemporalExpression;
60 import org.chronicj.TimePoint;
61
62 import java.util.Calendar;
63
64
65 /***
66 * Unit tests for {@link org.chronicj.impl.DefaultScheduleElement
67 * DefaultScheduleElement}.
68 *
69 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a>
70 *
71 * @see <a href="http://junit.org">JUnit</a>
72 */
73 public class DefaultScheduleElementTest extends BaseTestCase {
74 private Calendar cal;
75
76 /*
77 * Class to test for boolean isOccuring(Event, TimePoint)
78 */
79 public void testConstructorWithNullParameter() {
80 try {
81 ScheduleElement anElement = new DefaultScheduleElement(null, null);
82 fail(
83 "new DefaultScheduleElement(null,null) did not throw an error.");
84 } catch (RuntimeException expected) {
85 try {
86 ScheduleElement anotherElement = new DefaultScheduleElement(new MockEvent(
87 getName()), null);
88 fail(
89 "new DefaultScheduleElement(MockEventInstance,null) did not throw an error.");
90 } catch (RuntimeException alsoExpected) {
91 try {
92 ScheduleElement yetAnotherElement = new DefaultScheduleElement(null,
93 new UnionTE());
94 fail(
95 "new DefaultScheduleElement(MockEventInstance,null) did not throw an error.");
96 } catch (RuntimeException expectedYetAgain) {
97 //YAY! we passed. YAY!
98 }
99 }
100 }
101 }
102
103 /*
104 * Class to test for boolean isOccuring(Event, DateRange)
105 */
106 public void testIsOccuringEventDateRange1() {
107 Event anEvent = new MockEvent("ChronicJ IPO");
108
109 /* ### First Thursday and last Wednesday of the month ### */
110 UnionTE firstThursLastWedOfMonth = new UnionTE();
111 firstThursLastWedOfMonth.add(new DayInMonthTE(5, 1)).add(new DayInMonthTE(
112 4, -1));
113
114 ScheduleElement aScheduleElement = new DefaultScheduleElement(anEvent,
115 firstThursLastWedOfMonth);
116
117 //Thursday, 4:30am to 2:30pm (December 4th, 2003)
118 DateRange rangeMatch = new DateRange(new TimePoint(2003, 12, 4, 4, 30),
119 new TimePoint(2003, 12, 4, 14, 30));
120 assertTrue(aScheduleElement.isOccuring(anEvent, rangeMatch));
121
122 //Overlap: Thursday, 2:30pm to Friday, 1:20am (December 4th to 5th, 2003)
123 DateRange rangeOverlapNotMatch = new DateRange(new TimePoint(2003, 12,
124 4, 14, 30), new TimePoint(2003, 12, 5, 1, 20));
125 assertTrue(!aScheduleElement.isOccuring(anEvent, rangeOverlapNotMatch));
126 }
127
128 public void testIsOccuringEventDateRange2() {
129 Event anEvent = new MockEvent("ChronicJ Moon Launch");
130
131 //6pm to 8pm
132 TemporalExpression sixToEightPM = new RangeEachDayTE(18, 0, 20, 0);
133
134 //Tuesday, 6pm to 8pm
135 IntersectionTE tuesday6to8pm = new IntersectionTE();
136 tuesday6to8pm.add(new DayInMonthTE(3)).add(sixToEightPM);
137
138 //Thursday, 6pm to 8pm
139 IntersectionTE thursday6to8pm = new IntersectionTE();
140 thursday6to8pm.add(new DayInMonthTE(5)).add(sixToEightPM);
141
142 //Every Tuesday and Thursday from 6 to 8pm
143 UnionTE tuesThurs6to8pm = new UnionTE();
144 tuesThurs6to8pm.add(tuesday6to8pm).add(thursday6to8pm);
145
146 ScheduleElement anElement = new DefaultScheduleElement(anEvent,
147 tuesThurs6to8pm);
148
149 //Thursday, 6:07pm (December 4th, 2003)
150 TimePoint thursdayAroundSix = new TimePoint(2003, 12, 4, 18, 07);
151
152 assertTrue(anElement.isOccuring(anEvent, thursdayAroundSix));
153
154 //Thursday, 8:00pm (December 4th, 2003)
155 TimePoint sameThursdayAtEight = new TimePoint(2003, 12, 4, 20, 00);
156
157 assertTrue(anElement.isOccuring(anEvent, sameThursdayAtEight));
158
159 //6:07 to 8pm Thursday, December 4th, 2003
160 DateRange range = new DateRange(thursdayAroundSix, sameThursdayAtEight);
161
162 assertTrue(anElement.isOccuring(anEvent, range));
163
164 //Try a different date:
165 //Tuesday, 7:15pm (October 28th, 2003)
166 TimePoint tuesdayAfterSeven = new TimePoint(2003, 10, 28, 19, 15);
167
168 assertTrue(anElement.isOccuring(anEvent, tuesdayAfterSeven));
169
170 //Tuesday, 5:15pm (October 28th, 2003)
171 TimePoint tuesdayAroundFive = new TimePoint(2003, 10, 28, 17, 15);
172
173 assertTrue(!anElement.isOccuring(anEvent, tuesdayAroundFive));
174 }
175
176 /*
177 * Class to test for boolean isOccuring(Event, TimePoint)
178 */
179 public void testIsOccuringEventTimePoint() {
180 Event anEvent = new MockEvent("ChronicJ Bankruptcy Hearing");
181
182 TemporalExpression january = new RangeEachYearTE(1);
183 TemporalExpression firstThursdayOfMonth = new DayInMonthTE(5, 1);
184 IntersectionTE firstThursdayOfJanuary2004 = new IntersectionTE();
185
186 firstThursdayOfJanuary2004.add(january);
187 firstThursdayOfJanuary2004.add(firstThursdayOfMonth);
188
189 clearToPrecision(cal, Calendar.HOUR);
190 cal.set(Calendar.YEAR, 2004);
191 cal.set(Calendar.MONTH, 0);
192 cal.set(Calendar.DAY_OF_MONTH, 1);
193
194 //Should be Thursday, January 1st, 2004
195 TimePoint shouldMatch = new TimePoint(cal.getTime());
196
197 ScheduleElement aScheduleElement = new DefaultScheduleElement(anEvent,
198 firstThursdayOfJanuary2004);
199
200 assertTrue(aScheduleElement.isOccuring(anEvent, shouldMatch));
201
202 cal.set(Calendar.MONTH, 2);
203 cal.set(Calendar.DAY_OF_MONTH, 4);
204
205 //Should be Thursday, March 4th, 2004 (also 1st Thursady of the month)
206 TimePoint shouldNotMatch = new TimePoint(cal.getTime());
207 assertTrue(!aScheduleElement.isOccuring(anEvent, shouldNotMatch));
208 }
209
210 /* (non-Javadoc)
211 * @see junit.framework.TestCase#setUp()
212 */
213 protected void setUp() throws Exception {
214 super.setUp();
215 cal = Calendar.getInstance();
216 }
217 }
This page was automatically generated by Maven