Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2012 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3781726 to your computer and use it in GitHub Desktop.
Save anonymous/3781726 to your computer and use it in GitHub Desktop.
Test for ical4j Recuring Events
import static org.junit.Assert.*;
import java.text.ParseException;
import java.util.GregorianCalendar;
import net.fortuna.ical4j.filter.Filter;
import net.fortuna.ical4j.filter.PeriodRule;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Dur;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.Recur;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.RRule;
import net.fortuna.ical4j.util.TimeZones;
import org.junit.Before;
import org.junit.Test;
public class TestRecuringEvent {
java.util.Calendar cal;
java.util.TimeZone tz;
@Before
public void setUp() {
tz = TimeZones.getDateTimeZone();
cal = new GregorianCalendar(tz);
}
private VEvent createEvent() {
cal.set(GregorianCalendar.MONTH, GregorianCalendar.SEPTEMBER);
cal.set(GregorianCalendar.DAY_OF_MONTH, 10);
cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
cal.clear(GregorianCalendar.MINUTE);
cal.clear(GregorianCalendar.SECOND);
cal.clear(GregorianCalendar.MILLISECOND);
GregorianCalendar start = (GregorianCalendar) cal.clone();
start.set(2012, GregorianCalendar.SEPTEMBER, 12);
GregorianCalendar end = (GregorianCalendar) cal.clone();
end.set(2012, GregorianCalendar.SEPTEMBER, 13);
return new VEvent(new Date(start.getTime()), new Date(end.getTime()),
"Generated Event");
}
@Test
public void testSingleDayEvent() throws ParseException {
VEvent event = createEvent();
ComponentList components = new ComponentList();
components.add(event);
assertMatchTimesInCurrentMonth(1, components);
}
@Test
public void testRecuringEventOneTimeCount() throws ParseException {
VEvent event = createEvent();
Recur recur = new Recur("FREQ=DAILY;COUNT=1;INTERVAL=1");
event.getProperties().add(new RRule(recur));
ComponentList components = new ComponentList();
components.add(event);
assertMatchTimesInCurrentMonth(1, components);
}
@Test
public void testRecuringEvent() throws ParseException {
VEvent event = createEvent();
Recur recur = new Recur("FREQ=DAILY;COUNT=3;INTERVAL=1");
event.getProperties().add(new RRule(recur));
ComponentList components = new ComponentList();
components.add(event);
assertMatchTimesInCurrentMonth(3, components);
}
private void assertMatchTimesInCurrentMonth(int timesToMatch,
ComponentList components) {
Dur dur = new Dur(1, 0, 0, 0);
int matched = 0;
while (cal.get(java.util.Calendar.DAY_OF_MONTH) < 20) {
PeriodRule rule = new PeriodRule(new Period(new DateTime(
cal.getTime()), dur));
Filter filter = new Filter(rule);
matched += filter.filter(components).size();
cal.add(java.util.Calendar.DAY_OF_MONTH, 1);
}
assertEquals("Event was found on days:", timesToMatch, matched);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment