View Javadoc

1   package com.atlassian.scheduler.core.tests;
2   
3   import com.atlassian.scheduler.core.tests.CronFactory.CronExpressionAdapter;
4   import org.hamcrest.Description;
5   import org.hamcrest.Matcher;
6   import org.hamcrest.TypeSafeMatcher;
7   import org.joda.time.DateTime;
8   import org.joda.time.DateTimeZone;
9   import org.joda.time.LocalDateTime;
10  
11  import java.util.Date;
12  
13  import static org.hamcrest.Matchers.allOf;
14  import static org.hamcrest.Matchers.not;
15  
16  /**
17   * Cron expression to date matcher.
18   * <p>
19   * Note that the order of the arguments matches that of the cron expressions so that it is
20   * easier to tell by inspection whether or not the expression should match.
21   * </p>
22   *
23   * @since v1.5
24   */
25  public class CronExpressionAssertions {
26      public static Matcher<CronExpressionAdapter> satisfiedBy(
27              final int second, final int minute, final int hour,
28              final int day, final int month, final int year) {
29          return satisfiedBy(second, minute, hour, day, month, year, DateTimeZone.getDefault());
30      }
31  
32      public static Matcher<CronExpressionAdapter> satisfiedBy(
33              final int second, final int minute, final int hour,
34              final int day, final int month, final int year,
35              final DateTimeZone zone) {
36          return new TypeSafeMatcher<CronExpressionAdapter>() {
37              @SuppressWarnings("MagicConstant")
38              @Override
39              protected boolean matchesSafely(final CronExpressionAdapter cronExpression) {
40                  final LocalDateTime dateTime = new LocalDateTime(year, month, day, hour, minute, second);
41                  return cronExpression.isSatisfiedBy(dateTime.toDateTime(zone).toDate());
42              }
43  
44              @Override
45              public void describeTo(final Description description) {
46                  description.appendText("(")
47                          .appendText(s(second)).appendText(", ")
48                          .appendText(s(minute)).appendText(", ")
49                          .appendText(s(hour)).appendText(", ")
50                          .appendText(s(day)).appendText(", ")
51                          .appendText(s(month)).appendText(", ")
52                          .appendText(s(year)).appendText(", ")
53                          .appendText(zone.getID()).appendText(")");
54              }
55          };
56      }
57  
58      public static Matcher<CronExpressionAdapter> unsatisfiedBy(
59              int second, int minute, int hour,
60              int day, int month, int year) {
61          return not(satisfiedBy(second, minute, hour, day, month, year));
62      }
63  
64      public static Matcher<CronExpressionAdapter> unsatisfiedBy(
65              int second, int minute, int hour,
66              int day, int month, int year,
67              DateTimeZone zone) {
68          return not(satisfiedBy(second, minute, hour, day, month, year, zone));
69      }
70  
71      public static Matcher<? super Date> runTime(final long timestamp) {
72          return new TypeSafeMatcher<Date>() {
73              @Override
74              protected boolean matchesSafely(Date item) {
75                  return item.getTime() == timestamp;
76              }
77  
78              @Override
79              public void describeTo(Description description) {
80                  description.appendText("<Date with timestamp=")
81                          .appendText(String.valueOf(timestamp))
82                          .appendText("=")
83                          .appendText(new Date(timestamp).toString())
84                          .appendText(">");
85              }
86  
87              @Override
88              protected void describeMismatchSafely(Date item, Description mismatchDescription) {
89                  mismatchDescription.appendText("<Date with timestamp=")
90                          .appendText(String.valueOf(item.getTime()))
91                          .appendText("=")
92                          .appendText(item.toString())
93                          .appendText(">");
94              }
95          };
96      }
97  
98      public static Matcher<Date> runTime(
99              final int second, final int minute, final int hour,
100             final int day, final int month, final int year,
101             final DateTimeZone zone) {
102         return new TypeSafeMatcher<Date>() {
103             @Override
104             protected boolean matchesSafely(final Date result) {
105                 final DateTime dateTime = new DateTime(result, zone);
106                 return dateTime.getYear() == year
107                         && dateTime.getMonthOfYear() == month
108                         && dateTime.getDayOfMonth() == day
109                         && dateTime.getHourOfDay() == hour
110                         && dateTime.getMinuteOfHour() == minute
111                         && dateTime.getSecondOfMinute() == second
112                         && dateTime.getMillisOfSecond() == 0;
113             }
114 
115             @Override
116             public void describeTo(Description description) {
117                 description.appendText("(")
118                         .appendText(s(second)).appendText(", ")
119                         .appendText(s(minute)).appendText(", ")
120                         .appendText(s(hour)).appendText(", ")
121                         .appendText(s(day)).appendText(", ")
122                         .appendText(s(month)).appendText(", ")
123                         .appendText(s(year)).appendText(", ")
124                         .appendText(zone.getID()).appendText(")");
125             }
126 
127             @Override
128             protected void describeMismatchSafely(Date item, Description mismatchDescription) {
129                 final DateTime dateTime = new DateTime(item, zone);
130                 mismatchDescription.appendText("(")
131                         .appendText(s(dateTime.getSecondOfMinute())).appendText(", ")
132                         .appendText(s(dateTime.getMinuteOfHour())).appendText(", ")
133                         .appendText(s(dateTime.getHourOfDay())).appendText(", ")
134                         .appendText(s(dateTime.getDayOfMonth())).appendText(", ")
135                         .appendText(s(dateTime.getMonthOfYear())).appendText(", ")
136                         .appendText(s(dateTime.getYear())).appendText(", ")
137                         .appendText(zone.getID()).appendText(")");
138             }
139         };
140     }
141 
142     public static Matcher<Date> runTime(
143             final int second, final int minute, final int hour,
144             final int day, final int month, final int year,
145             final DateTimeZone zone, final long millis) {
146         //noinspection unchecked
147         return allOf(runTime(millis), runTime(second, minute, hour, day, month, year, zone));
148     }
149 
150     static String s(int value) {
151         return String.valueOf(value);
152     }
153 }