Skip to content

Commit 492670c

Browse files
authored
Merge pull request #2 from siladu/datetime-format-support_1
Add @IgnoreUntil Support for "yyyy-MM-dd'T'HH:mm:ss" datetime format #1
2 parents 953f34d + ea2cd38 commit 492670c

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A collection of useful JUnit rules from Unruly's codebases
1515
</dependency>
1616
```
1717

18-
## Ignore tests until a certain date.
18+
## Ignore tests until a certain date or datetime.
1919

2020
This allows you to write an acceptance/integration test before implementing a feature, and integrate it into your codebase before the implementation is complete.
2121

@@ -27,6 +27,12 @@ IgnoreUntilRule rule = new IgnoreUntilRule();
2727
@Test
2828
public void example_test_ignored_until_a_date() {
2929

30+
}
31+
32+
@IgnoreUntil("2014-10-30T17:30:00")
33+
@Test
34+
public void example_test_ignored_until_a_datetime() {
35+
3036
}
3137
```
3238

src/main/java/co/unruly/junit/IgnoreUntilRule.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import org.junit.rules.TestRule;
66
import org.junit.runner.Description;
77
import org.junit.runners.model.Statement;
8+
import java.util.regex.Pattern;
89

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

1112
public class IgnoreUntilRule implements TestRule {
1213

14+
private static final Pattern DATE_REGEX = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
15+
private static final Pattern DATETIME_REGEX = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}");
16+
1317
@Override
1418
public Statement apply(Statement base, Description description) {
1519

@@ -24,13 +28,22 @@ public Statement apply(Statement base, Description description) {
2428
}
2529

2630
String ignoreUntilDate = ignoreUntil.value();
27-
28-
DateTime annotationDate = forPattern("yyyy-MM-dd").parseDateTime(ignoreUntilDate);
31+
DateTime annotationDate = parseDateTime(ignoreUntilDate);
2932

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

3235
}
3336

37+
private DateTime parseDateTime(String datetime) {
38+
if (DATE_REGEX.matcher(datetime).matches()) {
39+
return forPattern("yyyy-MM-dd").parseDateTime(datetime);
40+
} else if (DATETIME_REGEX.matcher(datetime).matches()) {
41+
return forPattern("yyyy-MM-dd'T'HH:mm:ss").parseDateTime(datetime);
42+
} else {
43+
throw new IllegalArgumentException("Please provide correct datetime pattern, one of: \nyyyy-MM-dd\nyyyy-MM-ddTHH:mm:ss");
44+
}
45+
}
46+
3447
public static class AlwaysPassesStatement extends Statement {
3548
@Override
3649
public void evaluate() throws Throwable {

src/test/java/co/unruly/junit/IgnoreUntilRuleTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,69 @@ public class IgnoreUntilRuleTest {
2020
@Mock Statement mockStatement;
2121
@Mock IgnoreUntil mockIgnoreUntilAnnotation;
2222
@Mock Description mockDescription;
23+
private final static String DATE_PATTERN = "yyyy-MM-dd";
24+
private final static String DATETIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
2325

2426
@InjectMocks
2527
IgnoreUntilRule rule = new IgnoreUntilRule();
2628

2729
@Test
2830
public void shouldReturnOriginalStatementIfAnnotationNotPresent() {
2931
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(null);
32+
assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
33+
}
34+
35+
@Test(expected = IllegalArgumentException.class)
36+
public void shouldThrowExceptionIfDateTimeFormatInvalid() {
37+
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
38+
when(mockIgnoreUntilAnnotation.value()).thenReturn("invalid");
39+
40+
rule.apply(mockStatement, mockDescription);
41+
}
42+
43+
@Test
44+
public void shouldReturnOriginalStatementIfIgnoreUntilDateTimeIsInThePast() {
45+
String oneSecondBefore = new DateTime().minusSeconds(1).toString(DATETIME_PATTERN);
46+
47+
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
48+
when(mockIgnoreUntilAnnotation.value()).thenReturn(oneSecondBefore);
3049

3150
assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
3251
}
3352

53+
@Test
54+
public void shouldReturnAlwaysPassesStatementIfIgnoreUntilDateTimeIsInTheFuture() {
55+
String oneSecondLater = new DateTime().plusSeconds(1).toString(DATETIME_PATTERN);
56+
57+
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
58+
when(mockIgnoreUntilAnnotation.value()).thenReturn(oneSecondLater);
59+
60+
assertTrue(rule.apply(mockStatement, mockDescription) instanceof IgnoreUntilRule.AlwaysPassesStatement);
61+
}
62+
3463
@Test
3564
public void shouldReturnOriginalStatementIfIgnoreUntilDateIsInThePast() {
36-
String twoDaysAgo = new DateTime().minusDays(2).toString("yyyy-MM-dd");
65+
String twoDaysAgo = new DateTime().minusDays(2).toString(DATE_PATTERN);
3766

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

4170
assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
4271
}
4372

73+
@Test
74+
public void shouldReturnOriginalStatementIfIgnoreUntilDateSameAsCurrentDate() {
75+
String sameDay = new DateTime().toString(DATE_PATTERN);
76+
77+
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
78+
when(mockIgnoreUntilAnnotation.value()).thenReturn(sameDay);
79+
80+
assertEquals(mockStatement, rule.apply(mockStatement, mockDescription));
81+
}
82+
4483
@Test
4584
public void shouldReturnAlwaysPassesStatementIfIgnoreUntilDateIsInTheFuture() {
46-
String tomorrow = new DateTime().plusDays(1).toString("yyyy-MM-dd");
85+
String tomorrow = new DateTime().plusDays(1).toString(DATE_PATTERN);
4786

4887
when(mockDescription.getAnnotation(IgnoreUntil.class)).thenReturn(mockIgnoreUntilAnnotation);
4988
when(mockIgnoreUntilAnnotation.value()).thenReturn(tomorrow);

0 commit comments

Comments
 (0)