Skip to content

Commit

Permalink
Fixed NPE in DateProperty.
Browse files Browse the repository at this point in the history
When no date is set, calling copy() and hashCode() resulted in NPE.
  • Loading branch information
schnatterer committed Feb 5, 2017
1 parent bb2f5b6 commit 540d6d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Expand Up @@ -133,7 +133,7 @@ public void setValue(final String value) throws ParseException {
// ensure timezone is null for VALUE=DATE properties..
updateTimeZone(null);
this.date = new Date(value);
} else {
} else if (value != null && !value.isEmpty()){
this.date = new DateTime(value, timeZone);
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public final TimeZone getTimeZone() {
*/
@Override
public int hashCode() {
return getDate().hashCode();
return getDate() != null ? getDate().hashCode() : 0;
}

/**
Expand Down
Expand Up @@ -31,17 +31,18 @@
*/
package net.fortuna.ical4j.model.property;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;

import junit.framework.TestSuite;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.DefaultTimeZoneRegistryFactory;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyTest;
import net.fortuna.ical4j.model.TimeZoneRegistry;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;

/**
* $Id$
*
Expand Down Expand Up @@ -86,6 +87,16 @@ public void testCopy() throws IOException, URISyntaxException,
}
}

public void testHashValue() throws Exception {
Date date = property.getDate();
if (date != null) {
assertEquals(date.hashCode(), property.hashCode());
} else {
assertEquals(0, property.hashCode());
}
}


/**
* @return
*/
Expand All @@ -98,11 +109,17 @@ public static TestSuite suite() {
// dtStamp.getParameters().add(new TzId("Australia/Melbourne"));
// dtStamp.setTimeZone(tzReg.getTimeZone("Australia/Melbourne"));
suite.addTest(new DatePropertyTest("testCopy", dtStamp));
suite.addTest(new DatePropertyTest("testHashValue", dtStamp));

DtStart dtStart = new DtStart(new DateTime());
// dtStart.getParameters().add(new TzId("Australia/Melbourne"));
dtStart.setTimeZone(tzReg.getTimeZone("Australia/Melbourne"));
suite.addTest(new DatePropertyTest("testCopy", dtStart));
suite.addTest(new DatePropertyTest("testHashValue", dtStart));

DtStart dtStartEmpty = new DtStart();
suite.addTest(new DatePropertyTest("testCopy", dtStartEmpty));
suite.addTest(new DatePropertyTest("testHashValue", dtStartEmpty));
return suite;
}
}

0 comments on commit 540d6d4

Please sign in to comment.