Skip to content

Commit

Permalink
Add @IgnoreUntil Support for "yyyy-MM-dd'T'HH:mm:ss" datetime format u…
Browse files Browse the repository at this point in the history
  • Loading branch information
simon.dudley committed Feb 21, 2018
1 parent ee34f7d commit 727a1a9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/main/java/co/unruly/junit/IgnoreUntilRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.util.regex.Pattern;

import static org.joda.time.format.DateTimeFormat.forPattern;

public class IgnoreUntilRule implements TestRule {

static final String DEFAULT_IGNORE = "2000-01-01";
private static final Pattern DATE_REGEX = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
private static final Pattern DATETIME_REGEX = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}");

@Override
public Statement apply(Statement base, Description description) {
Expand All @@ -26,13 +29,22 @@ public Statement apply(Statement base, Description description) {
}

String ignoreUntilDate = ignoreUntil.value();

DateTime annotationDate = forPattern("yyyy-MM-dd").parseDateTime(ignoreUntilDate);
DateTime annotationDate = parseDateTime(ignoreUntilDate);

return annotationDate.isAfterNow() ? new AlwaysPassesStatement() : base;

}

private DateTime parseDateTime(String datetime) {
if (DATE_REGEX.matcher(datetime).matches()) {
return forPattern("yyyy-MM-dd").parseDateTime(datetime);
} else if (DATETIME_REGEX.matcher(datetime).matches()) {
return forPattern("yyyy-MM-dd'T'HH:mm:ss").parseDateTime(datetime);
} else {
throw new IllegalArgumentException("Please provide correct datetime pattern, one of: \nyyyy-MM-dd\nyyyy-MM-ddTHH:mm:ss");
}
}

public static class AlwaysPassesStatement extends Statement {
@Override
public void evaluate() throws Throwable {
Expand Down
43 changes: 41 additions & 2 deletions src/test/java/co/unruly/junit/IgnoreUntilRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,69 @@ public class IgnoreUntilRuleTest {
@Mock Statement mockStatement;
@Mock IgnoreUntil mockIgnoreUntilAnnotation;
@Mock Description mockDescription;
private final static String DATE_PATTERN = "yyyy-MM-dd";
private final static String DATETIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

@InjectMocks
IgnoreUntilRule rule = new IgnoreUntilRule();

@Test
public void shouldReturnOriginalStatementIfAnnotationNotPresent() {
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(null);
assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfDateTimeFormatInvalid() {
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn("invalid");

rule.apply(mockStatement, mockDescription);
}

@Test
public void shouldReturnOriginalStatementIfIgnoreUntilDateTimeIsInThePast() {
String oneSecondBefore = new DateTime().minusSeconds(1).toString(DATETIME_PATTERN);

when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn(oneSecondBefore);

assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
}

@Test
public void shouldReturnAlwaysPassesStatementIfIgnoreUntilDateTimeIsInTheFuture() {
String oneSecondLater = new DateTime().plusSeconds(1).toString(DATETIME_PATTERN);

when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn(oneSecondLater);

assertTrue(rule.apply(mockStatement, mockDescription) instanceof IgnoreUntilRule.AlwaysPassesStatement);
}

@Test
public void shouldReturnOriginalStatementIfIgnoreUntilDateIsInThePast() {
String twoDaysAgo = new DateTime().minusDays(2).toString("yyyy-MM-dd");
String twoDaysAgo = new DateTime().minusDays(2).toString(DATE_PATTERN);

when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn(twoDaysAgo);

assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
}

@Test
public void shouldReturnOriginalStatementIfIgnoreUntilDateSameAsCurrentDate() {
String sameDay = new DateTime().toString(DATE_PATTERN);

when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn(sameDay);

assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
}

@Test
public void shouldReturnAlwaysPassesStatementIfIgnoreUntilDateIsInTheFuture() {
String tomorrow = new DateTime().plusDays(1).toString("yyyy-MM-dd");
String tomorrow = new DateTime().plusDays(1).toString(DATE_PATTERN);

when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
when(mockIgnoreUntilAnnotation.value()).thenReturn(tomorrow);
Expand Down

0 comments on commit 727a1a9

Please sign in to comment.