Skip to content

Commit ba2e9fd

Browse files
committed
Fix ScheduledActions throwing an exception if no weedays have been set
Fixes an issue introduced in commit 8087978. We assumed at least one weekday was always set in the recurrence of weekly actions. The UI dialog enforces it. However, GnuCash desktop doesn't. So we must check it for the case of imported scheduled actions.
1 parent 893132a commit ba2e9fd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

app/src/main/java/org/gnucash/android/model/ScheduledAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,19 @@ private long computeNextScheduledExecutionTimeStartingAt(long startTime) {
241241
* Computes the next time that this weekly scheduled action is supposed to be
242242
* executed starting at startTime.
243243
*
244+
* If no weekdays have been set (GnuCash desktop allows it), it will return a
245+
* date in the future to ensure ScheduledActionService doesn't execute it.
246+
*
244247
* @param startTime LocalDateTime to use as start to compute the next schedule.
245248
*
246-
* @return Next run time as a LocalDateTime
249+
* @return Next run time as a LocalDateTime. A date in the future, if no weekdays
250+
* were set in the Recurrence.
247251
*/
248252
@NonNull
249253
private LocalDateTime computeNextWeeklyExecutionStartingAt(LocalDateTime startTime) {
254+
if (mRecurrence.getByDays().isEmpty())
255+
return LocalDateTime.now().plusDays(1); // Just a date in the future
256+
250257
// Look into the week of startTime for another scheduled weekday
251258
for (int weekDay : mRecurrence.getByDays() ) {
252259
int jodaWeekDay = convertCalendarWeekdayToJoda(weekDay);

app/src/test/java/org/gnucash/android/test/unit/model/ScheduledActionTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.gnucash.android.model.Recurrence;
2121
import org.gnucash.android.model.ScheduledAction;
2222
import org.joda.time.DateTime;
23+
import org.joda.time.LocalDateTime;
2324
import org.junit.Test;
2425

2526
import java.sql.Timestamp;
@@ -176,6 +177,25 @@ public void weeklyActionsWithMultiplier_shouldBeDueOnTheWeekdaySet() {
176177
.isEqualTo(expectedNextDueDate);
177178
}
178179

180+
/**
181+
* Weekly actions should return a date in the future when no
182+
* weekdays have been set in the recurrence.
183+
*
184+
* See ScheduledAction.computeNextTimeBasedScheduledExecutionTime()
185+
*/
186+
@Test
187+
public void weeklyActionsWithoutWeekdaySet_shouldReturnDateInTheFuture() {
188+
ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
189+
Recurrence recurrence = new Recurrence(PeriodType.WEEK);
190+
recurrence.setByDays(Collections.<Integer>emptyList());
191+
scheduledAction.setRecurrence(recurrence);
192+
scheduledAction.setStartTime(new DateTime(2016, 6, 6, 9, 0).getMillis());
193+
scheduledAction.setLastRun(new DateTime(2017, 4, 12, 9, 0).getMillis());
194+
195+
long now = LocalDateTime.now().toDate().getTime();
196+
assertThat(scheduledAction.computeNextTimeBasedScheduledExecutionTime()).isGreaterThan(now);
197+
}
198+
179199
private long getTimeInMillis(int year, int month, int day) {
180200
Calendar calendar = Calendar.getInstance();
181201
calendar.set(year, month, day);

0 commit comments

Comments
 (0)