Skip to content

Commit

Permalink
Added fluent api for components and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Mar 11, 2022
1 parent 3157f15 commit a1494f3
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 191 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/property/ExDate.java
Expand Up @@ -60,6 +60,13 @@ public ExDate() {
super(EXDATE);
}

/**
* @param aValue a value string for this component
*/
public ExDate(final String aValue) {
this(new ParameterList(), aValue);
}

/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
Expand Down
Expand Up @@ -2,11 +2,9 @@ package net.fortuna.ical4j.transform.recurrence

import net.fortuna.ical4j.model.*
import net.fortuna.ical4j.model.component.VEvent
import net.fortuna.ical4j.model.parameter.Value
import net.fortuna.ical4j.model.property.ExRule
import net.fortuna.ical4j.model.property.RRule
import net.fortuna.ical4j.util.RandomUidGenerator
import net.fortuna.ical4j.util.UidGenerator
import spock.lang.Specification

import java.time.DayOfWeek
Expand Down Expand Up @@ -44,12 +42,7 @@ class ByDayRuleTest extends Specification {

def 'test limit with FREQ=MINUTELY'() {
given: 'a calendar definition'
Calendar calendar = new Calendar().withDefaults().getFluentTarget();

TemporalAdapter<LocalDateTime> dateTime = TemporalAdapter.parse("20210104T130000");
VEvent e1 = new VEvent(dateTime.temporal, "even");
UidGenerator ug = new RandomUidGenerator();
e1.add(ug.generateUid());

// recurency
Recur recur = new Recur.Builder().frequency(Frequency.MINUTELY).interval(15).hourList(numberList(13, 17))
Expand All @@ -64,10 +57,14 @@ class ByDayRuleTest extends Specification {
Recur recurEx2 = new Recur.Builder().frequency(Frequency.MINUTELY).interval(15)
.hourList(numberList(13, 14)).minuteList(numberList(0, 30)).build();

e1.add(new ExRule(recurEx));
e1.add(new ExRule(recurEx2));
Calendar calendar = new Calendar().withDefaults()
.withProperty(new ExRule(recurEx))
.withProperty(new ExRule(recurEx2))
.withComponent(new VEvent(dateTime.temporal, "even")
.withProperty(new RandomUidGenerator().generateUid())
.getFluentTarget())
.getFluentTarget();

calendar.add(e1);
System.out.println(calendar);

expect: 'dates are calculated successfully'
Expand Down
13 changes: 3 additions & 10 deletions src/test/java/net/fortuna/ical4j/filter/FilterTest.java
Expand Up @@ -123,18 +123,11 @@ public static TestSuite suite() throws FileNotFoundException, IOException, Parse
Attendee a1 = new Attendee(new URI("Mailto:A@example.com"));
Attendee a2 = new Attendee(new URI("Mailto:C@example.com"));

VEvent e1 = new VEvent();
e1.add(organizer);
e1.add(a1);
VEvent e1 = new VEvent().withProperty(organizer).withProperty(a1).getFluentTarget();

VEvent e2 = new VEvent();
e2.add(organizer);
e2.add(a2);
VEvent e2 = new VEvent().withProperty(organizer).withProperty(a2).getFluentTarget();

VEvent e3 = new VEvent();
e3.add(organizer);
e3.add(a1);
e3.add(a2);
VEvent e3 = new VEvent().withProperty(organizer).withProperty(a1).withProperty(a2).getFluentTarget();

Calendar calendar = new Calendar(new ComponentList<>(Arrays.asList(e1, e2, e3)));

Expand Down
13 changes: 5 additions & 8 deletions src/test/java/net/fortuna/ical4j/model/CalendarTest.java
Expand Up @@ -64,10 +64,9 @@ public class CalendarTest {
@Before
public void setUp() {
calendar = new Calendar().withDefaults()
.withProdId("-//Ben Fortuna//iCal4j 1.0//EN").getFluentTarget();
VEvent vEvent = new VEvent();
vEvent.add(new Uid("1"));
calendar.add(vEvent);
.withProdId("-//Ben Fortuna//iCal4j 1.0//EN")
.withComponent(new VEvent().withProperty(new Uid("1")).getFluentTarget())
.getFluentTarget();
}

@Test
Expand All @@ -89,8 +88,7 @@ public void testValid2() throws URISyntaxException, ConstraintViolationException
ZonedDateTime end = start.plusYears(1);

VEvent week1UserA = new VEvent(start, java.time.Duration.ofHours(8), "Week 1 - User A");
week1UserA.getRequiredProperty(Property.DTSTART).add(tzParam);
week1UserA.getRequiredProperty(Property.DTSTART).add(Value.DATE);
week1UserA.getRequiredProperty(Property.DTSTART).add(tzParam).add(Value.DATE);

WeekDayList monToFri = new WeekDayList(MO, TU, WE, TH, FR);
Recur<ZonedDateTime> week1UserARecur = new Recur.Builder<ZonedDateTime>().frequency(Frequency.WEEKLY)
Expand All @@ -101,8 +99,7 @@ public void testValid2() throws URISyntaxException, ConstraintViolationException
end = end.plusWeeks(1);

VEvent week2UserB = new VEvent(start, java.time.Duration.ofHours(8), "Week 2 - User B");
week2UserB.getRequiredProperty(Property.DTSTART).add(tzParam);
week2UserB.getRequiredProperty(Property.DTSTART).add(Value.DATE);
week2UserB.getRequiredProperty(Property.DTSTART).add(tzParam).add(Value.DATE);

Recur<ZonedDateTime> week2UserBRecur = new Recur.Builder<ZonedDateTime>().frequency(Frequency.WEEKLY)
.until(end).interval(3).dayList(monToFri).hourList(new NumberList("9")).build();
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/net/fortuna/ical4j/model/PropertyTest.java
Expand Up @@ -42,6 +42,8 @@
import java.net.URISyntaxException;
import java.text.ParseException;

import static org.junit.Assert.assertNotEquals;

/**
* $Id$
*
Expand Down Expand Up @@ -147,8 +149,8 @@ public void testCopy() throws IOException, URISyntaxException {
assertEquals(property, copy);

copy.add(Value.BOOLEAN);
assertFalse(property.equals(copy));
assertFalse(copy.equals(property));
assertNotEquals(property, copy);
assertNotEquals(copy, property);
}

/**
Expand Down
Expand Up @@ -76,11 +76,9 @@ public static TestSuite suite() {
ParameterList tzParams = new ParameterList(Collections.singletonList(new TzId(ZoneId.systemDefault().getId())));
UidGenerator g = new RandomUidGenerator();

available = new Available();
available.add(g.generateUid())
.add(new DtStart<>(tzParams, ZonedDateTime.now()))
.add(new DtStamp())
.add(new Duration(java.time.Period.ofWeeks(1)));
available = new Available().withProperty(g.generateUid())
.withProperty(new DtStart<>(tzParams, ZonedDateTime.now()))
.withProperty(new DtStamp()).withProperty(new Duration(java.time.Period.ofWeeks(1))).getFluentTarget();

suite.addTest(new AvailableTest("testValidation", available));
return suite;
Expand Down
Expand Up @@ -58,11 +58,11 @@ public class ObservanceTest extends TestCase {
*/
@Override
protected void setUp() throws Exception {
observance = new Standard();
observance.add(new DtStart("16010101T030000"))
.add(new TzOffsetFrom("+0200"))
.add(new TzOffsetTo("+0100"))
.add(new RRule("FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU"));
observance = new Standard().withProperty(new DtStart("16010101T030000"))
.withProperty(new TzOffsetFrom("+0200"))
.withProperty(new TzOffsetTo("+0100"))
.withProperty(new RRule("FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU"))
.getFluentTarget();
}

/**
Expand Down
Expand Up @@ -69,10 +69,9 @@ public static TestSuite suite() throws SocketException {
suite.addTest(new ParticipantTest("testValidationException", p));

final UidGenerator g = new RandomUidGenerator();
p = new Participant();
p.add(g.generateUid());
p.add(ParticipantType.VOTER);
p.add(new DtStamp());
p = new Participant().withProperty(g.generateUid())
.withProperty(ParticipantType.VOTER)
.withProperty(new DtStamp()).getFluentTarget();
suite.addTest(new ParticipantTest("testValidation", p));
return suite;
}
Expand Down
Expand Up @@ -33,7 +33,6 @@

import junit.framework.TestSuite;
import net.fortuna.ical4j.model.ComponentTest;
import net.fortuna.ical4j.model.ParameterList;
import net.fortuna.ical4j.model.parameter.TzId;
import net.fortuna.ical4j.model.property.DtStart;
import net.fortuna.ical4j.util.RandomUidGenerator;
Expand All @@ -42,7 +41,6 @@
import java.net.SocketException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections;

/**
* $Id$
Expand Down Expand Up @@ -72,10 +70,12 @@ public static TestSuite suite() {
suite.addTest(new VAvailabilityTest("testValidationException", a));

UidGenerator g = new RandomUidGenerator();
a = new VAvailability();
a.add(g.generateUid());
ParameterList tzParams = new ParameterList(Collections.singletonList(new TzId(ZoneId.systemDefault().getId())));
a.add(new DtStart<>(tzParams, ZonedDateTime.now()));
a = new VAvailability().withProperty(g.generateUid())
.withProperty(new DtStart<>(ZonedDateTime.now())
.withParameter(new TzId(ZoneId.systemDefault().getId()))
.getFluentTarget())
.getFluentTarget();

suite.addTest(new VAvailabilityTest("testValidation", a));

return suite;
Expand Down
70 changes: 24 additions & 46 deletions src/test/java/net/fortuna/ical4j/model/component/VEventTest.java
Expand Up @@ -70,7 +70,7 @@
*
* @author Ben Fortuna
*/
public class VEventTest<T extends Temporal> extends CalendarComponentTest {
public class VEventTest<T extends Temporal> extends CalendarComponentTest<T> {

private static Logger log = LoggerFactory.getLogger(VEventTest.class);

Expand Down Expand Up @@ -176,8 +176,7 @@ public final void testChristmas() {

Summary summary = new Summary("Christmas Day; \n this is a, test\\");

VEvent christmas = new VEvent();
christmas.add(start).add(summary);
VEvent christmas = new VEvent().withProperty(start).withProperty(summary).getFluentTarget();

log.info(christmas.toString());
}
Expand Down Expand Up @@ -404,8 +403,7 @@ public void testGetConsumedTimeByCount() {
// log.info(recur.getDates(start, end, Value.DATE_TIME));

RRule rrule = new RRule(recur);
VEvent event = new VEvent(start, end, "Test recurrence COUNT");
event.add(rrule);
VEvent event = new VEvent(start, end, "Test recurrence COUNT").withProperty(rrule).getFluentTarget();
log.info(event.toString());

ZonedDateTime rangeStart = ZonedDateTime.now();
Expand Down Expand Up @@ -448,15 +446,11 @@ public final void testEventEndDateWithTimeZone() throws ParseException {
public void testGetConsumedTimeWithExDate() throws ParseException {

VEvent event1 = new VEvent(TemporalAdapter.parse("20050103T080000").getTemporal(),
java.time.Duration.ofMinutes(15), "Event 1");

Recur<LocalDateTime> rRuleRecur = new Recur<>("FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR");
RRule<LocalDateTime> rRule = new RRule<>(rRuleRecur);
event1.add(rRule);

ParameterList parameterList = new ParameterList(Collections.singletonList(Value.DATE));
ExDate<LocalDateTime> exDate = new ExDate<>(parameterList, "20050106T000000");
event1.add(exDate);
java.time.Duration.ofMinutes(15), "Event 1")
.withProperty(new RRule<>(new Recur<>("FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR")))
.withProperty(new ExDate<LocalDateTime>("20050106T000000")
.withParameter(Value.DATE).getFluentTarget())
.getFluentTarget();

TemporalAdapter<LocalDateTime> start = TemporalAdapter.parse("20050106T000000");
TemporalAdapter<LocalDateTime> end = TemporalAdapter.parse("20050107T000000");
Expand Down Expand Up @@ -554,44 +548,29 @@ public static TestSuite suite() throws ValidationException, IOException, ParserE
.interval(1).weekStartDay(MO).build();
RRule<ZonedDateTime> rruleMonthly = new RRule<>(recurMonthly);

Summary summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED WEEKLY");

ParameterList tzParams = new ParameterList(Collections.singletonList(new TzId("Australia/Melbourne")));

VEvent weekdayNineToFiveEvents = new VEvent();
weekdayNineToFiveEvents.add(rruleWeekly).add(summary);
DtStart<ZonedDateTime> dtStart = new DtStart<>(tzParams, weekday9AM);
// dtStart.getParameters().add(Value.DATE);
weekdayNineToFiveEvents.add(dtStart);
DtEnd<ZonedDateTime> dtEnd = new DtEnd<>(tzParams, weekday5PM);
// dtEnd.getParameters().add(Value.DATE);
weekdayNineToFiveEvents.add(dtEnd).add(uidGenerator.generateUid());
VEvent weekdayNineToFiveEvents = new VEvent().withProperty(rruleWeekly)
.withProperty(new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED WEEKLY"))
.withProperty(new DtStart<>(tzParams, weekday9AM))
.withProperty(new DtEnd<>(tzParams, weekday5PM))
.withProperty(uidGenerator.generateUid()).getFluentTarget();
// ensure event is valid..
weekdayNineToFiveEvents.validate();

summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED DAILY");

VEvent dailyWeekdayEvents = new VEvent();
dailyWeekdayEvents.add(rruleDaily).add(summary);
DtStart<ZonedDateTime> dtStart2 = new DtStart<>(tzParams, weekday9AM);
// dtStart2.getParameters().add(Value.DATE);
dailyWeekdayEvents.add(dtStart2);
DtEnd<ZonedDateTime> dtEnd2 = new DtEnd<>(tzParams, weekday5PM);
// dtEnd2.getParameters().add(Value.DATE);
dailyWeekdayEvents.add(dtEnd2).add(uidGenerator.generateUid());
VEvent dailyWeekdayEvents = new VEvent().withProperty(rruleDaily)
.withProperty(new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED DAILY"))
.withProperty(new DtStart<>(tzParams, weekday9AM))
.withProperty(new DtEnd<>(tzParams, weekday5PM))
.withProperty(uidGenerator.generateUid()).getFluentTarget();
// ensure event is valid..
dailyWeekdayEvents.validate();

summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED MONTHLY");

VEvent monthlyWeekdayEvents = new VEvent();
monthlyWeekdayEvents.add(rruleMonthly).add(summary);
DtStart<ZonedDateTime> dtStart3 = new DtStart<>(tzParams, weekday9AM);
// dtStart3.getParameters().add(Value.DATE);
monthlyWeekdayEvents.add(dtStart3);
DtEnd<ZonedDateTime> dtEnd3 = new DtEnd<>(tzParams, weekday5PM);
// dtEnd3.getParameters().add(Value.DATE);
monthlyWeekdayEvents.add(dtEnd3).add(uidGenerator.generateUid());
VEvent monthlyWeekdayEvents = new VEvent().withProperty(rruleMonthly)
.withProperty(new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED MONTHLY"))
.withProperty(new DtStart<>(tzParams, weekday9AM))
.withProperty(new DtEnd<>(tzParams, weekday5PM))
.withProperty(uidGenerator.generateUid()).getFluentTarget();
// ensure event is valid..
monthlyWeekdayEvents.validate();

Expand Down Expand Up @@ -626,8 +605,7 @@ public static TestSuite suite() throws ValidationException, IOException, ParserE

ParameterList endParams = new ParameterList(Collections.singletonList(Value.DATE));
DtEnd<LocalDate> end = new DtEnd<>(endParams, LocalDate.now());
VEvent event = new VEvent();
event.add(uid).add(start).add(end);
VEvent event = new VEvent().withProperty(uid).withProperty(start).withProperty(end).getFluentTarget();
suite.addTest(new VEventTest<>("testValidation", event));

event = event.copy();
Expand Down

0 comments on commit a1494f3

Please sign in to comment.