Skip to content

Commit

Permalink
Extracted transformations to external class to support lightweight mo…
Browse files Browse the repository at this point in the history
…del classes
  • Loading branch information
benfortuna committed Dec 18, 2018
1 parent 8b6b3f5 commit 17c6fde
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 76 deletions.
71 changes: 0 additions & 71 deletions src/main/java/net/fortuna/ical4j/model/Calendar.java
Expand Up @@ -36,7 +36,6 @@
import net.fortuna.ical4j.model.property.Method;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.transform.rfc5545.RuleManager;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.AbstractCalendarValidatorFactory;
import net.fortuna.ical4j.validate.ValidationException;
Expand All @@ -46,10 +45,8 @@

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;

/**
* $Id$ [Apr 5, 2004]
Expand Down Expand Up @@ -351,72 +348,4 @@ public final int hashCode() {
return new HashCodeBuilder().append(getProperties()).append(
getComponents()).toHashCode();
}

@SuppressWarnings("unchecked")
public void conformToRfc5545() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{

conformPropertiesToRfc5545(properties);

for(Component component : components){
CountableProperties.removeExceededPropertiesForComponent(component);

//each component
conformComponentToRfc5545(component);

//each component property
conformPropertiesToRfc5545(component.getProperties());

for(java.lang.reflect.Method m : component.getClass().getDeclaredMethods()){
if(ComponentList.class.isAssignableFrom(m.getReturnType()) &&
m.getName().startsWith("get")){
List<Component> components = (List<Component>) m.invoke(component);
for(Component c : components){
//each inner component
conformComponentToRfc5545(c);

//each inner component properties
conformPropertiesToRfc5545(c.getProperties());
}
}
}
}
}

private static void conformPropertiesToRfc5545(List<Property> properties) {
for (Property property : properties) {
RuleManager.applyTo(property);
}
}

private static void conformComponentToRfc5545(Component component){
RuleManager.applyTo(component);
}

private enum CountableProperties{
STATUS(Property.STATUS, 1);
private int maxApparitionNumber;
private String name;

CountableProperties(String name, int maxApparitionNumber){
this.maxApparitionNumber = maxApparitionNumber;
this.name = name;
}

protected void limitApparitionsNumberIn(Component component){
PropertyList<? extends Property> propertyList = component.getProperties(name);

if(propertyList.size() <= maxApparitionNumber){
return;
}
int toRemove = propertyList.size() - maxApparitionNumber;
for(int i = 0; i < toRemove; i++){
component.getProperties().remove(propertyList.get(i)); }
}

private static void removeExceededPropertiesForComponent(Component component){
for(CountableProperties cp: values()){
cp.limitApparitionsNumberIn(component);
}
}
}
}
Expand Up @@ -4,6 +4,7 @@
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.validate.ValidationException;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -15,14 +16,21 @@

public class Rfc5545TransformerTest {

private Rfc5545Transformer transformer;

@Before
public void setUp() {
transformer = new Rfc5545Transformer();
}

@Test
public void shouldCorrectCalendarBody() throws IOException, ParserException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {

String[] calendarNames = { "yahoo1.txt", "yahoo2.txt", "outlook1.txt", "outlook2.txt", "apple.txt" };
for (String calendarName : calendarNames) {
Calendar calendar = buildCalendar(calendarName);
calendar.conformToRfc5545();
calendar = transformer.transform(calendar);
try {
calendar.validate();
} catch (ValidationException e) {
Expand All @@ -40,7 +48,7 @@ public void shouldCorrectMsSpecificTimeZones() throws IOException, ParserExcepti

for (int i = 0; i < actuals.length; i++) {
Calendar actual = buildCalendar(actuals[i]);
actual.conformToRfc5545();
actual = transformer.transform(actual);
Calendar expected = buildCalendar(expecteds[i]);
assertEquals("on from " + expecteds[i] + " and " + actuals[i] + " failed.", expected, actual);
}
Expand All @@ -51,8 +59,8 @@ public void shouldCorrectDTStampByAddingUTCTimezone() {
String calendarName = "dtstamp/invalid.txt";
try {
Calendar actual = buildCalendar(calendarName);
actual.conformToRfc5545();
} catch (IllegalAccessException | InvocationTargetException | IOException | ParserException e) {
actual = transformer.transform(actual);
} catch (RuntimeException | IOException | ParserException e) {
e.printStackTrace();
fail("RFC transformation failed for " + calendarName);
}
Expand All @@ -63,7 +71,7 @@ public void shouldSetTimezoneToUtcForNoTZdescription() {
String actualCalendar = "outlook/TZ-no-description.txt";
try {
Calendar actual = buildCalendar(actualCalendar);
actual.conformToRfc5545();
actual = transformer.transform(actual);
Calendar expected = buildCalendar("outlook/TZ-set-to-utc.txt");
assertEquals(expected.toString(), actual.toString());
assertEquals(expected, actual);
Expand Down

0 comments on commit 17c6fde

Please sign in to comment.