Skip to content

Commit

Permalink
Fix potential NPEs in DateHelper
Browse files Browse the repository at this point in the history
(cherry picked from commit f3d9c4a)
  • Loading branch information
Jochen Schalanda committed Apr 2, 2015
1 parent 74cb9ae commit ee3ce86
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 13 deletions.
33 changes: 21 additions & 12 deletions app/views/helpers/DateHelper.java
Expand Up @@ -25,21 +25,26 @@
import org.joda.time.format.PeriodFormat;
import org.joda.time.format.PeriodFormatter;
import play.twirl.api.Html;
import play.twirl.api.HtmlFormat;

/**
* @author Lennart Koopmann <lennart@torch.sh>
*/
public class DateHelper {

public static Html current() {
return timestamp(DateTime.now());
}

public static Html timestamp(DateTime instant) {
if (instant == null) {
return HtmlFormat.empty();
}

return views.html.partials.dates.instant.render(DateTools.inUserTimeZone(instant), DateTools.DEFAULT_DATE_FORMAT);
}

public static Html timestampShort(DateTime instant) {
if (instant == null) {
return HtmlFormat.empty();
}

return views.html.partials.dates.instant.render(DateTools.inUserTimeZone(instant), DateTools.SHORT_DATE_FORMAT);
}

Expand All @@ -48,26 +53,30 @@ public static Html timestampShortTZ(DateTime instant) {
}

public static Html timestampShortTZ(DateTime instant, boolean inUserTZ) {
DateTime date = instant;

if (inUserTZ) {
date = DateTools.inUserTimeZone(instant);
if (instant == null) {
return HtmlFormat.empty();
}
return views.html.partials.dates.instant.render(date, DateTools.SHORT_DATE_FORMAT_TZ);

return views.html.partials.dates.instant.render(inUserTZ ? DateTools.inUserTimeZone(instant) : instant, DateTools.SHORT_DATE_FORMAT_TZ);
}

public static Html readablePeriodFromNow(DateTime instant) {
return readablePeriodFromNow(instant, "");
}

public static Html readablePeriodFromNow(DateTime instant, String classes) {
if (instant == null) {
return HtmlFormat.empty();
}

return views.html.partials.dates.readable_period.render(DateTools.inUserTimeZone(instant), classes);
}

public static Html readableDuration(Duration duration) {
PeriodFormatter formatter = PeriodFormat.getDefault();
if (duration == null) {
return HtmlFormat.empty();
}

return views.html.partials.dates.duration.render(duration, formatter);

return views.html.partials.dates.duration.render(duration, PeriodFormat.getDefault());
}
}
4 changes: 3 additions & 1 deletion project/Build.scala
Expand Up @@ -31,7 +31,9 @@ object ApplicationBuild extends Build {
"com.sun.jersey" % "jersey-grizzly2" % "1.18.1",
"com.sun.jersey" % "jersey-bundle" % "1.18.1",

"org.mockito" % "mockito-all" % "1.9.5" % "test"
"junit" % "junit" % "4.12" % "test",
"org.mockito" % "mockito-all" % "1.9.5" % "test",
"org.assertj" % "assertj-core" % "2.0.0" % "test"
)
val repositories = Seq(
"Local Maven Repository" at "file:///" + Path.userHome.absolutePath + "/.m2/repository",
Expand Down
91 changes: 91 additions & 0 deletions test/views/helpers/DateHelperTest.java
@@ -0,0 +1,91 @@
package views.helpers;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;

public class DateHelperTest {
private static final Locale DEFAULT_LOCALE = Locale.getDefault();

@BeforeClass
public static void setUp() {
Locale.setDefault(Locale.ENGLISH);
DateTimeUtils.setCurrentMillisFixed(0L);
}

@AfterClass
public static void tearDown() {
Locale.setDefault(DEFAULT_LOCALE);
DateTimeUtils.setCurrentMillisSystem();
}

@Test
public void testCurrent() throws Exception {
assertThat(DateHelper.current().body())
.contains("1970-01-01T00:00:00.000Z")
.contains("Thu Jan 01 1970 00:00:00.000 +00:00")
.endsWith("</time>");
}

@Test
public void testTimestamp() throws Exception {
assertThat(DateHelper.timestamp(null).body())
.isEmpty();
assertThat(DateHelper.timestamp(DateTime.now(DateTimeZone.UTC)).body())
.contains("1970-01-01T00:00:00.000Z")
.endsWith("</time>");
}

@Test
public void testTimestampShort() throws Exception {
assertThat(DateHelper.timestampShort(null).body())
.isEmpty();
assertThat(DateHelper.timestampShort(DateTime.now(DateTimeZone.UTC)).body())
.contains("1970-01-01T00:00:00.000Z")
.endsWith("</time>");
}

@Test
public void testTimestampShortTZ() throws Exception {
assertThat(DateHelper.timestampShortTZ(null).body())
.isEmpty();
assertThat(DateHelper.timestampShortTZ(null, true).body())
.isEmpty();
assertThat(DateHelper.timestampShortTZ(null, false).body())
.isEmpty();
assertThat(DateHelper.timestampShortTZ(DateTime.now(DateTimeZone.UTC), true).body())
.contains("1970-01-01T00:00:00.000Z")
.endsWith("</time>");
assertThat(DateHelper.timestampShortTZ(DateTime.now(DateTimeZone.UTC), false).body())
.contains("1970-01-01T00:00:00.000Z")
.endsWith("</time>");
}

@Test
public void testReadablePeriodFromNow() throws Exception {
assertThat(DateHelper.readablePeriodFromNow(null).body())
.isEmpty();
assertThat(DateHelper.readablePeriodFromNow(DateTime.now(DateTimeZone.UTC)).body())
.contains("1970-01-01T00:00:00.000Z")
.endsWith("</time>");
}

@Test
public void testReadableDuration() throws Exception {
assertThat(DateHelper.readableDuration(null).body())
.isEmpty();
assertThat(DateHelper.readableDuration(Duration.standardHours(1L)).body())
.contains("1 hour")
.endsWith("</time>");
}
}

0 comments on commit ee3ce86

Please sign in to comment.