1 /* 2 * ==================================================================== The 3 * Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2003 Digital Clash LLC. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 1. 9 * Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 2. Redistributions in 11 * binary form must reproduce the above copyright notice, this list of 12 * conditions and the following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 3. The end-user documentation 14 * included with the redistribution, if any, must include the following 15 * acknowledgment: "This product includes software developed by the ChronicJ 16 * team (http://www.chronicj.org/)." Alternately, this acknowledgment may 17 * appear in the software itself, if and wherever such third-party 18 * acknowledgments normally appear. 4. The names "ChronicJ" and "Digital Clash" 19 * not be used to endorse or promote products derived from this software 20 * without prior written permission. For written permission, please contact 21 * info@digitalclash.com. 5. Products derived from this software may not be 22 * called "ChronicJ", "Digital Clash", nor may "ChronicJ" or "Digital Clash" 23 * appear in their name, without prior written permission of Digital Clash LLC. 24 * 25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 27 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 28 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * ==================================================================== This 36 * product includes software developed by the by the Apache Software Foundation 37 * (http://www.apache.org/). 38 * ==================================================================== 39 */ 40 package org.chronicj.impl; 41 42 import org.chronicj.BaseTestCase; 43 import org.chronicj.DatePrecision; 44 import org.chronicj.DateRange; 45 import org.chronicj.Event; 46 import org.chronicj.MockEvent; 47 import org.chronicj.Schedule; 48 import org.chronicj.TemporalExpression; 49 import org.chronicj.TimePoint; 50 51 import java.util.Calendar; 52 import java.util.Date; 53 import java.util.GregorianCalendar; 54 import java.util.Iterator; 55 import java.util.List; 56 57 58 /*** 59 * Unit tests for {@link DefaultSchedule DefaultSchedule}. 60 * 61 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a> 62 * 63 * @see <a href="http://junit.org">JUnit</a> 64 */ 65 public class DefaultScheduleTest extends BaseTestCase { 66 private Calendar cal; 67 private DateRange rangePmToAm; 68 private DateRange outsideRangePmToAm; 69 private TemporalExpression matchesRangePmToAmTE; 70 private Event anEvent; 71 72 /* 73 * Class to test for boolean isOccuring(Event, DateRange) 74 */ 75 public void testIsOccuringEventDateRange() { 76 Schedule aSchedule = new DefaultSchedule(); 77 aSchedule.add(anEvent, matchesRangePmToAmTE); 78 79 assertTrue(aSchedule.isOccuring(anEvent, rangePmToAm)); 80 81 cal.setTime(rangePmToAm.start().getTime()); 82 83 //15 minutes after start 84 cal.add(Calendar.MINUTE, 15); 85 86 Date subRangeStart = cal.getTime(); 87 cal.setTime(rangePmToAm.end().getTime()); 88 89 //45 minutes before end 90 cal.add(Calendar.MINUTE, -45); 91 92 Date subRangeEnd = cal.getTime(); 93 DateRange subRange = new DateRange(subRangeStart, subRangeEnd); 94 print(subRange); 95 assertTrue(!subRange.isEmpty()); 96 97 assertTrue(aSchedule.isOccuring(anEvent, subRange)); 98 assertTrue(!aSchedule.isOccuring(anEvent, outsideRangePmToAm)); 99 } 100 101 /* 102 * Class to test for boolean isOccuring(Event, TimePoint) 103 */ 104 public void testIsOccuringEventTimePoint() { 105 } 106 107 /* 108 * Class to test for void add(Event, TemporalExpression) 109 */ 110 public void testAdd() { 111 } 112 113 public void testDates() { 114 Schedule aSchedule = new DefaultSchedule(); 115 116 //Last Tuesday of the month 117 aSchedule.add(anEvent, new DayInMonthTE(3, -1)); 118 119 //October 2003 to February 2004 120 DateRange aRange = new DateRange(new TimePoint( 121 new GregorianCalendar(2003, 9, 28), DatePrecision.DATE), 122 new TimePoint(new GregorianCalendar(2004, 1, 14), 123 DatePrecision.DATE)); 124 125 List dates = aSchedule.dates(anEvent, aRange); 126 assertNotNull(dates); 127 128 assertTrue(dates.size() == 4); 129 130 for (Iterator iter = dates.iterator(); iter.hasNext();) { 131 print((TimePoint) iter.next()); 132 } 133 } 134 135 public void testNextOccurence() { 136 } 137 138 /* 139 * (non-Javadoc) 140 * 141 * @see junit.framework.TestCase#setUp() 142 */ 143 protected void setUp() throws Exception { 144 super.setUp(); 145 cal = Calendar.getInstance(); 146 rangePmToAm = getDateRangeSpanningMidnight(); 147 print(rangePmToAm); 148 matchesRangePmToAmTE = getMatchingExpression(rangePmToAm); 149 assertTrue(matchesRangePmToAmTE.includes(rangePmToAm.start())); 150 anEvent = new MockEvent("Music - Rehearsal"); 151 outsideRangePmToAm = getNotMatchingRange(rangePmToAm); 152 assertTrue(!rangePmToAm.includes(outsideRangePmToAm)); 153 assertTrue(!rangePmToAm.overlaps(outsideRangePmToAm)); 154 } 155 156 private TemporalExpression getMatchingExpression(DateRange aRange) { 157 TemporalExpression month = new RangeEachYearTE(aRange.start().getMonth()); 158 TemporalExpression timeRange = new RangeEachDayTE(aRange.start() 159 .getHourOfDay(), 160 aRange.start().getMinute() - 5, aRange.end().getHourOfDay(), 161 aRange.end().getMinute() + 5); 162 IntersectionTE result = new IntersectionTE(); 163 164 return result.add(month).add(timeRange); 165 } 166 167 private DateRange getNotMatchingRange(DateRange aRange) { 168 return new DateRange(aRange.end().increment(10), 169 aRange.end().increment(20)); 170 } 171 172 private DateRange getDateRangeSpanningMidnight() { 173 assertNotNull(cal); 174 clearToPrecision(cal, Calendar.MINUTE); 175 cal.set(Calendar.YEAR, 2004); 176 cal.set(Calendar.MONTH, 0); 177 cal.set(Calendar.DATE, 4); 178 cal.set(Calendar.HOUR_OF_DAY, 22); 179 cal.set(Calendar.MINUTE, 37); 180 181 TimePoint start = new TimePoint(cal.getTime()); 182 cal.add(Calendar.HOUR_OF_DAY, 2); 183 184 TimePoint end = new TimePoint(cal.getTime()); 185 186 //Sun, 4 Jan 2004 10:37 PM to Mon, 5 Jan 2004 12:37 AM 187 return new DateRange(start, end); 188 } 189 }

This page was automatically generated by Maven